Author Topic: lwpoly23dpoly - 3dpoly2lwpoly.lsp  (Read 6445 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
lwpoly23dpoly - 3dpoly2lwpoly.lsp
« on: September 14, 2012, 10:15:36 AM »
According to my tests this should work, please inform me if something's wrong :

Code - Auto/Visual Lisp: [Select]
  1. ; transptucs & transptwcs by M.R. (Marko Ribar, d.i.a.)
  2. ; arguments :
  3. ; pt - point to be transformed from WCS to imaginary UCS with transptucs and from imaginary UCS to WCS with transptwcs
  4. ; pt1 - origin of imaginary UCS
  5. ; pt2 - point to define X axis of imaginary UCS (vector pt1-pt2 represents X axis)
  6. ; pt3 - point to define Y axis of imaginary UCS (vector pt1-pt3 represents Y axis)
  7. ; important note : angle between X and Y axises of imaginary UCS must always be 90 degree for correct transformation calculation
  8.  
  9. ;; Unit Vector - M.R.
  10. ;; Args: v - vector in R^n
  11.  
  12. (defun unit ( v / d )
  13.   (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-6))
  14.     (mapcar '(lambda ( x ) (/ x d)) v)
  15.   )
  16. )
  17.  
  18. ;; Matrix x Vector - Vladimir Nesterovsky
  19. ;; Args: m - nxn matrix, v - vector in R^n
  20.  
  21. (defun mxv ( m v )
  22.   (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  23. )
  24.  
  25. ;; Vector Cross Product - Lee Mac
  26. ;; Args: u,v - vectors in R^3
  27.  
  28. (defun v^v ( u v )
  29.   (list
  30.     (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
  31.     (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))
  32.     (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))
  33.   )
  34. )
  35.  
  36. (defun transptucs ( pt p1 p2 p3 / ux uy uz )
  37.   (setq uz (unit (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))))
  38.   (setq ux (unit (mapcar '- p2 p1)))
  39.   (setq uy (unit (mapcar '- p3 p1)))
  40.  
  41.   (mxv (list ux uy uz) (mapcar '- pt p1))
  42. )
  43.  
  44. (defun transptwcs ( pt pt1 pt2 pt3 / pt1n pt2n pt3n )
  45.   (setq pt1n (transptucs '(0.0 0.0 0.0) pt1 pt2 pt3))
  46.   (setq pt2n (transptucs '(1.0 0.0 0.0) pt1 pt2 pt3))
  47.   (setq pt3n (transptucs '(0.0 1.0 0.0) pt1 pt2 pt3))
  48.   (transptucs pt pt1n pt2n pt3n)
  49. )
  50.  
  51. (defun entmakelwpoly3dpts ( ptlst 70dxfflag / ux uy uz uptlst )
  52.   (setq uz (unit (v^v (mapcar '- (cadr ptlst) (car ptlst)) (mapcar '- (caddr ptlst) (car ptlst)))))
  53.   (if (equal uz '(0.0 0.0 1.0) 1e-8) (setq ux '(1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
  54.   (if (equal uz '(0.0 0.0 -1.0) 1e-8) (setq ux '(-1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
  55.   (if (not (or (equal uz '(0.0 0.0 1.0) 1e-8) (equal uz '(0.0 0.0 -1.0) 1e-8))) (setq ux (unit (v^v '(0.0 0.0 1.0) uz))))
  56.   (if (not uy) (setq uy (unit (v^v uz ux))))
  57.   (setq uptlst (mapcar '(lambda ( p ) (transptucs p '(0.0 0.0 0.0) ux uy)) ptlst))
  58.   (entmake
  59.     (append
  60.       (list
  61.         '(0 . "LWPOLYLINE")
  62.         '(100 . "AcDbEntity")
  63.         '(100 . "AcDbPolyline")
  64.         (cons 90 (length uptlst))
  65.         (cons 70 70dxfflag)
  66.         '(62 . 3)
  67.         (cons 38 (caddar uptlst))
  68.       )
  69.       (mapcar '(lambda ( x ) (list 10 (car x) (cadr x))) uptlst)
  70.       (list (cons 210 uz))
  71.     )
  72.   )
  73.   (princ)
  74. )
  75.  
  76. (defun c:lwpoly23dpoly ( / lwpol lwdxf lwptl lwel ux uy uz ptlst )
  77.   (setq lwpol (car (entsel "\nPick lwpolyline to convert to 3dpolyline")))
  78.   (if (and lwpol (= (cdr (assoc 0 (setq lwdxf (entget lwpol)))) "LWPOLYLINE"))
  79.     (progn
  80.       (setq lwptl (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) lwdxf))
  81.       (setq lwptl (mapcar '(lambda ( x ) (cdr x)) lwptl))
  82.       (setq lwel (cdr (assoc 38 lwdxf)))
  83.       (setq lwptl (mapcar '(lambda ( x ) (list (car x) (cadr x) lwel)) lwptl))
  84.       (setq uz (cdr (assoc 210 lwdxf)))
  85.       (if (equal uz '(0.0 0.0 1.0) 1e-8) (setq ux '(1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
  86.       (if (equal uz '(0.0 0.0 -1.0) 1e-8) (setq ux '(-1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
  87.       (if (not (or (equal uz '(0.0 0.0 1.0) 1e-8) (equal uz '(0.0 0.0 -1.0) 1e-8))) (setq ux (unit (v^v '(0.0 0.0 1.0) uz))))
  88.       (if (not uy) (setq uy (unit (v^v uz ux))))
  89.       (setq ptlst (mapcar '(lambda ( p ) (transptwcs p '(0.0 0.0 0.0) ux uy)) lwptl))
  90.       (entmake
  91.         (list
  92.           '(0 . "POLYLINE")
  93.           '(100 . "AcDbEntity")
  94.           '(100 . "AcDb3dPolyline")
  95.           '(66 . 1)
  96.           '(62 . 1)
  97.           '(10 0.0 0.0 0.0)
  98.           (cons 70 (+ 8 (cdr (assoc 70 lwdxf))))
  99.           '(210 0.0 0.0 1.0)
  100.         )
  101.       )
  102.       (foreach pt ptlst
  103.         (entmake
  104.           (list
  105.             '(0 . "VERTEX")
  106.             '(100 . "AcDbEntity")
  107.             '(100 . "AcDbVertex")
  108.             '(100 . "AcDb3dPolylineVertex")
  109.             (cons 10 pt)
  110.             '(70 . 32)
  111.           )
  112.         )
  113.       )
  114.       (entmake
  115.         (list
  116.           '(0 . "SEQEND")
  117.           '(100 . "AcDbEntity")
  118.         )
  119.       )
  120.     )
  121.     (prompt "\nNo lwpolyline picked")
  122.   )
  123.   (princ)
  124. )
  125.  
  126. (defun c:3dpoly2lwpoly ( / pol vert pt ptlst )
  127.   (setq pol (car (entsel "\nPick 3dpolyline to convert to lwpolyline")))
  128.   (if (and pol (= (cdr (assoc 100 (cdr (member (assoc 100 (entget pol)) (entget pol))))) "AcDb3dPolyline"))
  129.     (progn
  130.       (setq vert (entnext pol))
  131.       (while (= (cdr (assoc 0 (entget vert))) "VERTEX")
  132.         (setq pt (cdr (assoc 10 (entget vert))))
  133.         (setq ptlst (cons pt ptlst))
  134.         (setq vert (entnext vert))
  135.       )
  136.       (setq ptlst (reverse ptlst))
  137.       (entmakelwpoly3dpts ptlst (- (cdr (assoc 70 (entget pol))) 8))
  138.     )
  139.     (prompt "\nNo 3dpolyline picked")
  140.   )
  141.   (princ)
  142. )
  143.  
  144. (prompt "\nDefined functions are c:lwpoly23dpoly and c:3dpoly2lwpoly")
  145.  

Regards, M.R.
« Last Edit: March 14, 2024, 10:27:41 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

chlh_jd

  • Guest
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #1 on: March 05, 2014, 11:21:56 AM »
Hi ribarm .
to make 3d lwpolyline , can be simplified .
Code: [Select]
(defun _3dlwpolyline  (l col d70 / vl v1 v2 norm z)
  (setq vl (mapcar (function (lambda (a b) (mapcar (function -) a b))) (cdr l) l))
  (setq v1 (car vl)
vl (cdr vl))
  (while (and vl (not norm))
    (setq v2 (cadr vl)
  vl (cdr vl))
    (if (equal (setq norm (v^v v1 v2)) '(0 0 0) 1e-6)
      (setq norm nil)))
  (if norm
    (progn
      (setq z (caddr (trans (car l) 0 norm))
    l (mapcar
(function (lambda (a)
    (reverse (cdr (reverse (trans a 0 norm))))))
l))
      (entmakex
(append
  (list
    (cons 0 "LWPOLYLINE")
    (cons 100 "AcDbEntity")
    (cons 100 "AcDbPolyline")
    (cons 90 (length l))
    (cons 70 d70)
    (cons 38 z))
  (mapcar (function (lambda (a) (cons 10 a))) l)
  (list (cons 62 col)
(cons 210 norm))
  )))
    )
  )
« Last Edit: March 05, 2014, 11:26:08 AM by chlh_jd »

pedroantonio

  • Guest
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #2 on: May 01, 2014, 03:45:30 AM »
ribarm I use your code and conver 3d polylines to 2d polylines not to lwpolylines (simple polylines).Why ?

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #3 on: May 01, 2014, 04:09:23 AM »
It converts 3dpolylines to lwpolylines - have you tested my code?... Important note : As 3dpolylines have vertices in 3d when converting to lwpolyline - vertices of 3dpolyline are transformed into plane defined by first 3 vertices of 3dpolyline - only that way conversion is possible as lwpolyline is 2d planar entity... But rather than this if someone draw planar 3dpolyline, you can simply set UCS to that plane using "3p" option, and then explode 3dpolyline and then join previous selection to lwpolyline so it'll be 2d polyline and by it's definition - lwpolyline is always planar entity...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

pedroantonio

  • Guest
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #4 on: May 01, 2014, 04:34:11 AM »
I test your code and then i try to export the coordinates of this 2d polyline with this lisp i use all the times.This lisp work  with LWPOLYLINE

Code - Auto/Visual Lisp: [Select]
  1. (defun c:2dpe ()
  2.   (setq sset (ssget '((-4 . "<OR")(0 . "POINT")
  3.                       (0 . "LWPOLYLINE")(-4 . "OR>"))))
  4.   (if sset
  5.     (progn
  6.       (setq itm 0 num (sslength sset))
  7.       (setq fn (getfiled "save to file &#935;,&#933;" "" "txt" 1))
  8.       (if (/= fn nil)
  9.         (progn
  10.           (setq fh (open fn "w"))
  11.           (while (< itm num)
  12.             (setq hnd (ssname sset itm))
  13.             (setq ent (entget hnd))
  14.             (setq obj (cdr (assoc 0 ent)))
  15.             (cond
  16.               ((= obj "POINT")
  17.                 (setq pnt (cdr (assoc 10 ent)))
  18.                 (setq pnt (trans pnt 0 1));;**CAB
  19.                 (princ (strcat (rtos (car pnt) 2 3) ","
  20.                                (rtos (cadr pnt) 2 3)) fh)
  21.                 (princ "\n" fh)
  22.               )
  23.               ((= obj "LWPOLYLINE")
  24.                 (if (= (cdr (assoc 38 ent)) nil)
  25.                   (setq elv 0.0)
  26.                   (setq elv (cdr (assoc 38 ent)))
  27.                 )
  28.                 (foreach rec ent
  29.                   (if (= (car rec) 10)
  30.                     (progn
  31.                       (setq pnt (cdr rec))
  32.                       (setq pnt (trans pnt 0 1));;**CAB
  33.                       (princ (strcat (rtos (car pnt) 2 3) ","
  34.                                      (rtos (cadr pnt) 2 3)) fh)
  35.                       (princ "\n" fh)
  36.                     )
  37.                   )
  38.                 )
  39.               )
  40.               (t nil)
  41.             )
  42.             (setq itm (1+ itm))
  43.           )
  44.           (close fh)
  45.         )
  46.       )
  47.     )
  48.   )
  49.   (princ)
  50. )
  51.  
  52.  

here is my test.dwg

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #5 on: May 01, 2014, 04:46:33 AM »
If you are exporting vertices data from current UCS, than you should use this inside (cond) for LWPOLYLINE :

Code - Auto/Visual Lisp: [Select]
  1.                    (progn
  2.                      (setq pnt (cdr rec))
  3.                      (setq pnt (trans (list (car pnt) (cadr pnt) elv) hnd 1));;**CAB
  4.                      (princ (strcat (rtos (car pnt) 2 3) ","
  5.                                     (rtos (cadr pnt) 2 3)) fh)
  6.                      (princ "\n" fh)
  7.                    )
  8.  

If you are exporting vertices data from WCS, than you should use this inside (cond) for LWPOLYLINE :

Code - Auto/Visual Lisp: [Select]
  1.                    (progn
  2.                      (setq pnt (cdr rec))
  3.                      (setq pnt (trans (list (car pnt) (cadr pnt) elv) hnd 0));;**CAB
  4.                      (princ (strcat (rtos (car pnt) 2 3) ","
  5.                                     (rtos (cadr pnt) 2 3)) ","
  6.                                     (rtos (caddr pnt) 2 3)) fh)
  7.                      (princ "\n" fh)
  8.                    )
  9.  
« Last Edit: May 01, 2014, 04:51:21 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

pedroantonio

  • Guest
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #6 on: May 01, 2014, 05:21:36 AM »
i make this two changes but

Quote
Command: 2DPE
Select objects: 0 found

My lisp work and export coordinates x,y from all my drawings. The problem is only 2d polylines. Is any way to update it to wark for

2d polyline,lwpolyline,polyline

Thanks

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #7 on: May 01, 2014, 05:43:24 AM »
Maybe you are using routine to grab data which isn't designed for that entities... Using routine where you have specified (ssget '((0 . "LWPOLYLINE))) on entity that is 3d polyline or old heavy 2d polyline, will select nothing... Also using routine where you have specified (ssget '((0 . "POLYLINE"))) on entity lwpolyline, will also select nothing... Please use appropriate routine on appropriate entities, or combine your 2 routines into one that will operate on every entity, but you must specify that in appropriate (ssget '((0 . "*POLYLINE"))) filter...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Rosamund

  • Guest
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #8 on: May 02, 2017, 04:58:30 AM »
Hi ribarm

Cant't conv  Arc segment ?

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #9 on: May 02, 2017, 05:03:07 AM »
Hi ribarm

Cant't conv  Arc segment ?

For arced plines, you need to do segmentation firstly and then convert it to 3d poly, if that's what you are looking for... For segmentation search for "PLINTOOLS BY MR+GC+LM.zip" posted at www.cadtutor.net

Here is the link for PLINETOOLS BY MR+GC+LM
http://www.cadtutor.net/forum/showthread.php?67924-Draw-polyline-along-with-2-or-more-adjacent-closed-polylines/page3&p=#25

M.R.
« Last Edit: May 02, 2017, 05:07:35 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

jtm2020hyo

  • Newt
  • Posts: 198
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #10 on: April 04, 2020, 09:35:15 PM »



your link does not works.

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

jtm2020hyo

  • Newt
  • Posts: 198
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #12 on: April 05, 2020, 01:35:57 AM »



your link does not works.

Marks for permalinks have changed at cadtutor...
Try this link instead :
https://www.cadtutor.net/forum/topic/37726-draw-polyline-along-with-2-or-more-adjacent-closed-polylines/?do=findComment&comment=378497

wow, are a lot of lisp, awesome, thanks.

1 exist more lisp routines in the PLINETOOLS in others post?
2 if exist more routines, can you publish an updated post where you attach all your PLINETOOLS
3 PLINETOOLS are the same that PLTOOLS? PLTOOLS is a Russian polyline tool that too is awesome, the problem is that have Russian annotations and need be translated ( https://dwg.ru/dnl/607 )


Peter2

  • Swamp Rat
  • Posts: 650
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #13 on: March 07, 2024, 02:07:24 PM »
@ribarm
According to my tests this should work, please inform me if something's wrong :..
Hi Marco
it's now 12 years ago, but I found your code now and tried it.
With all the transformations and that stuff it is great, but I could create a reproducible crash in Acad Map 2023.
The attached DWG contains only one 3Dpoly and it crashes with a "division / 0" in "defun units .."

I don't understand all the subroutines in detail, but it would be great if you or someone else could find the issue behind it.

Thanks for the tool and have a fine day!
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

mailmaverick

  • Bull Frog
  • Posts: 494
Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
« Reply #14 on: March 10, 2024, 11:21:49 PM »



your link does not works.

Marks for permalinks have changed at cadtutor...
Try this link instead :
https://www.cadtutor.net/forum/topic/37726-draw-polyline-along-with-2-or-more-adjacent-closed-polylines/?do=findComment&comment=378497

Dear Marko, ZIP file is unavailable for download at this link. Could you please share the ZIP file in this thread ?
Thanks.