LISP TO CONVERT LINE TO PLINE without joining lines

LISP TO CONVERT LINE TO PLINE without joining lines

Sunny2006
Enthusiast Enthusiast
1,733 Views
6 Replies
Message 1 of 7

LISP TO CONVERT LINE TO PLINE without joining lines

Sunny2006
Enthusiast
Enthusiast

i know we can cover the line to PL by join cmd. but it's making all lines to one single pline. any method to convert just to each segment only?

 

or any alternative just separate all segments into individual polilines, just line explode pline to individual lines instead of simple lines. 

0 Likes
1,734 Views
6 Replies
Replies (6)
Message 2 of 7

CADaSchtroumpf
Advisor
Advisor

@Sunny2006  a écrit :

 

or any alternative just separate all segments into individual polilines, just line explode pline to individual lines instead of simple lines. 


This

(defun c:break_lw ( / js i ent dxf_obj dxf_43 dxf_38 dxf_39 dxf_10 dxf_40 dxf_41 dxf_42 dxf_39 dxf_210 n)
  (initget "All Select")
  (if (eq (getkword "\nLWPolylines to cut at each vertex? [All/Select] <Select>: ") "All")
    (setq
      js
        (ssget "_X" 
          (list
            (cons 0 "LWPOLYLINE")
            (cons 67 (if (eq (getvar "CVPORT") 2) 0 1))
            (cons 410 (if (eq (getvar "CVPORT") 2) "Model" (getvar "CTAB")))
          )
        )
      i -1
    )
    (setq
      js
        (ssget
          (list
            (cons 0 "LWPOLYLINE")
            (cons 67 (if (eq (getvar "CVPORT") 2) 0 1))
            (cons 410 (if (eq (getvar "CVPORT") 2) "Model" (getvar "CTAB")))
          )
        )
      i -1
    )
  )
  (cond
    (js
      (repeat (sslength js)
        (setq dxf_obj (entget (setq ent (ssname js (setq i (1+ i))))))
        (if (cdr (assoc 43 dxf_obj))
          (setq dxf_43 (cdr (assoc 43 dxf_obj)))
          (setq dxf_43 0.0)
        )
        (if (cdr (assoc 38 dxf_obj))
          (setq dxf_38 (cdr (assoc 38 dxf_obj)))
          (setq dxf_38 0.0)
        )
        (if (cdr (assoc 39 dxf_obj))
          (setq dxf_39 (cdr (assoc 39 dxf_obj)))
          (setq dxf_39 0.0)
        )
        (setq
          dxf_10 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) dxf_obj))
          dxf_40 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 40)) dxf_obj))
          dxf_41 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 41)) dxf_obj))
          dxf_42 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 42)) dxf_obj))
          dxf_210 (cdr (assoc 210 dxf_obj))
        )
        (if (not (zerop (boole 1 (cdr (assoc 70 dxf_obj)) 1)))
          (setq
            dxf_10 (append dxf_10 (list (car dxf_10)))
            dxf_40 (append dxf_40 (list (car dxf_40)))
            dxf_41 (append dxf_41 (list (car dxf_41)))
            dxf_42 (append dxf_42 (list (car dxf_42)))
            n (cdr (assoc 90 dxf_obj))
          )
          (setq n (1- (cdr (assoc 90 dxf_obj))))
        )
        (repeat n
          (entmake
            (list
              (cons 0 "LWPOLYLINE")
              (cons 100 "AcDbEntity")
              (assoc 67 dxf_obj)
              (assoc 410 dxf_obj)
              (assoc 8 dxf_obj)
              (if (assoc 62 dxf_obj) (assoc 62 dxf_obj) (cons 62 256))
              (if (assoc 6 dxf_obj) (assoc 6 dxf_obj) (cons 6 "BYLAYER"))
              (if (assoc 370 dxf_obj) (assoc 370 dxf_obj) (cons 370 -1))
              (cons 100 "AcDbPolyline")
              (cons 90 2)
              (cons 70 (boole 1 (cdr (assoc 70 dxf_obj)) 128))
              (cons 38 dxf_38)
              (cons 39 dxf_39)
              (cons 10 (car dxf_10))
              (cons 40 (car dxf_40))
              (cons 41 (car dxf_41))
              (cons 42 (car dxf_42))
              (cons 10 (cadr dxf_10))
              (cons 40 (cadr dxf_40))
              (cons 41 (cadr dxf_41))
              (cons 42 (cadr dxf_42))
              (assoc 210 dxf_obj)
            )
          )
          (setq dxf_10 (cdr dxf_10) dxf_40 (cdr dxf_40) dxf_41 (cdr dxf_41) dxf_42 (cdr dxf_42))
        )
        (entdel ent)
      )
      (print (sslength js)) (princ " LWpolyline(s) cut at its vertices.")
    )
  )
  (prin1)
)
0 Likes
Message 3 of 7

john.uhden
Mentor
Mentor

@Sunny2006 ,

The 1st part is easy, especially if you setvar "PEDITACCEPT" to 1.

Let's say we assign the symbol name E to the line.

(command "_.PEDIT" e "")  ;; that's all.

The second part is a little tougher.  We'll use the BREAK command.

The good news is that it will retain arced segments and widths.

The bad news is that it will not work on closed polylines.

(defun c:DIVIDEPOLY ( / *error* var vals e etype done elast)
  (vl-load-com)
  (setq *acad* (vlax-get-acad-object))
  (vla-StartUndoMark (vla-get-activedocument *acad*))
  ;; Internal error handler and to reset upon exit
  (defun *error* (err)
    (mapcar 'setvar vars vals)
    (vla-EndUndoMark (vla-get-activedocument *acad*))
    (cond
      ((not err))
      ((not (wcmatch (strcase err) "*QUIT*,*CANCEL*"))
        (princ (strcat "\nERROR: " err "\n"))
      )
    )
    (princ)
  )
  (setq vars '("cmdecho"))
  (setq vals (mapcar 'getvar vars))
  (mapcar 'setvar vars '(0))
  (and
    (setq e (car (entsel "\nSelect a polyline to divide: ")))
    (setq etype (cdr (assoc 0 (entget e))))
    (setq elast (entlast))
    (or (= etype "LWPOLYLINE")
      (prompt (strcat "\nObject selected is a(n) " etype "."))
    )
    (or (= (vlax-get (vlax-ename->vla-object e) 'closed) 0)
      (prompt "\nCannot break a closed polyline at one point.")
    )
    (while (not done)
      (setq p (vlax-curve-getpointatparam e 1))
      (vl-cmdf "_.BREAK" e "_non" p "_non" p)
      (if (not (equal elast (entlast)))
        (setq e (entlast) elast e)
      )
      (if (= (vlax-curve-getendparam e) 1)
        (setq done 1)
      )
    )
  )
  (*error* nil)
)
(defun c:DP ()(c:DIVIDEPOLY))

  

John F. Uhden

0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

....

The 1st part is easy, especially if you setvar "PEDITACCEPT" to 1.

Let's say we assign the symbol name E to the line.

(command "_.PEDIT" e "")  ;; that's all.

....


It can be even easier than that.  You don't need to do them individually, but can convert any number of Lines to single-line-segment Polylines, all at once, without joining together any that meet at their ends.  Let's say we assign the symbol SS to a selection set of Lines [and/or Arcs if you like]:

(command "_.PEDIT" "_multiple" SS "" "")  ;; that's all.  They all get converted to Polylines, and then if you don't do anything further with them [such as Joining], but simply conclude the command, they remain independent.

Kent Cooper, AIA
Message 5 of 7

devitg
Advisor
Advisor

@Sunny2006  Please test it 

 

(defun c:line-2-plines ( /
CECOLOR LINE-ENT-LIST LINE-ENT-SS OSMODE P10 P11
			)
  (VL-LOAD-COM)
  (setq line-ent-ss (ssget "_x" '((0 . "line"))))
  (Setq	line-ent-list
	 (vl-remove-if
	   'listp
	   (mapcar 'cadr (ssnamex line-ent-ss))
	 ) ;_ end of vl-remove-if
  ) ;_ end of Setq
  (setq osmode (getvar 'osmode))
  (setvar 'osmode 0)
  (vl-cmdf "layer" "M" "poly")
  (setq CECOLOR (getvar 'cecolor))
  (setvar 'cecolor "1")
  (FOREACH line-ent line-ent-list
    (setq p10 (reverse (CDR (REVERSE (cdr (assoc 10 (entget line-ent))))))
    ) ;_ end of setq
    (setq p11 (reverse (CDR (REVERSE (cdr (assoc 11 (entget line-ent))))))
    ) ;_ end of setq
    (vl-cmdf "pline" p10 p11 "")
    (vl-cmdf "erase" line-ent "")
  ) ;_ end of FOREACH
)
0 Likes
Message 6 of 7

komondormrex
Mentor
Mentor

aio, converts lines/arcs into lw-pline segments and splits lw-plines to each segment.

 

;**************************************************************************************************************************************************************************

(defun make_vertex_bulge (pline / indices point_bulge_list)
	(setq indices '(0))
	(repeat (fix (vlax-curve-getendparam pline)) (setq indices (append indices (list (1+ (last indices))))))
	(if (minusp (vlax-get pline 'closed)) (setq indices (subst 0 (last indices) indices)))
	(mapcar '(lambda (index) (list (vlax-safearray->list (vlax-variant-value (vla-get-coordinate pline index))) 
								   (vla-getbulge pline index)
							 )
			 )
			 indices
	)
)

;**************************************************************************************************************************************************************************

(defun c:line_arc_pline_to_segments ()
	(vla-startundomark (vla-get-activedocument (vlax-get-acad-object)))
	(if (setq la_sset (ssget '((0 . "arc,line,lwpolyline"))))
		(foreach object (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex la_sset))))
			(cond
				((= "AcDbLine" (vla-get-objectname object))
					(setq segment_list (list (vla-addlightweightpolyline (vla-objectidtoobject (vla-get-activedocument (vlax-get-acad-object)) (vla-get-ownerid object))
															  			 (vlax-safearray-fill (vlax-make-safearray vlax-vbdouble (cons 0 3))
															  								  (apply 'append (list (mapcar '+ '(0 0) (vlax-get object 'startpoint)) 
															  									  				   (mapcar '+ '(0 0) (vlax-get object 'endpoint))
															  												 )
															  								  )
															  			 )
											 )
							      		)
					)
				)
				((= "AcDbArc" (vla-get-objectname object))
					(setq segment_list (list (vla-addlightweightpolyline (vla-objectidtoobject (vla-get-activedocument (vlax-get-acad-object)) (vla-get-ownerid object))
															  			 (vlax-safearray-fill (vlax-make-safearray vlax-vbdouble (cons 0 3))
															  								  (apply 'append (list (mapcar '+ '(0 0) (vlax-get object 'startpoint)) 
															  								  					   (mapcar '+ '(0 0) (vlax-get object 'endpoint))
															  												 )
															  								  )
															  			 )
											 )
							      	   )
					)
					(vla-setbulge (car segment_list) 0 (/ (sin (* 0.25 (vla-get-totalangle object))) (cos (* 0.25 (vla-get-totalangle object)))))
				)
				(t
					(setq vertex_list (make_vertex_bulge object)
						  segments (mapcar 'list vertex_list (cdr vertex_list))
						  segment_list (mapcar '(lambda (segment) (progn (setq segment_object (vla-addlightweightpolyline (vla-objectidtoobject (vla-get-activedocument (vlax-get-acad-object)) (vla-get-ownerid object))
															  			 					  					  		  (vlax-safearray-fill (vlax-make-safearray vlax-vbdouble (cons 0 3))
															  			 					  					  							   (apply 'append (list (caar segment) (caadr segment)))
															  			 					  					  		  )
																					  		  ) 
																		 )
																		 (vla-setbulge segment_object 0 (cadar segment)) 
																		 segment_object
											 					  )
												)
												segments
									   )
					)
				)
			)
			(foreach segment segment_list 
				(foreach property '(layer linetype lineweight linetypescale truecolor)
		   			(vl-catch-all-apply (read (strcat "vla-put-" (vl-princ-to-string property)))
		   								(list segment (eval (list (read (strcat "vla-get-" (vl-princ-to-string property))) object)))
		   			)
		   		)
			)
			(vla-erase object)
		)
	)
	(vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
	(princ)
)

;**************************************************************************************************************************************************************************

 

0 Likes
Message 7 of 7

Kent1Cooper
Consultant
Consultant

What a lot of complications....  I repeat [and now include in a command definition], this is all you need, to do what Message 1 requests:

 

(defun C:L2PLI ; = Line[s] {to} PolyLine[s] Independently
  (/ ss)
  (if (setq ss (ssget "_:L" '((0 . "LINE"))))
    (command "_.PEDIT" "_multiple" SS "" ""); then
    (prompt "\nNo Line(s) on unlocked Layer(s) selected."); else
  ); if
  (prin1)
)

 

If you don't normally have PEDITACCEPT set to 1, that can be built into it, or the answer to the conversion question can be built into it, or it could even check that setting and proceed accordingly.

I assume you want to select the Lines to be converted, not do all Lines in the drawing.  But if the latter, and if some may be in different Layouts, this approach would need to move to each one's Layout to convert it.

Kent Cooper, AIA
0 Likes