Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LSP to control block swap

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
joe_kohli
480 Views, 9 Replies

LSP to control block swap

I am using this script to swap out an old template with a new one. However, I want to convert it to a LSP so that I can run it from a tool palette. I currently have to type "script" in the command bar and navigate to the .scr file location, select it, and then hit enter in Autocad to get it to run. I want to simply run it by the tool palette. How can I convert this over?

 

;	script to replace OLD block NEW block
;
;	ATTREQ = 0 turns off prompt to fill in attrbutes
attreq
0
;
;	insert command to replace
;	OLD BLOCK = NEW BLOCK
;	new block name needs to include location as shown is spaces in file name needs quotes
;
-insert
"PCS(24x36)_Titleblock=C:\User\AutoCAD\pidblks_dyn\NET(24x36)_Titleblock.dwg"
Y
0,0
1
1
0
;
;	variable to turn prompt of attributes back on
;
attreq
1
;	erases temporary insert of block (L = last object placed)
erase
L

;	ATTSYNC = sinces the attrubutes 
attsync
name
PCS(24x36)_Titleblock
;
;	command; RENAME to change OLD block name to NEW block name
;
rename
block
PCS(24x36)_Titleblock
"NET(24x36)_Titleblock"
;
;	save changes
;
qsave
9 REPLIES 9
Message 2 of 10
LDShaw
in reply to: joe_kohli

Got pulled off for real work. Try this. 

(defun c:ReplaceBlock ( / oldBlk newBlk attReqVal tempEnt)
  (setq oldBlk "iNSERT_XBORD.dwg"
        newBlk "C:\\dcapps\iNSERT_XBORD.dwg"
        attReqVal (getvar 'ATTREQ)
  )
  ;; Disable attribute prompts
  (setvar 'ATTREQ 0)

  ;; Insert the new block temporarily at 0,0
  (command "-insert" (strcat oldBlk "=" newBlk) "Y" "0,0" "1" "1" "0")
  
  ;; Capture the last inserted entity
  (setq tempEnt (entlast))
  
  ;; Enable attribute prompts back
  (setvar 'ATTREQ attReqVal)

  ;; Erase the temporary block insertion
  (command "erase" tempEnt)

  ;; Sync attributes in the old block
  (command "attsync" "name" oldBlk)
  
  ;; Rename the old block to the new block's name
  (command "rename" "block" oldBlk "iNSERT_XBORD.dwg")

  ;; Save the drawing
  (command "qsave")
  (princ)
)

No warranty but a start for anyone that has some time

 

Message 3 of 10
joe_kohli
in reply to: LDShaw

It is a good start. I tried it but when I run the LSP it will insert the new block where the old one is, it also updates the attribute fields with all the old block information and places it into the newly inserted block. 

 

Command: REPLACEBLOCK
-insert Enter block name or [?]: OLD_Titleblock=C:\User\Drive\NEW_Titleblock.dwg Block "OLD_Titleblock" redefined
Units: Inches Conversion: 1.0000
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]: Y Specify Y scale factor <1>: 0,0 Specify second point: 1 Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]: 1
Specify rotation angle <0>: 0
Command: erase
Select objects: 1 found
Select objects: attsync
*Invalid selection*
Expects a point or Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle/SUbobject/Object
; error: Function cancelled

 

At this point in the code, it asked me to "select object". Why? It also inserts a duplicate "NEW_Titleblock.dwg" beside the already inserted New template. I just want to run the script so that it automatically replaces the OLD block with the NEW block and auto attsync's the attributes on the newly inserted block.

Message 4 of 10
Kent1Cooper
in reply to: joe_kohli

Omit the "Y" from Message 2's code line 10.  The command-line history in Message 3 shows that it's not asking the question of whether to redefine, for which that would be the "Yes" answer, but just doing so [maybe a function of how -INSERT works when in an AutoLisp (command) function?].  So the "Y" is being taken as a Y-scale-factor option, and subsequent inputs are likewise inappropriate to what's being asked for.

[This, by the way, is an example of why it's better to spell out options, instead of using just the abbreviations.  The main reason is in case a command changes in the future and acquires some additional option that starts with the same letter as an existing one, to avoid confusing them.  But here, if you had it as "Yes" instead of just "Y", it would have been clearer in the command history, because if it's not asking the whether-to-redefine question, at the point where "Y" as a Y-scale-factor option is an acceptable input, "Yes" would not be, and you would see that more explicitly spelled out.]

Kent Cooper, AIA
Message 5 of 10
joe_kohli
in reply to: Kent1Cooper

Ah, I see! I omitted the "Y" and now it places the new block correctly. However, it still asked me to select object to delete and does not ATTSYNC the newly inserted block. What do I have wrong?

Message 6 of 10
Kent1Cooper
in reply to: joe_kohli


@joe_kohli wrote:

... it still asked me to select object to delete and does not ATTSYNC the newly inserted block. What do I have wrong?


You can Erase more than one thing at a time -- it's waiting for further selection.  You need to tell it you're done selecting with an Enter:

(command "erase" tempEnt "")

Or, use this instead:

(entdel tempEnt)

 

EDIT:  Actually, if you're only bringing in the new one to update the Block definition, once it has gotten to redefining it, that's over, and there's no need to complete the Insertion.  A (command) function with no arguments amounts to a cancel, so you can do this [I think this would be the "recommended way" under AutoLisp Reference descriptions]:

(command "-insert" (strcat oldBlk "=" newBlk)) (command)

But I believe this also works to cancel it without finishing:

(command "-insert" (strcat oldBlk "=" newBlk) nil)

Then, in addition to not needing to Erase it [because it never got Inserted], and therefore not needing the 'tempEnt' variable, it should also not be necessary to deal with the ATTREQ setting at all [saving or resetting], and you could also skip the variable used for that.

 

[Come to think of it, the fact that it doesn't ask the whether-to-redefine question kind of surprised me, because I feel like that has been involved before.  Now I suspect it may not be the inside-a-(command)-function reason I wondered about, but could be that you have the EXPERT System Variable set to a value that precludes that question.  Either way, if omitting the "Y" solves it for you, great, but the same may not work for others.]

 

Kent Cooper, AIA
Message 7 of 10
LDShaw
in reply to: joe_kohli

Can you give us your lsp, block you are inserting and the dwg you are working with so we can create your scenario?

I feel bad I left you hanging. I may have a little time later today to help if someone else does not help.

Also I did just what you asked. Perhaps if you explain what you are trying to accomplish someone may come up with an easier way of doing it.

Message 8 of 10
joe_kohli
in reply to: Kent1Cooper

Thanks Kent! You were correct. Did some minor tweeks to the LSP and it now works the way I want it to. 

 

 

@LDShaw It's completely ok. I ended up getting it to work. Thank you for the initial direction.

Message 9 of 10
Kent1Cooper
in reply to: joe_kohli

[See the EDIT at Message 6, which went up after your latest Message.]

Kent Cooper, AIA
Message 10 of 10
LDShaw
in reply to: joe_kohli

Glad you got it. 

Perhaps you could post your code so others that find this can see how you fixed it up?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report