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

0 Members and 1 Guest are viewing this topic.

#### ribarm

• Water Moccasin
• Posts: 1682
• 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]
`; transptucs & transptwcs by M.R. (Marko Ribar, d.i.a.); arguments : ; pt - point to be transformed from WCS to imaginary UCS with transptucs and from imaginary UCS to WCS with transptwcs; pt1 - origin of imaginary UCS; pt2 - point to define X axis of imaginary UCS (vector pt1-pt2 represents X axis); pt3 - point to define Y axis of imaginary UCS (vector pt1-pt3 represents Y axis); important note : angle between X and Y axises of imaginary UCS must always be 90 degree for correct transformation calculation ;; Unit Vector - M.R.;; Args: v - vector in R^n (defun unit ( v )  (mapcar '(lambda ( x ) (/ x (distance '(0.0 0.0 0.0) v))) v)) ;; Matrix x Vector - Vladimir Nesterovsky;; Args: m - nxn matrix, v - vector in R^n (defun mxv ( m v )  (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)) ;; Vector Cross Product - Lee Mac;; Args: u,v - vectors in R^3 (defun v^v ( u v )  (list    (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))    (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))    (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))  )) (defun transptucs ( pt p1 p2 p3 / ux uy uz )  (setq uz (unit (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))))  (setq ux (unit (mapcar '- p2 p1)))  (setq uy (unit (mapcar '- p3 p1)))   (mxv (list ux uy uz) (mapcar '- pt p1))) (defun transptwcs ( pt pt1 pt2 pt3 / pt1n pt2n pt3n )  (setq pt1n (transptucs '(0.0 0.0 0.0) pt1 pt2 pt3))  (setq pt2n (transptucs '(1.0 0.0 0.0) pt1 pt2 pt3))  (setq pt3n (transptucs '(0.0 1.0 0.0) pt1 pt2 pt3))  (transptucs pt pt1n pt2n pt3n)) (defun entmakelwpoly3dpts ( ptlst 70dxfflag / ux uy uz uptlst )  (setq uz (unit (v^v (mapcar '- (cadr ptlst) (car ptlst)) (mapcar '- (caddr ptlst) (car ptlst)))))  (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)))  (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)))  (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))))  (if (not uy) (setq uy (unit (v^v uz ux))))  (setq uptlst (mapcar '(lambda ( p ) (transptucs p '(0.0 0.0 0.0) ux uy)) ptlst))  (entmake     (append      (list        '(0 . "LWPOLYLINE")        '(100 . "AcDbEntity")        '(100 . "AcDbPolyline")        (cons 90 (length uptlst))        (cons 70 70dxfflag)        '(62 . 3)        (cons 38 (caddar uptlst))      )      (mapcar '(lambda ( x ) (list 10 (car x) (cadr x))) uptlst)      (list (cons 210 uz))    )  )  (princ)) (defun c:lwpoly23dpoly ( / lwpol lwdxf lwptl lwel ux uy uz ptlst )  (setq lwpol (car (entsel "\nPick lwpolyline to convert to 3dpolyline")))  (if (and lwpol (= (cdr (assoc 0 (setq lwdxf (entget lwpol)))) "LWPOLYLINE"))    (progn      (setq lwptl (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) lwdxf))      (setq lwptl (mapcar '(lambda ( x ) (cdr x)) lwptl))      (setq lwel (cdr (assoc 38 lwdxf)))      (setq lwptl (mapcar '(lambda ( x ) (list (car x) (cadr x) lwel)) lwptl))      (setq uz (cdr (assoc 210 lwdxf)))      (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)))      (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)))      (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))))      (if (not uy) (setq uy (unit (v^v uz ux))))      (setq ptlst (mapcar '(lambda ( p ) (transptwcs p '(0.0 0.0 0.0) ux uy)) lwptl))      (entmake         (list          '(0 . "POLYLINE")          '(100 . "AcDbEntity")          '(100 . "AcDb3dPolyline")          '(66 . 1)          '(62 . 1)          '(10 0.0 0.0 0.0)          (cons 70 (+ 8 (cdr (assoc 70 lwdxf))))          '(210 0.0 0.0 1.0)        )      )      (foreach pt ptlst        (entmake          (list            '(0 . "VERTEX")            '(100 . "AcDbEntity")            '(100 . "AcDbVertex")            '(100 . "AcDb3dPolylineVertex")            (cons 10 pt)            '(70 . 32)          )        )      )      (entmake        (list          '(0 . "SEQEND")          '(100 . "AcDbEntity")        )      )    )    (prompt "\nNo lwpolyline picked")  )  (princ)) (defun c:3dpoly2lwpoly ( / pol vert pt ptlst )  (setq pol (car (entsel "\nPick 3dpolyline to convert to lwpolyline")))  (if (and pol (= (cdr (assoc 100 (cdr (member (assoc 100 (entget pol)) (entget pol))))) "AcDb3dPolyline"))    (progn      (setq vert (entnext pol))      (while (= (cdr (assoc 0 (entget vert))) "VERTEX")        (setq pt (cdr (assoc 10 (entget vert))))        (setq ptlst (cons pt ptlst))        (setq vert (entnext vert))      )      (setq ptlst (reverse ptlst))      (entmakelwpoly3dpts ptlst (- (cdr (assoc 70 (entget pol))) 8))    )    (prompt "\nNo 3dpolyline picked")  )  (princ)) (prompt "\nDefined functions are c:lwpoly23dpoly and c:3dpoly2lwpoly")(princ) `

Regards, M.R.
« Last Edit: September 20, 2013, 04:29:04 pm by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

#### 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 »

#### Topographer

• Bull Frog
• Posts: 491
##### 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

• Water Moccasin
• Posts: 1682
• 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)

#### Topographer

• Bull Frog
• Posts: 491
##### 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]
`(defun c:2dpe ()  (setq sset (ssget '((-4 . "<OR")(0 . "POINT")                      (0 . "LWPOLYLINE")(-4 . "OR>"))))  (if sset    (progn      (setq itm 0 num (sslength sset))      (setq fn (getfiled "save to file &#935;,&#933;" "" "txt" 1))      (if (/= fn nil)        (progn          (setq fh (open fn "w"))          (while (< itm num)            (setq hnd (ssname sset itm))            (setq ent (entget hnd))            (setq obj (cdr (assoc 0 ent)))            (cond              ((= obj "POINT")                (setq pnt (cdr (assoc 10 ent)))                (setq pnt (trans pnt 0 1));;**CAB                (princ (strcat (rtos (car pnt) 2 3) ","                               (rtos (cadr pnt) 2 3)) fh)                (princ "\n" fh)              )              ((= obj "LWPOLYLINE")                (if (= (cdr (assoc 38 ent)) nil)                  (setq elv 0.0)                  (setq elv (cdr (assoc 38 ent)))                )                (foreach rec ent                  (if (= (car rec) 10)                    (progn                      (setq pnt (cdr rec))                      (setq pnt (trans pnt 0 1));;**CAB                      (princ (strcat (rtos (car pnt) 2 3) ","                                     (rtos (cadr pnt) 2 3)) fh)                      (princ "\n" fh)                    )                  )                )              )              (t nil)            )            (setq itm (1+ itm))          )          (close fh)        )      )    )  )  (princ))(princ)  `

here is my test.dwg

#### ribarm

• Water Moccasin
• Posts: 1682
• 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]
`                   (progn                     (setq pnt (cdr rec))                     (setq pnt (trans (list (car pnt) (cadr pnt) elv) hnd 1));;**CAB                     (princ (strcat (rtos (car pnt) 2 3) ","                                    (rtos (cadr pnt) 2 3)) fh)                     (princ "\n" fh)                   ) `

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

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

#### Topographer

• Bull Frog
• Posts: 491
##### 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

• Water Moccasin
• Posts: 1682
• 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)

#### Rosamund

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

Cant't conv  Arc segment ?

#### ribarm

• Water Moccasin
• Posts: 1682
• 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