Author Topic: Convert closed lwpolyline to open lwpolyline  (Read 260 times)

0 Members and 1 Guest are viewing this topic.

hmspe

  • Bull Frog
  • Posts: 333
Convert closed lwpolyline to open lwpolyline
« on: February 05, 2019, 10:31:02 AM »
I'm looking for a function to open a closed lwpolyline at the start point without removing the closing segment.  I thought I'd ask if anyone has a function they could share before I reinvent the wheel.  I have a trim function that uses the BREAK command that misbehaves when trimming  closed lwpolylines if the part to be trimed includes the start point.  Open lwpolylines trim correctly.



"Science is the belief in the ignorance of experts." - Richard Feynman

ribarm

  • Water Moccasin
  • Posts: 1969
  • Marko Ribar, architect
Re: Convert closed lwpolyline to open lwpolyline
« Reply #1 on: February 05, 2019, 10:56:26 AM »
Untested, but I think it should work...

Code: [Select]
(defun c:openclosedlwatstartseg ( / s lw lwx )
  (while
    (or
      (prompt "\nPick closed lwpolyline...")
      (not (setq s (ssget "_.+:E:S:L" '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1)))))
    )
    (prompt "\nMissed...")
  )
  (setq lw (ssname s 0))
  (setq lwx (entget lw))
  (setq lwx (subst (cons 90 (1- (cdr (assoc 90 lwx)))) (assoc 90 lwx) lwx))
  (setq lwx (subst (cons 70 (1- (cdr (assoc 70 lwx)))) (assoc 70 lwx) lwx))
  (setq lwx (vl-remove (assoc 10 lwx) lwx))
  (entupd (cdr (assoc -1 (entmod lwx))))
  (princ)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Water Moccasin
  • Posts: 1969
  • Marko Ribar, architect
Re: Convert closed lwpolyline to open lwpolyline
« Reply #2 on: February 05, 2019, 11:19:40 AM »
Sorry, I had a blunder...

Code: [Select]
(defun c:openclosedlwstartseg ( / chiv s lw lwx )

  (defun chiv ( e m / ed edd eddd eddd1 eddd2 eddd3 newed n i ) ; args e - ename of lwpolyline, m - parameter of initial vertex ;;; ( e p / ed edd eddd eddd1 eddd2 eddd3 newed m n i ) ; args e - ename of lwpolyline, p - new initial vertex point in WCS

    (vl-load-com)

    (setq ed (entget e))
    (setq edd nil)
    (foreach ec ed
      (if (not
            (or (eq (car ec) 10) (eq (car ec) 40) (eq (car ec) 41) (eq (car ec) 42) (eq (car ec) 91) (eq (car ec) 210))
          )
          (setq edd (cons ec edd))
      )
    )
    (setq edd (reverse edd))
    (setq eddd nil)
    (setq eddd1 nil)
    (setq eddd2 nil)
    (setq eddd (member (assoc 10 ed) ed))
    ;;;(setq p (getpoint "\nPick vertex you want to become initial"))
    ;;;(setq m (vlax-curve-getparamatpoint e (vlax-curve-getclosestpointto e p)))
    (if (assoc 91 ed) (setq n (* m 5)) (setq n (* m 4)))
    (setq i 0)
    (foreach ec eddd
      (progn
        (setq i (+ i 1))
        (if (<= i n)
          (setq eddd1 (cons ec eddd1))
        )
        (if (> i n)
          (setq eddd2 (cons ec eddd2))
        )
      )
    )
    (setq eddd1 (reverse eddd1))
    (setq eddd3 (list (assoc 210 eddd2)))
    (setq eddd2 (cdr eddd2))
    (setq eddd2 (reverse eddd2))
    (setq newed (append edd eddd2 eddd1 eddd3))
    (entmod newed)
    (entupd e)
  )

  (while
    (or
      (prompt "\nPick closed lwpolyline on unlocked layer...")
      (not (setq s (ssget "_+.:E:S:L" '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1)))))
    )
    (prompt "\nMissed...")
  )
  (setq lw (ssname s 0))
  (chiv lw 1.0)
  (setq lwx (entget lw))
  (setq lwx (subst (cons 70 (1- (cdr (assoc 70 lwx)))) (assoc 70 lwx) lwx))
  (entupd (cdr (assoc -1 (entmod lwx))))
  (princ)
)

HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12151
  • London, England
Re: Convert closed lwpolyline to open lwpolyline
« Reply #3 on: February 05, 2019, 12:48:37 PM »
Something like this should suffice -
Code - Auto/Visual Lisp: [Select]
  1. (defun c:openply ( / i s x )
  2.    (if (setq s (ssget "_:L" '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1))))
  3.        (repeat (setq i (sslength s))
  4.            (setq i (1- i)
  5.                  x (entget (ssname s i))
  6.                  x (subst (cons 90 (1+ (cdr (assoc 90 x)))) (assoc 90 x) x)
  7.                  x (subst (cons 70 (logand (cdr (assoc 70 x)) (~ 1))) (assoc 70 x) x)
  8.            )
  9.            (entmod
  10.                (append
  11.                    (reverse (cdr (reverse x)))
  12.                    (mapcar '(lambda ( k ) (assoc k x)) '(10 40 41 42 91))
  13.                    (list (assoc 210 x))
  14.                )
  15.            )  
  16.        )
  17.    )
  18.    (princ)
  19. )

Lee Mac

  • Seagull
  • Posts: 12151
  • London, England
Re: Convert closed lwpolyline to open lwpolyline
« Reply #4 on: February 05, 2019, 03:02:11 PM »
@Lee: Strange: your code creates a new entity list with 2 gc 91 items, and yet it does work.

Sorry Roy, I'm not sure what you mean - in AutoCAD, each LWPolyline vertex has entries:
Code - Auto/Visual Lisp: [Select]
  1. (10 x y)
  2. (40 . width)
  3. (41 . width)
  4. (42 . bulge)
  5. (91 . 0)

roy_043

  • Water Moccasin
  • Posts: 1655
  • BricsCAD 18
Re: Convert closed lwpolyline to open lwpolyline
« Reply #5 on: February 05, 2019, 03:16:54 PM »
Sorry, I realized my mistake and erased my message while you were answering. But I must admit I am confused by the gc 91 items. They should be vertex identifiers, but their value is always(?) zero.

Lee Mac

  • Seagull
  • Posts: 12151
  • London, England
Re: Convert closed lwpolyline to open lwpolyline
« Reply #6 on: February 05, 2019, 03:59:05 PM »
Sorry, I realized my mistake and erased my message while you were answering.

No worries my friend.

But I must admit I am confused by the gc 91 items. They should be vertex identifiers, but their value is always(?) zero.

Indeed, as far as I'm aware they are completely redundant and hence I could have omitted that group code in the append expression - I think this is perhaps an unfinished aspect of the LWPOLYLINE implementation in AutoCAD when originally developed.

hmspe

  • Bull Frog
  • Posts: 333
Re: Convert closed lwpolyline to open lwpolyline
« Reply #7 on: February 05, 2019, 04:07:27 PM »
Thanks for the help. 
"Science is the belief in the ignorance of experts." - Richard Feynman