Create the point on mid of the polyline

Create the point on mid of the polyline

ishaq03
Advocate Advocate
3,217 Views
10 Replies
Message 1 of 11

Create the point on mid of the polyline

ishaq03
Advocate
Advocate

I have multiple polylines and I need to create the point on mid of the poly lines instead of vertex. Please check the attached pic for  more clarification

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

CodeDing
Advisor
Advisor

@ishaq03 ,

 


@ishaq03 wrote:

...I need to create the point on mid of the poly lines...


Do you mean, you want to create a POINT entity, add a VERTEX, or merely need the COORDINATES for lisp?

0 Likes
Message 3 of 11

ishaq03
Advocate
Advocate

Only poin entity on mid of the poly lines 

0 Likes
Message 4 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

You can use MarkMidPoints.lsp with its MMP command, available >here<.  Choose the Point option for the marker, and it has an option for Polylines whether to mark the overall midpoint or the midpoint of every segment.

Kent Cooper, AIA
Message 5 of 11

hak_vz
Advisor
Advisor

Try this

(defun c:mips ( / *error* take pointlist2d pointlist3d adoc e eo pts ss i)
	(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))
	(setq adoc (vla-get-activedocument (vlax-get-acad-object))) 
	(vla-endundomark adoc)
	(vla-startundomark adoc)
	(princ "\nSelect entities to add segment midpoints >")
	(setq ss (ssget '((0 . "*LINE"))) i -1)
	(while (< (setq i (1+ i))(sslength ss))
		(setq 
			e (ssname ss i) 
			eo (vlax-ename->vla-object e)
		)
		(cond 
			((or (= (vlax-get eo 'ObjectName) "AcDb2DPolyline")(= (vlax-get eo 'ObjectName) "AcDbSpline"))
				(command "_.splinedit" e "_P" 4)
				(setq eo (vlax-ename->vla-object (entlast)))
			)
		)
		(cond 
			((= (vlax-get eo 'ObjectName) "AcDbLine")
				(setq pts (list (vlax-get eo 'StartPoint)(vlax-get eo 'EndPoint)))
			)
			((or(= (vlax-get eo 'ObjectName) "AcDbPolyline"))
				(setq pts (pointlist2d (vlax-get eo 'Coordinates)))
			)
			((= (vlax-get eo 'ObjectName) "AcDb3dPolyline")
				(setq pts (pointlist3d (vlax-get eo 'Coordinates)))
			)
		)
		(while (> (length pts) 1)
			(setq e (take 2 pts))
			(setq pts (cdr pts))
			(entmakex 
				(list
					(cons 0 "POINT")
					(cons 100 "AcDbPoint")
					(cons 100 "AcDbEntity")
					(cons 10 
						(vlax-curve-getPointAtParam eo
							(* 0.5 
								(+ 
										(vlax-curve-getParamAtPOint eo (vlax-curve-getClosestPointTo eo (car e)))
										(vlax-curve-getParamAtPOint eo (vlax-curve-getClosestPointTo eo (cadr e)))
								)
							)
						)
					)
				)
			)
		)
	)
	(vla-endundomark adoc)
	(princ "\nDone!")
	(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 6 of 11

pbejse
Mentor
Mentor

@ishaq03 wrote:

I have multiple polylines and I need to create the point on mid of the poly lines instead of vertex. Please check the attached pic for  more clarification


Heres a short code you can study

 

(Defun c:Middleman (/ ssi e n)
  (if (setq ss (ssget '((0 . "LWPOLYLINE"))))
    (repeat (setq i (sslength ss))
      (setq e (ssname ss (setq i (1- i))))
      (repeat (1- (setq n (cdr (Assoc 90 (entget e)))))
	(entmakex
	  (list	(cons 0 "POINT")
		(cons 10 (vlax-curve-getPointAtParam e (- (Setq n (1- n)) 0.5)))
	  )
	)
      )
    )
  )
  (princ)
)

 

Command: Middleman

 

Message 7 of 11

Kent1Cooper
Consultant
Consultant

@pbejse wrote:
....

Heres a short code you can study

 

....
      (repeat (1- (setq n (cdr (Assoc 90 (entget e)))))
....

I think that needs to make a differentiation between open and closed Polylines.  I believe the above will omit one segment from closed ones.

Kent Cooper, AIA
Message 8 of 11

pbejse
Mentor
Mentor

@Kent1Cooper wrote:

 

I think that needs to make a differentiation between open and closed Polylines.  I believe the above will omit one segment from closed ones.

Oopsie! You are right about closed polylines. 

Good catch @Kent1Cooper  👍

 

 

 

0 Likes
Message 9 of 11

Sea-Haven
Mentor
Mentor

Caught me out a few times,

get a pline put co-ords into a list

(if plent (setq lst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent))))))

if closed (setq lst (cons (last lst) lst)) adds 1st point back in as last pt.

Message 10 of 11

pbejse
Mentor
Mentor

@Sea-Haven wrote:

Caught me out a few times,


That's true, to be honest I wasnt even thingking of closed polylines after seeing the image posted by the OP . But yeah, better have and not need than need and not have.

 

 

0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

.... get a pline put co-ords into a list

(if plent (setq lst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent))))))

if closed (setq lst (cons (last lst) lst)) adds 1st point back in as last pt.


In this situation, it isn't necessary to get a list of vertex coordinates.  The parameter-based approach in @pbejse 's Message 6 eliminates the need for that, and has another benefit.  In a Polyline, if you step around to the last vertex, you have reached the end if it's open, but you haven't reached the end if it's closed.  But if you go around to the end parameter value, you have reached the end, whether or not it's closed.  It's only a question of the number of Points to draw, and using parameter values, you don't need to consider whether it's open or closed, if you structure the procedure the right way.  I think [untested] that Message 6 could be altered in this way:

 

(Defun c:Middleman (/ ss i e n)
  (if (setq ss (ssget '((0 . "LWPOLYLINE"))))
    (repeat (setq i (sslength ss)); then
      (setq e (ssname ss (setq i (1- i))))
      (repeat (setq n (fix (vlax-curve-getEndParam e)))
        (entmakex ; [could be just (entmake)]
          (list

            (cons 0 "POINT")
            (cons 10 (vlax-curve-getPointAtParam e (+ (Setq n (1- n)) 0.5)))

          ); list
        ); entmake[x]
      ); repeat [Points]
    ); repeat [Polylines]
  ); if
  (princ)
)

 

[Note that in addition to the different determination of the count, I changed a minus sign to a plus sign.]

Kent Cooper, AIA