Author Topic: LWPOLY to SPLINE  (Read 2813 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
LWPOLY to SPLINE
« on: May 30, 2013, 08:21:09 AM »
I am really thin when splines are to be constructed and I really want this to come to reality... I want to convert LWPOLY to SPLINE entity with as less as possible difference to original... As LWPOLY is actually 2nd degree curve, I want that created SPLINE be also 2nd degree curve... I want that straight segments be also straight and arcs segments also arcs but spline segments...

All this is because, I have set of lisps for creating lattice structures by using revsurf command applied to 1st and 2nd layer curve (works with SPLINES - but not with LWPOLYS) and routine will entmake set of lattice lines between these 2 layers upper and lower struts... I'll attach one example (mero-revsurf.lsp) and mero-revsurf.dwg with LWPOLYS as layers... But as they are LWPOLYS, CAD will mess with vertexes when surfaces are exploded and vertices are obtained... As SPLINE objects have parameters equally set along curve from start to end parameter, somehow when CAD constructs surfaces with SPLINES as generic curves, 3dfaces on revolved surfaces are exactly created with correct order of vertices for creating lattice structures...

So I am looking for entmaking SPLINE 2nd degree curve based on LWPOLY data...

For more info there is also youtube video with my routines for those that are interested...
Video - click here

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

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: LWPOLY to SPLINE
« Reply #1 on: May 30, 2013, 12:35:18 PM »
why don't you want to use standard acad commands?

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: LWPOLY to SPLINE
« Reply #2 on: May 30, 2013, 01:32:12 PM »
I can use standard commands, but I don't know how to convert straight LINE entities and ARC entities to SPLINE entities... For ARC I think, I can do it - there is a code I wrote for ELLIPSE and ELLIPTIC ARCS and also there is code I wrote to convert ARC, CIRCLE entities to ELLIPSE entities, so I can use them combined... But I don't know how to convert LINE entities to SPLINE entities... Finally if I solve this for LINES, I can explode LWPOLYLINE and convert all entities (LINES + ARCS) to SPLINE entities and finally use SPLINEDIT -> JOIN...

So my question is actually how to convert LINE to SPLINE... There must be something for such simple task...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: LWPOLY to SPLINE
« Reply #3 on: May 30, 2013, 03:27:34 PM »
I figured this one...

This is my discovery :

((-1 . <Entity name: 7ef034e0>) (0 . "SPLINE") (330 . <Entity
name: 7ef01cf8>) (5 . "184") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 .
"0") (100 . "AcDbSpline") (210 0.0 0.0 1.0) (70 . 24) (71 . 1) (72 . 4) (73 .
2) (74 . 0) (42 . 1.0e-010) (43 . 1.0e-010) (40 . 0.0) (40 . 0.0) (40 . 10.0)
(40 . 10.0) (10 0.0 0.0 0.0) (10 10.0 0.0 0.0))

SPLINE from vertex (0.0 0.0 0.0) to vertex (10.0 0.0 0.0) - curve of 1st degree...

And for ARCs - I've did that earlier here - ellipse2spline and here - circle+arcs2ellipse

So, can someone help me to automatize this process for LWPOLY and method described above in my previous post...

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

:)

M.R. on Youtube

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: LWPOLY to SPLINE
« Reply #4 on: May 31, 2013, 04:59:34 AM »
This may do the trick:
(ucs = w)
 
Code: [Select]
(defun c:test ( / el ss obj p1 sp e1 e2)
    ;Gian Paolo Cattaneo - 31.05.2013
    (if (> (atof (getvar 'acadver)) 18.0)
        (if
            (and
                (setq el (entlast))
                (setq ss (ssadd))
                (setq obj (vlax-ename->vla-object (car (entsel "\nSelect object: "))))
                (setq p1 (vlax-curve-getStartPoint obj))
                (vl-position
                    (vlax-get obj 'ObjectName)
                    '("AcDbPolyline" "AcDb2dPolyline" "AcDb3dPolyline" "AcDbArc" "AcDbLine")
                )
                (not (vlax-curve-isClosed obj))
                (setq sp
                     (entmakex
                         (list
                             '(0 . "SPLINE")
                             '(100 . "AcDbEntity")
                             '(100 . "AcDbSpline")
                             '(70 . 
                             '(71 . 3)
                             '(74 . 2)
                             '(11 0.0 0.0 0.0)
                             (cons 11 p1)
                         )
                     )
                )
                (vl-cmdf "_join" sp (vlax-vla-object->ename obj) "")
             )
             (progn
                 (vl-cmdf "_break" "_L" "_non" p1 "_non" p1)
                 (while el
                    (if (setq el (entnext el)) (ssadd el ss))     
                 )
                 (setq e1 (ssname ss 0)
                       e2 (ssname ss 1))
                 (if (equal
                         (vlax-curve-getDistAtParam (vlax-ename->vla-object e1)
                             (vlax-curve-getEndParam (vlax-ename->vla-object e1)))
                         (distance '(0.0 0.0 0.0) p1) 1e-5
                     )
                     (entdel e1)
                     (entdel e2)
                 )
            )
        )
        (alert "This tool requires AutoCAD 2011 or higher")
    )
    (princ)
)

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: LWPOLY to SPLINE
« Reply #5 on: May 31, 2013, 06:56:42 AM »
Thanks GP, I solved it with help of Stefan's code for ARCs...

It works fine...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:lwpoly2spl ( / *error* arc2spl line2spl loop pl e s ss )
  2.  
  3.  
  4.   (defun *error* ( msg )
  5.   )
  6.  
  7.   (defun arc2spl ( e / make_spline points q1 q2 a pc f pe ps w )
  8.  
  9.            (setq q1 (vlax-curve-GetStartParam e)
  10.                  q2 (vlax-curve-GetEndParam e)
  11.                  a  (/ (- (vlax-curve-GetEndParam e) (vlax-curve-GetStartParam e)) 3.0) ; a - parameter interval... and angle
  12.                  pc (mapcar                              ; pc - points on contur
  13.                       (function
  14.                         (lambda (p)
  15.                          (vlax-curve-GetPointAtParam e p)
  16.                           )
  17.                         )
  18.                       (list q1 (+ q1 a) (- q2 a) q2)
  19.                     )
  20.                  f  (mapcar                               ; f - first deriv on pc
  21.                       (function
  22.                         (lambda (p)
  23.                           (vlax-curve-GetFirstDeriv e p)
  24.                           )
  25.                         )
  26.                       (list q1 (+ q1 a) (- q2 a) q2)
  27.                     )
  28.                  pe (mapcar                              ; pe - extra control points for spline construction
  29.                       (function
  30.                         (lambda (p1 p2 d1 d2)
  31.                           (inters p1 (mapcar '+ p1 d1)
  32.                                   p2 (mapcar '+ p2 d2)
  33.                                   nil
  34.                                   )
  35.                         )
  36.                       )
  37.                      pc (cdr pc) f (cdr f)
  38.                     )
  39.                  ps  (list (car pc) (car pe) (cadr pc) (cadr pe) (caddr pc) (caddr pe) (cadddr pc)) ; ps - control points for spline
  40.                  w   (list 1.0 (cos (/ a 2)) 1.0 (cos (/ a 2)) 1.0 (cos (/ a 2)) 1.0)  ; weights for spline
  41.            )
  42.  
  43.     (defun make_spline ( pts )
  44.       (entmakex
  45.         (append
  46.            '((0 . "SPLINE") (100 . "AcDbEntity") (100 . "AcDbSpline")
  47.               (70 . 4) (71 . 2) (72 . 10) (73 . 7) (74 . 0)
  48.               (42 . 1.0e-010) (43 . 1.0e-010)
  49.               (40 . 0.0) (40 . 0.0) (40 . 0.0) (40 . 1.0) (40 . 1.0)
  50.               (40 . 2.0) (40 . 2.0) (40 . 3.0) (40 . 3.0) (40 . 3.0))
  51.            pts
  52.         )
  53.       )
  54.     )
  55.    
  56.     (defun points ( p w )
  57.       (apply 'append (mapcar '(lambda (a b) (list (cons 10 a) (cons 41 b))) p w))
  58.     )
  59.    
  60.     (entdel e)
  61.     (make_spline (points ps w))
  62.    
  63.   )
  64.  
  65.   (defun line2spl ( e / sp ep d )
  66.    
  67.     (setq sp (cdr (assoc 10 (entget e)))
  68.           ep (cdr (assoc 11 (entget e)))
  69.           d (distance sp ep)
  70.     )
  71.    
  72.     (entdel e)
  73.    
  74.     (entmakex
  75.       (list
  76.         '(0 . "SPLINE") '(100 . "AcDbEntity") '(100 . "AcDbSpline") '(210 0.0 0.0 1.0) '(71 . 1) '(73 . 2)
  77.         '(42 . 1.0e-010) '(43 . 1.0e-010) '(40 . 0.0) '(40 . 0.0) (cons 40 d) (cons 40 d) (cons 10 sp) (cons 10 ep)
  78.       )
  79.     )
  80.    
  81.   )
  82.  
  83.   (setq loop T)
  84.   (while loop
  85.     (setq pl (car (entsel "\nPick LWPOLYLINE to convert it to SPLINE")))
  86.     (if (eq (cdr (assoc 0 (entget pl))) "LWPOLYLINE") (setq loop nil))
  87.   )
  88.   (setq e (entlast))
  89.   (command "_.explode" pl "")
  90.   (setq ss (ssadd))
  91.   (while (setq e (entnext e))
  92.     (if (eq (cdr (assoc 0 (entget e))) "LINE")
  93.       (progn
  94.         (setq s (line2spl e))
  95.         (ssadd s ss)
  96.       )
  97.     )
  98.     (if (eq (cdr (assoc 0 (entget e))) "ARC")
  99.       (progn
  100.         (setq s (arc2spl e))
  101.         (ssadd s ss)
  102.       )
  103.     )
  104.   )
  105.   (command "_.splinedit" (ssname ss 0) "j" ss "" "")
  106.   (*error* nil)
  107.   (princ)
  108. )
  109.  

I had to sleep - it was late yesterday, so now is all OK.

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

:)

M.R. on Youtube