LISP to update block in drawing using "-insert blkname=" approach

LISP to update block in drawing using "-insert blkname=" approach

Edgetrimmer
Contributor Contributor
895 Views
5 Replies
Message 1 of 6

LISP to update block in drawing using "-insert blkname=" approach

Edgetrimmer
Contributor
Contributor

I want a LISP routing to update block in drawing using the "-insert blkname=" approach. Below are LISP lines I wrote to try to accomplish this but the routine doesn't work.  Help from any AutoLISP gurus will be appreciated.

 

-Bruce

 

(defun c:blkupdate (blknm)
(command-s "select" "l" "")
(setq blknm (getvar "INSNAME"))
(setq blknm (strcat blknm "="))
(command-s "insert" blknm pause "" "" "")
(command-s "erase" "l" "")
)

0 Likes
896 Views
5 Replies
Replies (5)
Message 2 of 6

paullimapa
Mentor
Mentor

Well you need to set the new block dwg location first 

For example if your block name is "MyBlock" and the updated block dwg is here: c:\\autodesk\\MyBlock.dwg

Then your code will be this:

(setq blknm "MyBlock")

(setq dwgnm "c:\\autodesk\\MyBlock.dwg")

(setq insnm (strcat blknm "=" dwgnm))

(command ".-Insert" insnm)(command)


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 6

Kent1Cooper
Consultant
Consultant

[You don't need to provide a filepath if the drawing file is in a place where AutoCAD knows to look.]

The potential problem I see is that INSNAME is a System Variable, holding the Block name to be offered as default for the next Insert command.  It is not the name of a selected Block.  If the idea is to Insert one and then immediately [never if anything else has been drawn since Insertion of the desired Block] run this command to update it [suggested by Select using Last for selection], then that should give you the right name, but will you always and only use it immediately after an Insert?

 

Even if the answer is Yes, you will be asked the question of whether you want to replace the definition, which your code does not address.  After you've answered that, the definition change will have been accomplished, and you can cancel the Insert without finishing it -- giving it nil when it's asking for an insertion point will do.  I would do this with the command part:

(command "_.insert" blknm "_yes" nil)

and having not completed the Insert, there will be no need to Erase it.

 

If you want to take the Block name to update from an already-Inserted Block without needing that to have happened immediately before, that's different [but not difficult, though slightly more complex if it's a dynamic Block].

Kent Cooper, AIA
Message 4 of 6

Edgetrimmer
Contributor
Contributor

Thank you for your input.  I've been tinkering with my code to pick an obsolete version of a block on screen to update from my master blocks file.  Below is my code now. It works but I still can't get the INSERT command to do it's job without it wanting me to insert a copy of the block.  I hit escape to exit the INSERT command but that's messy.  Any idea what I need to do to solve that?

 

Also I believe you mentioned the problem with dynamic blocks.  My code doesn't work with those as the block name returned is some "*U" number.  How do I solve that?  Thank you for your help!

 


(defun c:BUP (/ flg e enm elst blknm blklay)
(setvar "cmdecho" 0)
(setq flg T)
(while flg
(setq e (entsel "\nPick BLOCK to update"))
(if e
(progn
(setq enm (car e) elst (entget enm))
(if (= (cdr (assoc 0 elst)) "INSERT")
(setq flg nil)
(princ "\nObject must be a block. Try again ...")
)
) ; end progn
) ; end if e
)

(setq blknm (cdr (assoc 2 elst)))
(princ (strcat "\n " (strcase blknm) " Redefined"))
(setq blknm (strcat blknm "="))
(command "insert" blknm "b" "0,0" "" "")

(setvar "cmdecho" 1)
(princ)
)
(princ "\n\"BUP\" runs command.")
(princ)

0 Likes
Message 5 of 6

Kent1Cooper
Consultant
Consultant

@Edgetrimmer wrote:

... to pick an obsolete version of a block on screen to update from my master blocks file.  ....


Is your "master blocks file" a drawing file with proper/current Block definitions in it?  If so, you can't use this approach.  It requires the replacing definition to be a drawing file of its own with that name, not a Block within a drawing file.  Your "master blocks file" would need to be a folder containing separate drawing files, and it would need to be listed in the Support File Search Path list in OPTIONS for AutoCAD to be able to find the drawing.

Kent Cooper, AIA
0 Likes
Message 6 of 6

Edgetrimmer
Contributor
Contributor
In answer to your question about our "master blocks file", yes we have a separate drawing for each standard block. We are an engineering company so, for example we have an electrical blocks folder named "E" with outlet blocks, etc. All of the standard blocks folder are included in our AutoCAD search path.
Thank you for your question and for your help.
0 Likes