LISP Command is Re-entered in the middle of LISP routine

dtaxon
Contributor
Contributor

LISP Command is Re-entered in the middle of LISP routine

dtaxon
Contributor
Contributor

Hello all,

 

I have run up against an issue where when I run a LISP routine, it creates an error by re-entering the command used to start the LISP routine.  Below is a segment of my code:

 

(defun C:LAYERMERGEUPDATE ()
(setvar "CMDECHO" 0)
(if (tblsearch "LAYER" "S-ANNO-TTLB")(command ".-laymrg" "n" "S-ANNO-TTLB" "" "n" "0" "y"))
(if (tblsearch "LAYER" "Defpoints")(command ".-laymrg" "n" "Defpoints" "" "n" "0-Defpoints" "y"))
(if (tblsearch "LAYER" "S-ANNO-DIMS-CENT")(command ".-laymrg" "n" "S-ANNO-DIMS-CENT" "" "n" "ANNO-CENTERLINE" "y"))
(if (tblsearch "LAYER" "S-ANNO-DIMS")(command ".-laymrg" "n" "S-ANNO-DIMS" "" "n" "ANNO-DIMENSIONS" "y"))
)
 
If I run this in a drawing, it searches for the layers that are present and should appropriately merge them to the new layer that already exists in the drawing. The problem I am having is that, for example, it finds "Defpoints" and merges it to "0-Defpoints", but when it gets to "S-ANNO-DIMS", I get the following output in my command line. 
 
dtaxon_1-1672764363268.png

(Ignore the block redefining)

 

I think it has something to do with the double quotes to trigger an "enter" in the ".laymrg" command, but don't see a way around this. 

 

I have a longer version of this code with all the old layer names mapped to the new layer names, but I can't get it to work for more then one layer.

 

Any assistance is greatly appreciated! Thanks!

0 Likes
Reply
667 Views
12 Replies
Replies (12)

Moshe-A
Mentor
Mentor

@dtaxon hi,

 

1. you can not merge to current layer so i suggest to create dummy layer before and remove if at end.

2. defpoints is special layer for use of internal purposes as dimensions, i do not think you are allowed to move data from there.

 

Moshe

 

0 Likes

Kent1Cooper
Consultant
Consultant

Try temporarily commenting out the suppression of command echoing:

; (setvar "CMDECHO" 0)

for a more detailed view of where it is in what command when the error occurs.

Kent Cooper, AIA
0 Likes

Moshe-A
Mentor
Mentor

@dtaxon hi,

 

Would like to suggest you a little advanced way to do it 😀

your main data is a list i called it Layers^ which contains pairs of layers name.

in the pairs, the first string is the source layer name and the second is the target layer name for merge.

 

using (foreach) to iterate through layers^ list. each step variable pair^ holds pair of layers

(vl-every) function will return T if both the source & target layers are exist (otherwise no point in doing laymrg)

before the loop i'm going to dummy layer and at end switch back to layer 0 and dummy is killed 😀

 

How that sounds?

Moshe

 

(defun C:LAYERMERGEUPDATE (/ Layers^)

;| (if (tblsearch "LAYER" "S-ANNO-TTLB")      (command ".-laymrg" "n" "S-ANNO-TTLB" "" "n" "0" "y"))
 (if (tblsearch "LAYER" "Defpoints")        (command ".-laymrg" "n" "Defpoints" "" "n" "0-Defpoints" "y"))
 (if (tblsearch "LAYER" "S-ANNO-DIMS-CENT") (command ".-laymrg" "n" "S-ANNO-DIMS-CENT" "" "n" "ANNO-CENTERLINE" "y"))
 (if (tblsearch "LAYER" "S-ANNO-DIMS")      (command ".-laymrg" "n" "S-ANNO-DIMS" "" "n" "ANNO-DIMENSIONS" "y")) |;


 (setvar "cmdecho" 0)
 (command "._undo" "_begin")
  
 ;                     source        target
 (setq Layers^ '(("S-ANNO-TTLB"      "0")
		 ("S-ANNO-DIMS-CENT" "ANNO-CENTERLINE")
		 ("S-ANNO-DIMS"      "ANNO-DIMENSIONS")
		); list
 ); setq

 (command "._layer" "_make" "dummy" "")  ; switch to dummy layer

 ; do merge 
 (foreach pair^ Layers^
  (if (vl-every (function (lambda (lay) (tblsearch "layer" lay))) pair^)
    (command "._laymrg" "_Name" (car pair^) "" "_Name" (cadr pair^) "_Y")
    (princ "\nFAILS")
  )
 ); foreach


 (command "._layer" "_set" "0" "") 		; switch back to 0
 (command "._laydel" "_Name" "dummy" "" "_Y")	; delete layer dummy
  
 (command "._undo" "_end")
 (setvar "cmdecho" 1)
 (princ) 
)

 

0 Likes

dtaxon
Contributor
Contributor

@Moshe-A  Thank you for your response.

 

I gave it a try and I am still getting the same error - see screenshot.

dtaxon_0-1672774854644.png

 

 

Also, oddly enough "Defpoints" is the one layer that I am not having an issue with....

 

FWIW, my original code was based off of this discussion
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/merge-layers-to-one/td-p/6329661

 

 

0 Likes

Moshe-A
Mentor
Mentor

@dtaxon ,

 

did you made change to my code?

 

post a sample dwg, so i can check

 

moshe

0 Likes

dtaxon
Contributor
Contributor

@Moshe-A 

 

I did make your changes. I am attaching a version of the LISP file and a CAD file with the layers/lines.

0 Likes

Moshe-A
Mentor
Mentor

@dtaxon ,

 

The lisp is working fine for me, obviously i did not see your real dwg and there were no dimensions in.

i test it on R2018 and i think LAYMRG command was change from previous releases so what version you have?

from the picture you post, i see FAILS in text window, that means a pair of layers is not exist so make sure you specify the exact layers name.

 

Moshe

 

 

0 Likes

dtaxon
Contributor
Contributor

@Moshe-A 

My testing was done in 2023. I just ran it on the test file I posted and it does work. Let me work out a file that it doesn't work in and I will share that one.

0 Likes

dtaxon
Contributor
Contributor

@Moshe-A 

 

Here is a file that the code does not work in and an updated LISP file with more layers.  Let me know what you find this time.

0 Likes

Moshe-A
Mentor
Mentor

@dtaxon ,

 

That's was easy 😀 as i said in my first reply (message 2 of 10),  you can not get rid of Defpoints that easy so why don't you switch roles? make 0-defpoints source and defpoints target.

 

works prefect!  🤣

 

made fine tuning to code.

 

Moshe

 

 

(defun C:LAYERMERGEUPDATE (/ Layers^)

(setvar "CMDECHO" 0)

 ;                     source        target
 (setq Layers^ '(("S-ANNO-TTLB" "0")   ;layer should fail
		 ("0-Defpoints" "Defpoints")
		 ("S-ANNO-DIMS" "ANNO-DIMENSIONS")
		 ("S-ANNO-HATC" "ANNO-HATCHING")
		 ("S-ANNO-NOTE" "ANNO-NOTES")
		 ("S-BEAM-CONC" "CONC-FOUNDATION-DETAILS")
		 ("S-DET-STEL-EXST-HIDD" "STL-DETAILS-EXISTING-HIDDEN") ;layer should fail
		 ("S-DET-STEL-HIDD" "STL-DETAILS-HIDDEN")
		 ("S-DET-STEL-MISC" "STL-DETAILS-MISC")
		 ("S-BEAM-STEL" "STL-PLANS-BEAM")
		 ("S-EXST-BEAM-STEL" "STL-PLANS-EXISTING")
     	       ); list
 ); setq


 (command "._layer" "_make" "dummy" "")  ; switch to dummy layer

 ; do merge 
 (foreach pair^ Layers^
  (if (vl-every (function (lambda (lay) (tblsearch "layer" lay))) pair^)
   (command "._laymrg" "_Name" (car pair^) "" "_Name" (cadr pair^) "_Y")
   (princ "\nSource\\Targer or Both layers are not exist.")
  )
 ); foreach


 (command "._layer" "_set" "0" "") 		; switch back to 0
 (command "._laydel" "_Name" "dummy" "" "_Y")	; delete layer dummy
  
 (command "._undo" "_end")
 (setvar "cmdecho" 1)
 (princ) 
)
  

 

 

 

0 Likes

Kent1Cooper
Consultant
Consultant

@Moshe-A wrote:

...  you can not get rid of Defpoints that easy so why don't you switch roles? make 0-defpoints source and defpoints target.

....


Just as I would recommend against moving things on the DEFPOINTS Layer to another Layer [unless you put them there as a bad practice], I would also recommend against moving anything to that Layer from any other.  I say leave it entirely alone, and don't put anything on it yourself in the first place, but let AutoCAD use it in its own way for its unique purposes.  Using it yourself is an invitation to "issues."  If you want things of yours to not plot, put them on Layers named for what they're for, and that you have set to not plot.

Kent Cooper, AIA
0 Likes

Moshe-A
Mentor
Mentor

@Kent1Cooper hi,

 

@dtaxon hi,

 

1. you can not merge to current layer so i suggest to create dummy layer before and remove if at end.

2. defpoints is special layer for use of internal purposes as dimensions, i do not think you are allowed to move data from there.

 

brought here my first reply to OP, you can see what was my first recommendation. although i absolutely agree with you i think there are other delicate matters (also from OPs side but not this one particularly)  involves here that we do not know and lets leave it this way 😀

 

thank you very much for your comments.

Moshe

 

0 Likes