Swapping blocks using LSP

Swapping blocks using LSP

joe_kohli
Advocate Advocate
767 Views
11 Replies
Message 1 of 12

Swapping blocks using LSP

joe_kohli
Advocate
Advocate

I attached a LSP that I run to convert older (out of date) blocks to my new blocks with all my updated information. This LSP only works if all your attributes match, otherwise if they don't, it throws a code which is an awesome feature. When you run this, it asked you to select the "base" block and then it asked you to select the new block you want all the information (from the old block to the new block) you want to use. Can this LSP be changed so that the function does this:

 

When you prompt "MTB" via the command, it will automatically select a particular block you have chosen at a specific location on your computer and run the rest of the code as normal (meaning, select the "base" block and replace with the new block)? or even better, automatically erase the old block after you have selected it and the new block would be placed directly at the same place the old block use to reside at.

 

;THIS LSP WILL CONVERT AN OLD BLOCK TO WHATEVER NEW BLOCK YOU WANT. THE ATTRIBUTE "TAGS" NEED TO MATCH FOR THIS TO WORK. INSERT THE MTB.LSP AND ENTER "MTB" INTO AUTOCAD COMMAND LINE TO ACTIVATE IT.



(princ "\nType MTB to Run")
(defun C:MTB (/)
  (setq baselist (list))      
  (setq ename (car (entsel "\nSelect Base Block:")))
  (while (= ename nil)
     (princ "\nNothing Picked")
     (setq ename (car (entsel "\nSelect Base Block:")))
  );end while
  (setq ename1 (car (entsel "\nSelect Title Block To Apply Changes:")))
  (while (= ename1 nil)
     (princ "\nNothing Picked")
     (setq ename1 (car (entsel "\nSelect Block To Apply Changes:")))
  );end while
  (setq ename (entnext ename))
  (setq elist (entget ename))   ;the entity list of the base border
  (setq etype (cdr (assoc 0 elist)))   ;should be attrib
  (while (= etype "ATTRIB")      ;puts all the attribute in a list
     (setq tag (cdr (assoc 2 elist)))      ;the attribute tag
     (setq val (cdr (assoc 1 elist)));the attribute value
     (setq baselist (append (list (list tag val)) baselist));put the attribute in list
     (setq ename (entnext ename))         ;move onto the next attribute
     (setq elist (entget ename))
     (setq etype (cdr (assoc 0 elist)))
  );end while
  (setq ename1 (entnext ename1))            ;get the next entity, should be "ATTRIB"
  (setq elist1 (entget ename1))            ;the entity list of the border
  (setq etype1 (cdr (assoc 0 elist1)))         ;should be attrib
  (while (= etype1 "ATTRIB")
     (setq attval nil)
     (setq tag (cdr (assoc 2 elist1)));the attribute tag
     (foreach item baselist
        (if (= tag (nth 0 item))
           (progn   
              (setq attval (nth 1 item))
           );end then
           (progn);else do nothing go to next in list till tag matches
        );end if
     );end foreach
     (if (/= attval nil)
        (progn   (setq elist1 (subst (cons 1 attval) (assoc 1 elist1) elist1))
           (entmod elist1));end then
        (progn);end else
     );end if
     (setq ename1 (entnext ename1))   ;move onto the next attribute
     (setq elist1 (entget ename1))
     (setq etype1 (cdr (assoc 0 elist1)))
  );end while
  (command "REGEN")
);end defun
(princ) 

 

 

0 Likes
768 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

@joe_kohli wrote:

.... or even better, automatically erase the old block after you have selected it and the new block would be placed directly at the same place the old block use to reside at.


For that part, does BLOCKREPLACE not do what you want?  Along with a getting of Attribute values from the old to assign to the new?

Kent Cooper, AIA
0 Likes
Message 3 of 12

joe_kohli
Advocate
Advocate

Blockreplace works great for many of my applications, however, it does not transfer the attribute values very well when using title blocks in a drawing template. The orientation and centering of text many times gets skewed and does not come out right.

 

The "MTB" command lsp that I run will force all the attributes of the old title block and push them into all the correct spots of the new title block. The only disadvantage to this method is that you have to pull in the new drawing template into the drawing itself and then select and so on. I just wanted to limit the mouse clicks.

0 Likes
Message 4 of 12

Moshe-A
Mentor
Mentor

@joe_kohli  hi,

 

Attached a beautiful command called EXBR (EXtendend Block Replace) which is a huge enhancement  to BLOCKREPLACE exactly what you are trying to write. it works with nice dialog box driven by >> OpenDCL << 

Are you familiar with OpenDCL?

 

to run it, first install opendcl than load exbr.vlx. invoke EXBR command. use intuition to run it, it's very easy to use and if you have question, i am here 😀

 

enjoy

Moshe

 

 

0 Likes
Message 5 of 12

joe_kohli
Advocate
Advocate

Moshe-A, I don't think this will be an option for me. This requires administrative approval for my computer and I would like to explore options just within the LSP functions of Autocad. I'm sure it works awesome, but this may not be a route I can take due to security reasons on my workstation.

0 Likes
Message 6 of 12

pendean
Community Legend
Community Legend

@joe_kohli wrote:

Blockreplace works great for many of my applications, however, it does not transfer the attribute values very well when using title blocks in a drawing template....


There is just no getting away from having to use ATTSYNC in the program for tasks like that (if I read your descriptions correctly). 

0 Likes
Message 7 of 12

Moshe-A
Mentor
Mentor

@joe_kohli hi,

 

Wel, i'll tell what i would do, i post the request to the network administrator + CC to big boss

and explain them i need this tool badly, it speedup my work, make my life brighter and let them reject you

and if they do (which i can't believe) let them walk beside you with head down, they won't bite you 

on the other hand if you're personally interested on solving this on your own, welcome to AutoLISP\ActiveX world 😀

 

Moshe

 

0 Likes
Message 8 of 12

komondormrex
Mentor
Mentor

@joe_kohli wrote:

...select a particular block you have chosen at a specific location on your computer...


you literally mean that it's not in a dwg file, but somewhere on the computer?

0 Likes
Message 9 of 12

joe_kohli
Advocate
Advocate

I have a .dwg file that I would like to use at a specific location on my C: drive. That .dwg file is also the block too. Sorry, I wasn't clear on that.

 

The MTB lsp that I have works awesome. The downside to this lsp is that you have to manually insert the "new" block into the current drawing. This allows you to select the base (aka "old") block and then select the "new" block to allow all the information to be transferred. I am only trying to bypass having to insert the "new" block into the existing drawing. I want the MTB lsp to automatically insert the "new" block so that it keeps me from having to manually go find it and insert it. More less, I'm trying to reduce mouse clicks (aka lazy).

0 Likes
Message 10 of 12

pendean
Community Legend
Community Legend

@joe_kohli wrote:

... so that it keeps me from having to manually go find it and insert it. More less, I'm trying to reduce mouse clicks (aka lazy).


That is your problem AKA where yo fix it: your external DWG file needs to reside in a folder that is in the SEARCH PATHs of your AutoCAD session so the program (and your LISP) can "automatically find it".

 

OR... you hard code that location (that should never ever change) as part of your -INSERT command functions in your LISP to bring in that external file for you.

 

Do you just need help writing our the -INSERT function that does that? That's all you need to do.

0 Likes
Message 11 of 12

joe_kohli
Advocate
Advocate



@pendean wrote:


 

 

OR... you hard code that location (that should never ever change) as part of your -INSERT command functions in your LISP to bring in that external file for you.

 

Do you just need help writing our the -INSERT function that does that? That's all you need to do.





 

Yes, this would be tremendous. The location will never change.

0 Likes
Message 12 of 12

ec-cad
Collaborator
Collaborator

I think the issue is trying to 'remap' the tagname / values of the 'old' title block to the 'new'

title block. The attached needs to be edited for path & name of the 'new' title block, and

specific 'old' blockname / attribute tagnames, and the 'new' title block / attribute tagnames.

Then, it gets the 'old' title block (by name), gets the attribute values, erases it, inserts

the 'new' title block, and fills in the attribute values. The lisp filename begins with a "T_", indicating

you need to 'tune' it before use.

Becomes clear if you review the Lisp check the comments.

Once setup, you can run it in a Batch, and it will

swap out hundreds of title blocks (while you sip coffee) 🙂

 

ECCAD

0 Likes