Jump to content

UCS Select object/poly segment and zoom to it


3dwannab

Recommended Posts

I have these two lisps below, this first one allows me to pick an object and zoom to it.

 

The trouble is that it doesn't behave like the second program below where it gets a segment of a polyline.

 

My question is is there a way to zoom to the object when using the code in the second option? Or make the first option behave like the second where it gets a segment?

 

First one:

(defun c:UCS_Pick_Object_BuiltInCmd (/ ent) 

  (setq ent (car (entsel "\nSelect object to zoom to after UCS (ESC to exit): ")))

  (if ent 
    (progn 
      (command "ucs" "_OB" ent "plan" "")
      (command "_.zoom" "_O" ent "")
      (princ "UCS now set to the line or the objects polyline segment")
      (princ)
    )
  )
)

 

Second one:

(defun c:UCS_Pick_Object_BuiltInCmd (/) 

  (command "ucs" "_OB" pause "plan" "")

  ; Is it possilbe to access the picked object from the pause command is to select the object?
  ; (command "_.zoom" "_O" ent "")

  (princ "UCS now set to the line or the objects polyline segment")
  (princ)
)

 

Link to comment
Share on other sites

What do you mean - "segment"... Your code is coded to zoom to object, not segment IMHO... When you say segment - what do you mean - line or arc of LWPOLYLINE... It's possible to preliminary explode "ent" - zoom to exploded segment and erase appended explosions of database... Is this what you're looking for? And what if you miss or pick different master entity than LWPOLYLINE? I think that method may work, but not 100% sure with other entity types...

Edited by marko_ribar
Link to comment
Share on other sites

Here is my sample... Untested though...

 

(defun c:zoo ( / e ss s vsz ) ;;; zoom to object of object

  (vl-load-com)

  (if (setq e (entsel "\nPick object segment you want to zoom to..."))
    (progn
      (setq ss (vlax-invoke (vlax-ename->vla-object (car e)) 'explode))
      (setq s (vlax-vla-object->ename (car (vl-sort ss '(lambda ( a b ) (< (distance (vlax-curve-getclosestpointto a (trans (cadr e) 1 0)) (trans (cadr e) 1 0)) (distance (vlax-curve-getclosestpointto b (trans (cadr e) 1 0)) (trans (cadr e) 1 0))))))))
      (setq vsz (getvar 'viewsize))
      (vl-cmdf "_.UCS" "_OB" s)
      (vl-cmdf "_.PLAN" "")
      (vl-cmdf "_.ZOOM" "_OB" s "")
      (vl-cmdf "_.ZOOM" "_C" "_non" (getvar 'viewctr) vsz)
      (mapcar 'vla-delete ss)
    )
  )
  (princ)
)

 

Edited by marko_ribar
  • Like 1
Link to comment
Share on other sites

If you use entsel then (cadr....) this gives you the point you selected, somrthing like this https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/list-of-vertices-of-a-polyline/td-p/775742 will list all the vertices in a polyline. 

 

So you could find the nearest point in the polyline to the mouse click, work out the distances to the 2 adjacent points and thirdly where your mouse clicked is closer to. Use something like (Distance... ) to do them.

 

This should give you 2 points on the polyline - and you can zoom 'window' to these

 

Most of this should be on the internet somewhere I reckon.

Link to comment
Share on other sites

Thanks, @marko_ribar and @Steven P. I like how the built-in UCS command handles a picked object. Depending on which side of a polyline or line segment you pick determines the orientation of the new UCS.

 

So I came up with the idea of retrieving the previous window coordinates before the UCS command was issued. With help from a fn from Lee Mac of course.

 

This gets it close enough to where I need to be after running the UCS command and not send me into outer space after running the plan command. 😆

 

GIF and code:

new.thumb.gif.6082f925225851fdf820b0df4582cb73.gif

 

 

;;
;; Restores the zoomed in window after running the command "UCS" "_OB" followed by the plan command
;; Written on 2023.07.18 by 3dwannab
;; LM:ViewportExtents by Lee Mac (THANKS AGAIN)
;;
(defun c:I_LIKE_TO_ZOOM_IT_ZOOM_IT (/ *error* acDoc LM:ViewportExtents pt1 pt2 vp_pts) 

  (vl-load-com)

  ;; Viewport Extents  -  Lee Mac
  ;; Returns two WCS points describing the lower-left and
  ;; upper-right corners of the active viewport.

  (defun LM:ViewportExtents (/ c h v) 
    (setq c (trans (getvar 'viewctr) 1 0)
          h (/ (getvar 'viewsize) 2.0)
          v (list (* h (apply '/ (getvar 'screensize))) h)
    )
    (list (mapcar '- c v) (mapcar '+ c v))
  )

  (defun *error* (errmsg) 
    (and acDoc (vla-EndUndoMark acDoc))
    (and errmsg 
         (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
         (princ (strcat "\n<< Error: " errmsg " >>\n"))
    )
  )

  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

  (setq vp_pts (LM:ViewportExtents))

  (command "ucs" "_OB" pause "plan" "")

  (setq pt1 (vlax-3d-point (car vp_pts)))
  (setq pt2 (vlax-3d-point (cadr vp_pts)))

  ; Restores the portion of the screen you were originally in
  (vla-ZoomWindow acadObj pt1 pt2)

  (vla-EndUndoMark acDoc)

  (*error* nil)
  (princ)
)

(c:I_LIKE_TO_ZOOM_IT_ZOOM_IT)

 

Edited by 3dwannab
  • Like 1
  • Funny 1
Link to comment
Share on other sites

11 hours ago, BIGAL said:

Why not use Zoom C Point Scale. You tend to know what scale to use.

 

It's good but not sure if this would be how you would do it. 

 

;;
;; Restores the zoomed in window after running the command "UCS" "_OB" followed by the "PLAN" command
;; Written on 2023.07.18 by 3dwannab
;; Revision on 2023.07.19 by 3dwannab based on BigAl comments here: https://www.cadtutor.net/forum/topic/77919-ucs-select-objectpoly-segment-and-zoom-to-it/?do=findComment&comment=621006
;;
(defun c:UCS_Pick_Object_BuiltInCmd (/ *error* acDoc var_cmdecho vct vsizer) 

  (vl-load-com)

  (defun *error* (errmsg) 
    (and acDoc (vla-EndUndoMark acDoc))
    (and errmsg 
         (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
         (princ (strcat "\n<< Error: " errmsg " >>\n"))
    )
    (setvar 'cmdecho var_cmdecho)
  )

  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

  (setq var_cmdecho (getvar 'cmdecho))
  ; (setq vctr (trans (getvar 'viewctr) 1 0))
  (setq vctr (getvar 'viewctr))
  (setq vsize (getvar 'viewsize))

  (setvar 'cmdecho 0)

  (progn 
    (command "ucs" "_OB" pause "plan" "")
    (command "_.zoom" "_C" vctr vsize)
    (princ "UCS now set to the line, objects polyline segment or objects UCS")
  )

  (vla-EndUndoMark acDoc)

  (*error* nil)
  (princ)
)

 

 

Link to comment
Share on other sites

  • 3 weeks later...

And with your code modified simply...

(defun c:UCS_Pick_Object_BuiltInCmd (/ *error* acDoc var_cmdecho vsize) 

  (vl-load-com)
(defun *error* (errmsg) 
    (and acDoc (vla-EndUndoMark acDoc))
    (and errmsg 
         (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
         (princ (strcat "\n<< Error: " errmsg " >>\n"))
    )
    (setvar 'cmdecho var_cmdecho)
  )

  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

  (setq var_cmdecho (getvar 'cmdecho))
  (setq vsize (getvar 'viewsize))

  (setvar 'cmdecho 0)

  (progn 
    (command "_.ucs" "_Object" pause "_plan" "")
    (command "_.zoom" "_Ce" (trans (getvar 'ucsorg) 0 1) vsize)
    (princ "UCS now set to the line, objects polyline segment or objects UCS")
  )

  (vla-EndUndoMark acDoc)

  (*error* nil)
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

 

I was literally doing a similar thing for my script a few weeks ago. 

 

I did it this way: 

;********************************************************;
; Function: zoom_susp
; Arguments:
;   susp (entity): Suspension entity
; Returns:
;   vpinfo (list): List of viewport center and custom scale
; Description:
;   This function performs a zoom operation in AutoCAD to zoom to the given suspension entity.
;   It retrieves the center point of the suspension entity using the (cdr (assoc 10 (entget))) expression.
;   The function then retrieves the active viewport entity and VLA object using cvport->ent function.
;   It temporarily unlocks the display, performs the zoom operation using the "_.zoom" command,
;   and locks the display again. Finally, it returns a list containing the viewport center and custom scale.
; Usage: (zoom_susp susp)
(defun zoom_susp (susp vpinfo / susp-cent ent obj vpcenter vpscale) 
  (setq susp-cent (cdr (assoc 10 (entget susp))))
  (setq ent (cvport->ent))
  (setq obj (vlax-ename->vla-object ent))
  (command "_.mspace")
  ;(vlax-put-property obj 'DisplayLocked :vlax-false)
  ;(vla-put-lock obj :vlax-false)
  (vla-put-displaylocked obj 0)
  ;(command Regen)
  (setq vpcenter (car vpinfo))
  (setq vpscale (cadr vpinfo))
  (command "_.zoom" "C" susp-cent (strcat (rtos 0.1 2 6) "XP"))
  ;(vlax-put-property obj 'DisplayLocked :vlax-true)
  (vla-put-displaylocked obj -1)
  ;(vla-put-lock obj :vlax-true)
  ;(command "_.pspace")
  ;(list vpcenter vpscale)
)

 

With a return zoom function like this:

;********************************************************;
; Function: zoom_return
; Arguments:
;   input (list): A list containing viewport center and scale values
; Returns: None
; Description:
;   This function performs a zoom operation in AutoCAD based on the given viewport center and scale values.
;   It retrieves the viewport entity and VLA object, unlocks the display, performs the zoom operation,
;   locks the display again, and switches back to paper space.
; Usage: (zoom_return input)
(defun zoom_return (input / vpcenter vpscale ent obj) 
  (setq vpcenter (car input))
  (setq vpscale (cadr input))
  (setq ent (cvport->ent))
  (setq obj (vlax-ename->vla-object ent))
  (command "mspace")
  (vlax-put-property obj 'DisplayLocked :vlax-false)
  (command "_.zoom" "C" vpcenter (strcat (rtos vpscale) "XP"))
  (vlax-put-property obj 'DisplayLocked :vlax-true)
  ;(command "pspace")
  (princ)
)

 

And the VPINFO function that looks like this:

;********************************************************;
;; Function: vp-info
;; Returns:
;;    vpinfo (list): List of viewport center and custom scale
;; Description:
;;    This function retrieves the viewport center and custom scale of the active viewport.
;;    It uses the cvport->ent function to retrieve the viewport entity and VLA object.
;;    The function then gets the viewport center using the "viewctr" system variable,
;;    and gets the custom scale using the vla-get-customscale method of the viewport object.
;;    Finally, it returns a list containing the viewport center and custom scale.
;; Usage: (vp-info)
(defun vp-info (/ ent obj vpinfo) 
  (if (setq ent (cvport->ent)) 
    (progn 
      (setq obj (vlax-ename->vla-object ent))
      ; (setq vpcenter (getvar "viewctr"))
      ; (setq vpscale (vla-get-customscale obj))
      (setq vpinfo (list (getvar "viewctr") 
                         (vla-get-customscale obj)
                   )
      )
      vpinfo
    )
  )
)

 

 

The command line (command "_.zoom" "C" susp-cent (strcat (rtos 0.1 2 6) "XP")), with the value 0.1 to be modified as intended for each type of block that you want to zoom into.

 

Works really well in tandem with a search function into a drawing and zooming into them to see the details. 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...