API, How to get P3D Thread connection object / properties by LineNumber​?

API, How to get P3D Thread connection object / properties by LineNumber​?

rflin-CTCIM
Advocate Advocate
1,426 Views
3 Replies
Message 1 of 4

API, How to get P3D Thread connection object / properties by LineNumber​?

rflin-CTCIM
Advocate
Advocate

API, How to get P3D Thread connection object / properties by LineNumber​?

 

0 Likes
Accepted solutions (3)
1,427 Views
3 Replies
Replies (3)
Message 2 of 4

sergey.utkin
Enthusiast
Enthusiast
Accepted solution

Please try this code.

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using Autodesk.ProcessPower.ProjectManager; // add reference to 'PnPProjectManagerMgd.dll'
using Autodesk.ProcessPower.PlantInstance;
using Autodesk.ProcessPower.DataLinks; // add reference to 'PnPDataLinks.dll'
using Autodesk.ProcessPower.DataObjects; // add reference to 'PnPDataObjects.dll'

public class Commands {
    [CommandMethod("GTOL")]
    public static void GetThreadsOnLinenumber() {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        // We prompt for the 'Tag' of linenumber
        PromptStringOptions pso = new PromptStringOptions("\nEnter the 'Tag' of linenumber: ");
        PromptResult pr = ed.GetString(pso);
        if (pr.Status != PromptStatus.OK) {
            return;
        }
        string linetag = pr.StringResult;

        // We get the project piping database
        PlantProject plantproj = PlantApplication.CurrentProject;
        Project proj = plantproj.ProjectParts["Piping"];
        DataLinksManager dlm = proj.DataLinksManager;
        PnPDatabase pipingdb = dlm.GetPnPDatabase();

        // We query the 'P3dLineGroup' table for records with our tag
        PnPRow[] linegroup = pipingdb.Tables["P3dLineGroup"].Select("Tag='" + linetag + "'");
        if (linegroup.Length == 0) {
            // No records found
            ed.WriteMessage("\nThere is no linenumber with this 'Tag'");
            return;
        }

        // We found a record.
        // Now we get the ids of all parts related to our linenumber
        PnPRowIdArray relatedids = dlm.GetRelatedRowIds("P3dLineGroupPartRelationship", "LineGroup", linegroup[0].RowId, "Part");
        if (relatedids.Count == 0) {
            // No related parts
            ed.WriteMessage("\nThere are no parts on this line number");
            return;
        }

        // There are some related parts.
        // We create a header
        ed.WriteMessage("\n-------------Threads and their connectors on '{0}'-------------", linetag);

        // We iterate through the related parts
        foreach (int partid in relatedids) {
            // We query the 'PnPDataLinks' table 
            // for the 'Thread' RowClassName
            PnPRow[] subpartrows = pipingdb.Tables["PnPDataLinks"].Select("RowId=" + partid + " AND RowClassName='Thread'");
            if (subpartrows.Length == 0) {
                // Current part is not a thread
                continue;
            }

            // The object is part of thread connection 
            // We get the record from 'EngineeringItems' table
            PnPRow[] engitemsrows = pipingdb.Tables["EngineeringItems"].Select("PnPID=" + partid);
            if (engitemsrows.Length == 0) {
                continue;
            }

            // And from 'Thread' table
            PnPRow[] threadrows = pipingdb.Tables["Thread"].Select("PnPID=" + partid);
            if (threadrows.Length == 0) {
                continue;
            }

            // We query the 'PnPDataLinks' table 
            // for the connector (the main part of the thread)
            // 'Thread' and its 'P3dConnector'
            // share the same 'DwgId' and 'DwgHandleLow' field values
            PnPRow[] mainpartrows = pipingdb.Tables["PnPDataLinks"].Select("DwgId=" + subpartrows[0]["DwgId"] + " AND DwgHandleLow=" + subpartrows[0]["DwgHandleLow"] + " AND RowClassName='P3dConnector'");
            if (mainpartrows.Length == 0) {
                continue;
            }

            // We query the 'P3dConnector' table 
            // to get info from it too
            PnPRow[] connectorrows = pipingdb.Tables["P3dConnector"].Select("PnPID=" + mainpartrows[0]["RowId"]);
            if (connectorrows.Length == 0) {
                continue;
            }

            // Now we output some data for the current thread
            // and its connector from 3 tables:
            // 'EngineeringItem's (thread), 'Thread' (thread)
            // and 'PnPDataLinks' (connector)
            ed.WriteMessage("\nThread-PnPId : {0}   EngItems-Spec : {1}   EngItems-NominalDiameter : {2}   EngItems-NominalUnit : {3}    Thread-Shop_Field : {4}    Conn-PnPId : {5}    Conn-JointType : {6}", partid, engitemsrows[0]["Spec"], engitemsrows[0]["NominalDiameter"], engitemsrows[0]["NominalUnit"], threadrows[0]["Shop_Field"], connectorrows[0]["PnPID"], connectorrows[0]["JointType"]);
        }
    }
}

0 Likes
Message 3 of 4

rflin-CTCIM
Advocate
Advocate
Accepted solution

Thanks'

 the linegroup[0].RowId value equal P3dLineGroup field PnPID value, Yes or Not?

 

0 Likes
Message 4 of 4

sergey.utkin
Enthusiast
Enthusiast
Accepted solution

Yes, you are right

0 Likes