Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Distance between vlax-3d-points

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
surfer96
1322 Views, 6 Replies

Distance between vlax-3d-points

Is it possible to get the distance between vlax-3d-points (not simple AutLISP points) by using something like:

 

(distance vlax-3d-point1 vlax-3d-point2)

or

(vla-GetDistance vlax-3d-point1 vlax-3d-point2) ?

 

The whole geometry should be built with ActiveX, like vlax-3d-points -> vla-AddLine to speed up calculation and get the following code working...

 

 

(vl-load-com)
(defun c:rdline	(/ bw r1 r2 pt1 pt2 dbp)

  (setq	acadObj	   (vlax-get-acad-object)
	c_doc	   (vla-get-Activedocument acadObj)
	modelSpace (vla-get-ModelSpace c_doc)
  )


  (setq
    bw	100				;box width
    r1	(* (LM:rand) bw)
    r2	(* (LM:rand) bw)
    pt1	(vlax-3d-point 0 r1 0)
    pt2	(vlax-3d-point bw r2 0)
  )

  (setq lineObj (vla-AddLine modelSpace pt1 pt2))
					;line from random points 
  (setq dbp (distance pt1 pt2))		;distance between points

  (princ
    (strcat "\nDistance between points:" (rtos dbp 2 4))
  )
  (princ)
)
(defun LM:rand (/ a c m)
  (setq	m   4294967296.0
	a   1664525.0
	c   1013904223.0
	$xn (rem (+ c
		    (* a
		       (cond ($xn)
			     ((getvar 'date))
		       )
		    )
		 )
		 m
	    )
  )
  (/ $xn m)
)

 

 

6 REPLIES 6
Message 2 of 7
ronjonp
in reply to: surfer96

Call addline like this:

;; Don't need vlax-3d-point
(vlax-invoke modelSpace 'addline pt1 pt2)

And set your points like so:

  (setq
    bw	100.				;box width
    r1	(* (LM:rand) bw)
    r2	(* (LM:rand) bw)
    pt1	(list 0. r1 0.)
    pt2	(list bw r2 0.)
  )

Or even simpler, use entmakex:

(entmakex (list '(0 . "line") (cons 10 pt1) (cons 11 pt2)))

If you use the addline method, you will also need to convert all the integers within the list to reals .. with entmakex, it does not matter: 

image.png

And if you want to use DISTANCE with VLAX-3D-POINT you'll need to convert them back to a list like so.

(distance (vlax-safearray->list (vlax-variant-value (vlax-3d-point 0. 0. 0.)))
	  (vlax-safearray->list (vlax-variant-value (vlax-3d-point 8. 8. 8.)))
)
Message 3 of 7
surfer96
in reply to: ronjonp

The entmakex method works fine but 'addline doesn't?

I simplified the example without using random variables, but it returns neither line nor distance?

(vl-load-com)
(defun c:addline (/ pt1 pt2 lineObj dbp)

  (setq	acadObj	   (vlax-get-acad-object)
	c_doc	   (vla-get-Activedocument acadObj)
	modelSpace (vla-get-ModelSpace c_doc)
  )

  (setq
    pt1	(list 100 100 0)
    pt2	(list 0 0 0)
  )

  (setq	lineObj	(vlax-invoke modelSpace 'addline pt1 pt2)
	dbp	(distance pt1 pt2)
  )					;line / distance between points

  (princ
    (strcat "\nDistance between points:" (rtos dbp 2 4))
  )
  (princ)
)

 

 

Message 4 of 7
surfer96
in reply to: ronjonp

I did not see the end of your message at first sight. Just changed integers to reals and now 'addline is working.

Sorry for that...

Message 5 of 7
ronjonp
in reply to: surfer96

Glad you got it sorted (y).

Message 6 of 7
CodeDing
in reply to: surfer96

@surfer96 ,

 

Perhaps a consideration could be to retrieve the Length of your line rather than distance between points? I am not sure which would be faster, but in my head it makes more sense to grab an existing property than to calculate distance with geom/trig.

(setq dbp (vla-get-Length lineObj))

Just a thought.

Best,

~DD


Need AutoLisp help? Try my custom GPT 'AutoLISP Ace':
https://chat.openai.com/g/g-Zt0xFNpOH-autolisp-ace
Message 7 of 7
surfer96
in reply to: CodeDing

@CodeDing 

(vla-get-Length lineObj) will probably be faster in this particular case.

Although not applicable for situations where distances between points will be needed that are not line enpoints or startpoints...

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report