public void addPNPevent() { // 5.1 Declare a variable as a PlantProject. Instantiate it using // the CurrentProject of PlantApplication PlantProject mainPrj = PlantApplication.CurrentProject; // 5.2 Declare a Project and instantiate it using // ProjectParts[] of the PlantProject from step 4.1 // use "PnId" for the name. This will get the P&ID project Project prj = mainPrj.ProjectParts["PnId"]; // 5.3 Declare a variable as a DataLinksManager. Instantiate it using // the DataLinksManager property of the Project from 5.2. DataLinksManager dlm = prj.DataLinksManager; // 5.4 Add a DataLinkOperationOccurred event. (use the DataLinksManager // from step 5.3) Use += and use new to create a new DataLinkEventHandler // pass in the name of the function that you will create in step 5.5. // (dlm_DataLinkOperationOccurred) dlm.DataLinkOperationOccurred += new DataLinkEventHandler(dlm_DataLinkOperationOccurred); }
public static void NewPipe() { string strSpec = "CS300"; PlantProject proj = PlantApplication.CurrentProject; PipingProject prjpart = (PipingProject)proj.ProjectParts["Piping"]; // Current state of API does not include a .NET version of the spec // manager so the lower part's repository API must be used. // Use project to get spec folder, for now use CS300 spec PartRep.SpecPart specPart = null; string strSpecFileName = Path.Combine(prjpart.SpecSheetsDirectory, strSpec + ".pspx"); // Open the spec using (PartRep.PipePartSpecification spec = (PartRep.PipePartSpecification)PartRep.PipePartSpecification.OpenSpecification(strSpecFileName)) { NominalDiameter nd = new NominalDiameter("in", 10.0); using (PartQueryResults results = spec.SelectParts("Pipe", nd)) { if (results != null) { // use first row in spec for a 10inch pipe specPart = (PartRep.SpecPart)results.NextPart(); } } // Create the pipe and return the ObjectId ObjectId newPipeId = CreatePipe(); //try //{ // int cacheId = AddPartToProject(prjpart, strSpec, newPipeId, specPart); // // if cacheId == -1 then AddPartToProject failed // if (cacheId == -1) // { // // Failed to add part to project // Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; // ed.WriteMessage("Error creating part in the project erasing the pipe entity"); // // Erase the pipe entity // Database db = HostApplicationServices.WorkingDatabase; // using (Transaction trans = db.TransactionManager.StartTransaction()) // { // PnP3d.Pipe p = trans.GetObject(newPipeId, OpenMode.ForWrite) as PnP3d.Pipe; // if (p != null) // { // p.Erase(); // trans.Commit(); // return; // } // } // return; // } // setPartGeometry(prjpart, newPipeId, cacheId); // int groupId = createUnassignedLineGroup(prjpart); // assignToLineGroup(prjpart, newPipeId, cacheId, groupId); //} //catch(Autodesk.AutoCAD.Runtime.Exception ex) //{ // // Some other problem occurred // Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; // ed.WriteMessage("Error - erasing the pipe entity"); // ed.WriteMessage(ex.ToString()); // // Erase the pipe entity // Database db = HostApplicationServices.WorkingDatabase; // using (Transaction trans = db.TransactionManager.StartTransaction()) // { // PnP3d.Pipe p = trans.GetObject(newPipeId, OpenMode.ForWrite) as PnP3d.Pipe; // if (p != null) // { // p.Erase(); // trans.Commit(); // } // } //} spec.Close(); } }
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()); } }
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()); } }
public void dataChange() { Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor; try { PlantProject mainPrj = PlantApplication.CurrentProject; Project prj = mainPrj.ProjectParts["PnId"]; DataLinksManager dlm = prj.DataLinksManager; PromptIntegerOptions pOpts = new PromptIntegerOptions("Enter a PnPID number"); PromptIntegerResult prIntRes = ed.GetInteger(pOpts); int iRowId = prIntRes.Value; if (prIntRes.Status == PromptStatus.OK) { // Declare a List (System.Collections.Generic) of the properties // from the selected entity List <KeyValuePair <string, string> > list_Properties; list_Properties = dlm.GetAllProperties(iRowId, true); // 3.20 Create a new KeyValuePair (System.Collections.Generic) // name it something like oldVal. Make it equal a new KeyValuePair // use string for the Key and Value. (use null for the string key // and string value. KeyValuePair <string, string> oldVal = new KeyValuePair <string, string>(null, null); // 3.21 Create another KeyValuePair (System.Collections.Generic) // name it something like newVal. Make it equal a new KeyValuePair // use string for the Key and Value. (use null for the string key // and string value. KeyValuePair <string, string> newVal = new KeyValuePair <string, string>(null, null); // Iterate through the entries in the list. for (int i = 0; i < list_Properties.Count; i++) { //areMake the the string for the key from // step 3.15 equal to the Key property of the list in // this iteration of the loop "[i]". (The list form step 3.14 //string strKey = list_Properties[i].Key; // 3.22 Use and if statement and see if the Key property // of the list in this iteration of the loop "[i]" is // equal to "Manufacturer" // Note: put the closing curly brace after step 3.25 if (list_Properties[i].Key == "Manufacturer") { // 3.23 Make the KeyValuePair created in step 3.20 equal to // a new KeyValuePair. Use string for the Key and Value. For the // Key, use Key property of the List in this iteration of the loop // [i]. For the Value use the Value property of the List in this // iteration of the loop. oldVal = new KeyValuePair <string, string>(list_Properties[i].Key, list_Properties[i].Value); // 3.24 Declare a string variable and make it equal to something // like "Some new Manufacturer" string txtNewVal = "Some new Manufacturer"; // 3.25 Make the KeyValuePair created in step 3.21 equal to // a new KeyValuePair. Use string for the Key and Value. For the // Key, use Key property of the List in this iteration of the loop // [i]. For the Value use the Value property use the string // from step 3.24 newVal = new KeyValuePair <string, string>(list_Properties[i].Key, txtNewVal); // 3.25 exit the for loop by adding break. break; } } // 3.26 Remove the old KeyValuePair from the List by calling the // Remove method from the list created above. (list_Properties) Pass // in the old KeyValuePair from step 3.20 list_Properties.Remove(oldVal); // 3.27 Add the new KeyValuePair to the List by calling the // Add method from the list created above. (list_Properties) Pass // in the KeyValuePair from step 3.21 list_Properties.Add(newVal); // 3.28 Declare a new System.Collections.Specialized.StringCollection // name it something like strNames. Instantiate it by making it // equal to a new System.Collections.Specialized.StringCollection() System.Collections.Specialized.StringCollection strNames = new System.Collections.Specialized.StringCollection(); // 3.29 Declare a new System.Collections.Specialized.StringCollection // name it something like strValues. Instantiate it by making it // equal to a new System.Collections.Specialized.StringCollection() System.Collections.Specialized.StringCollection strValues = new System.Collections.Specialized.StringCollection(); // 3.30 Iterate through the List declared above (list_Properties) // Note: Put the closing curly brace below step 3.34 for (int i = 0; i < list_Properties.Count; i++) { // 3.31 Declare a string named something like "name" make // it equal to the Key Property of this Iteration of the list // in the loop. "[i]" String name = list_Properties[i].Key; // 3.32 Declare a string named something like "value" make // it equal to the Value property of this Iteration of the list // in the loop. "[i]" String value = list_Properties[i].Value; // 3.33 Use the Add method of the StringCollection created in // step 3.28. Pass in the string from step 3.31 strNames.Add(name); // 3.34 Use the Add method of the StringCollection created in // step 3.29. Pass in the string from step 3.32 strValues.Add(value); } // 3.35 Use the SetProperties method of the DataLinksManager created // above (dlm) to update the properties. Pass in the Row Id that was // provided by the user above. (iRowId) For the first StringCollection // pass in the one from step 3.28. For the second StringCollection // pass in the one from step 3.29. dlm.SetProperties(iRowId, strNames, strValues); // Build debug and test this code. In the DataManager // You should see a new value for the Manufacturer. } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }
public void getDataUsingManager() { Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor; try { PlantProject mainPrj = PlantApplication.CurrentProject; Project prj = mainPrj.ProjectParts["PnId"]; DataLinksManager dlm = prj.DataLinksManager; PromptIntegerOptions pOpts = new PromptIntegerOptions("Enter a PnPID number"); PromptIntegerResult prIntRes = ed.GetInteger(pOpts); if (prIntRes.Status == PromptStatus.OK) { // 3.13 Declare a List (System.Collections.Generic) Use KeyValuePair // with string for the two elements in the List. (Key, Value) List <KeyValuePair <string, string> > list_Properties; // 3.14 Instantiate the List from step 3.13. Use the // GetAllProperties method of the DataLinksManager // from above (dlm). For the RowId argument using the // Value property of the PromptIntegerResult from above. // (prIntRes) Use true for the current version property. list_Properties = dlm.GetAllProperties(prIntRes.Value, true); // 3.15 Declare a couple of strings. These will be used to get // the Key and the Value from the entries in the list. Name them // something like strKey and strValue. string strKey, strValue = null; // 3.16 Iterate through the entries in the list. // Use a for statement. Something like the example below. (Change // list_Properties to the name of the List from step 3.14. // for (int i = 0; i < list_Properties.Count; i++) // Note: put the closing curly brace below step 3.19 for (int i = 0; i < list_Properties.Count; i++) { // 3.17 Make the string for the key from // step 3.15 equal to the Key property of the list in // this iteration of the loop "[i]". (The list form step 3.14) strKey = list_Properties[i].Key; // 3.18 Make the string for the value from step 3.15 // equal to the Value property of the list in // this iteration of the loop "[i]" of the list form step 3.14 strValue = list_Properties[i].Value; // 3.19 Use the WriteMessage function of the editor (ed) and print // the values of the string with the key and the string with the // the value (from steps 3.17 and 3.18) Use "\n" for a return. // (add it to the end + "\n") // Build, debug and test this code. (Command "GetDataByMGR") // Note: Continue to step 3.20 below ed.WriteMessage("Key = " + strKey + " Value = " + strValue + "\n"); } } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }
public void pipeWalk() { // 4.1 Declare a variable as a PlantProject. Instantiate it using // the CurrentProject of PlantApplication PlantProject mainPrj = PlantApplication.CurrentProject; // 4.2 Declare a Project and instantiate it using // ProjectParts[] of the PlantProject from step 4.1 // use "Piping" for the name. This will get the Piping project Project prj = mainPrj.ProjectParts["Piping"]; // 4.3 Declare a variable as a DataLinksManager. Instantiate it using // the DataLinksManager property of the Project from 4.2. DataLinksManager dlm = prj.DataLinksManager; // PipingProject pipingPrj = (PipingProject) mainPrj.ProjectParts["Piping"]; // DataLinksManager dlm = pipingPrj.DataLinksManager; // Get the TransactionManager // Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = //AcadApp.DocumentManager.MdiActiveDocument.Database.TransactionManager; // Get the AutoCAD editor Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // Prompt the user to select a pipe entity PromptEntityOptions pmtEntOpts = new PromptEntityOptions("Select a Pipe : "); PromptEntityResult pmtEntRes = ed.GetEntity(pmtEntOpts); if (pmtEntRes.Status == PromptStatus.OK) { // Get the ObjectId of the selected entity ObjectId entId = pmtEntRes.ObjectId; // Use the using statement and start a transaction // Use the transactionManager created above (tm) using (Transaction tr = db.TransactionManager.StartTransaction()) { //#region transaction //try //{ // // 4.4 Declare a variable as a Part. Instantiate it using // // the GetObject Method of the Transaction created above (tr) // // for the ObjectId argument use the ObjectId from above (entId) // // Open it for read. (need to cast it to Part) //Part pPart = tr.GetObject(entId, OpenMode.ForRead); // // 4.5 Declare a variable as a PortCollection. Instantiate it // // using the GetPorts method of the Part from step 4.4. // // use PortType.All for the PortType. // PortCollection portCol = pPart.GetPorts(PortType.All); // (PortType.Both); // // 4.6 Use the WriteMessage function of the Editor created above (ed) // // print the Count property of the PortCollection from step 4.5. // // use a string similar to this: "\n port collection count = " // ed.WriteMessage("\n port collection count = " + portCol.Count); // // 4.7 Declare a ConnectionManager variable. // // (Autodesk.ProcessPower.PnP3dObjects.ConnectionManager) // // Instantiate the ConnectionManager variable by making it // // equal to a new Autodesk.ProcessPower.PnP3dObjects.ConnectionManager(); // ConnectionManager conMgr = new Autodesk.ProcessPower.PnP3dObjects.ConnectionManager(); // // 4.8 Declare a bool variable named bPartIsConnected and make it false // bool bPartIsConnected = false; // // 4.9 Use a foreach loop and iterate through all of the Port in // // the PortCollection from step 4.5. // // Note: Put the closing curly brace below step 4.18 // foreach (Port pPort in portCol) // { // // 4.10 Use the WriteMessage function of the Editor created above (ed) // // print the Name property of the Port (looping through the ports) // // use a string similar to this: "\nName of this Port = " + // ed.WriteMessage("\nName of this Port = " + pPort.Name); // // 4.11 Use the WriteMessage function of the Editor created above (ed) // // print the X property of the Position from the Port // // use a string similar to this: "\nX of this Port = " + // ed.WriteMessage("\nX of this Port = " + pPort.Position.X.ToString()); // // 4.12 Declare a variable as a Pair and make it equal to a // // new Pair(). // Pair pair1 = new Pair(); // // 4.13 Make the ObjectId property of the Pair created in step 4.10 // // equal to the ObjectId of the selected Part (entId) // pair1.ObjectId = entId; // // 4.14 Make the Port property of the Pair created in step 4.10 // // equal to the port from the foreach cycle (step 4.7) // pair1.Port = pPort; // // 4.15 Use an if else and the IsConnected method of the ConnectionManager // // from step 4.7. Pass in the Pair from step 4.12 // // Note: Put the else statement below step 4.17 and the closing curly // // brace for the else below step 4.18 // if (conMgr.IsConnected(pair1)) // { // // 4.16 Use the WriteMessage function of the Editor (ed) // // and put this on the command line: // // "\n Pair is connected " // ed.WriteMessage("\n Pair is connected "); // // 4.17 Make the bool from step 4.8 equal to true. // // This is used in an if statement in step 4.19. // bPartIsConnected = true; // } // else // { // // 4.18 Use the WriteMessage function of the Editor (ed) // // and put this on the command line: // // "\n Pair is NOT connected " // ed.WriteMessage("\n Pair is NOT connected "); // } // } // // 4.19 Use an If statement and the bool from step 4.8. This will be // // true if one of the pairs tested in loop above loop was connected. // // Note: Put the closing curly brace after step 4.26 // if (bPartIsConnected) // { // // 4.20 Declare an ObjectId named curObjID make it // // equal to ObjectId.Null // ObjectId curObjId = ObjectId.Null; // // 4.21 Declare an int name it rowId // int rowId; // // 4.22 Declare a variable as a ConnectionIterator instantiate it // // using the NewIterator method of ConnectionIterator (Autodesk.ProcessPower.PnP3dObjects.) // // Use the ObjectId property of the Part from step 4.4 // ConnectionIterator connectIter = ConnectionIterator.NewIterator(pPart.ObjectId); //need PnP3dObjectsMgd.dll // // You could Also use this, need to ensure that pPort is connected // // Use the ConnectionManager and a Pair as in the example above. // // conIter = ConnectionIterator.NewIterator(pPart.ObjectId, pPort); // // 4.23 Use a for loop and loop through the connections in the // // ConnectionIterator from step 4.22. The initializer can be empty. // // Use !.Done for the condition. use .Next for the iterator. // // Note: Put the closing curly brace after step 4.26 // for (; !connectIter.Done(); connectIter.Next()) // { // // 4.24 Make the ObjectId from step 4.20 equal to the ObjectId // // property of the ConnectionIterator // curObjId = connectIter.ObjectId; // // 4.25 Make the integer from step 4.21 equal to the return from // // FindAcPpRowId method of the DataLinksManager from step 4.3. // // pass in the ObjectId from step 4.24 // rowId = dlm.FindAcPpRowId(curObjId); // //4.26 Use the WriteMessage function of the Editor (ed) // // and pring the integer from step 4.25. Use a string similar to this // // this on the command line: // // "\n PnId = " + // ed.WriteMessage("\n PnId = " + rowId); // } // } //} //catch (System.Exception ex) //{ // ed.WriteMessage(ex.ToString()); //} //#endregion } } }
public static void Project_Structure() { // 1.5 Instantiate an variable as an Editor using // AcadApp.DocumentManager.MdiActiveDocument.Editor; // (AcadApp is the name of the variable from step 3) Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor; // 1.6 Declare a variable as a PlantProject. Instantiate it using // the CurrentProject of PlantApplication. (PlantApplication is a singleton) PlantProject currentProj = PlantApplication.CurrentProject; // 1.7 Declare a variable as a ProjectPartCollection. // Instantiate it using the ProjectParts property of the CurrentProject // of the PlantProject from step 1.6. ProjectPartCollection projPartCol = currentProj.ProjectParts; // 1.8 Use a foreach loop and iterate through the Projects in the // ProjectPartCollection from step 1.7. // Note: put the closing curley brace below step 1.17. foreach (Project proj in projPartCol) { // 1.9 Use WriteMessage() of the Editor (ed) created above to print // the name of ProjectName of the project. Use a string similar to this // "\nProject Name = " plus the ProjectName property of the Project. // (foreach is looping through projects) ed.WriteMessage("\nProject Name = " + proj.ProjectName); // 1.10 Declare a string and make it equal to the return from // the ProjectPartName method of the PlantProject from step 1.6 // Pass in the Project. Name it something like "projPartName". string projPartName = currentProj.ProjectPartName(proj); // 1.11 Use WriteMessage() of the Editor (ed) created above to print // the name of project part. Use a string similar to this // "\nProject Part Name = " plus the string from step 1.10 ed.WriteMessage("\nProject Part Name = " + projPartName); // 1.12 Declare a string variable and make it equal to // "\n Project Directory = " plus the ProjectDirectory property // of the Project (used in the foreach loop) string strMsg = "\n Project Directory = " + proj.ProjectDirectory; // 1.13 Use the WriteMessage function of the Editor variable from // step 1.5 Pass in the string from step 1.12. This will print the // ProjectDirectory on the commmand line. ed.WriteMessage(strMsg); // 1.14 Declare a List<> of PnPProjectDrawing named dwgList // instantiate it using the GetPnPDrawingFiles() of the project List <PnPProjectDrawing> dwgList = proj.GetPnPDrawingFiles(); // 1.15 Use a foreach loop and iterate through the PnPProjectDrawing // in the List<> from step 1.14 // Note: put the closing curley brace below step 1.17 foreach (PnPProjectDrawing dwg in dwgList) { // 1.16 Declare a string variable and make it equal to // "\n Absolute File name = " + the AbsoluteFileName of PnPProjectDrawing // variable used in this foreach loop (step 1.16) strMsg = "\n Absolute File name = " + dwg.AbsoluteFileName; // 1.17 Use the WriteMessage function of the Editor variable from // step 1.5. Pass in the string from step 1.16 This will print the // AbsoluteFileName on the commmand line. ed.WriteMessage(strMsg); } } }
public static void Project_Settings() { Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor; // 1.18 Declare a variable as a PlantProject. Instantiate it using // the CurrentProject of PlantApplication. (PlantApplication is a singleton) PlantProject currentProj = PlantApplication.CurrentProject; try { // 1.19 Declare a variable as a PnIdProject. Instantiate it using the // ProjectParts property of the CurrentProject of the PlantProject from step 1.18. // Use square brackets [] to specify a string identifier. For the string // property use "PnID". Need to cast it using (PnIdProject) // Note: Need to add a reference to PnIdProjectPartsMgd.dll this is dll is Missing // from the PlantSDK. Reference it from the ACAD.EXE folder instead. PnIdProject PnIdProj = (PnIdProject)currentProj.ProjectParts["PnID"]; // 1.20 Use the WriteMessage function of the editor created above (ed). // Print out the GapWidth property of the PnIdProject from step 1.19. // Use a string similar to this: "\nGap Width = " + use ToString() ed.WriteMessage("\nGap Width = " + PnIdProj.GapWidth.ToString()); // 1.21 Change the GapWidth property to .125 PnIdProj.GapWidth = .125; // 1.22 Use the WriteMessage function of the editor created above (ed). // Print out the value of the GapLoop property of the PnIdProject from step 1.19. // Use a string similar to this: "\nGapLoop = " + use ToString(). // (this value is a bool) ed.WriteMessage("\nGapLoop = " + PnIdProj.GapLoop().ToString()); // 1.23 Declare a variable as a PipingProject. Instantiate it using the // ProjectParts property of the CurrentProject of the PlantProject from step 1.18. // Use square brackets [] to specify a string identifier. For the string // property use "Piping". Need to cast it using (PipingProject) // Note: Need to add a reference to PnP3dProjectPartsMgd.dll for PipingProject PipingProject pipeProj = (PipingProject)currentProj.ProjectParts["Piping"]; // 1.24 Use the WriteMessage function of the editor created above (ed). // Print out the Minimum Pipe Length property of the PipingProject from step 1.23. // Use a string similar to this: "\nMinimum Pipe Length = " + use ToString() ed.WriteMessage("\nMinimum Pipe Length = " + pipeProj.MinimumPipeLength.ToString()); // 1.25 Change the MinimumPipeLength property of the PipingProject from step 1.23. // to 10 pipeProj.MinimumPipeLength = 10; // 1.26 Change the WeldGapSize property of the PipingProject from step 1.23. // to .125 pipeProj.WeldGapSize = .125; // 1.27 Use the WriteMessage function of the editor created above (ed). // Print out the value of UseWeldGaps of the PipingProject from step 1.23. // Use a string similar to this: "\nUse Weld Gaps = " + use ToString() ed.WriteMessage("\nUse Weld Gaps = " + pipeProj.UseWeldGaps.ToString()); // 1.28 Change the UseWeldGaps of the PipingProject to true pipeProj.UseWeldGaps = true; } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }
public void AddProperties() { //file name to save different data string filePath = @"C: \Users\LinP3\source\repos\Plant3DApiTester\Resource\plant_3d_AddProperties.txt"; ///all available drawing type string DwgType_Piping = "Piping"; string DwgType_PnId = "PnId"; //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(filePath, $"{doc.Name}\n"); //get current project PlantProject mainPrj = PlantApplication.CurrentProject; Project pipingPrj = mainPrj.ProjectParts[DwgType_Piping]; DataLinksManager dlm = pipingPrj.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; } PnPDatabase pDB = dlm.GetPnPDatabase(); PnPTable pTable = pDB.Tables["EngineeringItems"]; string colName = "Api_added_Property1"; bool isInTable = false; if (pTable != null) { ///list all columns in table PnPColumns columns = pTable.AllColumns; foreach (PnPColumn col in columns) { File.AppendAllText(filePath, col.Name + Environment.NewLine); if (colName == col.Name) { isInTable = true; } } ///add new column try { if (isInTable == false) { PnPColumn newCol = new PnPColumn(colName, typeof(string), 256); newCol.DefaultValue = "api default value"; pTable.Columns.Add(newCol); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(ex.ToString()); } } }
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 }
public void Retrive_Project_Data() { //file name to save different data string filePath = @"C: \Users\LinP3\source\repos\Plant3DApiTester\Resource\plant_3d_data.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(filePath, $"{doc.Name}\n"); ///get user data var userdata = doc.UserData.Values; foreach (var d in userdata) { File.AppendAllText(filePath, "user data:" + d.ToString()); } ///get application information File.AppendAllText(filePath, "application info? " + AcadApp.AcadApplication.ToString()); ///last version var version = db.LastSavedAsVersion; File.AppendAllText(filePath, "version: " + version); File.AppendAllText(filePath, "OriginalFileVersion: " + db.OriginalFileVersion); print(db, "db", filePath); print(doc, "doc", filePath); //get current project PlantProject mainPrj = PlantApplication.CurrentProject; Project pipingPrj = mainPrj.ProjectParts[DwgType_Piping]; Project pnIdPrj = mainPrj.ProjectParts[DwgType_PnId]; Project orthoPrj = mainPrj.ProjectParts[DwgType_Ortho]; Project isoPrj = mainPrj.ProjectParts[DwgType_Iso]; DataLinksManager dlm = pipingPrj.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; } //print information about main Project and Piping Project print(mainPrj, "mainPrj", filePath); print(pipingPrj, "pipingPrj", filePath); #region AutogenPropertyKeys for piping project == what is in AutogenProperty? StringCollection askeys; pipingPrj.GetAutogenPropertyKeys(true, out askeys); string autogenPropertyStr = getHeaderText("GetAutogenPropertyKeys"); foreach (String sKey in askeys) { int nLastUsed, nIncrement; pipingPrj.GetProjectAutogenPropertyValue(sKey, out nLastUsed, out nIncrement); autogenPropertyStr += "\n" + sKey + ": " + "Last (" + nLastUsed + ") Increment (" + nIncrement + ")"; } #endregion #region add/display project detail information ///add new category and property try { string catName = "ProjectDBInfo"; string property1 = "SQLServerInstance"; ProjectCategory newCat = new ProjectCategory(catName, "Information of Project DB"); /////all four database must be wrote, so that it will be visiable in Project Setup ///add to pipingProject, the PnPProject under Piping.dcf addCategoryAndPropertyToProject(pipingPrj, newCat, property1, "Instance of Server", "LocalInstance"); ///add to PnId project, the PnPProject table under ProcessPower.dcf addCategoryAndPropertyToProject(pnIdPrj, newCat, property1, "Instance of Server", "LocalInstance"); ///add to Iso Project, the PnPProject table under Iso.dcf addCategoryAndPropertyToProject(isoPrj, newCat, property1, "Instance of Server", "LocalInstance"); ///add to Ortho Project, the PnPProject table under Ortho.dcf addCategoryAndPropertyToProject(orthoPrj, newCat, property1, "Instance of Server", "LocalInstance"); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(ex.ToString()); } ///look through all project details int no = 1; List <ProjectCategory> projectCategories = pipingPrj.GetProjectCategoryMetadata(); foreach (ProjectCategory projectCategory in projectCategories) { print(projectCategory, $"({no})projectCategory", filePath); List <ProjectProperty> projectProperties = pipingPrj.GetProjectPropertyMetadata(projectCategory.Name); int no2 = 1; foreach (ProjectProperty projectProperty in projectProperties) { print(projectProperty, $"({no}.{no2})projectProperty", filePath); File.AppendAllText(filePath, $"({no}.{no2}){projectProperty.Name} = {pipingPrj.GetProjectPropertyValue(projectProperty)}\n"); no2++; } no++; } #endregion }