Okay here is a full dump of code I'm running right now to test this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
namespace ClassLibrary2
{
public class Material
{
public String BlockName { get; set; }
public String BlockID { get; set; }
public String Function { get; set; }
public String Tag { get; set; }
public String Type { get; set; }
public String Size { get; set; }
public String DrawingNumber { get; set; }
/// <summary>
/// The Sync Change type, the default value is 'Unspecified'
/// </summary>
public ChangeTypeCode ChangeType { get; set; }
}
public enum ChangeTypeCode
{
Unspecified = 0,
Same = 1,
Updated = 2,
Deleted = 3,
New = 4,
UpdatedFromEDM = 5
}
public class Class1
{
[CommandMethod("TestDB")]
public static void CollectMaterials()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
string dwg = "C:\\E001977 EDM Testing\\4. Drafting\\P&ID\\E001977-03-116 SHT 1.DWG";
using (Database db = new Database(false, false))
{
db.ReadDwgFile(dwg, FileShare.ReadWrite, false, "");
List<Material> dwgComponents = CollectMaterials(db, ed, dwg);
foreach (Material material in dwgComponents)
{
ed.WriteMessage("\nHandle: {0} Block Name: {1} Tag: {2}", material.BlockID, material.BlockName, material.Tag);
}
}
}
public static List<Material> CollectMaterials(Database db, Editor ed, string filename)
{
List<Material> dwgComponents = new List<Material>();
//purge unreferenced entities
ObjectIdCollection purgedIds = new ObjectIdCollection();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead, true) as BlockTable;
foreach (ObjectId btrid in bt)
{
BlockTableRecord btr = tr.GetObject(btrid, OpenMode.ForRead, true) as BlockTableRecord;
if (btr.XrefStatus == XrefStatus.NotAnXref)
{
foreach (ObjectId oid in btr)
{
purgedIds.Add(oid);
}
}
}
tr.Commit();
}
db.Purge(purgedIds);
foreach (ObjectId oid in purgedIds)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = (Entity)tr.GetObject(oid, OpenMode.ForWrite, true);
try
{
ent.Erase();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message);
}
tr.Commit();
}
}
try
{
bool fixErrors = true;
bool echo = true;
db.Audit(fixErrors, echo);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message);
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary gd = (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead, false);
foreach (DBDictionaryEntry gdentry in gd)
{
ObjectId groupid = gdentry.Value;
Group dictionarygroup = (Group)tr.GetObject(groupid, OpenMode.ForRead, false);
ResultBuffer rb = dictionarygroup.GetXDataForApplication("CFD1");
if (rb != null)
{
//group entity
//need to find tag and shortdesc
Material comp = new Material();
bool exclude = false;
string tag = "";
string shortdesc = "";
string blockname = "";
string blockid = dictionarygroup.Handle.ToString();
foreach (ObjectId entityid in dictionarygroup.GetAllEntityIds())
{
Entity ent = (Entity)tr.GetObject(entityid, OpenMode.ForRead, false);
if ((ent as BlockReference != null))
{
BlockReference bref = ent as BlockReference;
if (bref.Name == "LABEL_EQUIP" || bref.Name == "TAG_1_I")
{
foreach (ObjectId attid in bref.AttributeCollection)
{
AttributeReference attref = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead, false);
switch (attref.Tag)
{
case "TAG":
{
tag = attref.TextString;
if (tag.ToUpper().Contains("%%U"))
{
tag = tag.ToUpper().Replace("%%U", "");
}
break;
}
case "SHORTDESC":
shortdesc = attref.TextString;
break;
}
}
}
}
if ((shortdesc.ToUpper().Contains("BOTTLE") || shortdesc.ToUpper().Contains("SCRUBBER")))
exclude = true;
if (shortdesc.ToUpper().Contains("CYLINDER"))
exclude = true;
if (shortdesc.ToUpper().Contains("COMPRESSOR FRAME"))
exclude = true;
if (shortdesc.ToUpper().Contains("JUNCTION BOX"))
exclude = true;
}
if (!exclude)
{
comp.Tag = tag;
comp.BlockID = blockid;
comp.DrawingNumber = filename;
comp.BlockName = blockname;
if (!string.IsNullOrEmpty(tag))
dwgComponents.Add(comp);
}
}
}
tr.Commit();
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead, false) as BlockTable;
foreach (ObjectId btrid in bt)
{
BlockTableRecord btr = tr.GetObject(btrid, OpenMode.ForRead, false) as BlockTableRecord;
if (btr.XrefStatus == XrefStatus.NotAnXref)
{
foreach (ObjectId oid in btr)
{
bool exclude = false;
Entity ent = (Entity)tr.GetObject(oid, OpenMode.ForRead, false);
ResultBuffer rb = ent.GetXDataForApplication("CFD1");
if (rb != null)
{
TypedValue[] tv = rb.AsArray();
string cadworxComponentType = tv[8].Value.ToString();
switch (cadworxComponentType)
{
case "301":
exclude = true;
break;
case "306":
exclude = true;
break;
case "307":
exclude = true;
break;
case "308":
exclude = true;
break;
case "341":
exclude = true;
break;
}
if ((ent as BlockReference != null) && !exclude)
{
BlockReference bref = ent as BlockReference;
string brefName = null;
if (bref.IsDynamicBlock)
{
BlockTableRecord dynBtr = (BlockTableRecord)tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false);
brefName = dynBtr.Name;
}
else
{
brefName = bref.Name;
}
if (brefName.ToUpper().Contains("DYN_CO_") || brefName.ToUpper().Contains("DYN_LO_") || brefName.ToUpper().Contains("DYN_SH_") || brefName.ToUpper().Contains("DYN_JB") || brefName.ToUpper().Contains("DYN_PI_") || brefName.ToUpper().Contains("NOZZLE_"))
{
// hmi/plc/safety component, do not include
exclude = true;
}
if (!exclude)
{
Material comp = new Material();
string size = null;
string function = null;
string tag = null;
string type = null;
string blockid = bref.Handle.ToString();
foreach (ObjectId attid in bref.AttributeCollection)
{
AttributeReference attref = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead, false);
switch (attref.Tag)
{
case "SIZE":
size = attref.TextString;
break;
case "TAG":
{
tag = attref.TextString;
if (tag.ToUpper().Contains("%%U"))
{
tag = tag.ToUpper().Replace("%%U", "");
}
break;
}
case "TYPE":
type = attref.TextString;
break;
case "FUNCTION":
function = attref.TextString;
break;
}
}
if (size == "")
size = null;
comp.Size = size;
if (function == "")
function = null;
comp.Function = function;
comp.Tag = tag;
if (type == "")
type = null;
comp.Type = type;
comp.BlockID = blockid;
comp.DrawingNumber = filename;
comp.BlockName = brefName;
if (!string.IsNullOrEmpty(tag))
dwgComponents.Add(comp);
}
}
}
}
}
}
tr.Commit();
}
return dwgComponents;
}
}
}And here is the result of me running the command:
Command: TESTDB
Auditing Header
Auditing Tables
Auditing Entities Pass 1
Pass 1 6800 objects audited
Auditing Entities Pass 2
Pass 2 6800 objects audited
Auditing Blocks
101 Blocks audited
Auditing AcDsRecords
Total errors found 0 fixed 0
Erased 0 objects
Handle: 1265C Block Name: Tag: F-1101K
Handle: 1265F Block Name: Tag: SL-1101
Handle: 1466F Block Name: FLEX_HOSE Tag: SP-1608
Handle: 14735 Block Name: Y_STRAINER Tag: SCN1
Handle: 1266F Block Name: DYN_DI_LOCAL Tag: F1101K
Handle: 12677 Block Name: DYN_DI_LOCAL Tag: F1101K
Handle: 1268D Block Name: DYN_VAL_BALL Tag: BSN2,PL
Handle: 12696 Block Name: DYN_VAL_BALL Tag: BSN2,PL
Handle: 126D6 Block Name: DYN_VAL_NEEDLE_BLEED Tag: NSM2
Handle: 1271A Block Name: DYN_DI_LOCAL Tag: 1101K1
Handle: 12726 Block Name: DYN_VAL_NEEDLE_BLEED Tag: NSM2
Handle: 127A7 Block Name: DYN_DI_LOCAL Tag: SRV
Handle: 127C3 Block Name: DYN_DI_LOCAL Tag: SRV
Handle: 1281A Block Name: BLEED_WITH_VALVE Tag: BR-1101K1
Handle: 12820 Block Name: DYN_VAL_RELIEF_F Tag: PSV-1101K1
Handle: 12828 Block Name: DYN_VAL_NEEDLE_BLEED Tag: NSM2
Handle: 12832 Block Name: DYN_VAL_BALL_F Tag: BC12,PL
Handle: 12879 Block Name: FLEX_HOSE_FLG Tag: NX-1101N1
Handle: 128CF Block Name: DYN_VAL_CHECK_SW_L Tag: CCS2
Handle: 128E2 Block Name: DYN_VAL_BALL_F Tag: BC12,PL
Handle: 12926 Block Name: BLEED_WITH_VALVE Tag: BR-1101K3
Handle: 1292C Block Name: DYN_VAL_RELIEF_F Tag: PSV-1101K3
Handle: 12934 Block Name: DYN_VAL_NEEDLE_BLEED Tag: NSM2
Handle: 1293E Block Name: DYN_VAL_BALL_F Tag: BC12,PL
Handle: 12968 Block Name: BLEED_WITH_VALVE Tag: BR-1101K2
Handle: 1296E Block Name: DYN_VAL_NEEDLE_BLEED Tag: NSM2
Handle: 12978 Block Name: DYN_VAL_BALL_F Tag: BC12,PL
Handle: 129A1 Block Name: DYN_VAL_RELIEF_F Tag: PSV-1101K2
Handle: 129EA Block Name: DYN_VAL_BALL_F Tag: BC12,PL
Handle: 12A51 Block Name: DYN_DI_LOCAL Tag: F1101K
Handle: 12A8B Block Name: FLEX_HOSE Tag: SP-1506
Handle: 12A91 Block Name: DYN_VAL_CHECK Tag: CST4
Handle: 12AFD Block Name: FLEX_HOSE_FLG Tag: NX-1101M1
Handle: 12B1A Block Name: DYN_VAL_GATE Tag: NSM2
Handle: 12B43 Block Name: DYN_VAL_GATE Tag: NSM2
Handle: 12BAE Block Name: DYN_VAL_BALL Tag: BC12,PL
Handle: 12C2D Block Name: DYN_DI_LOCAL Tag: F1101K
Handle: 12C4B Block Name: Dyn_Val_Ctrl_Ball_F Tag: PCV-1503
And here is a screenshot of the drawing, opened, with only two "tagged" components in the drawing:

I don't understand where all those other entities that are being reported are coming from.