TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ribarm on September 14, 2012, 10:15:36 AM

Title: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm 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.
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: chlh_jd 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))
  )))
    )
  )
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: pedroantonio 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 ?
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm 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...
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: pedroantonio 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
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm 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.  
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: pedroantonio 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
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm 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...
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: Rosamund on May 02, 2017, 04:58:30 AM
Hi ribarm

Cant't conv  Arc segment ?
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm 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.
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: jtm2020hyo on April 04, 2020, 09:35:15 PM



your link does not works.
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm on April 04, 2020, 10:34:26 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
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: jtm2020hyo 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 )

Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: Peter2 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!
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: mailmaverick 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.
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm on March 11, 2024, 11:24:05 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

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

It is available, you just have to be logged on cadtutor.net with secured method using your e-mail as username... I just downloaded it from there... So, here is it in this attachment...
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: Peter2 on March 13, 2024, 06:14:54 AM
...but I could create a reproducible crash in Acad Map 2023...
Hi
some ideas?
Peter
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm on March 13, 2024, 12:55:22 PM
I don't have Acad Map 2023, so you'll have to debug it on your own... Sorry...
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: kdub_nz on March 13, 2024, 07:18:47 PM
...but I could create a reproducible crash in Acad Map 2023...
Hi
some ideas?
Peter

@Peter
I can load the file in AutoCAD Vanilla, so :

How is the error reproducible ?
What was your workflow ( which of the files are you running ) ?

I think the error may be in a call to the Unit() function [ not "units" as you mention ].

A divide by zero is fairly easy to test for.

Regards,
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: Peter2 on March 14, 2024, 03:30:42 AM
Hi all

- the file contains only a standard 3dpoly, so it should be used in "every" software
- I have the same problem in Bricscad 23
- it works fine on thousands of lines, but stops with this line
- I use the code from the first posting here in this thread
- I call the function "3dpoly2lwpoly"
Code: [Select]
Befehl: 3DPOLY2LWPOLY
Pick 3dpolyline to convert to lwpolyline
; ----- LISP : Call Stack -----
; [0]...C:3DPOLY2LWPOLY
; [1].....ENTMAKELWPOLY3DPTS
; [2].......UNIT <<--
;
; ----- Error around expression -----
; '(0.0 0.0 0.0)
; in file :
; D:\Downloads\x.lsp
;
; error : divide by zero at [/]

(yes, it is in "defun unit.." and not in "defun units ...")
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: kdub_nz on March 14, 2024, 04:54:30 AM

Peter , what you renamed it to means nothing to me.

Which of the files in the  PLINETOOLS BY MR+GC+LM.zip is causing the problem ?
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: Peter2 on March 14, 2024, 07:21:53 AM
Hi kdub_nz
..Which of the files in the  PLINETOOLS BY MR+GC+LM.zip is causing the problem ?
None.
The problem of this thread is that it drifted away from the OT (a code posted in the first posting) to the discussion about the ZIP file.
I used the code here: https://www.theswamp.org/index.php?topic=42773.msg479708#msg479708 - and hoping, that it is still correct.
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm on March 14, 2024, 10:36:25 AM
@Peter2

Look, I've changed slightly (unit) sub function - first from beggining, not to throw "divide by zero" error, but I doubt that error is solved - it should passed and wihtout my intervention... Simply something is not good in the drawing... Like I said first 3 points define plane at which 3dpoly should be projected to lwpoly, and for lwpoly to 3dpoly, there should be no arced segments and it should perform conversion from any plane as lwpoly is planar entity... Are you sure your UCS is WCS before starting routine, if not, set it that way, but I think that that's not the problem...
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm on March 14, 2024, 10:41:02 AM
I just saw : from error report...
You are using 3dpoly2lwpoly...
Why not just exploding 3dpoly and then change from Properties Z=0 (current UCS plane) join previous => lwpolyline, or => 3dpolyline if 3dpoly had one or more not coplanar vertices - you haven't specified Z=0 in UCS...
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm on March 14, 2024, 12:35:39 PM
Another issue :
Maybe your starting 3 vertices from 3dpoly don't define plane, but rather vector direction - they are collinear and coplanarity in it's real terms can't be assumed as unique... You can set infinitely number of planes passing through that vector... So for sure, I'd recommend that you write the code that is behaving like I described in previous post - EXPLODE => change Z=0 in current UCS (whatever orientation you want) and finally JOIN => lwpolyline...
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: ribarm on March 14, 2024, 12:46:18 PM
Just watch out with JOIN command - there is bug...
So here is the link to overcome this issue : https://www.theswamp.org/index.php?topic=55918.0
Title: Re: lwpoly23dpoly - 3dpoly2lwpoly.lsp
Post by: Peter2 on March 14, 2024, 01:42:27 PM
Hi

I think I found the reason for the problem:
- The first and second point have a distance of  0.00224.
- I seems that this difference leads to a (rounded) 0 and to the division by 0
- also the command "list" tells something like "the first 3 points define no plane. No area calculated".

When I remove the second point, both the Lisp and the command "list" work fine.

And just while typing the strings "define no plane" I found the real problem - the weird issue with "dwgunits / not coplanar".
A well know issue for a long time ...
https://www.google.com/search?&q=dwgunits+not+coplanar

Thanks to all for their help and replies, especially to Marco.