Message 1 of 8
Code to rotate Multiple UCS of viewports with Manual and automatic options.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear All,
I am working on a code which can rotate UCS of viewports.
There are 2 methods.
1. Manual : The user will select the viewports he want to rotate.
2.Automatic: UCS of all the viewports will get rotate.
I am facing issues . Kindly help.
Code below for Manual and Automatic(not working, Contains error!) :
(defun c:RotateUCSInLayouts (/ doc layouts layout angle mode selectedVPorts vpList vp ent layoutName ownerID currentLayout)
(vl-load-com)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
;; Get the rotation angle from the user
(setq angle (getreal "\nEnter UCS rotation angle in degrees: "))
(if angle
(progn
;; Ask the user for mode of operation
(initget "M A")
(setq mode (getkword "\nApply to [Manual (M) / Automatic (A)] <M>: "))
(if (null mode) (setq mode "M")) ;; Default to Manual if not specified
;; Store the current active layout
(setq currentLayout (vla-get-Name (vla-get-ActiveLayout doc)))
;; If manual mode, let the user select viewports (only in the current layout)
(if (= mode "M")
(progn
(princ "\nSelect Viewports in the current Layout:")
(setq selectedVPorts (ssget '((0 . "VIEWPORT")))) ;; User selects viewports
(if selectedVPorts
(progn
(setq vpList '())
(foreach vp (mapcar 'cadr (ssnamex selectedVPorts))
(setq ent (vlax-ename->vla-object vp))
(if (and ent (vlax-property-available-p ent 'OwnerID)) ;; Ensure it's a valid VLA object
(progn
;; Get the owner (paper space block) of the viewport
(setq ownerID (vla-get-OwnerID ent))
;; Get the layout associated with the viewport
(setq layout (vla-Item (vla-get-Layouts doc) ownerID))
(setq layoutName (vla-get-Name layout))
;; Ensure the selected viewport belongs to the current layout
(if (= (strcase layoutName) (strcase currentLayout))
(setq vpList (cons ent vpList))
(progn
(princ "\nViewports from multiple layouts cannot be selected in Manual mode. Exiting.")
(exit)
)
)
)
(princ "\nSelected object is not a valid viewport. Skipping.")
)
)
)
(progn
(princ "\nNo viewports selected. Exiting.")
(exit)
)
)
)
)
;; Process each layout
(setq layouts (vla-get-layouts doc))
(vlax-for layout layouts
(if (/= (strcase (vla-get-name layout)) "MODEL") ; Skip Model Space
(progn
(setvar "TILEMODE" 0) ;; Ensure we are in Layout Mode
(if (vlax-property-available-p layout 'Name)) ;; Ensure it's a valid layout
(progn
(vla-put-ActiveLayout doc layout) ;; Activate the layout
(princ (strcat "\nProcessing layout: " (vla-get-Name layout)))
;; If Automatic mode OR current layout is selected, apply changes
(if (or (= mode "A") (= (strcase (vla-get-Name layout)) (strcase currentLayout)))
(progn
;; If in Manual Mode, process each selected viewport
(if (= mode "M")
(foreach vp vpList
(if (vlax-property-available-p vp 'Number)) ;; Ensure it's a valid paper space viewport
(progn
(princ (strcat "\nProcessing viewport: " (vl-princ-to-string vp)))
(if (vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ()
(vla-put-ActivePViewport doc vp) ;; Activate viewport
(command "_.MSPACE") ;; Enter Model Space
(command "_.UCS" "Z" angle) ;; Rotate UCS by given angle
(command "_.PLAN" "Current") ;; Align view to UCS
(command "_.PSPACE") ;; Return to Paper Space
)
)
)
(princ "\nError processing viewport. Skipping.")
)
)
(princ "\nInvalid viewport. Skipping.")
)
)
;; If Automatic Mode, apply UCS change to all viewports in each layout
(progn
(command "_.MSPACE") ;; Enter Model Space
(command "_.UCS" "Z" angle) ;; Rotate UCS by given angle
(command "_.PLAN" "Current") ;; Align view to UCS
(command "_.PSPACE") ;; Return to Paper Space
)
)
)
)
)
(princ "\nInvalid layout. Skipping.")
)
)
)
)
(princ "\nUCS rotated and PLAN set in selected layouts.")
)
(princ "\nInvalid angle entered. Operation aborted.")
)
(princ)
)
Below is the working code for the Automatic:
(defun c:AutoRotateUCS_Layouts (/ doc layouts layout angle vports vp)
(vl-load-com)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
;; Get the rotation angle from the user
(setq angle (getreal "\nEnter UCS rotation angle in degrees: "))
(if angle
(progn
(setq layouts (vla-get-layouts doc))
(vlax-for layout layouts
(if (/= (strcase (vla-get-name layout)) "MODEL") ; Skip Model Space
(progn
(setvar "TILEMODE" 0) ;; Ensure we are in Layout Mode
(vla-put-ActiveLayout doc layout) ;; Activate the layout
;; Get all viewports in the layout
(setq vports (vla-get-ActiveLayout doc))
(if vports
(progn
(command "_.MSPACE") ;; Enter Model Space
(command "_.UCS" "Z" angle) ;; Rotate UCS by given angle
(command "_.PLAN" "Current") ;; Align view to UCS
(command "_.PSPACE") ;; Return to Paper Space
)
(princ "\nNo viewport found in layout: " (vla-get-name layout))
)
)
)
)
(princ "\nUCS rotated and PLAN set in all layouts.")
)
(princ "\nInvalid angle entered. Operation aborted.")
)
(princ)
)