简体   繁体   中英

How do I get all the entities on a specific layer in a .DWG file without opening the file (C#.NET/AutoCAD)?

I am writing a C#.NET program that interacts with AutoCAD through the AutoCAD .NET API. The program loops through DWG files in a directory and checks every text entity on the "testLayer" layer to see if it matches "testText". I got this to work by opening up every file and making a Selectionfilter to get all of the entities on the "testLayer" layer.

        Application.DocumentManager.Open(curfile.FullName, false);
        ....
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;

        using (Transaction acTrans = doc.TransactionManager.StartTransaction())
        {
            ObjectIdCollection ents = new ObjectIdCollection();

            // Set up filter and filter on layer name
            TypedValue[] tvs = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, "testLayer")};
            SelectionFilter sf = new SelectionFilter(tvs);
            PromptSelectionResult psr = ed.SelectAll(sf);

            if (psr.Status == PromptStatus.OK)
            {
                // Get the object ids for all of the entities for the filtered layer
                ents = new ObjectIdCollection(psr.Value.GetObjectIds());

                foreach (ObjectId objid in ents)
                {
                    DBText dbText = acTrans.GetObject(objid, OpenMode.ForRead) as DBText;
                    if (dbText.TextString.Contains("testText")
                    {
                         return dbText.TextString;
                    }
                }
                return "";
            }
            else
            {
                return "";
            }
        }           
    }

But now I am converting my program to side-load the underlying databases because it was taking too long for the program to open and close every .DWG file. The problem is that now I am using

db.ReadDwgFile(currentDWG, FileOpenMode.OpenForReadAndAllShare, true, string.Empty);

to read files without actually opening them so I can't use

Editor ed = Application.DocumentManager.MdiActiveDocument.Editor

and ed.SelectAll(sf) for the selection filter strategy I was using earlier because the document isn't actually open. So how can I can get all of the text entities on each layer named "testLayer" without actually opening the DWG file?

In a 'side database', to mimic SelectAll, you have to iterate through all entities in all the layouts and check the entity layer.

EDIT: In a 'side database', to mimic SelectAll, you have to iterate through all entities in all the layouts and check the entity type and layer.

    private IEnumerable<ObjectId> GetTextEntitiesOnLayer(Database db, string layerName)
    {
        using (var tr = db.TransactionManager.StartOpenCloseTransaction())
        {
            var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
            foreach (ObjectId btrId in blockTable)
            {
                var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                var textClass = RXObject.GetClass(typeof(DBText));
                if (btr.IsLayout)
                {
                    foreach (ObjectId id in btr)
                    {
                        if (id.ObjectClass == textClass)
                        {
                            var text = (DBText)tr.GetObject(id, OpenMode.ForRead);
                            if (text.Layer.Equals(layerName, System.StringComparison.CurrentCultureIgnoreCase))
                            {
                                yield return id;
                            }
                        }
                    }
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM