Viewport Lisp
I needed a lisp routine that will, when called lock all viewports in the current drawing and set them on the proper layer. A quick google search led me to a bit of code that got me started, I added the functionality to change the layer of each viewport to a specific layer, works great except for one flaw. The paperspace viewports are also changed to layer "x", however they must remain on layer 0 - which is evident after running an audit on the drawing following using the lisp routine.
The lisp steps through each viewport (using vla and such), a simple if statement should allow me to filter out the paperspace viewports when changing the layer, trouble is, I do not know what to search for. The following is a shot in the dark:
(if (/= (vla-get-name obj) paperspaceviewport)
(setq obj (vla-put-layer obj VPLayer))
)
I tried to step through each viewport object in the database and output the name to the command like like this:
(princ (vla-get-name obj)) but I get an error, any ideas?
I also can't seem to find any documentation on the vla functions aside from a list...
Comments
-
A quick one:
;;; Get all viewports in the dwg except the paperspace viewports.
;;; The function is based on the fact that PS viewports are, or seem to be, always the first item in a PS block.
(defun GetViewPorts ( / actDoc)
(setq actDoc (vla-get-activedocument (vlax-get-acad-object)))
(apply
'append
(mapcar
'(lambda (a)
(vl-remove-if-not
'(lambda (b)
(= (vla-get-objectname b) "AcDbViewport")
)
(cdr (vla-collection->list a))
)
)
(vl-remove
(vla-get-modelspace actDoc)
(mapcar
'vla-get-block
(vla-collection->list (vla-get-layouts actDoc))
)
)
)
)
)0 -
Dear Scott, Dear Roy,
the paperspace viewport always has CVPORT being 1 ... :-)
Regarding documentations on VLA functions :
basically, those are > 1500 in total ... you can view the ObjectModel
by a COM/OLE browser, or by VB/VBA IDE, where the COM Typelibrary informations
are shown, for Bricscad object.
Many greetings, Torsten0 -
Many thanks for that tip Torsten.
So basically the code snippet Scott is looking for could look like this:
(if (/= (cdr (assoc 69 (entget (vlax-vla-object->ename obj)))) 1)
(vla-put-layer obj VPLayer)
)0 -
;|ViewportsProGraph Solutions - www.prograph.caTaken from Autodesk.com forums; commented, cleaned up and functionality to change layers to a defined layer added - see line 30 by Scott McKenzieSept 28, 2011This routine will lock all viewports in the drawing, and upon loading will run**** This Release ****; - Initial**** Future Release ****; - None planned**** Bugs ****; - None found - report bugs to smckenzie@prograph.ca**** Routines Start ****|;(princ "\nLOADING VIEWPORTS - ProGraph Solutions");View port controls(defun c:vl () ;Define the viewport layer name (setq VPLayer "VP") ;Set a changelayer flag if the layer exists in the database (setq ChangeLayer (tblsearch "LAYER" VPLayer)) ;initialize ActiveX (vl-load-com) ;Define the application (setq acad-app (vlax-get-acad-object)) ;Define the document (setq acad-doc (vla-get-ActiveDocument acad-app)) ;Create and empty selection set (setq vportss (ssadd)) ;create a selection set of all the viewports (setq vportss (ssget "x" '((0 . "VIEWPORT")))) ;Determine the number of viewports in the selection set (setq numvports (sslength vportss)) ;Set an index, this is the counter for the while loop (setq index 0) ;verify status of Display Locked and change to yes if required (while (< index numvports) ;grab the viewport object from the selection set at index [index] (setq ent (ssname vportss index)) ;Create an object from the current viewport (setq obj (vlax-ename->vla-object ent)) ;Set a boolean flag telling us if the viewport is locked (setq locked (vla-get-DisplayLocked obj)) ;Check if the viewport is locked (if (= locked :vlax-false) ;It is not locked, so lock it (setq obj (vla-put-DisplayLocked obj :vlax-true)) ) ;What layer is the viewport on (setq currlayer (vla-get-Layer (vlax-ename->vla-object ent))) ;Is this the right layer? (if (/= currlayer VPLayer) ;If not make it right, only if this is not the paperspace viewport (if (/= (cdr (assoc 69 (entget (vlax-vla-object->ename obj)))) 1) (vla-put-layer obj VPLayer) ) ) ;Increment the counter (setq index (1+ index)) ) (princ "\nALL VIEWPORTS LOCKED") ;Exit Cleanly (princ));Upon loading the script, lock all viewports(c:vl);Exit quietly(princ)
The above is the code that will lock all viewports and change their layers to a user defined layer (line 30). I took the base of the code from Autodesk.com forums, commented it and added the layer chaning functionality. The script will run on loading, see line 119. Thanks for the help and enjoy...
0 -
BTW: You can also view the Bricscad automation object model in the COM section of the Developer Reference.
0 -
Cool, that's what I was looking for... thanks
0 -
Came across this code on http://www.theswamp.org. Useful for getting the properties and methods for the selected object:
(defun c:dump ()
(vl-load-com)
(vlax-dump-object (vlax-ename->vla-object (car (entsel))) T)
(textscr)
(princ)
)Regards,
Jason Bourhill
0 -
I think it would be cool to be able to set the viewports in paper space to other layers besides 1. If I could do that I could set the scale for each viewport in paper space and then just go in and only turn on the layer for the viewport with the scale I want to operate in. That is of course if you could turn off the viewport by turning of the layer with that viewport.
s.
0 -
Here is some updated code - I had to squish a bug that appeared when loading the command at startup of a new drawing that did not contain user drawn viewports...[code];View port controls(defun c:vl ();Define the viewport layer name(setq VPLayer "VP");Set a changelayer flag if the layer exists in the database(setq ChangeLayer (tblsearch "LAYER" VPLayer));initialize ActiveX(vl-load-com);Define the application(setq acad-app (vlax-get-acad-object));Define the document(setq acad-doc (vla-get-ActiveDocument acad-app));Create and empty selection set(setq vportss (ssadd));create a selection set of all the viewports(setq vportss (ssget "x" '((0 . "VIEWPORT"))));Determine the number of viewports in the selection set(setq numvports (sslength vportss));Set an index, this is the counter for the while loop(setq index 0);verify status of Display Locked and change to yes if required(while (< index numvports);grab the viewport object from the selection set at index [index](setq ent (ssname vportss index));Create an object from the current viewport(setq obj (vlax-ename->vla-object ent));Set a boolean flag telling us if the viewport is locked(setq locked (vla-get-DisplayLocked obj));Check if the viewport is locked(if (= locked :vlax-false);It is not locked, so lock it(setq obj (vla-put-DisplayLocked obj :vlax-true)));Check to see if the change layer flag is true(if (/= changelayer nil)(progn;What layer is the viewport on(setq currlayer (vla-get-Layer (vlax-ename->vla-object ent)));Is this the right layer?(if (/= currlayer VPLayer);If not make it right, only if this is not the paperspace viewport(if (/= (cdr (assoc 69 (entget ent))) 1)(vla-put-layer obj VPLayer)))));Increment the counter(setq index (1+ index)));Let the user know what is going on(if (/= changelayer nil)(setq notify (strcat "\nALL VIEWPORTS LOCKED AND CHANGED TO LAYER \"" vplayer "\""))(setq notify (strcat "\nALL VIEWPORTS LOCKED - WARNING LAYER NOT CHANGED, LAYER: \"" vplayer "\" DOES NOT EXIST IN THE DRAWING")))(princ notify);Exit Cleanly(princ));Upon loading the script, lock all viewports(c:vl);Exit quietly0