Nate Holt's Blog

October 15, 2009

‘ZOOM to HANDLE’ utility

Filed under: Tips — nateholt @ 6:26 pm

Quickly create a tool to find and zoom to a given entity using its handle number.

You know an entity/object’s “handle” number, but where in the world is it in your jumbled drawing?

Complex library creation and testing has been on my plate for a couple weeks. The new code spits out the AutoCAD handle of various entities as processing proceeds. Sometimes there’s a need to go back in and find a problem entity on the drawing. Have the handle reference but that’s it. What is it, where is it?

Manual Method – Crude with  no-frills

Here’s the crude, no-frills way. Open the entity and display its guts in the command window.

Let’s say that you’ve run your program in a “debugging” mode and find that there is some issue with object handle “74F” in the active drawing. What and where is this entity?

Type this AutoLISP expression at the “Command:” prompt

Command: (entget (handent “74F”)) [Enter]

It returns this block of data about this entity.

((-1 . <Entity name: 7ef152f8>) (0 . “LINE”) (330 . <Entity name: 7ef11c10>) (5 . “74F”) (100 . “AcDbEntity”) (67 . 0) (410 . “Model”) (8 . “0”) (100 .
“AcDbLine”) (10 6.09375 13.5 0.0) (11 6.12676 13.5 0.0) (210 0.0 0.0 1.0))

The “0” entry flags the entity type: “LINE” and the “10” and “11” parts give the XYZ start and end points of the LINE segment.

Crude, but it does work.

Better Method: auto-ZOOM to the entity

Here’s a little AutoLISP utility thrown together to help in troubleshooting. It zooms in on the target object given the typed-in handle.

The syntax is pretty simple. After the utility is APPLOADed, type ZH at the AutoCAD command prompt and then enter the handle number at the next prompt…

Command: zh [Enter]
Handle  =74F [Enter]

The utility opens up the entity. If it is a simple graphical entity like an arc, text, block insert, line, then it opens it to find “where” it is.

Entity type: LINE XY=(6.11026 13.5 0.0)

… and then it issues a ZOOM “Centered” command to display the object in the middle of the screen. It then tries to highlight it. Now you can zoom in or out with “I” or “O” keystrokes or use your mouse “wheel” to take a better look in context.

Here’s the handle “74F” example… it was an incomplete wire connection on the high side of a control transformer.

zh001

Here’s the utility itself. Cut and paste into an ASCII text file, let’s call it “zh.lsp”.

; ** 13-Oct-09 NEHolt - http://n8consultants.com
(defun c:zh ( / hdl xy enttype ed en xy2)
  ; ZOOM to entity "handle". Does not check model vs layout space.
  (while (/= (setq hdl (getstring (strcat
              "\nHandle " (if en "(or I=Zoom IN, O=Zoom out)" "") " ="))) "")
    (cond
      ((= (strcase hdl) "I")(command "_.ZOOM" "1.5x"))
      ((= (strcase hdl) "O")(command "_.ZOOM" "0.75x"))
      (T
        (if (setq en (handent hdl)) ; convert handle string to its "entity name"
          (progn ; appears to be a valid handle number
            (if (setq ed (entget en)) ; open up the entity
              (progn ; success, okay to continue
                (setq xy nil)
                (setq enttype (cdr (assoc 0 ed))) ; extract the entity type
                (princ "\nEntity type: ")(princ enttype)
                (if (= enttype "INSERT")
                  (progn ; this is a block INSERT instance
                    (princ " blk nam=")
                    (princ (cdr (assoc 2 ed))) ; display block name
                ) )
                ; Figure where entity is located
                (if (cdr (assoc 10 ed))
                  (progn
                    (setq xy (cdr (assoc 10 ed))) ; 10 subrec often gives a location value
                    (if (= enttype "LINE")
                      (progn ; LINE entity. Reset to zoom to line's midpoint
                        (setq xy2 (cdr (assoc 11 ed))) ; XY of other end
                        ; Calc midpoint coordinate
                        (setq xy (list (+ (car xy) (* 0.5 (- (car xy2) (car xy))))
                                       (+ (cadr xy) (* 0.5 (- (cadr xy2) (cadr xy))))
                                       (+ (caddr xy) (* 0.5 (- (caddr xy2) (caddr xy))))))
                ) ) ) )
                (if xy
                  (progn ; Non-blank XY extracted from picked entity. Go ahead and
                           ; display the value and ZOOM in to that point on the drawing.
                    (princ " XY=")(princ xy)
                    (command "_.ZOOM" "_C" xy 1.0) ; use ZOOM CENTER command
                    (redraw en 3) ; trigger entity to "highlight"
                ) )
        ) ) ) )
      )
    )
  )
  (command "_.REGEN") ; brute force - unhighlight everything
  (princ) ; quiet return
)

That should be it. APPLOAD this file and that should expose the utility to use on your active drawing.

UPDATE: typo introduced when the above program was cut/paste into this posting. Very sorry. Should be okay now. – Nate.

About these ads

7 Comments »

  1. Hi Nate,

    There are some “mistakes” in this lisproutine.
    For example
    (setq xy (list (+ (car xy) (* 0.5 (- (car xy2) (car xy))))
    (+ (cadr cadr “>xy) (* 0.5 (- (cadr xy2) (cadr xy))))
    (+ (caddr caddr “>xy) (* 0.5 (- (caddr xy2) (caddr xy))))))

    Causes a error in Visual lisp, it’s “>XY which is not recognized correctly.

    Regards,

    Gerald

    Comment by Gerald — December 11, 2009 @ 8:09 am

    • You are exactly right. There were several typos in the source file. I think something bad happened when I cut/pasted the source code into the blog posting. Very sorry. It should be okay now.

      Comment by nateholt — December 11, 2009 @ 10:14 am

  2. Works great for standard AutoCAD elements (lines, arcs, solids, etc).
    It won’t work for AutoCAD MEP MvParts.
    At the prompt: “Handle = “, I entered the handle for the part and hit
    Next Prompt: “Entity type: AECB_MVPART”
    Next Prompt: “Handle (or I-Zoom IN, O=Zoom out) = ”
    This last prompt returns over and over, zooming in and/or out, based on input, centered on the current screen view, and will not zoom to the entity.

    Comment by Dave Billion — April 30, 2012 @ 11:54 am

  3. David,

    Have you found a workaround for AutoCAD MEP entities such as pipes, fittings and MvParts? Regards,

    Emmanuel

    Comment by caddguru — August 5, 2012 @ 11:52 pm

    • I found a lsp routine out there (can’t remember the source – I cannot and will not claim credit):

      (defun C:ENTFIND (/ handle ent)
      (setq handle (getstring “\nEnter handle to search for: “))
      (setq ent (handent handle))
      (if ent
      (progn ;line added
      (sssetfirst nil (ssadd ent))
      (command “_zoom” “_Object” ent)
      )
      ;Else
      (ALERT
      “The handle you entered does not exist in this drawing!”
      )
      )
      (princ)
      )
      ;;
      (defun OwnerName (handle)
      (cdr
      (assoc 2
      (entget (cdr (assoc 330 (entget (handent handle)))))
      )
      )
      )

      You will be prompted to enter the handle of the object to locate. After entering the handle and hitting , hit a second time to exit the selection process.

      Comment by Dave Billion — August 7, 2012 @ 5:48 am

  4. Thank you Nate! that was excellent!

    Comment by tonymynd — May 22, 2013 @ 6:31 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: