I hope someone can help me with this. I'm writing a self made command for Autocad P&ID 2014 in C#. With this method, I want to search for a tag in my project and draw a circle around it. At the moment, I can give in my self made command, named 'GETTAG', give in a tagvalue in the editor and then my self made command searches for this tag in the modelspace of the 'MdiActiveDocument'. If the tag is found, a circle is drawn around the tag. This is achieved by searching the BlockTableRecord of the ModelSpace of the current drawing.
editor = AcadApp.DocumentManager.MdiActiveDocument.Editor;
DB = AcadApp.DocumentManager.MdiActiveDocument.Database
trans.TransactionManager transActionManager = DB.TransactionManager;
using (Transaction transAction = transActionManager.StartTransaction())
BlockTable blockTable = (BlockTable)transAction.GetObject(DB.BlockTableId, OpenMode.ForRead);
BlockTableRecord blockTableRecord = (BlockTableRecord)transAction.GetObject(blockTable
// An P&Id object is a Linesegment or an Asset
BlockTableRecordEnumerator iter = blockTableRecord.GetEnumerator();
DBObject dbObject = transAction.GetObject(iter.Current, OpenMode.ForRead);
LineSegment lineSegm = dbObject as LineSegment;
if (lineSegm != null)
if (lineSegm.TagValue == tagName)
strMsg = "\nLinesegment with tagvalue: " + lineSegm.TagValue + " is from class: " + lineSegm.ClassName;
Asset asset = dbObject as Asset;
if (asset != null)
if (asset.TagValue == tagName)
strMsg = "\nAsset with tagvalue: " + asset.TagValue + " is from class: " + asset.ClassName;
But what if I give in a tagvalue, that is placed on a other drawing then the active drawing? Then my method has to check every drawing which belong to the project and then, if the tag is found, my method has to open this drawing and then draw a circle around the tag.
My big question is, how do I iterate trough all the drawings of my project (whitout opening the drawings) and search for the correct tag? Can I acces the modelspaces of the drawings without opening the drawings (with Documentmanager.Open())
If someone can help me, please let me know.
Solved! Go to Solution.
Database.ReadDwgFile() allows you to open an access DWG files without having to open them in the AutoCAD editor.
You would have to use that to search for something in a file that isn't open in the editor.
Ok, many thanks for your reply. I'll try if this works. On which database should I execute the method "ReadDwgFile()"? The database of the active drawing? Or do I have to create an variable of the type "Database"?
You have to create a Database using the constructor (new Database(), and use the correct parameters for reading in a file, and you must ensure that the Database is disposed when you are done using it.
Search here for 'ReadDwgFile()' and you should find some examples.
Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register