Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Break Polyline at every vertex...

10 REPLIES 10
Reply
Message 1 of 11
Anonymous
5099 Views, 10 Replies

Break Polyline at every vertex...

This is what I came up with on short notice...however, it bombs AutoCAD and gives me an internal error..I could really use some help on this...Thanks in advance..

(defun c:pb ()
(princ "Pick Polyline...")
(setq ss1 (ssget))
(command "pedit" ss1 "e" "n" "n" "n" "n" "n" "p" "b" "g"
"p" "b" "g" "p" "b" "g" "p" "b" "g"
"p" "b" "g" "p" "b" "x" "")
)
10 REPLIES 10
Message 2 of 11
BillZ
in reply to: Anonymous

Why don't you just use explode on it?
Message 3 of 11
Anonymous
in reply to: Anonymous

I want to retain it's polyline qualities... I made these guys a command to select as many pollines as they need a make them the width they need them...Point of fact: they could just explode the pline and use that tool to make them the correct thickness... That would yet be too easy for me...they want something else...
Message 4 of 11
BillZ
in reply to: Anonymous

You would be beter off just exploding the pline, setting a selection set of the "Previous" and then looping through the entities and making them back into polylines with width and so on.
(command "explode" entity)
(setq ss (ssget "P"))
(repeat (sslength ss and so on..........

BillZ
Message 5 of 11
Anonymous
in reply to: Anonymous

Bill,

Here is my attempt at braking at every vertex, took me
a minute or two to figure it out. I was trying to make it
much more complicated that what it needed to be (I
seem to have that habit!)

; Jason Piercey . April 4th, 2003
; Break lightweight and heavyweight polylines
; at each vertex. This is different than the
; explode command as it will retain any width
; properties that have been applied.

; [ename] - LWPOLYLINE or POLYLINE entity name
(defun getVertexPts (ename / data ent pts)
(setq data (entget ename))
(setq ent (cdr (assoc 0 data)))
(cond
((= "LWPOLYLINE" ent)
(foreach x data
(if (= 10 (car x))
(setq pts (cons (cdr x) pts)))
)
)

((= "POLYLINE" ent)
(setq ent (entnext ename)
data (entget ent) )
(while
(and (setq data (entget ent))
(/= "SEQEND" (cdr (assoc 0 data))) )
(setq pts (cons (cdr (assoc 10 data)) pts))
(setq ent (entnext ent))
)
)
(t nil)
)
(reverse pts)
)

(defun c:breakVert (/ cmdecho osmode *error* ss i ename data ent pts)
(setq cmdecho (getvar "cmdecho")
osmode (getvar "osmode") )

(defun *error* (msg)
(cond
((and
msg
(member msg '("quit / exit abort" "Function cancelled"))
)
(setvar "cmdecho" cmdecho)
(setvar "osmode" osmode)
(princ)
)
(t (princ (strcat "\n; breakVert error: " msg)))
)
)

(if (setq ss (ssget '((0 . "POLYLINE,LWPOLYLINE"))))
(progn
(setvar "cmdecho" 0)
(setvar "osmode" 0)
(setq i -1)
(repeat (sslength ss)
(setq ename (ssname ss (setq i (1+ i)))
data (entget ename)
ent (cdr (assoc 0 data))
pts (getVertexPts ename) )
(cond
((< 2 (length pts))
(setq pts (cdr pts))
(if (not (= 1 (logand (cdr (assoc 70 data)))))
(setq pts (reverse (cdr (reverse pts)))) )

(command ".break" ename (car pts) "@")
(foreach x (cdr pts) (command ".break" (entlast) x "@"))
(setq pts nil)
)
(t nil)
)
)
(setvar "cmdecho" cmdecho)
(setvar "osmode" osmode)
(princ "\nbreaking complete")
)
)
(princ)
)
(princ "\ntype breakVert to execute")


and just incase my co-worker is reading this... yes, Mark..
I used the (command) function 😉
--

-Jason
Member of the Autodesk Discussion Forum Moderator Program


"BillZ" wrote in message news:f154a3c.2@WebX.maYIadrTaRb...
> You would be beter off just exploding the pline, setting a
> selection set of the "Previous" and then looping through the
> entities and making them back into polylines with width
> and so on.
Message 6 of 11
arun.panneerselvam2016
in reply to: Anonymous

;; Save and restore variables on your own

;; A short & sweet method to break polylines

;; let me know if further updates can be done

 

(defun vrtxcnt ( / ent e v p vrtxpnts turns)
(setq ent (entget (setq e (car (entsel "Select a Path: ")))) v 0)
(cond
((eq (cdr (assoc 0 ent)) "LWPOLYLINE")
(setq v (cdr (assoc 90 ent)))
)
((eq (cdr (assoc 0 ent)) "POLYLINE")
(while (eq (cdr (assoc 0 (entget (setq e (entnext e))))) "VERTEX")
(setq vrtxpnts (cons (cdr (assoc 10 (entget e) )) vrtxpnts))
(setq v (1+ v))

)
)
)
(setq turns (cdr (vl-remove (last vrtxpnts) vrtxpnts)))
(princ (list ent vrtxpnts turns))
)


(defun c:b@v ( / vinfo ent vp vt)
(setq vinfo (vrtxcnt))
(setq vt (caddr vinfo))
(foreach X vt
( command "_break" (cdr (assoc -1 (car vinfo))) X "@" )
)
)

Message 7 of 11

Here's the way I came up with to do that, a few years back.  You can pick as many Polylines of any type(s) as you want in one running of the command and it will do it to all of them, it saves and restores System Variables for you, it works in different UCS's, and it does it in a way that doesn't need to figure out which type each one is nor pre-determine a count of how many places to Break each one.  If you use AutoCAD 2015+, change the (command function name in the *error* handler near the top to (command-s .

 

Spoiler
;;  PolylineSubDivide.lsp [command name: PSD]
;;  SubDivide Polylines into separate Polylines of individual
;;  line and arc segments, maintaining the original width(s) of
;;  each segment, if any. Works on Lightweight 2D Polylines,
;;  "heavy" 2D Polylines, and 3D Polylines.
;;  Kent Cooper, April 2010
(defun C:PSD (/ *error* cmde osm blipm plset pl)
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
    ); end if
    (setvar 'osmode osm)
    (setvar 'blipmode blipm)
    (command "_.undo" "_end")
    (setvar 'cmdecho cmde)
  ); end defun - *error*
  (setq cmde (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (command "_.undo" "_begin")
  (setq osm (getvar 'osmode))
  (setvar 'osmode 0)
  (setq blipm (getvar 'blipmode))
  (setvar 'blipmode 0)
  (prompt "\nTo SubDivide Polylines,")
  (setq plset (ssget '((0 . "*POLYLINE"))))
  (if plset
    (repeat (sslength plset)
      (setq pl (ssname plset 0))
      (if (> (vlax-curve-getEndParam pl) 1)
        (progn ; then - more than 1 segment
          (command
            "_.break"
            pl
            (trans (vlax-curve-getPointAtParam pl 1) 0 1)
            "@"
          ); end command
          (while (> (vlax-curve-getEndParam (entlast)) 1)
            (command
              "_.break"
              (entlast)
              (trans (vlax-curve-getPointAtParam (entlast) 1) 0 1)
              "@"
            ); end command
          ); end while
        ); end progn
      ); end if
      (ssdel pl plset)
    ); end repeat
  ); end if
  (setvar 'osmode osm)
  (setvar 'blipmode blipm)
  (command "_.undo" "_end")
  (setvar 'cmdecho cmde)
  (princ)
); end defun
(prompt "\nType PSD to SubDivide Polylines into separate segments.")
Kent Cooper, AIA
Message 8 of 11
Kent1Cooper
in reply to: BillZ


@BillZ wrote:
You would be beter off just exploding the pline, setting a selection set of the "Previous" and then looping through the entities and making them back into polylines with width and so on. ....

..

[I know this is Replying to a very old Post, but for future generations....]

 

That may be relatively easy if the Polyline you started with has a constant width.  But it would be impossible if the width varies.  That's the main reason, in my opinion, that single-point-Breaking them at every vertex is the better way to go.

Kent Cooper, AIA
Message 9 of 11
pbejse
in reply to: Kent1Cooper

Not sure why this topic is zombified.

 

@Anonymous

 

If you use AutoCAD 2015+, change the (command function name in the *error* handler near the top to (command-s .

 

Hope you don't mind giving us more info of command-s, 

 

i encountered an error i've never seen before that refers to vl.crx and more arx files when running programs on Autocad 14  using  command-s on programs written for Autocad 2017, specifically  (command-s "_Reverse" e). It took me a while to realize to whats going on.

 

pBe

 

 

 

 

Message 10 of 11
Kent1Cooper
in reply to: pbejse


@pbejse wrote:

.... 

Hope you don't mind giving us more info of command-s, 

 

i encountered an error i've never seen before that refers to vl.crx and more arx files when running programs on Autocad 14  using  command-s on programs written for Autocad 2017, specifically  (command-s "_Reverse" e). It took me a while to realize to whats going on.

 

pBe


Here is Help [for 2016] on (command-s).  Partway down it discusses differences from the (command) function.  You can't use (command) inside an *error* handler [it's still perfectly acceptable elsewhere] in 2015+ unless you go through some other shenanigans to make it allowable.  But too-early versions don't recognize (command-s) at all [there may be some overlap, perhaps a version or two in which (command-s) had been introduced but you could still use (command) in *error* handlers -- I'm not sure].  So since the main thing I've typically wanted to do with a command in an *error* handler is Undo End, I've taken to using (vl... methods for Undo begin and end, because they work in all versions, so the same code can be used anywhere, without making separate files for older and newer versions of AutoCAD.

Kent Cooper, AIA
Message 11 of 11
pbejse
in reply to: Anonymous

Thank you for the info Kent. appreciate the time.

pBe

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost