perpendicular line from polyline to entity

perpendicular line from polyline to entity

neam
Collaborator Collaborator
3,297 Views
10 Replies
Message 1 of 11

perpendicular line from polyline to entity

neam
Collaborator
Collaborator

Hi everybody

How to draw perpendicular line from polyline to another polyline like attachment file.

In a way that is perpendicular to the first polyline and extends to the middle polyline and finally be perpendicular to the second polyline.

Thank you for your help.

0 Likes
Accepted solutions (2)
3,298 Views
10 Replies
Replies (10)
Message 2 of 11

Sea-Haven
Mentor
Mentor

It is done very easy using vlisp getclosestpointto function. Simply select inner and outer the you can get a square  point to both of the pline, do you know anything about lisp ?

 

(setq pt1 (vlax-curve-getclosestpointto object1 pt))
(setq pt2 (vlax-curve-getclosestpointto object2 pt))
(command "line" pt2 pt pt1 "")

 

0 Likes
Message 3 of 11

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can get inspiration from this:

 

(defun c:test (/ pl1 pl2 pl3 pt1 param vec xl pts)
  (and
    (setq pl1 (car (entsel "\nSelect first polyline: ")))
    (= (cdr (assoc 0 (entget pl1))) "LWPOLYLINE")
    (setq pl2 (car (entsel "\nSelect second polyline: ")))
    (= (cdr (assoc 0 (entget pl2))) "LWPOLYLINE")
    (setq pl3 (car (entsel "\nSelect third polyline: ")))
    (= (cdr (assoc 0 (entget pl3))) "LWPOLYLINE")
    (setq pt1 (getpoint "\nPick a point on the first polyline: "))
    (setq pt1 (osnap (trans pt1 1 pl1) "_nea"))
    (setq param (vlax-curve-getParamAtPoint pl1 pt1))
    (setq vec (vlax-curve-getFirstDeriv pl1 param))
    (setq vec (list (- (cadr vec)) (car vec) (caddr vec)))
    (setq xl (entmakex(list (cons 0 "XLINE") (cons 100 "AcDbEntity") (cons 100 "AcDbXline") (cons 10 pt1) (cons 11 vec))))
    (setq pt2 (vlax-invoke (vlax-ename->vla-object pl2) 'IntersectWith (vlax-ename->vla-object xl) acExtendNone))
    (setq pt3 (vlax-curve-getClosestPointTo pl3 pt2))
    (entdel xl)
    (entmake (list (cons 0 "LINE") (cons 10 pt1) (cons 11 pt2) (cons 62 6)))
    (entmake (list (cons 0 "LINE") (cons 10 pt2) (cons 11 pt3) (cons 62 4)))
  )
  (princ)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 11

_gile
Consultant
Consultant

@Sea-Haven if I do not misundertand the OP, it's not that simple because the first line have to perpendicular to the first polyline at a point on the first polyline.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 11

hak_vz
Advisor
Advisor

Here is a quick mockup without using vlisp. Command DPERP will create perpendicular line from a point to line, polyline or any object selectable with "nearest" osnap.  You first pick two points on object you want to create perp line, as nearest as possible to place where you have to place it, third point is start point of a line. Since I dont use vlisp this is aproximation  for curved polylines.

 

(defun perpendicular_from_point_to_line (lin1 lin2 p / x1 y1 x2 y2 x3 y3 k m n ret)
;returns point on a line (line1 line2) as a perpendicular projection from point p
	(mapcar 'set '(x1 x2 x3) (mapcar 'car (list lin1 lin2 p)))
	(mapcar 'set '(y1 y2 y3) (mapcar 'cadr (list lin1 lin2 p)))
	(setq 
		m (-(*(- y2 y1) (- x3 x1))(*(- x2 x1) (- y3 y1)))
		n (+(* (- y2 y1)(- y2 y1))(*(- x2 x1)(- x2 x1)))
	)
	(cond 
		((/= n 0.0) 
			(setq 
				k (/ m n)
				ret (list(- x3 (* k(- y2 y1)))(+ y3 (* k(- x2 x1))))
			)
		)
	)
	ret
)

(defun c:dperp ( / oldosm p1 p2 p3 p4 *error*)
(defun *error* ()(setvar "osmode" oldosm)(princ))
	(setq oldosm (getvar "osmode"))
	(setvar "osmode" 513)
	(setq p1 (getpoint "\nClick first vector point on polyline >"))
	(setq p2 (getpoint "\nClick second vector point on polyline >"))
	(setq p3 (getpoint "\nClick point from which to create perpendicular line  >"))
	(setq p4 (perpendicular_from_point_to_line p1 p2 p3))
	(command "_.line" p3 p4 "")
	(setvar "osmode" oldosm)
	(princ)
)

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 6 of 11

neam
Collaborator
Collaborator

thank you very much
Completely concluded

0 Likes
Message 7 of 11

hak_vz
Advisor
Advisor

@_gilehas provided good solution, unfortunately limited to Lwpolylines.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 8 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

I see no reason to limit it to Polylines.  This works [in very limited testing] with all kinds of path objects:

 

(defun C:LB3 ; = Lines Between 3 objects
  (/ ent1 ent2 ent3 pt1 obj1 obj2 pt2)
  (if
    (and
      (setq ent1 (car (entsel "\nFirst object to start perpendicularly from: ")))
      (setq ent2 (car (entsel "\nSecond intermediate object: ")))
      (setq ent3 (car (entsel "\nThird object to end perpendicularly on: ")))
      (setq pt1 (osnap (getpoint "\nStarting point on first object: ") "_nea"))
    ); and
    (progn ; then
      (command "_.line"
        "_none" pt1
        "_none"
          (polar
            pt1
            (+ (angle '(0 0 0) (vlax-curve-getFirstDeriv ent1 (vlax-curve-getParamAtPoint ent1 pt1))) (/ pi 2))
            1
          ); polar
        ""
      ); command
      (setq
        obj1 (vlax-ename->vla-object ent2)
        obj2 (vlax-ename->vla-object (entlast))
        pt2 (vlax-invoke obj1 'IntersectWith obj2 acExtendBoth)
      ); setq
      (entdel (entlast))
      (command "_.line" "_none" pt1 "_none" pt2 "_none" (vlax-curve-getClosestPointTo ent3 pt2) "")
    ); progn
  ); if
  (princ)
)

 

It doesn't [yet] do things it could be enhanced to do, such as verify appropriate kinds of objects are selected, or ask again if you pick something inappropriate or just miss, deal with the possibility that the projection of the temporary Line might IntersectWith the intermediate object more than once [a Circle or certain configurations of other object types], nor deal with the possibility of different coordinate systems.  All such things can be worked in if needed, and others [e.g. draw on a particular Layer?], but see whether it does what you need in simple situations.

Kent Cooper, AIA
Message 9 of 11

neam
Collaborator
Collaborator

thank you very much dear kent 🙏🙏

0 Likes
Message 10 of 11

dar.lombardi
Advocate
Advocate

Why does it work with the "xline" command and with command line (command "_line" pt1 vec "") it doesn't work? 

0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

@dar.lombardi wrote:

Why does it work with the "xline" command and with command line (command "_line" pt1 vec "") it doesn't work? 


Because the 'vec' variable, being used for the 11-code entry in the Xline's entity data, is not a point, but a unit vector defining only a one-unit-long direction relative to the 10-code entry [the base point of the Xline].

Kent Cooper, AIA