create the center line horizontally on close polyline and extend 1 meter away from the polyline object

create the center line horizontally on close polyline and extend 1 meter away from the polyline object

ishaq03
Advocate Advocate
3,787 Views
25 Replies
Message 1 of 26

create the center line horizontally on close polyline and extend 1 meter away from the polyline object

ishaq03
Advocate
Advocate

Please I Need a lisp command to create the center line on closed poly line Horizontally, And 1 meter extend from the closed polyline. For you reference I am attaching the sample dwg.

Note: green line showing the center line of the object in the dwg

           red line is showing 1 meter away from the object in the dwg

0 Likes
3,788 Views
25 Replies
Replies (25)
Message 2 of 26

hak_vz
Advisor
Advisor

@ishaq03 

Before jumping in code generation.

Does acad command CENTERLINE create correct centerline in your case?

Or you want mayor axes of inertia cerates through center of mass?

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.
0 Likes
Message 3 of 26

ishaq03
Advocate
Advocate

Please check my attached video, I have a lot of closed polylines and my target is create a line from center of the polyline then extend the one meter from the closed polylines.

0 Likes
Message 4 of 26

Kent1Cooper
Consultant
Consultant

[Is there a reason for starting with a vertical Xline going through the center, then drawing horizontally from that?  Can you just draw from the center horizontally to begin?]

Kent Cooper, AIA
0 Likes
Message 5 of 26

ishaq03
Advocate
Advocate

 yes my target is draw from the center horizontally to extend the one meter from closed polyline

0 Likes
Message 6 of 26

hak_vz
Advisor
Advisor

@ishaq03   Try this.  Select polyline at the edge you want perpendicular centerline

(defun c:ccl 
	(/
	*error* take pointlist2d pointlist3d get_intersection_points get_intersections 
	pick_poly e pt eo m mo po pt1 pts i p1 p2 ang
	)
	;Author:  hak_vz 
	;Sunday, September 26, 2021 
	;https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5530556
	;Posted at 
	;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-the-center-line-horizontally-on-close-polyline-and-extend/td-p/10647768
	;Create centerline to closed polyline at selection point
	
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ)
		)
		(if (and adoc) (vla-endundomark adoc))
		(princ)
	)
	(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
	(defun pointlist2d (lst / ret) (while lst (setq	ret (cons (take 2 lst) ret) lst (cddr lst))) (reverse ret))
	(defun pointlist3d (lst / ret) (while lst (setq	ret (cons (take 3 lst) ret) lst (cdddr lst))) (reverse ret))
	(defun get_intersections	(obj1 obj2 / var)
		(setq var (vlax-variant-value (vla-intersectwith obj1 obj2 2)))
		(if (< 0 (vlax-safearray-get-u-bound var 1))(vlax-safearray->list var))
	)
	(defun get_intersection_points (obj1 obj2) (pointlist3d (get_intersections obj1 obj2)))
	(defun pick_poly ()
		(setq e (entsel "\nPick a point on polylines' edge to create centerline >"))
		(if (and (not e) (= (getvar 'Errno) 7)) (pick_poly) e)
	)
	(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
	(vla-endundomark adoc)
	(vla-startundomark adoc)
	(setq e (pick_poly) pt1 (cadr e) e (car e) eo (vlax-ename->vla-object e))
	(command "_.copy" e "" '(0 0 0)'(0 0 0) "")
	(command "_.region" (entlast) "")
	(setq 
		m  (entlast) 
		mo (vlax-ename->vla-object m)
		cp (vlax-get mo 'Centroid) 
	)
	(vlax-release-object mo)
	(entdel m)
	(setq pt1 (vlax-curve-getClosestPointTo eo pt1))
	(setq pts (pointlist2d(vlax-get eo 'Coordinates)))
	(if (= (vlax-get eo 'Closed) -1)
		(setq pts (append pts (list (vlax-curve-getpointatdist eo (- (vlax-get eo 'Length) 0.001 )))))
	)
	(setq i 0)
	(while (<= (vlax-curve-getDistAtPoint eo (nth i pts))(vlax-curve-getDistAtPoint eo pt1))
		(setq i (1+ i))
	)
	(setq p1 (nth i pts))
	(setq i (1- (length pts)))
	(while (>= (vlax-curve-getDistAtPoint eo (nth i pts))(vlax-curve-getDistAtPoint eo pt1))
		(setq i (1- i))
	)
	(setq p2 (nth i pts))
	(setq ang (- (angle p2 p1) (/ PI 2)))
	(setq pt (polar cp ang 1.0))
	(setq po (vlax-ename->vla-object (entmakex (list '(0 . "LINE") (cons 10 cp) (cons 11 pt)))))
	(setq int_pts (get_intersection_points eo po))
	(setq p1 (polar cp (angle cp (car int_pts)) (1+ (distance cp (car int_pts)))))
	(setq p2 (polar cp (angle cp (cadr int_pts)) (1+ (distance cp (cadr int_pts)))))
	(vlax-put po 'StartPoint (append p1 (list 0.0)))
	(vlax-put po 'EndPoint (append p2 (list 0.0)))
	(vla-endundomark adoc)
	(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.
0 Likes
Message 7 of 26

Kent1Cooper
Consultant
Consultant

@ishaq03 wrote:

 yes my target is draw from the center horizontally to extend the one meter from closed polyline


Try this:

(defun C:HL+1 (/ esel ent etype xl ins int)
  (if
    (and
      (setq esel (entsel "\nObject to draw extended horizontal Line through: "))
      (setq ent (car esel))
      (wcmatch (setq etype (cdr (assoc 0 (entget ent)))) "*POLYLINE,CIRCLE,ELLIPSE,SPLINE")
      (vlax-curve-isClosed ent)
    ); and
    (progn ; then
      (command "_.xline" "_hor" (if (wcmatch etype "CIRCLE,ELLIPSE") "_cen" "_gcen") (cadr esel) "")
      (setq
        xl (entlast)
        ins (getvar 'lastpoint)
        int (vlax-invoke (vlax-ename->vla-object ent) 'IntersectWith (vlax-ename->vla-object xl) acExtendNone)
      ); setq
      (entdel xl)
      (command
        "_.line"
          "_non" (list (nth 0 int) (nth 1 int) (nth 2 int))
          "_non" (list (nth 3 int) (nth 4 int) (nth 5 int))
          ""
        "_.lengthen" "_delta" 1 ins ins ""
      ); command
    ); progn
  ); if
  (princ)
)

It works on closed shapes including Polylines but also Circles, Ellipses and Splines.  It assumes a horizontal Xline through the center of the shape will intersect it twice and only twice, but if a shape might be such that it intersects more than twice, I can imagine how to account for that.

It doesn't account for other Coordinate Systems, or have the usual enhancements, yet, but all that can be added if it does what you want.

And if you really want a Polyline, rather than a Line, as the result, just change  "_.line"  to  "_.pline" .  And consider whether to force zero width on it if that's what you want, in case the current Polyline width might be something else.

 

Kent Cooper, AIA
0 Likes
Message 8 of 26

Kent1Cooper
Consultant
Consultant

Here's what the HL+1 command does:

HL+1.gif

 

Also, I just went back and looked at your original Sample drawing.  The green and red results are not horizontal, but my routine does it horizontally because of the wording of the Topic heading, and your video.  Is there some need for other-than-horizontal results?  What would determine the angle?

 

Kent Cooper, AIA
0 Likes
Message 9 of 26

hak_vz
Advisor
Advisor

@Kent1Cooper

In his sample @ishaq03  has requested this. This is not horizontal but perpendicular to left edge and passes through shape center. So the term "horizontal" has to be taken with reserve as long as we don't receive more details from OP.

Untitled.png

 

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.
0 Likes
Message 10 of 26

Kent1Cooper
Consultant
Consultant

@hak_vz wrote:

.... In his sample @ishaq03  has requested this. This is not horizontal but perpendicular to left edge .... So the term "horizontal" has to be taken with reserve as long as we don't receive more details from OP.


I agree, we need clarification.  But in their video, the result in the first case is horizontal, and not perpendicular to any edge of the Polyline shape.  [I don't understand the reason for their procedure, starting by drawing a vertical Xline, but the end result is horizontal.]  So I based my suggestion on that and the Topic wording, but asked in my last Reply about the disagreement in their sample drawing.

Kent Cooper, AIA
0 Likes
Message 11 of 26

ishaq03
Advocate
Advocate

Hello kent, As mentioned HAK I need a horizontal line on the object along with perpendicular angle. It's a great help if you provided me  a option to extend the line as per my choice because some time it's a one meter extend  some time it's a various. Please more clarification check my attached video

0 Likes
Message 12 of 26

hak_vz
Advisor
Advisor

@ishaq03 

Try this

 

(defun c:ccl 
	(/
	*error* take pointlist2d pointlist3d get_intersection_points get_intersections 
	pick_poly e pt eo m mo po pt1 pts i p1 p2 ang ext
	)
	;Author:  hak_vz 
	;Thursday, September 30, 2021
	;https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5530556
	;Posted at 
	;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-the-center-line-horizontally-on-close-polyline-and-extend/td-p/10647768
	;Create centerline to closed polyline at selection point
	
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ)
		)
		(if (and adoc) (vla-endundomark adoc))
		(princ)
	)
	(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
	(defun pointlist2d (lst / ret) (while lst (setq	ret (cons (take 2 lst) ret) lst (cddr lst))) (reverse ret))
	(defun pointlist3d (lst / ret) (while lst (setq	ret (cons (take 3 lst) ret) lst (cdddr lst))) (reverse ret))
	(defun get_intersections	(obj1 obj2 / var)
		(setq var (vlax-variant-value (vla-intersectwith obj1 obj2 2)))
		(if (< 0 (vlax-safearray-get-u-bound var 1))(vlax-safearray->list var))
	)
	(defun get_intersection_points (obj1 obj2) (pointlist3d (get_intersections obj1 obj2)))
	(defun pick_poly ()
		(setq e (entsel "\nPick a point on polylines' edge to create centerline >"))
		(if (and (not e) (= (getvar 'Errno) 7)) (pick_poly) e)
	)
	(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
	(vla-endundomark adoc)
	(vla-startundomark adoc)
	(setq e (pick_poly) pt1 (cadr e) e (car e) eo (vlax-ename->vla-object e))
	(setq ext (getreal "\nExtension length > "))
	(command "_.copy" e "" '(0 0 0)'(0 0 0) "")
	(command "_.region" (entlast) "")
	(setq 
		m  (entlast) 
		mo (vlax-ename->vla-object m)
		cp (vlax-get mo 'Centroid)
		v1 (take 2(vlax-get mo 'PrincipalDirections))
		v2 (cddr(vlax-get mo 'PrincipalDirections))
		p1 (mapcar '+ cp v1)
	)
	(entdel (entlast))
	(setq po (vlax-ename->vla-object (entmakex (list '(0 . "LINE") (cons 10 cp) (cons 11 p1)))))
	(setq int_pts (get_intersection_points eo po))
	(setq p1 (polar cp (angle cp (car int_pts)) (+ (distance cp (car int_pts))  ext)))
	(setq p2 (polar cp (angle cp (cadr int_pts)) (+ (distance cp (cadr int_pts)) ext)))
	(vlax-put po 'StartPoint (append p1 (list 0.0)))
	(vlax-put po 'EndPoint (append p2 (list 0.0)))
	(setq p1 (mapcar '+ cp v2))
	(setq po (vlax-ename->vla-object (entmakex (list '(0 . "LINE") (cons 10 cp) (cons 11 p1)))))
	(setq int_pts (get_intersection_points eo po))
	(setq p1 (polar cp (angle cp (car int_pts)) (+ (distance cp (car int_pts))  ext)))
	(setq p2 (polar cp (angle cp (cadr int_pts)) (+ (distance cp (cadr int_pts)) ext)))
	(vlax-put po 'StartPoint (append p1 (list 0.0)))
	(vlax-put po 'EndPoint (append p2 (list 0.0)))
	(vla-endundomark adoc)
	(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.
0 Likes
Message 13 of 26

ishaq03
Advocate
Advocate

hak I used the code, But line appearing on the object as a Horizontal and vertical both side and if object in a rotation angle the line is not a perpendicular and code allowing me to select one by one object I have a multiple objects which I 

want to select simultaneously all only horizontal on perpendicular to object. and extension should be as per user choice which you provided me correct in your code. 

0 Likes
Message 14 of 26

hak_vz
Advisor
Advisor

The line is not perpendicular to what?

In my code I have both axes of symmetry that are calculated by Acad. In case of rectangle, rotated or  not, axes are perpendicular to both edges and extended over edge for desired distance as you have in your video. Same is true for any symmetric object i.e circle, ellipse . If you want line to be perpendicular to some edge then my first posted code do that. 

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.
0 Likes
Message 15 of 26

ishaq03
Advocate
Advocate

hak your first code is ok, but selection option only for one object I want to be select all the objects one time. And provided extension as per user choice, Because I have been explained Before extension is various some time one meter some time more.  

0 Likes
Message 16 of 26

ishaq03
Advocate
Advocate

your first code is giving vertical extension not horizontal please check.

0 Likes
Message 17 of 26

hak_vz
Advisor
Advisor

@ishaq03 wrote:

your first code is giving vertical extension not horizontal please check.


Nop. My first code  creates  centerline perpendicular from the EDGE you pick. So according to your first sample you would, when selecting object'  click at that point on left side edge. Made to make faster selection and explained in my original post. This becomes important when shape is not symmetric and edges are not perpendicular.  Or when you need an edge perpendicular axis that passes through shape center point (symmetric shape) or its center of mass when shape is not symmetric.

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.
0 Likes
Message 18 of 26

Kent1Cooper
Consultant
Consultant

@ishaq03 wrote:

.... I need a horizontal line on the object along with perpendicular angle. It's a great help if you provided me  a option to extend the line as per my choice because some time it's a one meter extend  some time it's a various. ....


[Then why did you word the Topic heading so specifically about a horizontal line and 1 meter extension?]

 

I think, when not doing the horizontal line, it would unavoidably require you to pick twice on each object, because there are two perpendicular directions for the shapes in the video, and in some of them you went the short way but in others the long way.  If you always want to go the long way in rectangles and the short way in Ellipses, it should be possible to build that in to save you a step.  Is there any kind of standard such as that?

Kent Cooper, AIA
0 Likes
Message 19 of 26

ishaq03
Advocate
Advocate

Please check my attached video I got one lisp from internet, but lisp providing horizontal and vertical both side lines.

I Need only horizontal extension on the object. 

0 Likes
Message 20 of 26

ishaq03
Advocate
Advocate

Apologies for attaching a wrong file.

0 Likes