Message 1 of 14
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
This LISP removes zero-length segments from polylines. However, it's currently stripping the widths. Looking for help to modify it to retain the original polyline start and end widths. Thanks!
(vl-load-com)
(defun c:zlp (/ ss i ent exploded_ss exploded_ent len zero_length_count)
;; Initialize a counter for zero-length segments
(setq zero_length_count 0)
;; Get the ActiveDocument object for VBA Undo
(setq acadDocument (vla-get-ActiveDocument (vlax-get-acad-object)))
;; Start undo mark here
(vla-startundomark acadDocument)
(setvar 'cmdecho 0) ; Turn command echo off for cleaner execution
;; Get polylines selection
(princ "\nSelect polylines to process: ")
(setq ss (ssget '((0 . "LWPOLYLINE"))))
(if ss
(progn
(setvar 'peditaccept 1) ; Set PEDITACCEPT to 1 to avoid prompts
(setq i 0)
(repeat (sslength ss)
(setq ent (ssname ss i))
(setq i (1+ i)) ; Increment counter for next polyline
;; Explode the current polyline
(command "_.explode" ent)
;; Get the exploded parts (previous selection) immediately after explode
(setq exploded_ss (ssget "P"))
;; Delete zero-length parts
(if exploded_ss
(progn
(setq j 0)
(repeat (sslength exploded_ss)
(setq exploded_ent (ssname exploded_ss j))
(setq j (1+ j))
;; Check for zero length (using vlax-curve-getdistatparam for lines/arcs)
(if (or (= (cdr (assoc 0 (entget exploded_ent))) "LINE")
(= (cdr (assoc 0 (entget exploded_ent))) "ARC")
(= (cdr (assoc 0 (entget exploded_ent))) "LWPOLYLINE") ; in case explode results in polyline segments
)
(progn
(setq len (vlax-curve-getdistatparam exploded_ent (vlax-curve-getendparam exploded_ent)))
(if (< len 1e-6) ; Tolerance for zero length
(progn
;; Increment the zero_length_count before deleting
(setq zero_length_count (1+ zero_length_count))
(entdel exploded_ent) ; Delete zero-length entity
)
)
)
)
)
)
)
;; Rejoin the exploded parts back into a polyline
(if exploded_ss
(command "_.pedit" "_multiple" exploded_ss "" "_join" "" "")
)
) ; repeat for each polyline
;; Report the total number of deleted zero-length segments
(if (> zero_length_count 0)
(princ (strcat "\nPolylines processed and " (itoa zero_length_count) " zero-length segments removed."))
(princ "\nPolylines processed. No zero-length segments were removed.")
)
)
(princ "\nNo polylines selected.")
)
(setvar 'cmdecho 1) ; Turn command echo back on
;; End undo mark
(vla-endundomark acadDocument)
(princ)
)
Solved! Go to Solution.