Author Topic: Convert pline segment to pline with arcs  (Read 1779 times)

0 Members and 1 Guest are viewing this topic.

dgpuertas

  • Newt
  • Posts: 87
Convert pline segment to pline with arcs
« on: November 13, 2023, 06:31:17 AM »
Anyone knows code to convert a polyline by segment to a polyline with arcs and delete vertex?




ribarm

  • Gator
  • Posts: 3328
  • Marko Ribar, architect
Re: Convert pline segment to pline with arcs
« Reply #1 on: November 13, 2023, 04:13:16 PM »
Green one I can convert to simplified arced LWPOLYLINE, but red one which happen to be your case I can't... All I could think of is that you should find the way to overdraw with command PLINE and perhaps do it manually (automation I don't know how to achieve) - so this is challenge...

Look in attached *.DWG to see what I am talking about...
Regards...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

xdcad

  • Swamp Rat
  • Posts: 527
Re: Convert pline segment to pline with arcs
« Reply #2 on: November 19, 2023, 04:45:07 PM »
Anyone knows code to convert a polyline by segment to a polyline with arcs and delete vertex?

There is no DWG picture,
I don’t know if your lines are already LWPOLYLINE.
You have to do this job in two steps
1. First fit those polylines into arcs
2. The arc is combined with other LINEs to generate LWPOLYLINE

The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

xdcad

  • Swamp Rat
  • Posts: 527
Re: Convert pline segment to pline with arcs
« Reply #3 on: November 19, 2023, 06:55:15 PM »
Code - Auto/Visual Lisp: [Select]
  1. ;|
  2. Function name: XD::Pnts:InOtherPnts
  3. Calling format: (XD::Pnts:InOtherPnts pts1 pts2)
  4. Parameter description: pts1 ---------- point table
  5. pts2 ---------- Point table
  6. Return value: point list or nil
  7. Function introduction: Get the points of point table 1 in point table 2
  8. Function source: original
  9. Function author:
  10. Applicable versions: XDRX API
  11. Last updated: 2016-11-21
  12. |;
  13. (defun XD::Pnts:InOtherPnts (pts1 pts2)
  14.    (vl-remove nil
  15.               (mapcar '(lambda (x)
  16.                          (if (xd::pnt:isinside x pts2)
  17.                            x
  18.                            nil
  19.                          )
  20.                        )
  21.                       pts1
  22.               )
  23.    )
  24. )
  25. (defun c:tt ()
  26.   (while
  27.     (and (setq
  28.            e (car
  29.                (xdrx-entsel
  30.                  "\nPick the polyline to be processed into an arc<Exit>:"
  31.                  '((0 . "*polyline"))
  32.                )
  33.              )
  34.          )
  35.          (setq p1 (getpoint "\nPolyline range lower left point <exit>:"))
  36.          (setq
  37.            p2 (getcorner p1 "\nPolyline range upper right point <exit>:")
  38.          )
  39.     )
  40.      (progn
  41.        (setq ln        (xdge::constructor "kLineSeg3d" p1 p2)
  42.              ;;Construct LINE's AcGe geometric object
  43.              box       (xdrx-entity-box ln)
  44.              ;;Entity bounding box
  45.              verts     (xdrx-getpropertyvalue e "vertices")
  46.              ;;Polyline vertex table
  47.              inPnts    (XD::Pnts:InOtherPnts verts box)
  48.              ; Get the points of the point table verts in the point table box
  49.              startPt   (car inpnts)
  50.              endPt     (last inpnts)
  51.              sInx      (xdrx-getpropertyvalue e "NearIndex" startPt)
  52.              ;The index value of the polyline closest to the starting point of the polyline
  53.              prand     (nth (fix (/ (length inpnts) 2.0)) inpnts)
  54.              ;; Randomly pick a point in the polyline
  55.              arc       (xd::pnts:fitarc inpnts)
  56.              ;; The point table fits the circle
  57.              cen       (car arc)
  58.              r         (cadr arc)
  59.              gcirc     (xdge::constructor "kCircArc2d" cen r)
  60.              ; Constructs an AcGe geometric object with a specified center radius
  61.              midPt     (xdge::getpropertyvalue gcirc "closestPointTo" prand)
  62.              ; find the closest point and get the third point
  63.              garc      (xdge::constructor "kCircArc2d" startPt midPt endPt)
  64.              ; The starting point, the middle point, and the end point construct a circular arc geometric object.
  65.              bulge     (xdge::getpropertyvalue garc "bulge")
  66.              ; calculate Bulge
  67.              otherPnts (reverse (cdr (reverse (cdr inpnts))))
  68.              ; Remove the starting point and end point from the polyline, and delete the remaining points
  69.        )
  70.        (mapcar '(lambda (x) (xdrx-setpropertyvalue e "removeVertexAt" x))
  71.                otherpnts
  72.        );;Remove excess points of polyline
  73.        (xdrx-setpropertyvalue e "bulgeat" (list sInx bulge))
  74.        ;;Set the bulge of the starting index point and generate arc segments
  75.      )
  76.   )
  77.   (princ)
  78. )

=============

The above LISP code uses the XDRX-API, which can be downloaded from https://github.com/xdcad/XDrx-API and is updated at any time.

The XDRX API encapsulates AcDb, AcEd, AcGe, AcBr... C++ library, using C++ methods to develop LISP programs.Thousands of Lisp functions are available.
Modify message
« Last Edit: November 19, 2023, 09:38:21 PM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

BIGAL

  • Swamp Rat
  • Posts: 1474
  • 40 + years of using Autocad
Re: Convert pline segment to pline with arcs
« Reply #4 on: November 20, 2023, 01:13:58 AM »
The line segments do not look like a proposed arc is tangential, that is my question. If so then the end points of the 2 straights will move. Adopting one of the mid points as a control.
A man who never made a mistake never made anything

xdcad

  • Swamp Rat
  • Posts: 527
Re: Convert pline segment to pline with arcs
« Reply #5 on: November 20, 2023, 01:48:58 AM »
Green one I can convert to simplified arced LWPOLYLINE, but red one which happen to be your case I can't... All I could think of is that you should find the way to overdraw with command PLINE and perhaps do it manually (automation I don't know how to achieve) - so this is challenge...

Look in attached *.DWG to see what I am talking about...
Regards...



another version:

Code - Auto/Visual Lisp: [Select]
  1. (defun xd::pline:fitarc (e sInx eInx / i inx inpnts pnt startpt
  2.                          endpt arc cen r gcirc midpt garc bulge otherpnts)
  3.   (mapcar 'set '(sInx eInx) (vl-sort (list sInx eInx) '<))
  4.   (setq i 0
  5.         inx sInx
  6.         inpnts nil
  7.   )
  8.   (repeat (1+ (- eInx sInx))
  9.     (setq inx (+ sInx i))
  10.     (setq pnt    (xdrx-getpropertyvalue e "PointAt" inx)
  11.           inpnts (cons pnt inpnts)
  12.           i      (1+ i)
  13.     )
  14.   )
  15.   (setq inpnts  (reverse inpnts)
  16.         startPt (xdrx-getpropertyvalue e "PointAt" sInx)
  17.         endPt   (xdrx-getpropertyvalue e "PointAt" eInx)
  18.         prand     (nth (fix (/ (length inpnts) 2.0)) inpnts)
  19.         ;; Randomly pick a point in the polyline
  20.                 arc       (xd::pnts:fitarc inpnts)
  21.         ;; The point table fits the circle
  22.                 cen       (car arc)
  23.                 r         (cadr arc)
  24.                 gcirc     (xdge::constructor "kCircArc2d" cen r)
  25.         ; Constructs an AcGe geometric object with a specified center radius
  26.                 midPt     (xdge::getpropertyvalue gcirc "closestPointTo" prand)
  27.         ; find the closest point and get the third point
  28.                 garc      (xdge::constructor "kCircArc2d" startPt midPt endPt)
  29.         ; The starting point, the middle point, and the end point construct a circular arc geometric object.
  30.                 bulge     (xdge::getpropertyvalue garc "bulge")
  31.         ; calculate Bulge
  32.                 otherPnts (reverse (cdr (reverse (cdr inpnts))))
  33.         ; Remove the starting point and end point from the polyline, and delete the remaining points
  34.   )
  35.   (xdrx-object-release garc gcirc);;free Ge Object Memory
  36.   (list sInx eInx bulge otherPnts)
  37. )
  38. (defun c:tt ()
  39.   (if
  40.     (and (xdrx-sysvar-push '("osmode" 512))
  41.          (setq
  42.            e (car
  43.                (xdrx-entsel
  44.                  "\nPick the polyline to be processed into an arc<Exit>:"
  45.                  '((0 . "*polyline"))
  46.                )
  47.              )
  48.          )
  49.     )
  50.      (progn
  51.        (xdrx-begin)
  52.        (while
  53.          (and
  54.            (xdrx-entity-redraw e 1)
  55.            (setq p1 (getpoint "\nClick near the starting Point<exit>:"))
  56.            (setq inx1 (xdrx-getpropertyvalue e "nearindex" p1))
  57.            (setq
  58.              p2 (getpoint p1 "\nClick near the ending Point <exit>:")
  59.            )
  60.            (setq inx2 (xdrx-getpropertyvalue e "nearindex" p2))
  61.          )
  62.           (setq fitinfo   (XD::PLINE:FITARC e inx1 inx2)
  63.                 bulge     (nth 2 fitinfo)
  64.                 otherpnts (nth 3 fitinfo)
  65.                 sInx      (nth 0 fitinfo)
  66.           )
  67.           (mapcar
  68.             '(lambda (x) (xdrx-setpropertyvalue e "removeVertexAt" x))
  69.             otherpnts
  70.           )
  71.           ;;Remove excess points of polyline
  72.           (xdrx-setpropertyvalue e "bulgeat" (list sInx bulge))
  73.           ;;Set the bulge of the starting index point and generate arc segments
  74.        )
  75.        (xdrx-end)
  76.      )
  77.   )
  78.   (xdrx-entity-redraw e t)
  79.   (xdrx-sysvar-pop)
  80.   (princ)
  81. )

=============

The above LISP code uses the XDRX-API, which can be downloaded from https://github.com/xdcad/XDrx-API and is updated at any time.

The XDRX API encapsulates AcDb, AcEd, AcGe, AcBr... C++ library, using C++ methods to develop LISP programs.Thousands of Lisp functions are available.
Modify message
« Last Edit: November 20, 2023, 02:19:34 AM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

xdcad

  • Swamp Rat
  • Posts: 527
Re: Convert pline segment to pline with arcs
« Reply #6 on: November 20, 2023, 02:21:26 AM »
The line segments do not look like a proposed arc is tangential, that is my question. If so then the end points of the 2 straights will move. Adopting one of the mid points as a control.

After fitting the arc, and add the FILLET code
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

xdcad

  • Swamp Rat
  • Posts: 527
Re: Convert pline segment to pline with arcs
« Reply #7 on: November 20, 2023, 06:10:43 AM »
The line segments do not look like a proposed arc is tangential, that is my question. If so then the end points of the 2 straights will move. Adopting one of the mid points as a control.

Actually,
In actual work, there is a small probability that random fold lines like the original poster will appear.

Generally, it is as shown in the figure below. For some reasons, the arc segment becomes many small segments. Such a figure can be fitted with an accuracy of 99.999%, which means there is no need to deal with tangency. Condition

The following picture can also be processed fully automatically

« Last Edit: November 20, 2023, 06:15:27 AM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

xdcad

  • Swamp Rat
  • Posts: 527
Re: Convert pline segment to pline with arcs
« Reply #8 on: November 20, 2023, 07:53:43 AM »
For some 3D industrial software to generate DWG drawings, arcs may change into polylines.
The following code automatically selects multiple items for batch processing.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt ()
  2.   (defun _fitarc (e pts / startpt endpt   prand arc cen r
  3.                           gcirc   midpt   garc    bulge   otherpnts
  4.                  )
  5.     (setq startPt   (car pts)
  6.           endPt     (last pts)
  7.           prand     (nth (fix (/ (length pts) 2.0)) pts)
  8.           ;; Randomly pick a point in the polyline
  9.           arc       (xd::pnts:fitarc pts)
  10.           ;; The point table fits the circle
  11.           cen       (car arc)
  12.           r         (cadr arc)
  13.           gcirc     (xdge::constructor "kCircArc2d" cen r)
  14.           ; Constructs an AcGe geometric object with a specified center radius
  15.           midPt     (xdge::getpropertyvalue gcirc "closestPointTo" prand)
  16.           ; find the closest point and get the third point
  17.           garc      (xdge::constructor "kCircArc2d" startPt midPt endPt)
  18.           ; The starting point, the middle point, and the end point construct a circular arc geometric object.
  19.           bulge     (xdge::getpropertyvalue garc "bulge")
  20.           ; calculate Bulge
  21.           otherPnts (reverse (cdr (reverse (cdr pts))))
  22.           ; Remove the starting point and end point from the polyline, and delete the remaining points
  23.     )
  24.     (xdrx-object-release garc gcirc)
  25.     ;;free Ge Object Memory
  26.     (list bulge otherPnts)
  27.   )
  28.   (defun _process (e / numverts inx pts set len pnt info bulge otherpnts)
  29.     (xdrx-prompt "\nPolyLine " index " :")
  30.     (xdrx-prompt
  31.       (xdrx-string-format       "\n  Before Area: %.4f"
  32.         (xdrx-getpropertyvalue e "area")
  33.       )
  34.     )
  35.     (setq index (1+ index))
  36.     (setq numVerts (xdrx-getpropertyvalue e "numverts"))
  37.     (setq inx 0
  38.           pts nil
  39.     )
  40.     (repeat (1- numverts)
  41.       (setq seg (xdrx-getpropertyvalue e "segat" inx)
  42.       ;|The AcGe geometry object of the segment starting at the specified
  43.         index value position, which may be: kLineSeg3d or kCircArc3d|;
  44.             len (xdrx-getpropertyvalue seg "length")
  45.             pnt (xdrx-getpropertyvalue e "pointat" inx)
  46.       )
  47.       (xdrx-object-release seg)
  48.       (if (< len tol)
  49.         (progn
  50.           (if (not pts)
  51.             (setq sInx inx)
  52.           )
  53.           (setq pts (cons pnt pts))
  54.         )
  55.         (progn
  56.           (if (> (length pts) 3)
  57.             (progn
  58.               (setq pts
  59.                      (reverse
  60.                        (cons (xdrx-getpropertyvalue e "pointat" inx)
  61.                              pts
  62.                        )
  63.                      )
  64.               )
  65.               (setq info      (_fitarc e pts)
  66.                     bulge     (car info)
  67.                     otherpnts (cadr info)
  68.               )
  69.               (mapcar
  70.                 '(lambda (x)
  71.                    (xdrx-setpropertyvalue e "removeVertexAt" x)
  72.                  )
  73.                 otherpnts
  74.               )
  75.               ;;Remove excess points of polyline
  76.               (xdrx-setpropertyvalue e "bulgeat" (list sInx bulge))
  77.               (xdrx-prompt
  78.                 (xdrx-string-format
  79.                   "\n  After  Area: %.4f"
  80.                   (xdrx-getpropertyvalue e "area")
  81.                 )
  82.                 "\n========================"
  83.               )
  84.               (setq pts nil)
  85.             )
  86.           )
  87.         )
  88.       )
  89.       (setq inx (1+ inx))
  90.     )
  91.     (princ)
  92.   )
  93.   ;;Main =====================================
  94.   (if (not #tol)
  95.     (setq #tol 10.0)
  96.   )
  97.   (setq index 1)
  98.   (if (and
  99.         (or (setq
  100.               tol (getreal
  101.                     (xdrx-prompt "\nEnter tolerance value<" #tol ">:" t)
  102.                   )
  103.             )
  104.             (setq tol #tol)
  105.         )
  106.         (setq ss (xdrx-ssget
  107.                    "\nSelect polylines to Process <Exit>:"
  108.                    '((0 . "*polyline"))
  109.                  )
  110.         )
  111.       )
  112.     (progn
  113.       (xdrx-begin)
  114.       (setq #tol tol)
  115.       (xdrx-prompt "\n========================")
  116.       (mapcar '(lambda (x)
  117.                  (_process x)
  118.                )
  119.               (xdrx-pickset->ents ss)
  120.       )
  121.       (xdrx-entity-redraw ss 1)
  122.       (xdrx-end)
  123.     )
  124.   )
  125.   (princ)
  126. )

=============

The above LISP code uses the XDRX-API, which can be downloaded from https://github.com/xdcad/XDrx-API and is updated at any time.

The XDRX API encapsulates AcDb, AcEd, AcGe, AcBr... C++ library, using C++ methods to develop LISP programs.Thousands of Lisp functions are available.
Modify message
« Last Edit: November 20, 2023, 08:01:24 AM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net