Get Elevation through Viewport

Get Elevation through Viewport

Jonathan3891
Advisor Advisor
999 Views
10 Replies
Message 1 of 11

Get Elevation through Viewport

Jonathan3891
Advisor
Advisor

Is there a way to get the elevation of a point through a viewport without to activate the viewport and id a point?

 

Basically what I'm trying to achieve is get the elevation from paper space without ever having to enter the viewport.
I think I'll have to select the viewport then pick the point but I cant seem to get anything to work.

 

I'm not looking for someone to write the whole lisp, just get me on the right track.

 


Jonathan Norton
Blog | Linkedin
0 Likes
1,000 Views
10 Replies
Replies (10)
Message 2 of 11

CodeDing
Advisor
Advisor

@Jonathan3891 ,

 

I'm only on mobile right now, but in theory I think you could accomplish this with the following workflow:

- User select point

- check all viewports on your current paperspace tab to check if point was inside one of them.

- if so, calculate point position on viewport.

- get model space coordinate that viewport is centered on.

- translate user point to model space point.

- Check if any points exist at user selected location.

 

Best,

~DD

0 Likes
Message 3 of 11

pbejse
Mentor
Mentor

@Jonathan3891 wrote:

Is there a way to get the elevation of a point through a viewport without to activate the viewport and id a point?


 

Is that a POINT entity?  and are there multiple POINt objects on a viewport?  multiple viewports on layout tabs? multiple layout tab?

 

 

 

 

0 Likes
Message 4 of 11

Jonathan3891
Advisor
Advisor

I didn't mean a point entity. I was planning on using GETPOINT.

 

Yes, the layout will have multiple viewports and multiple layouts.


Jonathan Norton
Blog | Linkedin
0 Likes
Message 5 of 11

CodeDing
Advisor
Advisor

@Jonathan3891 ,

 

Will this always be just a basic rectangular viewport? I don't think I can help if it's a polygonal VP or VP from object.

 

Best,

~DD

0 Likes
Message 6 of 11

Jonathan3891
Advisor
Advisor

@CodeDing 

 

Yes, only using basic rectangular viewports.


Jonathan Norton
Blog | Linkedin
0 Likes
Message 7 of 11

CodeDing
Advisor
Advisor

@Jonathan3891 ,

 

I believe this will do you well. Hope it helps.

 

(defun c:ZVP ( / tmp ssVP vpList e ptPS ptMS ssPTS elev)
;elevation(Z) of point in ViewPort
;helper function(s)
  (defun ZVP_GetBBox (vp / x y w h)
    ;vp - ename, of rectangular VP
    (setq vp (entget vp)
	  x (cadr (assoc 10 vp)) y (caddr (assoc 10 vp))
	  w (* 0.5 (cdr (assoc 40 vp))) h (* 0.5 (cdr (assoc 41 vp))))
    (list (list (- x w) (- y h)) (list (+ x w) (+ y h)))
  );defun
  (defun ZVP_InsideVP (pt bbox)
    ;pt - point / bbox - list, of 2 points.. (LL UR)
    (if (and (> (car pt) (caar bbox)) (> (cadr pt) (cadar bbox))
	     (< (car pt) (caadr bbox)) (< (cadr pt) (cadadr bbox)))
      t nil
    );if
  );defun
  (defun ZVP_PTfromPStoMS (pt vp / egVP ptCtr xDvp ptCtrMSx ptCtrMSy scale)
    ;pt - point / vp - ename, viewport
    (setq egVP (entget vp '("ACAD"))
	  ptCtr (cdr (assoc 10 egVP))
	  xDvp (cdadr (assoc -3 egVP))
	  ptCtrMSx (cdaddr (member (assoc 1040 xDvp) xDvp))
	  ptCtrMSy (cdr (cadddr (member (assoc 1040 xDvp) xDvp)))
	  scale (/ 1.0 (getpropertyvalue vp "CustomScale")))
    (polar (list ptCtrMSx ptCtrMSy) (angle ptCtr pt) (* scale (distance ptCtr pt)))
  );defun
;initial check(s)
(cond
  ((eq "Model" (getvar 'CTAB)) (setq tmp "\nCommand cannot be used in Model Space.") (prompt tmp) (alert tmp) (exit))
  ((/= 1 (getvar 'CVPORT)) (setq tmp "\nMust be in Paper Space.") (prompt tmp) (alert tmp) (exit))
  ((not (setq ssVP (ssget "_X" (list '(0 . "VIEWPORT") (cons 410 (getvar 'CTAB)) '(-4 . "<NOT") '(69 . 1) '(-4 . "NOT>")))))
    (setq tmp "\nNo Viewports found in current layout.") (prompt tmp) (alert tmp) (exit))
);cond
;get VPs and bounding boxes
(setq e nil vpList (mapcar 'cadr (ssnamex ssVP)))
(setq vpList (mapcar '(lambda (x) (list x (ZVP_GetBBox x))) vpList))
;get user selected point
(initget 1) (setq ptPS (getpoint "\nSelect Point through Viewport: "))
;check if inside a VP
(foreach x vpList (if (ZVP_InsideVP ptPS (cadr x)) (setq e (car x))))
(if (not e)
  (progn (setq tmp "\nPoint was not located inside of a Viewport." ssVP (prompt tmp)) (alert tmp) (exit))
);if
;translate to MS point
(setq ptMS (ZVP_PTfromPStoMS ptPS e))
;check if acad point(s) in MS at location
(if (setq ssPTS (ssget "_X" (list '(0 . "POINT") '(410 . "Model") '(-4 . "=,=,*") (cons 10 ptMS))))
  (progn
    (setq elev (cadddr (assoc 10 (entget (ssname ssPTS 0)))))
    (setq tmp (strcat "\nPoint elevation: " (rtos elev 2 2)))
    (prompt tmp) (alert tmp)
    (prompt "\nZVP Complete.")
  );progn
;else
  (progn (setq tmp "\nNo Point entity was found at selected location in Model Space.") (prompt tmp) (alert tmp))
);if
;finish up (setq ssVP nil ssPTS nil) (princ) );defun

Best,

~DD

0 Likes
Message 8 of 11

Jonathan3891
Advisor
Advisor

@CodeDing 

 

Thank you for your help, that's much more involved that I was thinking!

 

I've tried your code on a point entity and I keep getting this alert: 

 

No Point entity was found at selected location in Model Space

I apologize , but I'm afraid that you missed my reply to @pbejse saying that it's not a point entity, instead it was getpoint. I'm using Cadworx and I want to ID top of steel or top of concrete elevations. I understand if you don't have the patience to go any further.


Jonathan Norton
Blog | Linkedin
0 Likes
Message 9 of 11

CodeDing
Advisor
Advisor

@Jonathan3891 ,

 

Oh wow, yes that is a bit more complicated. Can you post an example dwg? 

What kinds of entities will be selected through the VP?

 

Best,

~DD

0 Likes
Message 10 of 11

Jonathan3891
Advisor
Advisor

@CodeDing 

 

The foundations are basic 3d solids.

As for the steel it is also a line and 3d solid grouped together 

 

I'm not wanting to select any entity, instead use getpoint if possible.

 

I've attached a model.

 

 


Jonathan Norton
Blog | Linkedin
Message 11 of 11

CodeDing
Advisor
Advisor

@Jonathan3891 ,

 

Thanks for the dwg. So that would be QUITE a bit above my current skill level. It would require calculating vectors and intersections at planes (maybe irregularly shaped sometimes?) of solids in model space. I can grasp the concept, but the coding aspect of it is out of my reach..

 

BUT if you're willing to sacrifice hours of coding for 1 extra click, then give this one a go.. It activates the viewport (like you didn't want) but why reinvent what AutoCAD has already given us with our awesome (getpoint) ability?

It will also work if you are in model space or already in a viewport.

 

(defun c:GZ ( / *error* tm vp cvp startedInVP pt elev)
;Get Z value
;error handle
  (defun *error* (msg)
    (if (and (= 0 tm) (not startedInVP)) (command-s "_.PSPACE"))
    (if (not (member msg '("Function cancelled" "quit / exit abort")))
      (princ (strcat "\nError: " msg))
    );if
    (princ)
  );defun
;prep conditions
(cond
  ((and (= 1 (getvar 'CVPORT)) (= 0 (setq tm (getvar 'TILEMODE))))
    (while (not (progn (prompt "\nSelect ViewPort: ") (setq vp (ssget "_+.:E:S" '((0 . "VIEWPORT"))))))
      (prompt "...Invalid.")
    );while
    (setq vp (ssname vp 0) cvp (cdr (assoc 69 (entget vp))))
    (command "_.MSPACE") (setvar 'CVPORT cvp)
  );cond 1
  ((= 0 tm)
    (setq startedInVP t)
  );cond 2
);cond
;get elevations
(while (setq pt (getpoint "\nSelect point for Elevation (Enter to quit): "))
  (setq elev (caddr pt))
  (prompt (strcat "\nElevation: " (rtos elev 2 2)))
);while
;finish up
(if (and (= 0 tm) (not startedInVP)) (command "_.PSPACE"))
(prompt "\nGZ Complete.")
(princ)
);defun

That's the best I'm going to be able to do. Wish I could do more!

Best,

~DD

0 Likes