Jump to content

Replace multiple blocks using a table


Recommended Posts

I have done a fair bit of web scouring to find a good method for replacing multiple blocks. There are some pretty decent vba codes out there for block replacement, the Office Optimum "Replace Block" tool, for instance worked okay, but you have to buy it. I have also found some effective .NET solutions for importing block definitions from another drawing to override the definitions in the current drawing. However I keep wondering if there is a good (free) solution for replacing and redefining large numbers of blocks using lisp. I am sure it must be doable. I know this has been discussed before here, but I haven't seen anything yet which uses a simple customisable table to handle lots of replacements at once (Please chastise me if I'm wrong).


There are lots of 'piece meal' type block replacement lisps on offer, which rely on either specifying block names from drop down lists, or selecting blocks on the screen. I want something which handles lots of block exchanges at once.


I would ideally like a lisp which allows me to set up a table of existing block names, with the filepath for the replacement block along side it. I would like it to resemble this lisp for renaming blocks (i.e. using a table):


(defun c:renameb (/ btable badt)
 (setq btable '(("OLDNAME#1" . "NEWNAME#1")                    
                    ("OLDNAME#2" . "NEWNAME#2")
                    ("OLDNAME#3" . "NEWNAME#3")
                    ("OLDNAME#4" . "NEWNAME#4")
                    ("OLDNAME#5" . "NEWNAME#5")))   

(foreach b btable
   (cond ((and (not (tblsearch "BLOCK" (cdr b)))
               (tblsearch "BLOCK" (car b)))
          (command "_.RENAME" "_Block" (car b) (cdr b)))
         ((tblsearch "BLOCK" (cdr b))
          (setq badt (cons b badt)))))
 (and badt
     (prin1 badt)
     (alert "Unable To Rename All BLOCKs"))



I want a block replacement lisp which uses a table to identify the filepath of the replacement block.


I also want the routine to tell AutoCAD to replace the definition of any existing blocks with the same name as the one being brought in. Alanjt notes here that the use of the equals sign in the following line achieves that:


(vl-cmdf "_.-insert" (strcat nb "=") nil)



I also want to preserve the attribute values in the existing drawing.


I want to be able to control the scale of the blocks being brought into the drawing as replacements.


I want to ATTSYNC replacement blocks.


Marijn incorporated the following in a reply to a thread posted yesterday. See here.


 (if (tblsearch "BLOCK" "OLDNAMEOFBLOCK")
 (command "-rename" "b" "OLDNAMEOFBLOCK" "NEWNAMEOFBLOCK")
 (command "_.-insert" "NEWNAMEOFBLOCK=FILENAME" "y" nil);replaced convert template to new template.
(command "_.attsync" "n" "NEWNAMEOFBLOCK")
(scl 0.8 "NEWNAMEOFBLOCK");scales block to new scale
 (blknr13 "NEWNAMEOFBLOCK"); this was command to set a block the a layer. (not included can eb removed)



This takes care of the ATTSYNC and the scale specification. I'd like the OLDNAMEOFBLOCK and NEWNAMEOFBLOCK to essentially be variables, which draw on a pre-defined table to derive values and then apply the commands. Is this possible? I guess I'd be just as happy to input all the values one by one in an unhealthily long code, but a more streamlined answer would be nice.


By the by, Marijn's code aimed to incorporate replacing tag names also, when one block is replaced. Interesting. Haven't tried it out properly yet.


I would greatly appreciate some assistance in addressing and integrating the requirements outlined above in one amazing 'table based' lisp. I have already sent a post card to Santa politely asking for this for Christmas, so if I'm unsuccessful here, I always have christmas day as a backup.:P

Link to comment
Share on other sites

If you remove the " then it is a variable should put it at(defun c:renameb ( OLDNAMEOFBLOCK NEWNAMEOFBLOCK tag1old tag1new tag2old tag2new etc/ btable badt) , I threaded every block on its own so I could do specific stuff to it and I wasn't able to rewrite it at that time. At the end I think it wasn't really needed and one command could do 99% of all my block replacement.

I use this to replace old templates works pretty good only have 1 old templates that uses 2 blocks in stead of 1 *sigh* I made a block that was the same as the replacement block but cut of the parts that aren't used replaced the other one with the new template. I still have to manually merge them together. Glad it is only the scale, date and drawn tag.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...