TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: iliekater on December 17, 2007, 01:02:27 AM

Title: Zoom out to fit an object into screen
Post by: iliekater on December 17, 2007, 01:02:27 AM
Is it possiple to zoom out in order to make a specific object fit whole into the screen ?
Title: Re: Zoom out to fit an object into screen
Post by: Kerry on December 17, 2007, 01:55:28 AM
Yes, look at the object bounding-box which sets variables for the lower left and upper-right .. then zoom window using those 2 points.

this may help ;
Code: [Select]
(defun ObjectMidPoint (activeXObject / p1 p2)
   (vla-getboundingbox activeXObject 'p1 'p2)
   (setq p1 (vlax-safearray->list p1)
         p2 (vlax-safearray->list p2)
   )
   (mapcar '* '(0.5 0.5 0.5)(mapcar '+ p1 p2))
)

P1 and P2 are the box corners for your Zoom Command ..


Title: Re: Zoom out to fit an object into screen
Post by: gile on December 17, 2007, 02:29:32 AM
Hi,

If your ACAD version allows it, you can use the the option "object" of ZOOM command: (vl-cmdf "_.zoom" "_o" (ssget) "")

If it don't or if you'd rather not using command, I wrote a ZoomObject routine (cause vla-ZoomObject isn't provided)

Code: [Select]
;; ZoomObject
;; Zoom on objects countained in a list
;;
;; Argument
;; objlst : a vla-objects list
;;
;; Variables
;; dir : normal vector of the current view plane
;; ang : Twist angle of the current view
;; 3x3 : 3x3 transformation matrix fron WCS to current view
;; 4x4 : 4x4 transformation matrix fron WCS to current view
;; ptlst : list of all min and max points of objects bounding boxes

(defun ZoomObject (objlst / dir ang 3x3 4x4 ptlst)
  (vl-load-com)
  (setq dir (trans '(0 0 1) 2 0 T)
ang (- (getvar "viewtwist"))
3x3 (mxm (mapcar '(lambda (x) (trans x 0 dir))
'((1 0 0) (0 1 0) (0 0 1))
)
(list (list (cos ang) (- (sin ang)) 0)
       (list (sin ang) (cos ang) 0)
       '(0 0 1)
)
    )
4x4 (append
      (mapcar
'(lambda (v o)
   (append v (list o))
)
3x3
'(0 0 0)
      )
      (list '(0 0 0 1))
    )
  )
  (foreach obj objlst
    (vla-TransformBy obj (vlax-tmatrix (trp 4x4)))
    (vla-getBoundingBox obj 'minpt 'maxpt)
    (vla-TransformBy obj (vlax-tmatrix 4x4))
    (setq ptlst (cons (vlax-safearray->list minpt)
      (cons (vlax-safearray->list maxpt) ptlst)
)
    )
  )
  (vla-ZoomWindow
    (vlax-get-acad-object)
    (vlax-3d-point (mxv 3x3 (apply 'mapcar (cons 'min ptlst))))
    (vlax-3d-point (mxv 3x3 (apply 'mapcar (cons 'max ptlst))))
  )
)

;; transpose a matrix by Doug Wilson

(defun trp (m)
  (apply 'mapcar (cons 'list m))
)

;; Apply a transformation matrix to a vector by Vladimir Nesterovsky

(defun mxv (m v)
  (mapcar '(lambda (r) (apply '+ (mapcar '* r v))) m)
)

;; Multiply two matrices by Vladimir Nesterovsky

(defun mxm (m q)
  (mapcar '(lambda (r) (mxv (trp q) r)) m)
)

A test/calling function:

Code: [Select]
(defun c:zo (/ lst)
  (vl-load-com)
  (or *acdoc*
      (setq *acdoc* (vla-get-ActiveDocument (vlax-get-acad-object)))
  )
  (if
    (ssget)
     (progn
       (vla-StartUndoMark *acdoc*)
       (ZoomObject
(vlax-for o (vla-get-ActiveSelectionSet *acdoc*)
   (setq lst (cons o lst))
)
       )
       (vla-EndUndoMark *acdoc*)
     )
  )
  (princ)
)
Title: Re: Zoom out to fit an object into screen
Post by: GDF on December 17, 2007, 10:45:49 AM
Gile, nice routine.

Gary
Title: Re: Zoom out to fit an object into screen
Post by: gile on December 17, 2007, 11:58:17 AM
Thanks, Gary.
Title: Re: Zoom out to fit an object into screen
Post by: Lee Mac on September 21, 2009, 05:26:42 AM
Very nice Gile -

While I can follow the matrix calculations, I wouldn't have been able to come up with it myself...  :lol:
Title: Re: Zoom out to fit an object into screen
Post by: hermanm on September 21, 2009, 10:10:43 AM
Nice job, Gile. :)

I use this for one entity:

Code: [Select]
;;zoom to an object
;;accept ename or vla-object
(defun zoom-to (obj / lowleft upright)
  (if (eq (type obj) 'ENAME) (setq obj (vlax-ename->vla-object obj)))
  (vla-getboundingbox obj 'lowleft 'upright)
  (vlax-invoke-method (vlax-get-acad-object) 'zoomwindow lowleft upright)
)
Title: Re: Zoom out to fit an object into screen
Post by: xiaxiang on January 15, 2013, 02:34:59 AM
Hi,all.
Gile,Nice stuff.
My question is that who know the "zoom" method when using DBX?
As you know,command "zoom" is not permitted,and either vla-ZoomWindow or
vla-ZoomExtents is not permitted.
irneb,if you can see this topic...
Is there any way to change the properties of viewport to comply "zoom" function?
as follow topic
zoom extend the all opened drawings cadtutor
http://www.cadtutor.net/forum/showthread.php?55925-zoom-extend-the-all-opened-drawings
Maybe it's Impossible task,but I research for several days.
Many thanks!