Using AutoCAD command to change Vport layer

Using AutoCAD command to change Vport layer

mruPRQUJ
Advocate Advocate
3,913 Views
42 Replies
Message 1 of 43

Using AutoCAD command to change Vport layer

mruPRQUJ
Advocate
Advocate

Hi there,
I am wondering if it is possible to use AutoCAD command to finish below task, (to input AutoCAD command in command line, in order to create script later)
change all Vport to layer $X-ANNO-VPRT,
color by layer,

Bring Vport to Front (in draw order)

 

 

Thank you very much in advance! 🙂

0 Likes
3,914 Views
42 Replies
Replies (42)
Message 21 of 43

mruPRQUJ
Advocate
Advocate

Sorry to bother you, I just find below info regarding this lisp, is it ok? thanks.

 

_.-LAYER _MAKE $X-ANNO-VPRT _.-LAYER _LOCK $X-ANNO-VPRT _.TILEMODE 0 _.PSPACE _.MVIEW _LOCK _ON _ALL _.CHPROP _P No previous selection set.
_LAYER $X-ANNO-VPRT
Invalid option keyword.
; error: Function cancelled
_.Layer
Invalid option keyword.
; error: Automation Error. On locked layer
*Cancel*
Command:

0 Likes
Message 22 of 43

paullimapa
Mentor
Mentor
Accepted solution

Again, as I mentioned conditions that would cause the lisp to fail are:

1. Layer Vport is on is Locked

2. No Vports found in current Layout

So to make sure the Layer is not locked include this with the Layer commands at the beginning like this.

Perhaps share a sample dwg where the code fails

 

(defun c:LL()
; Layer Unlock everything
(Command "_.-LAYER" "UNL" "*" "")
; Layer Make sets Layer current
(Command "_.-LAYER" "_MAKE" "$X-ANNO-VPRT" "")
; Layer New creates Layer but does not set as current so you decide which one you want to use
;(Command "_.-LAYER" "_NEW" "$X-ANNO-VPRT" "")
; Now before running Mview command you need to first set Tilemode to 0
(COMMAND "_.TILEMODE" "0")
; then make sure is in Paper space & not in Model space inside a Vport
(Command "_.PSPACE")
; now run Mview command to Lock & turn On all Vports in current Layout
(Command "_.MVIEW" "_LOCK" "_ON" "_ALL" "")
; Chprop command Previous will only succeed if the current Vport's Layer is NOT Locked and there are Vports found in the current Layout
(Command "_.CHPROP" "_P" "" "_LAYER" "$X-ANNO-VPRT" "_COLOR" "BYLAYER" "")
; Draworder command only works if there are Previous Vports found
(Command "_.DRAWORDER" "_P" "" "_FRONT")
; Layer Locks the Vport layer
(Command "_.-LAYER" "_LOCK" "$X-ANNO-VPRT" "")
(princ)
)
; automatically run the command
(c:LL)

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 23 of 43

mruPRQUJ
Advocate
Advocate

Many thanks! 🙂

0 Likes
Message 24 of 43

paullimapa
Mentor
Mentor

sure & glad to have helped...but may I ask why you want just AutoCAD commands?

This would be more flexible if lisp functions are used as well.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 25 of 43

mruPRQUJ
Advocate
Advocate

After create command, I want to convert them to Macro. At that time, I did not know there is some problem in the original Macro, thanks. 🙂

0 Likes
Message 26 of 43

paullimapa
Mentor
Mentor

But lisp functions within a file like LL.lsp can be loaded and run in a menu macro too which is what we're doing here. The only thing I can think of to not use lisp is if running LT 2023 and below.

But 2024 LT will run most lisp functions as well.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 27 of 43

mruPRQUJ
Advocate
Advocate

Great! I use full version of 2021. Is it difficult to do it this way? thanks.

0 Likes
Message 28 of 43

paullimapa
Mentor
Mentor

Not difficult but less flexible like I mentioned.

Once you implement lisp functions then you can do the following:

1. Filter out Locked Layers, keep a list of these and UnLock them. Then at the end of the code run command to Lock those Layers again restoring to the way they were originally

2. Cycle through multiple Layouts searching for Paper space Vports and only run the Mview & Chprop commands when there are Vports on those Layouts. Actually there are lisp functions to select all the Vports turn On & Lock as well as changing the Vport's Layer without having to go into Paper Space.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 29 of 43

mruPRQUJ
Advocate
Advocate

Is it possible to make it simple. unlock all the layer, no need to lock them again. But Vport display need to be locked, thanks.

0 Likes
Message 30 of 43

paullimapa
Mentor
Mentor
Accepted solution

Here's a version that:

1. UnLocks all layers at the beginning

2. Cycles through all Layouts

3. Only runs Chprop & Displayorder commands when there are Vports found so you won't error out

4. Locks the Vport Layer at the very end so if LL is run again on the same drawing it won't error out

 

(defun c:LL()
; Layer Unlock everything
(Command "_.-LAYER" "UNL" "*" "")
; Layer Make sets Layer current
(Command "_.-LAYER" "_MAKE" "$X-ANNO-VPRT" "")
; Layer New creates Layer but does not set as current so you decide which one you want to use
;(Command "_.-LAYER" "_NEW" "$X-ANNO-VPRT" "")
; Now before running Mview command you need to first set Tilemode to 0
(COMMAND "_.TILEMODE" "0")
(foreach layout (layoutlist) ; cycle through all layouts
 (setvar "ctab" layout) ; set to that layout
; then make sure is in Paper space & not in Model space inside a Vport
 (Command "_.PSPACE")
; now run Mview command to Lock & turn On all Vports in current Layout
 (Command "_.MVIEW" "_LOCK" "_ON" "_ALL" "" "_.MVIEW" "_ON" "_ALL" "")
 (if (ssget "_P") ; check if there are vports selected
  (progn ; then do the following
 ; Chprop command on Previous Vports selected
   (Command "_.CHPROP" "_P" "" "_LAYER" "$X-ANNO-VPRT" "_COLOR" "BYLAYER" "")
 ; Draworder command on Previous Vports selected
   (Command "_.DRAWORDER" "_P" "" "_FRONT")
  ) ; progn
  (princ(strcat"\nNo Vports Found in Layout: " layout))
 ) ; if
) ; foreach
; Layer Locks the Vport layer
(Command "_.-LAYER" "_LOCK" "$X-ANNO-VPRT" "")
(princ)
) ; defun ll
; automatically run the command
(c:LL)

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 31 of 43

mruPRQUJ
Advocate
Advocate

It works very well, thank you so much for your great support! 🙂 

0 Likes
Message 32 of 43

paullimapa
Mentor
Mentor

once again glad to have helped...cheers!!!


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

mruPRQUJ
Advocate
Advocate

cheers!!!

0 Likes
Message 34 of 43

mruPRQUJ
Advocate
Advocate

Hi there,

 

"But lisp functions within a file like LL.lsp can be loaded and run in a menu macro too which is what we're doing here. "

 

Sorry to bother you, could you please advise me how to do it? many thanks.

0 Likes
Message 35 of 43

paullimapa
Mentor
Mentor
Accepted solution

the command within a menu macro is like what you're typing out at the command line except typically preceded with ^C characters that tell AutoCAD to cancel the current command sequence if any like this:

^C^C(load"LL")

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 36 of 43

mruPRQUJ
Advocate
Advocate

Thanks a lot! 🙂

0 Likes
Message 37 of 43

paullimapa
Mentor
Mentor

once again...cheers!!!


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

mruPRQUJ
Advocate
Advocate

Cheers! 🙂

0 Likes
Message 39 of 43

mruPRQUJ
Advocate
Advocate

Hi there,

 

It did not work. Macro is below

^C^C(load"ASet")

 

But, run ASET, it works. Could you please provide some advice to me?  many thanks.

 

 

 

 

 

0 Likes
Message 40 of 43

paullimapa
Mentor
Mentor
Accepted solution

Don’t mix apples with oranges. If you recall you wanted LL to run automatically after loading. But ASet doesn’t run automatically after loading so you have to follow with entering command ASet to run it. That means you need to include this in the macro:

^C^C(load"ASet");ASet

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos