Find Object in Viewport LISP

Find Object in Viewport LISP

jr_olmos23
Enthusiast Enthusiast
2,076 Views
18 Replies
Message 1 of 19

Find Object in Viewport LISP

jr_olmos23
Enthusiast
Enthusiast

So I used ChatGPT to write me a LISP code. I want to be able to select any object in my model space and have the LISP give the name of the paper space in which it is being viewed. For example, if I have an object (line, block, hatch, etc.), and that object is being viewed in a viewport--even when 3 or 4 viewports are in a paper space--and the paper space name is S1, I want the LISP to give me "S1". Or even if the object is being viewed in multiple paper spaces at the same time, for example, I would like it to give me the paper space names (e.g. S1, SH2, M3.5, etc...).

 

Here are some errors GPT gave me, but it seems it's having trouble fixing them:

 

 

  • Viewport Clipping: If the viewport is clipped or if its scale affects the visible area, the bounding box of the viewport may not correspond to what you see in the drawing.
  • Coordinate Space Mismatch: The bounding box of the object is in model space, while the viewport may need to be considered in paper space. We need to make sure that the viewport coordinates are being interpreted correctly.

 

 

I attached the LISP, but I always get the "No viewport contains the selected object." despite selecting an object that is clearly in a viewport. 

0 Likes
Accepted solutions (1)
2,077 Views
18 Replies
Replies (18)
Message 2 of 19

Sea-Haven
Mentor
Mentor

This may be the answer, The vpmin and max are paperspace values so they have no relevance to the world co-ordinates in Model, a test 

: !vpmin (100.0 362.739634646627 0.0) but in world same point is X=268699.536 Y=5781245.198 Z=0

 

So you need to use the "trans" function to make the VPmin & Vpmax  world co-ordinates. Just having problems making the trans work properly. Will come back to it.

 

 

0 Likes
Message 3 of 19

hosneyalaa
Advisor
Advisor

Can you support it with an example drawing?

0 Likes
Message 4 of 19

komondormrex
Mentor
Mentor

give the name of the paper space in which it is being viewed

if the object is viewed partially?

0 Likes
Message 5 of 19

jr_olmos23
Enthusiast
Enthusiast

Here's one attached. The LISP should return the "Layout1, Layout 2" names because that's where the line is viewed.

0 Likes
Message 6 of 19

jr_olmos23
Enthusiast
Enthusiast

Didn't think of that. I only had in mind if the object is within the viewport(s). If it shows partially I guess it can be ignored.

0 Likes
Message 7 of 19

jr_olmos23
Enthusiast
Enthusiast

Thanks for looking into it. I also tried this other method (attached), but am encountering some errors like this one: 

 

Select the object to find: ; error: bad argument type: numberp: (3581.04 -9317.62 0.0)

0 Likes
Message 8 of 19

pbejse
Mentor
Mentor

Pseudo Code

  • Select object(s) on Model Space 
  • Make a list of entity names out of the selected objects 
  • Select all paperspace view ports  
  • Retrieve coordinates of outline and translate to Model 
  • Use the points to select object
  • Test for ssmemb to check if the object(s) is within the outline

 

0 Likes
Message 9 of 19

ec-cad
Collaborator
Collaborator

Error caused by doing a (rtos xmin 2 2) on a LIST.

 

Here's ths issue:
(setq objBBox (list
(cdr (assoc 10 objData)) ; Minimum point X
(cdr (assoc 20 objData)) ; Minimum point Y
(cdr (assoc 11 objData)) ; Maximum point X
(cdr (assoc 21 objData)) ; Maximum point Y
))
Sets the 'bounding' box coordinates into a list of lists:
e.g.
Command: !objBBox
((23494.7 9577.99 0.0) nil (38739.3 9577.99 0.0) nil)

Then this section:
;; Unpack the bounding box coordinates
(setq xmin (car objBBox)
ymin (cadr objBBox)
xmax (caddr objBBox)
ymax (cadddr objBBox))
Sets xmin to the first 'List of objBBox
e.g.
Command: !xmin
(23494.7 9577.99 0.0)

Then, this section:
;; Debug: Print the bounding box of the selected object
(princ (strcat "\nSelected object bounding box: "
(rtos xmin 2 2) ", "
(rtos ymin 2 2) ", "
(rtos xmax 2 2) ", "
(rtos ymax 2 2)))
Tries to do a (rtos of a LIST. Not going to work.

xmin, ymin, xmax, ymax need to be 'reals, not Lists.

 

ECCAD

0 Likes
Message 10 of 19

Sea-Haven
Mentor
Mentor

@pbejse Thought about going down that route, can get viewport outline make a rectang and then use Chspace as this handles twists as well, in model get entlast and use that with ssget "CP. As you suggest does entity name exist. Just need time to play.

0 Likes
Message 11 of 19

Sea-Haven
Mentor
Mentor
Accepted solution

Thanks to @pbejse idea try this.

 

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/find-object-in-viewport-lisp/m-p/13060539/highlight/false#M472398


(defun c:wow ( / )
(setvar 'ctab "Model")
(setq objEnt (car (entsel "\nSelect the object to find: "))) ; Ensure user selects the object
(setq objhand (cdr (assoc 5 (entget objent))))
(setq lays (layoutlist))
(foreach lay lays
(setvar 'ctab lay)
(command "pspace")
(setq vp (ssget "X" (list (cons 0 "Viewport")(cons 410 (getvar 'ctab)))))
(repeat (setq x (- (sslength vp) 1))
 (setq obj (vlax-ename->vla-object (ssname vp (setq x (- x 1)))))
 (vla-GetBoundingBox obj 'minpoint 'maxpoint)
 (setq pointmin (vlax-safearray->list minpoint))
 (setq pointmax (vlax-safearray->list maxpoint))
 (command "rectang" pointmin pointmax)
 (command "chspace" (entlast) "" "")
 (setvar 'ctab "Model")
 (setq ent (entlast))
 (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget ent))))
 (command "erase" ent "")
 (setq ss (ssget "CP" co-ord))
 (if (= ss nil)
  (alert "No objects found")
  (repeat (setq k (sslength ss))
   (setq enthand (cdr (assoc 5 (entget (ssname ss (setq k (1- k)))))))
   (if (= enthand objhand)
    (alert (strcat "Found " lay " entity found"))
   )
  )
 )
)
)
(princ)
)
(c:wow)

 

 

0 Likes
Message 12 of 19

jr_olmos23
Enthusiast
Enthusiast

Okay this one did work for one block, but then when selecting other objects I just get this message:

 

Select objects: Set the TARGET viewport active and press ENTER to continue.: Restoring cached viewports.
Regenerating 2 modified entities.
erase
Invalid point.
; error: Function cancelled

 

Also, with other objects it doesn't give me the layout name and it returns the same error message.

 

Edit: I think it might have to do with having multiple viewports in one layout.

0 Likes
Message 13 of 19

komondormrex
Mentor
Mentor

just idea.  draw  with a lisp the borders of all viewports of all layouts in the model space and mark these with the corresponding layout's name to instantly see where the object of interest is being viewed.

0 Likes
Message 14 of 19

Sea-Haven
Mentor
Mentor

I have updated code above please try again.

0 Likes
Message 15 of 19

jr_olmos23
Enthusiast
Enthusiast

It glitches out; it just adds random rectangles in model space.

 

I was able to add a bit of code to it after 20 tries (see attached wow20), but I think it hallucinates as to where the selected object is. For example, if I select a line (see attached TEMP), it says that it was found in such and such layout despite it not being viewed in that viewport. And for example, when I select a block (see attached TEMP), it basically says it is not found in any viewport despite it being present in a few. 

0 Likes
Message 16 of 19

Sea-Haven
Mentor
Mentor

Was working for me on random dwg's. Will have a look later at your code and dwg.

0 Likes
Message 17 of 19

komondormrex
Mentor
Mentor

as a matter of interest continuation to the afore idea

komondormrex_0-1728369563469.gif

 

0 Likes
Message 18 of 19

jr_olmos23
Enthusiast
Enthusiast

How would I write that in LISP code though? I did end up modifying the WOW command by @Sea-Haven and got it to work just fine for now (see attached wow22.lsp). But your option seems easier to deal with since some of our drawings have like 100 layouts so it bugs down my computer iterating through each layout space, unless there's a way to make them iterate in the background similar to how your lisp works.

0 Likes
Message 19 of 19

jr_olmos23
Enthusiast
Enthusiast

Here, I was sort of able to figure something out and it works just enough for what I want it. If you have any comments or improvements that could be made (cause I'm sure there's many) that would be great to know!

 

I'm pretty much dropping the code on GPT and telling it the ideas you guys mentioned here in the forum and I guess it figured something out. I attached the LISP.

0 Likes