TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: sezbw on October 23, 2007, 10:22:14 PM

Title: Is there a way to insert & update multiple blocks at a time?
Post by: sezbw on October 23, 2007, 10:22:14 PM
OK so being the block loving nerd I am, I have got myself in a bit of a tricky spot.

I have a whole bunch of blocks (column) in numerous drawing plans ie, C1-800x300, C2-600DIA ETC... 

The blocks are saved outside my drawings in a folder.  I have updated these files now I need to re-import them & update them in a load of plans.  Is there a way to insert & update multiple blocks at a time, so I don't have to do them individually.

In the past on swamp I have found a fantastic block swap command, but this time I just want to globally change all blocks of one name to be the same as an external file of the same name.
 :ugly:
Sorry my powers of explanation seem to be failing me...

Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: CAB on October 23, 2007, 11:31:28 PM
Here are a few routines. You may need a script if you have lots of drawing files.

http://www.theswamp.org/index.php?topic=8823.msg113774#msg113774

http://www.theswamp.org/index.php?topic=8823.msg113895#msg113895

http://www.theswamp.org/index.php?topic=8823.msg113941#msg113941
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: FengK on October 24, 2007, 03:19:28 AM
i didn't finish reading the posts in the threads that Alan listed, but it appears to me the lisp commands in those posts are used for replacing block "A" with block "B" which is already in the drawing. and the problem sezbw is trying to solve is different.

don't quote me on this: suppose he has a block named "A" in drawing, and he has another drawing named "A.dwg" which is the new block he wants to replace the old one with. when manually inserting the block (type command "INSERT" and browse that "A.dwg" file), user will be prompted whether to replace the old block definition in drawing or not. but if the block is inserted via lisp, the old block definition will be used.

if what i said above was correct, one way to resolve it would be renaming the old block (to some unique name) in drawing first, inserting the new block, deleting the old block and purging the block definition with the "unique name". this seems to be a good opportunity to use objectDbx.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: jbuzbee on October 24, 2007, 07:31:34 AM
sounds like you should have used xrefs . . .
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: Krushert on October 24, 2007, 07:41:16 AM
sounds like you should have used xrefs . . .
:? :?
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: jbuzbee on October 24, 2007, 08:52:02 AM
sounds like you should have used xrefs . . .
:? :?


Quote
The blocks are saved outside my drawings in a folder.  I have updated these files now I need to re-import them & update them in a load of plans.

Am I missing something here??
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: Kerry on October 24, 2007, 08:53:20 AM
Have a look at Post Reply #7 here .. does that suit you ?

http://www.theswamp.org/index.php?topic=9229.msg118979#msg118979


added:

Though you could make a selectionSet of inserted blocks and only replace those from the FolderList ...
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: Krushert on October 24, 2007, 09:07:53 AM
sounds like you should have used xrefs . . .
:? :?


Quote
The blocks are saved outside my drawings in a folder.  I have updated these files now I need to re-import them & update them in a load of plans.

Am I missing something here??
You first statement sounded very opinionated, implying that xrefs is the only way.  That is all.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: daron on October 24, 2007, 09:16:58 AM
That was my first thought as well, that he should have used xrefs. There is an express tool for that purpose, but it's painfully slow. Another thought I also had in regards to xrefs is that if you have multiple xrefs in one drawing you end up with slower load time, so sometimes you are better off not using xrefs. I would assume that would be part of the reasoning for not having used them, but that's just me. They have their purpose, but not at the expense of load times. Wouldn't ODBX be usable here?
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: jbuzbee on October 24, 2007, 09:28:00 AM
Before Project Navigator I used to use a routine that would "attach" attributes to an XREF.  Basically you inserted the file first (became a block with attributes), renamed it, then Xrefed it.  Then I used entmod to change the xref to the inserted block and deleted the old reference.  You could attach the drawing as an xref then use something like the following to "point" the existing blocks to the xref: (you'll needd to make sure the two don't share a common name)

Code: [Select]
(setq block (entget (car(entsel"\nSelect Block")))
      xref-name(cdr(assoc 2(entget(car(entsel "\nSelect Xref"))))))
      (setq new-blk (subst (cons 2 xref-name) (assoc 2 block) block))
      (entmod new-blk)

(warning: this is very bad coding - only used as an illustration!)
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: CADaver on October 24, 2007, 10:25:23 AM
Have a look at Post Reply #7 here .. does that suit you ?

http://www.theswamp.org/index.php?topic=9229.msg118979#msg118979


added:

Though you could make a selectionSet of inserted blocks and only replace those from the FolderList ...
If you select the title link of the specific post in a thread you can paste it here, like this:

http://www.theswamp.org/index.php?topic=9229.msg119393#msg119393
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: Kerry on October 24, 2007, 10:36:51 AM
Have a look at Post Reply #7 here .. does that suit you ?

http://www.theswamp.org/index.php?topic=9229.msg118979#msg118979


added:

Though you could make a selectionSet of inserted blocks and only replace those from the FolderList ...
If you select the title link of the specific post in a thread you can paste it here, like this:

http://www.theswamp.org/index.php?topic=9229.msg119393#msg119393

Yes, I know. All the posts are tied together in my mind .. and I did say reply 7 ... do you think I made it too difficult for the OP ?
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: CADaver on October 24, 2007, 10:51:11 AM
Have a look at Post Reply #7 here .. does that suit you ?

http://www.theswamp.org/index.php?topic=9229.msg118979#msg118979


added:

Though you could make a selectionSet of inserted blocks and only replace those from the FolderList ...
If you select the title link of the specific post in a thread you can paste it here, like this:

http://www.theswamp.org/index.php?topic=9229.msg119393#msg119393

Yes, I know. All the posts are tied together in my mind .. and I did say reply 7 ... do you think I made it too difficult for the OP ?
Just offering the suggestion that in the time it took you to count the seventh post you could have linked it directly.  Silly of me to interfere.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: Kerry on October 24, 2007, 11:04:02 AM
 Re: Is there a way to insert & update multiple blocks at a time?
« Reply #12 on: Today at 00:51:11 »

Quote
.. Silly of me to interfere.
Perhaps it was.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: CADaver on October 24, 2007, 11:09:52 AM
Re: Is there a way to insert & update multiple blocks at a time?
« Reply #12 on: Today at 00:51:11 »

Quote
.. Silly of me to interfere.
Perhaps it was.
Sometimes suggestions are appreciated, sometimes not.  I'll make a note that Kerry never needs any kind of suggestions, thanks for the heads-up.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: CAB on October 24, 2007, 12:16:07 PM
What's wrong with using the plan old INSERt command?

From the Help file on INSERT command

Updating a Block Path: If you enter a block name without a path name, AutoCAD
first searches the current drawing data for an existing block definition by that
name. If no such block definition exists in the current drawing, AutoCAD
searches the library path for a file of the same name. If AutoCAD finds such a
file, it uses the file name for the block name upon insertion of the block
definition. AutoCAD uses the same block definition for subsequent insertions of
that block. You can replace an existing block definition with an external file
by entering the following at the Enter Block Name prompt:

block name=file name


You can simply use the command line version of the INSERT command like this:

Code: [Select]
(command "-INSERT" "YourBlockName=YourBlockName")
(command)

Your block name must be in the path else use this:

Code: [Select]
(command "-INSERT" (strcat "YourBlockName="  path "YourBlockName") )
(command)

The (command) aborts the INSERT but not before the block definition has been updates.

You can process a list of block names like this:

Code: [Select]
(foreach name '("b1" "b2" "b3")
  (command "-INSERT" (strcat name "=" name)
  (command)
)


You can add this to a script. The problem comes when the block is not found.
An error occurs and your script is stopped.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: ronjonp on October 24, 2007, 12:33:19 PM
Quote
...The problem comes when the block is not found.
An error occurs and your script is stopped.

Here is one to check if the block definition exists before trying to insert:

Code: [Select]
(foreach name '("b1" "b2" "b3")
  (if (tblsearch "block" name)
    (command "-INSERT"
     (strcat name "=" name)
    )
  )
)
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: FengK on October 24, 2007, 01:10:37 PM
Thanks Alan & ronjonp, i didn't know about this one:

(command "-INSERT" "YourBlockName=YourBlockName")
(command)
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: CADaver on October 24, 2007, 05:30:51 PM
Thanks Alan & ronjonp, i didn't know about this one:

(command "-INSERT" "YourBlockName=YourBlockName")
(command)
Just a suggestion, but the names can be different as well

Code: [Select]
(command "-INSERT" "YourBlockName=c:/MyDirOfSomeName/peanut/butter/SomeOtherDwgName")
(command)
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: FengK on October 24, 2007, 09:55:41 PM
Thanks Randy.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: CADaver on October 24, 2007, 11:25:14 PM
Thanks Randy.
You're quite welcome.
Title: Re: Is there a way to insert & update multiple blocks at a time?
Post by: Luke on April 11, 2008, 03:30:32 PM
OK so my code is as follows:
Quote
(command "-INSERT" "YourBlockName1=c:/MyDirOfSomeName/peanut/butter/YourBlockName1")
(command)

I'm almost embarrassed asking (but will anyhow..) how do i add a second line to get it to replace old block2 with newblock2?