.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Find all blocks on a certain layer

1 REPLY 1
SOLVED
Reply
Message 1 of 2
Anonymous
981 Views, 1 Reply

Find all blocks on a certain layer

I want to find all the blocks, lines, circles etc. on a layer. I have found two possible way of doing this but I am having problems with  them both. My first method was from here where I used something like:

 

        public static void LayerInfoGrab(string LayerName, int LayerType)
        {
            //link to the current document and databases
            Document Doc = Application.DocumentManager.MdiActiveDocument;
            Database db = Doc.Database;
            Editor ed = Doc.Editor;

            using (Transaction Trans = db.TransactionManager.StartTransaction())
            {
                //Open the block table open for read
                BlockTable Btr = Trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                //Open the model space database for read
                BlockTableRecord BtrModel = Trans.GetObject(Btr[BlockTableRecord.ModelSpace],OpenMode.ForRead) as BlockTableRecord;

                //Step through the objects and find any on the layer we want
                foreach (object SPAobject in BtrModel)
                {
                    //Test the object for block type
                    if (SPAobject is Line)
                    {

                    }

                    if (SPAobject is Circle)

But (SPAobject is Block) does not work. My second method is the selection filter but I don't have a name for the block, but I do want all the blocks on the layer. I didn't know if I could use something like "*" instead of the block name?

2018-08-18.png

 

 

 

1 REPLY 1
Message 2 of 2
_gile
in reply to: Anonymous

Hi,

 

Your question is not clear. Do you want to get all blocks (more accurately block references) on a layer or all entities (including bloc references, lines, circles, etc.) ?

 

To get all model space entities on a specified layer:

Iteratiing the model space

 

        static ObjectIdCollection GetByLayer(string layerName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                foreach (ObjectId id in ms)
                {
                    var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                    if (ent.Layer.Equals(layerName, StringComparison.CurrentCultureIgnoreCase))
                        ids.Add(id);
                }
                tr.Commit();
            }
            return ids;
        }

Using a selection filter

 

 

        static ObjectIdCollection SelectByLayer (string layerName)
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var filter = new SelectionFilter(new[] 
            {
                new TypedValue(8, layerName),   // layer
                new TypedValue(410, "Model")   // model space
            });
            var psr = ed.SelectAll(filter);
            if (psr.Status == PromptStatus.OK)
                return new ObjectIdCollection(psr.Value.GetObjectIds());
            else
                return new ObjectIdCollection();
        }

 

To get all model space block references on a specified layer:

Iteratiing the model space

        static ObjectIdCollection GetBlocksByLayer(string layerName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                foreach (ObjectId id in ms)
                {
                    if (id.ObjectClass.DxfName == "INSERT")
                    {
                        var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                        if (ent.Layer.Equals(layerName, StringComparison.CurrentCultureIgnoreCase))
                            ids.Add(id);
                    }
                }
                tr.Commit();
            }
            return ids;
        }

Using a selection filter

        static ObjectIdCollection SelectBlocksByLayer(string layerName)
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var filter = new SelectionFilter(new[]
            {
                new TypedValue(0, "INSERT"),    // block reference
                new TypedValue(8, layerName),   // layer
                new TypedValue(410, "Model")    // model space
            });
            var psr = ed.SelectAll(filter);
            if (psr.Status == PromptStatus.OK)
                return new ObjectIdCollection(psr.Value.GetObjectIds());
            else
                return new ObjectIdCollection();
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta