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 }