Create Viewport with Zoom to Coordinates

Create Viewport with Zoom to Coordinates

murmanator
Advocate Advocate
1,850 Views
4 Replies
Message 1 of 5

Create Viewport with Zoom to Coordinates

murmanator
Advocate
Advocate

Hello. Im trying to write a program that creates a viewport, then zooms the viewport to specific coordinates in model space. Im not concerned with the viewport scale. I want it to zoom to the coordinates no matter the size of the viewport that is drawn. I have everything working except for the zoom part. I did some research and tried a piece of code to attempt the zoom but it didnt work. This program is something I use for drawing a viewport, then setting the scale (you can see the scale line commented out). Id like to modify it to do this zoom thing. Thanks for any help.

(defun c:VPMAP (/ DS2 MYTILE P1 P2 USERLAYER USERVIEW VPS)
  (GV)
  (vl-load-com)
  (DEFUN *ERROR* (ERRMSG) (IF (/= ERRMSG "Function cancelled") (PRINC "\n Something failed...") (VL-CMDF "_.undo" "_e")))
  (SETVAR 'CLIPROMPTUPDATE 0)
  (SETVAR 'NOMUTT 1)
  					;===================Turn off command line responses==============
  (setvar 'CMDECHO 0)			;DO NOT CHANGE THIS LINE
					;=========================================================
  (COMMAND "_.undo" "_be")
  (setvar 'CLAYER "VP")
  (setq	userlayer (getvar 'clayer)
	ds2	  (getvar 'dimscale)
	userview  (getvar 'ctab)
  )
  (PRINC "\n\t CREATE VIEWPORT W/ZOOM TO MAP :: BEGIN \n")
  (vl-cmdf "mview" (setq p1 (getpoint "\n Select first corner of window opening: ")) (setq p2 (getcorner p1 "\n Select opposite corner of window opening: ")))
  (IF (AND (= 0 (GETVAR 'TILEMODE)) (= 1 (GETVAR 'CVPORT))) (VL-CMDF "._MSPACE"))
  (SETVAR 'CLIPROMPTUPDATE 1)
  (SETVAR 'NOMUTT 0)
  (PROGN
    (setq VPS (ssget "X" (list '(0 . "VIEWPORT") '(8 . "VP") (cons 410 userview)))) ; Get all viewports on "viewport" layer and current sheet layout
	
	(setq center (vlax-3d-point '(0.0 19.8 0.0))
		width 20.0
		height 20.0)

;    (vla-put-customscale (vlax-ename->vla-object (ssname VPS 0)) (/ 1 96.0)) ; set veiwport scale to 1/96
    (VL-CMDF PAUSE) ; pause for enter press
  )
  (VL-CMDF "._PSPACE")
  (setvar 'psltscale 0)
  (PRINC "\n DIMSCALE returning to original setting. \n")
  (setvar 'dimscale ds2)
					;===================Turn on command line responses==============
  (setvar 'CMDECHO 1)			;DO NOT CHANGE THIS LINE
					;======================================================
  (PRINC "\n Returning view and layer.....")
  (setvar 'ctab userview)
  (setvar 'clayer userlayer)
  (PRINC "\n View and layer returned! \n")
  (PRINC "\n\t CREATE VIEWPORT FOR MAP :: END \n")
  (VL-CMDF "_.undo" "_e")
  (SV)
  (PRINC)
  (PRINC)
)
0 Likes
Accepted solutions (1)
1,851 Views
4 Replies
Replies (4)
Message 2 of 5

CodeDing
Advisor
Advisor

@murmanator ,

 

I think this could be more simplistic? Let me know if this gets you on the right track:

(defun c:VPMAP ( / p1 p2 osm cmd vp)
(if (eq "Model" (getvar 'CTAB)) (progn (prompt "\nCannot run in Model Space...") (exit)))
(initget 1) (setq p1 (getpoint "\nSelect First Corner: ")) (princ p1)
(initget 1) (setq p2 (getcorner p1 "\nSelect Second Corner: ")) (princ p2)
(setq osm (getvar 'OSMODE) cmd (getvar 'CMDECHO))
(setvar 'OSMODE (logior 16384 osm)) (setvar 'CMDECHO 0)
(command "-VPORTS" p1 p2)
(setvar 'OSMODE osm) (setvar 'cmdecho cmd)
(setq vp (entlast))
(setpropertyvalue vp "ViewCenter/X" 0.0)
(setpropertyvalue vp "ViewCenter/Y" 19.8)
(setpropertyvalue vp "CustomScale" (/ 1.0 96.0))
(prompt "\nVPMAP Complete.")
(princ)
);defun

Best,

~DD

0 Likes
Message 3 of 5

murmanator
Advocate
Advocate

Thanks for the help! This is on the right track but not exactly what I was thinking. This is still setting the viewport scale to 1/8" which I dont need. I probably didnt provide enough info in my post. Below Ive copied the coordinates I would like the viewport to zoom to once drawn. Im trying to get the viewport to zoom to this "window" no matter what size you draw the viewport (I understand that the "window" is a square and if you draw a non-square viewport it could be off slightly).

 

at point X= -280'-8" Y= 39'-4" Z= 0'-0"
at point X= -239'-7" Y= 39'-4" Z= 0'-0"
at point X= -239'-7" Y= 0'-0" Z= 0'-0"
at point X= -280'-8" Y= 0'-0" Z= 0'-0"

0 Likes
Message 4 of 5

CodeDing
Advisor
Advisor
Accepted solution

@murmanator ,

 

Sure thing! We can use zoom > wondow to get that accomplished.

(defun c:VPMAP ( / p1 p2 osm cmd vp)
(if (eq "Model" (getvar 'CTAB)) (progn (prompt "\nCannot run in Model Space...") (exit)))
(initget 1) (setq p1 (getpoint "\nSelect First Corner: ")) (princ p1)
(initget 1) (setq p2 (getcorner p1 "\nSelect Second Corner: ")) (princ p2)
(setq osm (getvar 'OSMODE) cmd (getvar 'CMDECHO))
(setvar 'OSMODE (logior 16384 osm)) (setvar 'CMDECHO 0)
(command "-VPORTS" p1 p2)
(setq vp (entlast))
(command "_.MSPACE")
(setvar 'CVPORT (cdr (assoc 69 (entget vp))))
(command "_.ZOOM" "w" '(-3368.0 0.0 0.0) '(-2875.0 472.0 0.0))
(command "_.PSPACE")
(setvar 'OSMODE osm) (setvar 'cmdecho cmd)
(prompt "\nVPMAP Complete.")
(princ)
);defun

Best,

~DD

0 Likes
Message 5 of 5

murmanator
Advocate
Advocate

Yes! Thats perfect, thank you so much!!!

0 Likes