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

Search block by name

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
TesiferSrl
707 Views, 2 Replies

Search block by name

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

 

Daniele

2 REPLIES 2
Message 2 of 3
Hallex
in reply to: TesiferSrl

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");

                    return;
                }
                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;
                    }
                    else
                    {
                        btrec = (BlockTableRecord)tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false, true);
                        brefname = btrec.Name;
                    }


                    if (brefname.CompareTo(blockname) == 0)
                    {
                        objIds.Add(bref.ObjectId);
                    }


                        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  ]  //
                    }
                    
                }

                tr.Commit();
            }
        }

 Here is better explained article thanks to Stephen Preston:

http://adndevblog.typepad.com/autocad/2012/06/finding-all-block-references-of-a-dynamic-block.html

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 3
fieldguy
in reply to: TesiferSrl

Hi Daniele - welcome aboard!

 

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

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost