Пример #1
0
        public static void convertIds()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            try
            {
                // 3.1 Declare a variable as a PlantProject and instantiate
                // it using the CurrentProject of PlantApplication.
                PlantProject mainPrj = PlantApplication.CurrentProject;

                // 3.2 Declare a variable as a Project and instantiate it
                // using the ProjectParts property of the PlantProject from
                // step 3.1. for the strName property [] use "PnId"
                Project prj = mainPrj.ProjectParts["PnId"];


                //wb testing, commented out the line above
                // Project prj = mainPrj.ProjectParts["Piping"];

                // 3.3 Declare a DataLinksManager variable and instantiate it
                // using the DataLinksManager property of the Project from
                // step 3.2
                DataLinksManager dlm = prj.DataLinksManager;

                // 3.4 Declare a variable as an ObjectId. Instantiate it
                // use the GetEntity method of the Editor from above. (ed)
                // Use the ObjectId property.
                ObjectId entId = ed.GetEntity("Pick a P&ID item: ").ObjectId;

                // 3.5 Get the Row Id of the entity from the ObjectId
                // from step 3.4. Declare an int and make it equal to the
                // return from the FindAcPpRowId of the DataLinksManager  from
                // step 3.3. pass in the ObjectId from step 3.4.
                // Note: The int for the Row Id from this step and the Row Id from
                // step 7 will be the same, even though we are using ObjectId here
                // and PpObjectId in step 3.7
                int rowId1 = dlm.FindAcPpRowId(entId);

                // 3.6 Declare a variable as a PpObjectId and instantiate it
                // using the MakeAcPpObjectId method of the DataLinksManager
                // from step 3.3. Pass in the ObjectId from step 3.4.
                PpObjectId ppObjId = dlm.MakeAcPpObjectId(entId);

                // 3.7 Now we will get the Row Id of the entity from the PpObjectId
                // from step 3.5. Declare an int and make it equal to the
                // return from the FindAcPpRowId of the DataLinksManager  from
                // step 3.3. Pass in the PpObjectId 3.6
                int rowId2 = dlm.FindAcPpRowId(ppObjId);

                // 3.8 Use the WriteMessage function of the editor (ed) and print
                // the values of the int from step 3.7 and 3.5
                ed.WriteMessage("rowId1 = " + rowId1.ToString() + " rowId2 = " + rowId2);


                // 3.9 Declare a variable as a PpObjectIdArray. Instantiate it
                // using the FindAcPpObjectIds method of the DataLinksManager from
                // step 3.3. Pass in the row id from step or 3.5. (or step 3.7)
                // NOTE: FindAcPpObjectIds returns a COLLECTION of AcPpObjectId.
                // I.e., multiple AcDbObjectIds may be linked to a single RowID
                PpObjectIdArray ids = dlm.FindAcPpObjectIds(rowId1);

                // 3.10 Use a foreach and iterate through the PpObjectId  in the
                // PpObjectIdArray from step 3.9.
                // Note: put the closing curly brace below Step 3.12
                foreach (PpObjectId ppid in ids)
                {
                    // 3.11 Declare a variable as an ObjectId Instantiate it using
                    // the MakeAcDbObjectId variable of the DataLinksManager from
                    // step 3.3.
                    ObjectId oid = dlm.MakeAcDbObjectId(ppid);

                    // 3.12 Use the WriteMessage function of the editor (ed) and print
                    // the value of the ObjectId form step 3.11
                    // Build, debug and test this code. (Command "ConvertIds")
                    // Note: Continue to step 3.13 below
                    ed.WriteMessage("\n oid = " + oid.ToString() + "\n");
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.ToString());
            }
        }
Пример #2
0
        public static void getDataUsingString()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            try
            {
                PlantProject     mainPrj = PlantApplication.CurrentProject;
                Project          prj     = mainPrj.ProjectParts["PnId"];
                DataLinksManager dlm     = prj.DataLinksManager;

                ObjectId entId = ed.GetEntity("Pick a P&ID item: ").ObjectId;

                // x.x Declare a variable as a PpObjectId and instantiate it
                // using the MakeAcPpObjectId method of the DataLinksManager
                // from step 3.x. Pass in the ObjectId from step 3.4.
                PpObjectId pnpId = dlm.MakeAcPpObjectId(entId);

                // x.x Now let's do an opposite action that we did in step 3.5
                // Now we will get the ObjectId(s) of the entity from the PpObjectId
                // from step 3.x. Declare an int and make it equal to the
                // return from the FindAcPpRowId of the DataLinksManager  from
                // step 3.x.
                int rowId1 = dlm.FindAcPpRowId(entId); // You can use ObjectId
                int rowId2 = dlm.FindAcPpRowId(pnpId); //          or PpObjectId
                // rowId1 and rowId2 are always equal

                PpObjectIdArray ids = dlm.FindAcPpObjectIds(rowId1);
                // NOTE: It returns a COLLECTION of AcPpObjectId!
                //       I.e., multiple AcDbObjectIds may be linked to a single RowID

                // Now find the ObjectID for each PpObjectId
                foreach (PpObjectId ppid in ids)
                {
                    ObjectId oid = dlm.MakeAcDbObjectId(ppid);
                    ed.WriteMessage("\n oid=" + oid.ToString() + "\n");

                    // Evaluate the next two lines are not in the DevNote:
                    String sEval = "\n LineNumber = ";

                    sEval += FormatStringUtils.Evaluate
                                 ("#(TargetObject.LineNumber^@NNN)", oid); // - #(Project.General.Project_Name)", oid);

                    sEval += "\n Project_Name = ";
                    sEval += FormatStringUtils.Evaluate("#(Project.General.Project_Name)", oid);


                    ed.WriteMessage(sEval);

                    // String sEval2 = "\n TargeObject.Tag = ";
                    // String sTestIsValid = "#(TargetObject.Tag) - #(=TargetObject.Tag)";
                    //String sTestIsValid = "#(TargetObject.Tag)"; // - #(=TargetObject.Tag)";
                    String sTestIsValid = "#(GenericRotaryValve.Manufacturer)";

                    if (FormatStringUtils.IsValid(sTestIsValid))
                    {
                        String sEval2 = "\n Generic Rotary Valve Manufacturer = ";
                        // sEval2 += FormatStringUtils.Evaluate("#(TargetObject.Tag) - #(=TargetObject.Tag)");
                        sEval2 += FormatStringUtils.Evaluate(sTestIsValid, oid);
                        ed.WriteMessage(sEval2);
                    }
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.ToString());
            }
        }
Пример #3
0
        public void Retrive_Plant3DPart_data()
        {
            //file name to save different data
            string partDataFromObjPath = @"C: \Users\LinP3\source\repos\Plant3DApiTester\Resource\partDataFromObj.txt";
            string partDataFromDBPath  = @"C: \Users\LinP3\source\repos\Plant3DApiTester\Resource\partDataFromDB.txt";

            ///all available drawing type
            string DwgType_Piping = "Piping";
            string DwgType_PnId   = "PnId";
            string DwgType_Ortho  = "Ortho";
            string DwgType_Iso    = "Iso";

            //get current drawing
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            ///create a clean file, prepare to write
            File.WriteAllText(partDataFromObjPath, $"{doc.Name}\n");
            File.WriteAllText(partDataFromDBPath, $"{doc.Name}\n");

            //get current project
            PlantProject     mainPrj = PlantApplication.CurrentProject;
            Project          prj     = mainPrj.ProjectParts[DwgType_Piping];
            DataLinksManager dlm     = prj.DataLinksManager;

            //get current drawing type
            string dwgtype = PnPProjectUtils.GetActiveDocumentType();

            if (dwgtype != DwgType_PnId && dwgtype != DwgType_Piping)
            {  //"PnId", "Piping", "Ortho", "Iso"
                ed.WriteMessage("This drawing is not a P&ID or 3D Model in the current project.\n");
                return;
            }

            //method to get PnPDatabase for project
            PnPDatabase pnpDB = dlm.GetPnPDatabase();

            #region get all object in cad and check if it is plant 3d object, if it is, get its PnPId
            string msg = string.Empty;

            ///get just current project and the datalink
            Project          prjPart = PnPProjectUtils.GetProjectPartForCurrentDocument();
            DataLinksManager dlmPart = prjPart.DataLinksManager;
            //PnPDatabase dbPart = dlmPart.GetPnPDatabase();

            //check all information
            msg = getHeaderText("check PnPTable");
            PnPRowIdArray aPid = dlmPart.SelectAcPpRowIds(db);
            int           numberOfObjsInCurrentProj = aPid.Count();
            msg += "Number of objects in current project database =" + numberOfObjsInCurrentProj;
            int no = 1;
            foreach (int rid in aPid)
            {
                StringCollection sKeys = new StringCollection();
                sKeys.Add("Tag");
                sKeys.Add("Description");
                sKeys.Add("PartFamilyLongDesc");
                sKeys.Add("Line Number");
                StringCollection sVals = dlm.GetProperties(rid, sKeys, true);
                msg += $"\n[{no}]PnPId {rid} ?(rid), Tag = {sVals[0]} ({sVals[1]}) {sVals[2]} <{ sVals[3]}>.";
                no++;
            }
            File.AppendAllText(partDataFromDBPath, msg);
            #endregion

            #region to check all objects in current drawings
            PromptSelectionOptions pso = new PromptSelectionOptions();
            pso.MessageForAdding = "\nSelect " + dwgtype + " objects to <All>:";

            ///select object in drawing
            PromptSelectionResult result = ed.GetSelection(pso);
            if (result.Status == PromptStatus.Cancel)
            {
                return;
            }
            if (result.Status != PromptStatus.OK)
            {
                result = ed.SelectAll();
            }
            SelectionSet ss     = result.Value;
            ObjectId[]   objIds = ss.GetObjectIds();

            ///traves over all objects
            Autodesk.AutoCAD.DatabaseServices.TransactionManager tranMag = db.TransactionManager;
            int numberOfObjsInCurrentDrawing = objIds.Count();
            File.AppendAllText(partDataFromObjPath, $"Number of Plant element in Selection ={numberOfObjsInCurrentDrawing}");
            int numberOfPlantObjsInCurrentDrawing = 1;
            msg = getHeaderText("traves over all objects on selection");
            no  = 1;
            using (Transaction tran = tranMag.StartTransaction())
            {
                foreach (ObjectId objId in objIds)
                {
                    DBObject obj = tran.GetObject(objId, OpenMode.ForRead);
                    msg += getHeaderText($"({no})ClassID = {obj.ClassID}");

                    //return if it is not a plant object
                    if (!dlm.HasLinks(objId))
                    {
                        continue;
                    }

                    Handle handle = objId.Handle;
                    File.AppendAllText(partDataFromObjPath, $"({no})Handle = {handle.Value}, handle string = {handle.ToString()}\n");/////////////////// handle string is what is shown in Navisworks

                    try
                    {
                        //ObjectIdCollection objectIdCollection = dlm.GetRelatedAcDbObjectIds(objId);
                        File.AppendAllText(partDataFromObjPath, $"({no})ObjectId = {objId}, Id ={obj.Id}, ClassID ={obj.ClassID}\n");

                        //find row id
                        int rowId = dlmPart.FindAcPpRowId(objId);
                        File.AppendAllText(partDataFromObjPath, $"({no})rowId ={rowId}\n");
                        dlmPart.GetObjectClassname(rowId);

                        //find ppobjectid
                        PpObjectIdArray ppObjectIds = dlmPart.FindAcPpObjectIds(rowId);
                        foreach (PpObjectId ppObjectId in ppObjectIds)
                        {
                            File.AppendAllText(partDataFromObjPath, $"({no})dbHandle = {ppObjectId.dbHandle}, DwgId = {ppObjectId.DwgId}\n");
                        }

                        ///get property
                        //properties to lookup
                        StringCollection props = new StringCollection();
                        props.Add("WBS_Level1");
                        props.Add("WBS_Level2");
                        props.Add("WBS_Level3");
                        props.Add("WBS_Level4");
                        props.Add("WBS_Level5");
                        props.Add("WBS_Level6");
                        props.Add("WBS_Level7");
                        props.Add("PnPGuid");
                        //properties values
                        StringCollection propsValue = dlmPart.GetProperties(objId, props, true);

                        //update value
                        for (int i = 0; i < props.Count; i++)
                        {
                            ///set property when value is empty
                            if (string.IsNullOrEmpty(propsValue[i]))
                            {
                                propsValue[i] = $"WBS_Level{i}_Value";
                            }
                        }
                        ///set property
                        dlmPart.SetProperties(objId, props, propsValue);

                        ///print out to see
                        for (int i = 0; i < props.Count; i++)
                        {
                            File.AppendAllText(partDataFromObjPath, $"({no})props = {props[i]}, Value = {propsValue[i]}\n");
                        }

                        numberOfPlantObjsInCurrentDrawing++;
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(ex.ToString()); }

                    Part part = obj as Part;
                    if (part != null)
                    {
                        print(part, $"({no})Part", partDataFromObjPath);
                        File.AppendAllText(partDataFromObjPath, $"({no})GetType = {part.GetType()}\n");

                        ///type:
                        ///Autodesk.ProcessPower.PnP3dObjects.Connector
                        ///Autodesk.ProcessPower.PnP3dObjects.Pipe
                        ///Autodesk.ProcessPower.PnP3dObjects.PipeInlineAsset
                        ///Autodesk.ProcessPower.PnP3dObjects.Equipment
                        if (part.GetType() == typeof(Connector))
                        {
                            try
                            {
                                Connector item = part as Connector;
                                print(item, typeof(Connector).ToString(), partDataFromObjPath);
                            }
                            catch { }
                        }
                        if (part.GetType() == typeof(Pipe))
                        {
                            try
                            {
                                Pipe item = part as Pipe;
                                print(item, typeof(Pipe).ToString(), partDataFromObjPath);
                            }
                            catch { }
                        }
                        if (part.GetType() == typeof(PipeInlineAsset))
                        {
                            try
                            {
                                PipeInlineAsset item = part as PipeInlineAsset;
                                print(item, typeof(PipeInlineAsset).ToString(), partDataFromObjPath);
                            }
                            catch { }
                        }
                    }

                    File.AppendAllText(partDataFromObjPath, $"({no})++++++++++++++++++++++++++++++++\n");
                    no++;
                }
            }

            File.AppendAllText(partDataFromObjPath, $"Number of Plant element in drawing ={numberOfPlantObjsInCurrentDrawing}");
            #endregion
        }