Active Member
Posts: 6
Registered: ‎07-31-2012
Message 1 of 3 (196 Views)
Accepted Solution

Search block by name

196 Views, 2 Replies
07-31-2012 06:04 AM

Hi at all, my name is Daniele.

I started to develop autocad's application with .Net framework.

But i have some trouble, particularly when i try to find a block by name (not like id).

I would take a block by name, instead to get all block and find one with comparison.

How can resolve this problem?

Thank's very much, and sorry for my bad english



*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 3 (178 Views)

Re: Search block by name

07-31-2012 08:26 AM in reply to: Danius87

Try this code, not seriously tested though

This will select all blocks by name on the current layout


        [CommandMethod("AllBlockByName","abb", CommandFlags.Modal)]
        public void GetAllBlockRefsByName()
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            ObjectIdCollection objIds = new ObjectIdCollection();
            string ctab = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("ctab").ToString();
            string blockname = ed.GetString("\nEnter a block name: ").StringResult;
            using (Transaction tr = db.TransactionManager.StartTransaction())
                string bname = string.Concat( blockname,",*`U*");
                TypedValue[] tvs = new TypedValue[] { 
              new TypedValue(-4, "<and"),
              new TypedValue(0, "insert"),
              new TypedValue(2, bname),
              new TypedValue(410, ctab),
              new TypedValue(-4, "and>")
                SelectionFilter sf = new SelectionFilter(tvs);

                PromptSelectionResult psr = ed.SelectAll(sf);
                if (psr.Status != PromptStatus.OK)
                    ed.WriteMessage("\nWrong selection\n");

                ed.WriteMessage("\nCount of  selected block: {0}", psr.Value.Count);
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btrec = null;
                string brefname= "";
                foreach (SelectedObject selobj in psr.Value)
                    DBObject obj = (DBObject)tr.GetObject(selobj.ObjectId, OpenMode.ForRead,false) ;
                    BlockReference bref = obj as BlockReference;
                    if (bref == null) continue;
                    if (!bref.IsDynamicBlock)
                        brefname = bref.Name;
                        btrec = (BlockTableRecord)tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false, true);
                        brefname = btrec.Name;

                    if (brefname.CompareTo(blockname) == 0)

                        for (int n=0;n< objIds.Count;n++)
                        DBObject ent = (DBObject)tr.GetObject(objIds[n], OpenMode.ForRead, false);

                        BlockReference btref = ent as BlockReference;
                        ed.WriteMessage("\nPosition: {0}\n", btref.Position);

                        //  [  do your rest work with block instances here  ]  //


 Here is better explained article thanks to Stephen Preston:




Valued Mentor
Posts: 366
Registered: ‎03-31-2005
Message 3 of 3 (177 Views)

Re: Search block by name

07-31-2012 08:27 AM in reply to: Danius87

Hi Daniele - welcome aboard!


Please use the search tool 1st - this question is very common.  Enter your subject as is and start reading.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community