Issue with copying layouts in Autolisp

Issue with copying layouts in Autolisp

Anonymous
Not applicable
1,466 Views
19 Replies
Message 1 of 20

Issue with copying layouts in Autolisp

Anonymous
Not applicable

Hello all, I'm a basic autolisp programmer. I'm trying to create and rename layouts.

The code: (command "layout" "copy" (getvar "ctab") "") works fine as a stand alone but when used within a larger program it cancels that program. 

It's always followed by two lines of *Cancel* at the command line, which confirms this.

Any help with a workaround would be greatly appreciated. thanks!

0 Likes
1,467 Views
19 Replies
Replies (19)
Message 2 of 20

Anonymous
Not applicable

Attach the full code for a look !!

0 Likes
Message 3 of 20

Anonymous
Not applicable

Here's the code within a simple (while) command and the resulting error message below:

(defun lyt_test ()
(setq dsc (getstring "\nLayout description: ")
pg (getint "\nNew page #: ")
lyt (strcat dsc " (" (itoa pg) ")")
);setq

(command "layout" "c" (getvar "ctab") lyt);copies current tab

(while
(/= dsc "")
(setq dsc (getstring "\nNEXT Layout description: ")
pg (+ pg 1)
lyt (strcat dsc " (" (itoa pg) ")")
);setq

(command "layout" "c" (getvar "ctab") lyt);copies current tab
);while
);defun

 

The error message at the command line:

Command:
NEXT Layout description: *Cancel*
; error: Function cancelled

Command: *Cancel*

0 Likes
Message 4 of 20

ВeekeeCZ
Consultant
Consultant

I agree that the issue would be most likely elsewhere.

Try to add the active command check prior to this line.

 

Edit: Seeing your code... what should we fill into prompts? I¨ve tried something and it seems working...

 

0 Likes
Message 5 of 20

Anonymous
Not applicable

Thank you for your assistance..

Not sure what you mean by trying 'active command line check'. Could you please clarify?

My CMDACTIVE is set to 1 but it's read-only. I'm not familiar with this command.

It's encouraging that you're not having any issues running the code I posted so you must be entering input correctly.

 

0 Likes
Message 6 of 20

ВeekeeCZ
Consultant
Consultant

Hmm, that was before seeing the code. I thought it's more complex than it is.

I thought some simple user control, like this:

(if (> (getvar 'cmdactive) 0) (princ "Something is wrong prior this line... Some command is already active and should not be!!"))

 

Not really sure where is the culprit in your code because it works for me. But here is a little enhanced version. Try and see if it helps. 

 

(defun c:lyt_test (/ pg lyt dsc)

  (if (or (/= (getvar 'ctab) "Model")
	  (prompt "\nError: You're in model!")
	  )
    (while (/= "" (setq dsc (getstring T "\nLayout description: ")))
      (and (setq pg (getint "\nNew page #: "))
	   (setq lyt (strcat dsc " (" (itoa pg) ")"))
	   (or (not (member lyt (layoutlist)))
	       (prompt (strcat "\nError: Layout '" lyt "' already exists!")))
	   (vl-cmdf "_.layout" "copy" "" lyt)
	   )))
  (princ)
  )

 

Message 7 of 20

Anonymous
Not applicable

I ran your code and it successfully created the first new layout as desired but it still crashed.  Definitely sounds like something else is at work. I may have to figure out a workaround...

 

Here's the full command line display and resulting error message if you still feel like tackling it:

Command: LYT_TEST

Layout description: X2

New page #: 5
_.layout
Enter layout option [Copy/Delete/New/Template/Rename/SAveas/Set/?] <set>: copy
Enter name of layout to copy <X1 (4)>:
Enter layout name for copy <X1 (5)>: X2 (5) Layout "X1 (4)" copied to "X2 (5)".

Command:
Layout description: *Cancel*
; error: Function cancelled

Command: *Cancel*

0 Likes
Message 8 of 20

Jonathan3891
Advisor
Advisor

the code @ВeekeeCZ provided worked for me with no issues or crashes at all.


Jonathan Norton
Blog | Linkedin
0 Likes
Message 9 of 20

Anonymous
Not applicable

Hmm.. Okay that reinforces the probability that the problem lies with my system and/or its setting. Thanks for the additional input!

0 Likes
Message 10 of 20

ВeekeeCZ
Consultant
Consultant

Try this... btw it seems that you don't care about a position of the new layout... or do you?

 

(defun c:lyt_test (/ pg lyt dsc)

  (if (or (/= (getvar 'ctab) "Model")
	  (prompt "\nError: You're in model!")
	  )
    (while (/= "" (setq dsc (getstring T "\nLayout description: ")))
      (and (if (> (getvar 'cmdactive) 0)
	     (prompt "Something is wrong prior this line... Some command is already active and should not be!!")
	     T)
	   (setq pg (getint "\nNew page #: "))
	   (setq lyt (strcat dsc " (" (itoa pg) ")"))
	   (or (not (vl-position lyt (layoutlist)))
	       (prompt (strcat "\nError: Layout '" lyt "' already exists!")))
	   (or (not (vl-position "dummy" (layoutlist)))
	       (vl-cmdf "_.layout" "_delete" "dummy"))
	   (vl-cmdf "_.layout" "_copy" "" "dummy")
	   (vl-cmdf "_.layout" "_rename" "dummy" lyt)
	   )))
  (princ)
  )

 

0 Likes
Message 11 of 20

Anonymous
Not applicable

Thank you for the response. The program still crashes with this code..

With regards to the order, is that in reference to the page number prompt? I actually hope to have the new layouts in numerical order. I just have the initial page number prompted.

0 Likes
Message 12 of 20

ВeekeeCZ
Consultant
Consultant

Do you really need the naming pattern the same as default? Meaning LayoutName (index-in-parenthesis). Because it somehow may interfere with each other. See your listing in msg #7. The red numbers should be the same.

Enter layout name for copy <X1 (5)>: X2 (5) Layout "X1 (4)" copied to "X2 (5)".

How about index without parenthesis...

 

 

Anyway, still can't replicate the issue, so I cannot fix it. Running out of ideas at this point. You need to allow us to experience the issue: So take your drawing, remove all the content but layouts and somehow describe your workflow... a video capture would be good, a command-line listing may be enough... post the drawing. 

 

Also, I found this older code of mine, try and see if it works... The layout name patterns should look like this LayoutName01

(defun c:cnl  (/ num idx old bas new)
  
  (if (and (= 0 (getvar 'tilemode))  					; make sure some layout is current
           (setq num (getint "\nNumber of  copies of this tab: "))	
           (setq idx (getint "\nFirst suffix number: "))
           (setq old (getvar 'ctab))					; current name of layout
           (> (strlen old) 1)						; current name is longer the 1 character
           (setq bas (substr old 1 (- (strlen old) 2)))			; cut last 2 chars for current name to make base name
           )
    (repeat num								
      (setq str (itoa idx)						; make string of suffix integer
            str (if (= 1 (strlen str))					; if is only 1 digit long...
                  (strcat "0" str)					; ... add 0 before
                  str)
            new (strcat bas str))					; new name = base + string
      (if (member new (layoutlist))					; is the new name already used for layout?
        (princ (strcat "\nLayout '" new " ' already exists."))
        (command "._LAYOUT" "_Copy" old (strcat old ".")		; copy current tab, for new tab would be used dummy name (old + .) to make sure that new layout would be always right behing the old one
                 "._LAYOUT" "_Rename" (strcat old ".") new))		; rename dummy layout with real new name.
      (setq idx (1+ idx)						; add 1 to index
            old new)))							; set new name to old for next copy.
  (princ)
)
0 Likes
Message 13 of 20

Anonymous
Not applicable

Thank you.. I was also wondering about possible conflict with the numbers in parenthesis but I don't think it is (could still be wrong).

 Your code appears to work fine (although without the closing parenthesis) but I can still see something going on in the background. Looking at the text screen readout afterwards, I see a pattern: for every execution of a layout copy there are (2) displays of Command: *Cancel* when the program is done.

 

My objective is to start with any given layout tab description and starting page number for ex. "XX (3)" and continue creating additional layout tabs with any desired description with the next incremental page number, for ex. "YY (4)" until the RETURN key terminates during the description prompt.

My code (again entered below) will successfully create and name the first new layout tab but the program crashes after setting to the new layout tab.

I need to have the layout tabs numbered and there will always be at least (1) numbered layout tab already existing.

Drawing file is attached.  Below the program code I have pasted the text screen readout.

My code:

 

(defun C:lyt_test (/ dsc pg lyt)
(setq dsc (getstring "\nLayout description: ")
pg (getint "\nNew page #: ")
lyt (strcat dsc " (" (itoa pg) ")")
);setq

(command "layout" "c" (getvar "ctab") lyt);copies current tab
(command "layout" "s" lyt);makes the new layout current

(while
(/= dsc "")
(setq dsc (getstring "\nNEXT Layout description: ")
pg (+ pg 1)
lyt (strcat dsc " (" (itoa pg) ")")
);setq

(command "layout" "c" (getvar "ctab") lyt);copies current tab
(command "layout" "s" lyt);makes the new layout current
);while
);defun

 

Text screen readout:

 

Command: LYT_TEST

Layout description: SHT

New page #: 2
layout
Enter layout option [Copy/Delete/New/Template/Rename/SAveas/Set/?] <set>: c
Enter name of layout to copy <SHT (1)>: SHT (1)
Enter layout name for copy <SHT (2)>: SHT (2) Layout "SHT (1)" copied to "SHT (2)".

Command: layout
Enter layout option [Copy/Delete/New/Template/Rename/SAveas/Set/?] <set>: s
Enter layout to make current <SHT (1)>: SHT (2) Regenerating layout.

Command:
NEXT Layout description: *Cancel*
; error: Function cancelled

Command: *Cancel*

Command: 'VLIDE
Command:

0 Likes
Message 14 of 20

CodeDing
Advisor
Advisor

@Anonymous ,

 

This works for me. Also, BeeKeeCZ's code worked for me when testing also.

(defun c:LYT_TEST ( / ctab pg d lyt cmd)
(if (eq "Model" (setq ctab (getvar 'CTAB)))
  (progn (prompt "\n...Can Not be run in Model Space.\n") (exit))
);if
(initget 7) (setq pg (1- (getint "\nNew Page #: ")))
(while (not (eq "" (setq d (getstring (strcat "\nLayout (" (itoa (setq pg (1+ pg))) ") description: ")))))
  (setq lyt (strcat d " (" (itoa pg) ")"))
  (setq cmd (getvar 'CMDECHO)) (setvar 'CMDECHO 0)
  (command  "-LAYOUT" "c" ctab lyt "-LAYOUT" "s" lyt)
  (setvar 'CMDECHO cmd)
);while
(prompt "\nUser finished.. Complete..")
(princ)
);defun

Best,

~DD

0 Likes
Message 15 of 20

ВeekeeCZ
Consultant
Consultant

Hmm. Nothing new. Cannot replicate the issue. Your command-line listing looks like it was cancelled manually. Literally. If I hit ESC it looks the same. Maybe try to run the routine without VLIDE. Close VLIDE, load attached code by drag-n-drop into drawing, then type the command name to run it.

 

BTW any add-ons, reactors.... are use using anything what might cause *cancel*??

0 Likes
Message 16 of 20

Anonymous
Not applicable

Nope.. nothing else going on in the background. I'm stumpled.. Thank you for your help regardless.. much appreciated.

0 Likes
Message 17 of 20

ВeekeeCZ
Consultant
Consultant

Is this behavior associated with multiple -LAYOUT command usage only? Or all commands do this?

Does this happen only when using (getstring) user prompt? How about if you use (getkword)...

Do you use some devices with advanced drivers? (multi-button mice, keyboard with additional G-keys...)

 

0 Likes
Message 18 of 20

Anonymous
Not applicable

Thank you for the response.  I haven't noticed the issue before. It appears to be a direct result of the Command itself.

I did find a workaround: instead of using the 'copy' option in the LAYOUT command, I tried the 'template' option and had no issue. I just had to create a couple of templates based on the sheet sizes I need and am able to import them with no problem. Been celebrating all weekend..lol.

 

Thanks to all who tried to help!

0 Likes
Message 19 of 20

ВeekeeCZ
Consultant
Consultant

And you do experience the issue even with an empty layout..... Actually, it seems that there are some (almost) empty mtexts... It worth trying... with mtexts removed.

Otherwise better luck with other commands 🙂

 

0 Likes
Message 20 of 20

Anonymous
Not applicable

Still no luck. Oh well.. all's well that ends well. 

0 Likes