private static void setPartGeometry(PipingProject prjpart, ObjectId objId, int rowId) { Database db = objId.Database; DataLinksManager dlm = prjpart.DataLinksManager; StringCollection props = new StringCollection(); StringCollection vals = new StringCollection(); using (Transaction trans = db.TransactionManager.StartTransaction()) { try { PnP3d.Part part = trans.GetObject(objId, OpenMode.ForRead) as PnP3d.Part; //if (part is Pipe) if (part is PnP3d.Pipe) { PnP3d.Pipe pipe = (PnP3d.Pipe)part; props.Add("Length"); vals.Add(pipe.Length.ToString()); props.Add("CutLength"); vals.Add(pipe.Length.ToString()); props.Add("Position X"); vals.Add(pipe.StartPoint.X.ToString()); props.Add("Position Y"); vals.Add(pipe.StartPoint.Y.ToString()); props.Add("Position Z"); vals.Add(pipe.StartPoint.Z.ToString()); } // else if () // Handle other Plant3D entity types // { // } // else // { // } dlm.SetProperties(rowId, props, vals); trans.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { // display exception on the command line Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(ex.ToString()); trans.Abort(); } } }
private static int createUnassignedLineGroup(PipingProject prjpart) { // Create a new line group in project database DataLinksManager dlm = prjpart.DataLinksManager; PnPDatabase db = dlm.GetPnPDatabase(); PnPTable tbl = db.Tables["P3dLineGroup"]; PnPRow row = tbl.NewRow(); tbl.Rows.Add(row); return(row.RowId); }
private static void assignToLineGroup(PipingProject prjpart, ObjectId partId, int cacheId, int groupId) { // Assign an entity's row to line group DataLinksManager dlm = prjpart.DataLinksManager; PnPDatabase db = dlm.GetPnPDatabase(); // Relate objects to line group dlm.Relate("P3dLineGroupPartRelationship", "LineGroup", groupId, "Part", cacheId); // Also relate drawing to line group Database acdb = partId.Database; int dwgId = dlm.GetDrawingId(acdb); if (dwgId != -1) { dlm.Relate("P3dDrawingLineGroupRelationship", "Drawing", dwgId, "LineGroup", groupId); } }
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(); } }
private static int AddPartToProject(PipingProject prjpart, string strSpec, ObjectId partId, PartRep.SpecPart specPart) { Autodesk.ProcessPower.DataObjects.PnPDatabase db = prjpart.DataLinksManager.GetPnPDatabase(); PartsRepository rep = PartsRepository.AttachedRepository(db, false); // Create new part in project Autodesk.ProcessPower.PartsRepository.Part part = rep.NewPart(specPart.PartType); rep.AutoAccept = false; // Assign property values in project StringCollection props = specPart.PropertyNames; for (int i = 0; i < props.Count; ++i) { PartProperty prop = rep.GetPartProperty(specPart.PartType, props[i], false); if (prop == null || prop.IsExpression) { continue; // can't be assigned } try { part[props[i]] = specPart[props[i]]; } catch (Autodesk.AutoCAD.Runtime.Exception ex) { // display exception on the command line Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(ex.ToString()); } } // assign special spec property // try { part["Spec"] = strSpec; } catch (Autodesk.AutoCAD.Runtime.Exception ex) { // display exception on the command line Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(ex.ToString()); } // add reference to spec record only if it is not set yet // try { if (part["SpecRecordId"] == System.DBNull.Value) { part["SpecRecordId"] = specPart.PartId; } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { // display exception on the command line Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(ex.ToString()); } // Ok now deal with the ports // Autodesk.ProcessPower.PartsRepository.PortCollection ports = specPart.Ports; Autodesk.ProcessPower.PartsRepository.Port principal_port = ports[0]; foreach (Autodesk.ProcessPower.PartsRepository.Port port in ports) { System.Guid sizeRecId = System.Guid.Empty; if (string.Compare(port.Name, principal_port.Name) != 0) { sizeRecId = (System.Guid)port["SizeRecordId"]; } Autodesk.ProcessPower.PartsRepository.Port newPort = null; bool bNew = true; bool bNeedAccept = false; // Principal port is embedded. // if (sizeRecId != System.Guid.Empty) { newPort = part.NewPortBySizeRecordId(port.Name, sizeRecId.ToString(), out bNew); bNeedAccept = true; } else { newPort = part.NewPort(port.Name); } if (bNew) { foreach (string prop in port.PropertyNames) { if (string.Compare(prop, "PortName", true) == 0) { continue; // dont copy port name } try { newPort[prop] = port[prop]; } catch (Autodesk.AutoCAD.Runtime.Exception ex) { // display exception on the command line Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(ex.ToString()); } } if (bNeedAccept) { try { rep.CommitPort(newPort); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { // display exception on the command line Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(ex.ToString()); } } } part.Ports.Add(newPort); } // Add new part to the project database rep.AddPart(part); // Transform properties sentitive to current project's unit settings switch (prjpart.ProjectUnitsType) { case ProjectUnitsType.eMetric: part.TransformPropertiesToUnits(Units.Mm, Units.Mm); break; case ProjectUnitsType.eMixedMetric: part.TransformPropertiesToUnits(Units.Mm, Units.Inch); break; case ProjectUnitsType.eImperial: part.TransformPropertiesToUnits(Units.Inch, Units.Inch); break; } int cacheId = part.PartId; // Finally now we can link entity to row in project if (cacheId != -1) { Autodesk.ProcessPower.DataLinks.DataLinksManager dlm = prjpart.DataLinksManager; try { dlm.Link(partId, cacheId); } catch { cacheId = -1;; } } return(cacheId); // -1 for error }
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()); } }