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,843 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,844 Views
25 Replies
Replies (25)
Message 21 of 26

hak_vz
Advisor
Advisor

Try this for symmetric objects (circle, ellipse, rectangle). For irregular shapes I'll modify my original post but in that case you have to pick an edge, as @Kent1Cooper elaborated.

 

(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 closed curve [circle | ellipse | polylines >"))
		(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)
	(princ "\nSelect entities to create mayor centerline >")
	(setq ss (ssget '((0 . "*POLYLINE,CIRCLE,ELLIPSE,SPLINE"))) i -1)
	(setq ext (getreal "\nExtension length > "))
	(while (< (setq i (1+ i)) (sslength ss))
		(setq e (ssname ss i) 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)
			v1 (take 2(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)))
	)
	(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 22 of 26

Kent1Cooper
Consultant
Consultant

@ishaq03 wrote:

.... I want to be select all the objects one time. And provided extension as per user choice....  


See what you think of the attached LineCenterExt.lsp with its LCX command.  Read the comments at the top.

 

You can select all objects together, and it filters out those that have no CENter or Geometric-CENter Osnap option.  You can choose Horizontal or Vertical or User-picked direction for the Lines, and it remembers which and offers it as default the next time.  If the direction choice is User-picked, it asks you for a direction for each object.  You can set the extension distance, and it remembers it and offers it as default.

 

It will have unexpected results if the shape of a closed Polyline or Spline is contorted enough that its GCEN Osnap location is outside it.  And if you do Hor/Ver ones, which draw an Xline, and the shape is such that the Xline crosses the shape more than twice, it won't necessarily give you the extension from the extremes.  Maybe that can be accounted for with more code, to check for the number of intersections and figure out which are farthest apart.

 

It is not set up for other-than-World Coordinate Systems, but perhaps could be.

Kent Cooper, AIA
0 Likes
Message 23 of 26

ishaq03
Advocate
Advocate

Thanks kent command is fine, But kindly make few changes in the code provide the line extension horizontal and vertical as per perpendicular on the object and user choice keep it same as per user direction. Please more clarification check my attached video.

0 Likes
Message 24 of 26

ishaq03
Advocate
Advocate

kent Please keep it default as a Length of Extension beyond perimeter 0 and user choice the extension as per his need.

0 Likes
Message 25 of 26

Kent1Cooper
Consultant
Consultant

@ishaq03 wrote:

.... default as a Length of Extension beyond perimeter 0 ....


For that just change three 1's to 0's:

....

        (if LCXext (rtos LCXext) "0"); prior value default if present, otherwise 0
        ">:"
      ); strcat
    ); getdist
  ); User-input condition
  (LCXext); prior value on Enter if present
  (0); initial default on Enter with no prior value

....

Kent Cooper, AIA
0 Likes
Message 26 of 26

Kent1Cooper
Consultant
Consultant

@ishaq03 wrote:

... few changes in the code .... check my attached video.


I'm sorry, but I don't understand what you're asking, even with the video.  Can you explain it differently?

Kent Cooper, AIA
0 Likes