Need help creating a lisp to restore viewport layer state

Need help creating a lisp to restore viewport layer state

JGranata_GdB
Enthusiast Enthusiast
854 Views
4 Replies
Message 1 of 5

Need help creating a lisp to restore viewport layer state

JGranata_GdB
Enthusiast
Enthusiast

Hello,

 

I am an engineer who was assigned to do some programing by my boss. I understand basic logic, but i have no knowledge of the coding language that is used. I have multiple files that are on average 60 tabs each, and each file i need to restore a layerstate in the viewport on selected tabs. I do not mind opening each file and running it for each, to make it simpler and for the potential for easier use on future projects.

 

 

Idealy, im looking for a LISP that can:

-Ignores the Model

-Search the tabs for "B" and only apply changes to those tabs. (The tabs are labeled 1B, 1T, 1U, 1H, 2B, 2T, 2U, 2H....56B, 56T, ..... ect) 

-For the viewport in the tab, change the layerstate to "LayerStateName"

-Return to Paperspace

 

I assume that to do this I need to make an array/list, put all the tabs that contain a "B" into the array/list, and then run a for-each statement on the array/list that will restore the layerstate in just the viewport and not paperspace. then return to paper space.

 

Thank you in advance for the help

0 Likes
855 Views
4 Replies
Replies (4)
Message 2 of 5

Kent1Cooper
Consultant
Consultant

Welcome to these Forums!

 

Conveniently, the (layoutlist) function returns a list of all Layout names excluding "Model," which means it's not difficult to make a list of all non-Model tabs containing a "B":

 

(vl-load-com); if needed

(setq layouts (vl-remove-if-not '(lambda (x) (wcmatch x "*B*")) (layoutlist)))

 

If the "B" is always the last character in Layout names you want to process, as in your examples, you could omit the last asterisk above, and then it would ignore any that might happen to have a "B" somewhere earlier in its name [unless they also end with one].

 

Then, if there's only one Viewport [I am assuming that from your use of the phrase "the viewport"] in each such Layout:

(foreach layout layouts

  (setvar 'ctab layout); get into it

  (command

    "_.mspace" ; in case it's currently in Paper Space

    "_.layer" "_state" "_restore" "LayerStateName" "" ""

    "_.pspace"

  ); command

); foreach

 

Or, they could be conflated, omitting the 'layouts' variable:

 

(foreach layout (vl-remove-if-not '(lambda (x) (wcmatch x "*B*")) (layoutlist))

  (setvar 'ctab layout); get into it

  .... etc. as above

 

 

The number of Enters [""] finishing out the Layer command may vary slightly with the version of AutoCAD, so if it doesn't work, try it manually and see how many you need to close it out.

 

You could also have it return to whatever space/tab/viewport you were in when you started, if you like.

 

If there might be more than one Viewport in any such Layout, write back....

Kent Cooper, AIA
Message 3 of 5

JGranata_GdB
Enthusiast
Enthusiast

Kent,

 

Thank you for the quick reply! I am in the process of working on another project, but i will test this out shortly.

 

For what I need now, there is only one viewport on each tab. If I did need to restore two view ports with two different layer states, is that possible?

Such as the top view port is in "layerstate 2" and the bottom viewport is in "layerstate 3"

 

 

On a side not, if I wanted to run this again for "T' and "layerstate 4", is it possible to just add to the end of the script? See example below: 

 

 

(defun C:VPR ()
 (vl-load-com);

;; Search for B
 (setq layouts (vl-remove-if-not '(lambda (x) (wcmatch x "*B")) (layoutlist))
 (foreach layout layouts
  (setvar 'ctab layout); get into it
  (command
   "_.mspace" "_.layer" "_state" "_restore" "Layerstate1" "_.pspace"
  ); command
 ); foreach

;;Search for T
(setq layouts (vl-remove-if-not '(lambda (x) (wcmatch x "*T")) (layoutlist))
 (foreach layout layouts
  (setvar 'ctab layout); get into it
  (command
   "_.mspace" "_.layer" "_state" "_restore" "Layerstate4" "_.pspace"
  ); command
 ); foreach


 )
);defun

 

 

0 Likes
Message 4 of 5

Kent1Cooper
Consultant
Consultant

@JGranata_GdB wrote:

.... if I wanted to run this again for "T' and "layerstate 4", is it possible to just add to the end of the script? .... 


You can handle as many as you like in one pass, since (wcmatch) can look for multiple string patterns, this way:

 

 (setq layouts (vl-remove-if-not '(lambda (x) (wcmatch x "*B,*T")) (layoutlist))
; add as many to the (wcmatch) string as you like: "*B,*T,*X,*Y,*Z" (foreach layout layouts (setvar 'ctab layout); get into it (command "_.mspace"
"_.layer" "_state" "_restore"
(cond
((wcmatch layout "*B") "Layerstate1")
  ((wcmatch layout "*T") "Layerstate4")
((wcmatch layout "*X") "Layerstate8"); add more corresponding to added ones above
((wcmatch layout "*Y") "Layerstate32")
((wcmatch layout "*Z") "Layerstate459")
); cond
"" "" ; <--don't you need some of these to complete the Layer command?
"_.pspace" ); command ); foreach

 

Kent Cooper, AIA
Message 5 of 5

JGranata_GdB
Enthusiast
Enthusiast

I tried to run this (and the one above as well), and it is not working for me. No matter what file I try to use it in, it keeps changing the models space to the layer state, and this in turn messes the layer states on the other page view ports.

 

If I apply the layer state to each view port manually, everything is fine.

 

I am using AutoCAD 2014.

Just to give a better idea of the File setup:

1B has 1 viewport that I want the layerstate for that viewport to be LayerState1.
1T has 1 viewport that I want the layerstate for that viewport to be Layerstate2.
1U has 2 view ports that I have set up to be Layerstate3 and Layerstate4
1H has 2 view ports that I have set up to both be Layerstate 5.

 

For refernece, this is what i was running: 

 

 

(defun C:VPRB ()
	(vl-load-com)
	(setq layouts (vl-remove-if-not '(lambda (x) (wcmatch x "*B")) (layoutlist)))
	(foreach layout layouts
		(setvar 'ctab layout); Get into the tab
		(command
			"_.mspace" 
			"_.layer" "_state" "_restore"
			(cond
				((wcmatch layout "*B") "BASE")
				;;((wcmatch layout "*T") "TOPO") 
				;;((wcmatch layout "*U") "Layerstate-3")
				;;((wcmatch layout "*H") "Layerstate-4")
			); end cond
			"" "" 
			"_.pspace"
		); end command
	); end foreach

);defun

 

0 Likes