.NET

Reply
Mentor
Hugh_Compton
Posts: 709
Registered: ‎04-21-2006
Message 1 of 7 (395 Views)

Insert Multiple Blocks Within One DWG

395 Views, 6 Replies
06-01-2012 04:55 AM

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.

 

 

*Expert Elite*
Alfred.NESWADBA
Posts: 8,880
Registered: ‎06-29-2007
Message 2 of 7 (392 Views)

Re: Insert Multiple Blocks Within One DWG

06-01-2012 05:32 AM in reply to: Hugh_Compton

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
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Mentor
Hugh_Compton
Posts: 709
Registered: ‎04-21-2006
Message 3 of 7 (388 Views)

Re: Insert Multiple Blocks Within One DWG

06-01-2012 06:05 AM in reply to: Hugh_Compton

 

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 :smileysad:

Mentor
Hugh_Compton
Posts: 709
Registered: ‎04-21-2006
Message 4 of 7 (381 Views)

Re: Insert Multiple Blocks Within One DWG

06-01-2012 06:44 AM in reply to: Hugh_Compton

 

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

*Expert Elite*
Alfred.NESWADBA
Posts: 8,880
Registered: ‎06-29-2007
Message 5 of 7 (374 Views)

Re: Insert Multiple Blocks Within One DWG

06-01-2012 08:08 AM in reply to: Hugh_Compton

Hi,

 

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

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Mentor
Hugh_Compton
Posts: 709
Registered: ‎04-21-2006
Message 6 of 7 (372 Views)

Re: Insert Multiple Blocks Within One DWG

06-01-2012 08:13 AM in reply to: Hugh_Compton

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. 

*Expert Elite*
chiefbraincloud
Posts: 752
Registered: ‎02-13-2008
Message 7 of 7 (360 Views)

Re: Insert Multiple Blocks Within One DWG

06-01-2012 09:56 AM in reply to: Hugh_Compton

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

You are not logged in.

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

Announcements
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