Choose your top Archicad wishes!

Read more
Archicad C++ API
About Archicad add-on development using the C++ API.

Extracting the Polyline data for MEP Elements from the Model

Amit Tiwari
Contributor

 

  • What is API_Neig?

    I encountered the term API_Neig in the MEP Test example, where it is used for selecting an MEP element. Can someone explain what API_Neig represents and how it is used in the context of MEP elements?

Extracting Polyline Information for All Routing Elements in the Model

I want to extract the Polyline information for all Routing Elements from the entire model, not just from a specific selection. Here’s the workflow I’m using:

  • getElementList: Retrieve all elements.
  • Filter by TypeID: Check if it’s an MEP Element.
  • Check if Routing Element: Confirm if it’s a routing element.
  • get Routing Element for this guid: Retrieve the routing element.
  • extract the Polyline using getPolyline(): Attempt to get the polyline.

However, I’m unable to get any coordinates using this method. The same workflow successfully retrieves the polyline when a specific routing element is selected. What could be the issue with extracting polyline data for all routing elements in the model?    

 

CADDeveloper_0-1717050646993.png

 

CADDeveloper_1-1717050684496.png

 

 

14 REPLIES 14
Ralph Wessel
Mentor

Are you confident that the elements you're inspecting should have a polyline? I haven't tried myself yet, but I assume not all MEP elements have one.

 

If you haven't tried already I suggest building and running the API example project "MEP_Test". It includes a function WriteRoutingElementDetails which echoes what you're trying to do. If it works, we can try to work out what the difference is. If it doesn't work, I suspect it's a problem with the MEP elements you are trying to inspect.

Ralph Wessel BArch

 

  • Initial Success with MEP_Test:

    • I successfully built the MEP_Test project.
    • Using this project, I was able to extract polyline data from selected elements.
  • MEP_Test Functionality:

    • The MEP_Test project is designed to work based on user selection.
    • It uses the ACAPI_Selection_Get API to retrieve selected elements (API_Neig).
    • A unique ID is then generated from the neig_guid.
    • Finally, the routing element is extracted using RoutingElement::Get(UniqueId).
  • Objective:

    • My goal is to extract all routing elements present in the model automatically, without relying on manual selection.

 

 

void ExtractAllRoutingElements() {
    GS::Array<API_Guid> elementGuids;
    ACAPI_Element_GetElemList(API_ZombieElemID, &elementGuids);

    for (const API_Guid& guid : elementGuids) {
        API_Element element = {};
        element.header.guid = guid;
        
        if (ACAPI_Element_Get(&element) == NoError) {
            // Check if the element is a routing element
            if (element.header.typeID == API_ExternalElemID &&
                (IsBranch(element.header.type.classID) ||
                 IsAccessory(element.header.type.classID) ||
                 IsEquipment(element.header.type.classID) ||
                 IsTerminal(element.header.type.classID) ||
                 IsFitting(element.header.type.classID) ||
                 IsFlexibleSegment(element.header.type.classID) ||
                 IsRoutingElement(element.header.type.classID))) {
                
                // Process the routing element
                Adapter::UniqueID uniqueId(guid);
                WriteRoutingElementDetails(uniqueId);
            }
        }
    }
}

void WriteRoutingElementDetails(Adapter::UniqueID &uniqueId) {
    try {
        ACAPI::Result<RoutingElement> routingElementResult = RoutingElement::Get(uniqueId);

        if (routingElementResult.IsErr()) {
            const auto& error = routingElementResult.UnwrapErr();
            ACAPI_WriteReport(error.text.c_str(), false);
            return;
        }

        Reporter routingElementReporter;
        std::vector<API_Coord3D> polyline = routingElementResult->GetPolyLine();

        if (polyline.empty()) {
            ACAPI_WriteReport("Polyline is empty", false);
        } else {
            routingElementReporter.Add("Polyline of the routing element:");
            routingElementReporter.SetTabCount(1);

            for (const API_Coord3D& node : polyline) {
                routingElementReporter.Add(node);
            }

            routingElementReporter.Add("This is new Tab");
            routingElementReporter.Write();
        }

        polyline.clear();
    } catch (const std::exception& e) {
        ACAPI_WriteReport(std::string(e.what()).c_str(), false);
    } catch (...) {
        ACAPI_WriteReport("Unknown exception in WriteRoutingElementDetails", false);
    }
}

 



 

Implementation: 

  

  • ExtractAllRoutingElements Function:

    • Enumerates all elements in the model using ACAPI_Element_GetElemList.
    • Filters elements to find routing elements based on their type.
    • Calls WriteRoutingElementDetails for each routing element.
  • WriteRoutingElementDetails Function:

    • Extracts detailed information about the routing element using RoutingElement::Get.
    • Checks if the polyline data is empty and handles it appropriately.
    • Logs the polyline data.   
  • But my code fails in getting the polyline data. Whereas using the same workflow in Selection I am able to successfully obtain the polyline data

 

Ralph Wessel
Mentor

The function WriteRoutingElementDetails in the "MEP_Test" project only takes the guid to an element as a parameter, which means it should work the same way irrespective of being selected or not. Have you tried a copy/paste of that function to your code to see if it still works when the element guids have not come from a selection?

Ralph Wessel BArch

I tried copy pasting the same in my code ( writeRoutingElementDetails comes from MEP_Test Example ). It still doesn't work. 

Ralph Wessel
Mentor

Are the element guids the same in both cases?

Ralph Wessel BArch