Insert Multiple Blocks Within One DWG

Insert Multiple Blocks Within One DWG

Hugh_Compton
Advisor Advisor
1,472 Views
7 Replies
Message 1 of 8

Insert Multiple Blocks Within One DWG

Hugh_Compton
Advisor
Advisor

I'm trying to get a routine that creates and inserts a copy of all the block entities in a DWG.  For some reason the code causes CAD to hang?  See attachment for code.

 

 

0 Likes
1,473 Views
7 Replies
Replies (7)
Message 2 of 8

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

you scan all BlockTableRecords, and within them for all Blockreferences.

Eg. if ModelSpace has a BlockReference included you insert a new BlockReference based on the current one and so you have an endless loop.

With other words:

  • we start with ModelSpace having 1 BlockReference
  • You found it and insert this one in ModelSpace
  • Now you have two BlockRefences in Modelspace
  • Next you make as step forward in your loop and find your second BlockReference
  • From that you create a third one
  • and so on .... endless

 

Back to the roots, what should this code do?

If you want to make a list of all blockdefinitions then just go through your BlockTable, and each BlockTableRecord, that is not of type layout, create a BlockReference in ModelSpace.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 8

Hugh_Compton
Advisor
Advisor

 

Thanks for the hints.  I've tried to get the block obejct IDs first and then do the inserting afterwards (see attached) but CAD still hangs 😞

0 Likes
Message 4 of 8

Hugh_Compton
Advisor
Advisor

 

I've attached another attempt, using someone elses code for a block inserting sub but it still doesn't work...... very confused.....

0 Likes
Message 5 of 8

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

can you first try to explain what you like to do?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 6 of 8

Hugh_Compton
Advisor
Advisor

Hi Alfred

 

I do schematic drawings that have blocks representing valves / fittings etc.  Once the blocks are in position it is useful to create a legend.  To create the legend I have to insert one of each block in the schematic and then annotate what the block is (I have a tagging routine for the annotations that works well). 

 

I need a routine that will grab one instance of each of the blocks in modelspace and reinsert at a new location selected by me. 

0 Likes
Message 7 of 8

chiefbraincloud
Collaborator
Collaborator

In your revision code, you were still adding items to the blockTableRecord while looping through it.

 

In Attempt 3, all I see is that the dictionary entries are required to be unique.  I am assuming that you have more than one of each block in modelspace, so the second time you find the same block, you are getting a duplicate key exception.

 

you need to test the dictionary contents before adding each Id/name pair.

 

if not dict.ContainsKey(id) then dict.add(id,name)

 

If you are using 2009 or newer AutoCAD, then you could also benefit from using ObjectID.ObjectClass.Name to type test the objects before opening them, instead of opening everything.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 8 of 8

David125
Collaborator
Collaborator

Super easy, no lisp required. Xref all blocks in at one time on same coordinates and bind them afterward. I do it frequently. 

 

0 Likes