Converting 2D Polyline to 3D Polyline without losing Object Data

Converting 2D Polyline to 3D Polyline without losing Object Data

Anonymous
Not applicable
5,747 Views
16 Replies
Message 1 of 17

Converting 2D Polyline to 3D Polyline without losing Object Data

Anonymous
Not applicable

Hey,

 

I'm trying to convert some 2D Polylines with object data to 3d polylines without losing that data. There used to be a lisp routine called C3D that would do this but i can't seem to find it in AutoCAD 2022 and it doesn't work when i copy this routine from another source and import it. Does anyone know how to do this or have a varied lisp routine to do it.

 

Thankyou

0 Likes
Accepted solutions (2)
5,748 Views
16 Replies
Replies (16)
Message 2 of 17

CodeDing
Advisor
Advisor

@Anonymous ,

 

Can you post an example drawing with this Object Data?

 

Are you using C3D? Map 3D? Vanilla AutoCAD?

 

That would help if you could answer those.

Best,

~DD

0 Likes
Message 3 of 17

pendean
Community Legend
Community Legend
Can you also define exactly what you mean by "..without losing Object Data..."? Once you change object types like that a lot of things will change too.
0 Likes
Message 4 of 17

Sea-Haven
Mentor
Mentor

it doesn't work, post the lisp.

0 Likes
Message 5 of 17

Anonymous
Not applicable

Example drawing attached with a couple of 2d Polylines. I am running C3D but the object data is added using Map.

The Object Data on the polylines is lost when using grading/polyline utilities/convert 2d to 3d. The C3D lisp routine, also attached, used to do the conversion and maintain the object data.

0 Likes
Message 6 of 17

O_Eckmann
Mentor
Mentor
Accepted solution

Hi, 

 

Your C3D.lsp works fine in my Civil 3D 2022 and OD are keept. To load the lisp, just drag&drop from windows explorer into AutoCAD.

 

Here is file after conversion.

 

Olivier

Olivier Eckmann

EESignature

0 Likes
Message 7 of 17

Anonymous
Not applicable

awesome thanks, it was to do with the volume of lines and the drawing I was using. Copy and paste to original co-ordinates with only a couple of thousand lines at a time worked a treat

0 Likes
Message 8 of 17

CADaSchtroumpf
Advisor
Advisor

C3D does the job but only works on one table.
I therefore offer you a similar function ...
She converts a lwpolyline to 3Dpolyline
If the light polyline has:
- arcs, these are discretized. (1/36 of pi)
- an elevation, this one is used to produce the Z for the 3Dpolyline.
- xdata, these are transferred.
- one or more Object Data tables, they are transferred.
- data of objects containing several records are also transferred.

This function works (normally) also on a classic autocad for xdata if you don't have Map or Civil (of course the OD will not be processed)

 

(vl-load-com)
(defun def_bulg_pl (ls lb flag_closed / ls lb rad a l_new)
  (if (not (zerop flag_closed)) (setq ls (append ls (list (car ls)))))
  (while (cadr ls)
    (if (zerop (car lb))
      (setq l_new (append l_new (list (car ls))))
      (progn
        (setq
          rad (/ (distance (car ls) (cadr ls)) (sin (* 2.0 (atan (abs (car lb))))) 2.0)
          a (- (/ pi 2.0) (- pi (* 2.0 (atan (abs (car lb))))))
        )
        (if (< a 0.0) (setq a (- (* 2.0 pi) a)))
        (if (or (and (< (car lb) 0.0) (> (car lb) -1.0)) (> (car lb) 1.0))
          (setq l_new (append l_new (reverse (cdr (reverse (bulge_pts (polar (car ls) (- (angle (car ls) (cadr ls)) a) rad) (car ls) (cadr ls) rad (car lb)))))))
          (setq l_new (append l_new (reverse (cdr (reverse (bulge_pts (polar (car ls) (+ (angle (car ls) (cadr ls)) a) rad) (car ls) (cadr ls) rad (car lb)))))))
        )
      )
    )
    (setq ls (cdr ls) lb (cdr lb))
  )
  (append l_new (list (car ls)))
)
(defun bulge_pts (pt_cen pt_begin pt_end rad sens / inc ang nm p lst)
  (setq
    inc (angle pt_cen (if (< sens 0.0) pt_end pt_begin))
    ang (+ (* 2.0 pi) (angle pt_cen (if (< sens 0.0) pt_begin pt_end)))
    nm (1- (fix (/ (rem (- ang inc) (* 2.0 pi)) (/ (* pi 2.0) 36.0))))
    lst (list (polar pt_cen inc rad))
  )
  (repeat nm
    (setq
      inc (+ inc (/ (* pi 2.0) 36.0))
      p (polar pt_cen inc rad)
      lst (append lst (list p))
    )
  )
  (setq
    p (polar pt_cen ang rad)
    lst (append lst (list p))
  )
  (if (< sens 0.0) (reverse lst) lst)
)
(defun c:lwto3dpoly ( / js AcDoc Space n ent dxf_ent name_layer xd_l closed lst l_bulg nwent tbldef lst_data)
  (princ "\nSelect the light polylines to convert to 3D: ")
  (setq js
    (ssget
      (list
        (cons 0 "LWPOLYLINE")
        (cons 67 (if (eq (getvar "CVPORT") 2) 0 1))
        (cons 410 (if (eq (getvar "CVPORT") 2) "Model" (getvar "CTAB")))
      )
    )
  )
  (cond
    (js
      (setq
        AcDoc (vla-get-ActiveDocument (vlax-get-acad-object))
        Space
        (if (eq (getvar "CVPORT") 1)
          (vla-get-PaperSpace AcDoc)
          (vla-get-ModelSpace AcDoc)
        )
      )
      (repeat (setq n (sslength js))
        (setq
          ent (ssname js (setq n (1- n)))
          dxf_ent (entget ent '("*"))
          name_layer (cdr (assoc 8 dxf_ent))
          xd_l (assoc -3 dxf_ent)
          closed (boole 1 (cdr (assoc 70 dxf_ent)) 1)
          lst (mapcar '(lambda (x) (trans x ent 1)) (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 10)) dxf_ent)))
          l_bulg (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 42)) dxf_ent))
          lst (def_bulg_pl (mapcar '(lambda (x) (list (car x) (cadr x) (cdr (assoc 38 dxf_ent)))) lst) l_bulg closed)
        )
        (if
          (or
            (numberp (vl-string-search "Map 3D" (vla-get-caption (vlax-get-acad-object))))
            (numberp (vl-string-search "Civil 3D" (vla-get-caption (vlax-get-acad-object))))
          )
          (progn
            (cond
              ((listp (ade_odgettables ent))
                (foreach n_od (ade_odgettables ent)
                  (setq tbldef (ade_odtabledefn n_od))
                  (setq lst_data
                    (cons
                      (mapcar
                        '(lambda (fld / tmp_rec numrec)
                          (setq numrec (ade_odrecordqty ent n_od))
                          (cons
                            n_od
                            (while (not (zerop numrec))
                              (setq numrec (1- numrec))
                              (if (zerop numrec)
                                (if tmp_rec
                                  (cons fld (list (cons (ade_odgetfield ent n_od fld numrec) tmp_rec)))
                                  (cons fld (ade_odgetfield ent n_od fld numrec))
                                )
                                (setq tmp_rec (cons (ade_odgetfield ent n_od fld numrec) tmp_rec))
                              )
                            )
                          )
                        )
                        (mapcar 'cdar (cdaddr tbldef))
                      )
                      lst_data
                    )
                  )
                )
              )
              (T (setq lst_data nil))
            )
          )
          (setq lst_data nil)
        )
        (if (not (zerop closed)) (setq lst (reverse (cdr (reverse lst)))))
        (vla-put-Closed (vlax-invoke Space 'Add3dPoly (apply 'append lst)) closed)
        (setq nwent (entlast))
        (if xd_l
          (entmod (append (entget nwent) (list xd_l)))
        )
        (cond
          (lst_data
            (mapcar
              '(lambda (x / ct)
                (while (< (ade_odrecordqty nwent (caar x)) (ade_odrecordqty ent (caar x)))
                  (ade_odaddrecord nwent (caar x))
                )
                (foreach el (mapcar 'cdr x)
                  (if (listp (cdr el))
                    (progn
                      (setq ct -1)
                      (mapcar
                        '(lambda (y / )
                          (ade_odsetfield nwent (caar x) (car el) (setq ct (1+ ct)) y)
                        )
                        (cadr el)
                      )
                    )
                    (ade_odsetfield nwent (caar x) (car el) 0 (cdr el))
                  )
                )
              )
              lst_data
            )
            (setq lst_data nil)
          )
        )
        (vla-put-Layer (vlax-ename->vla-object nwent) name_layer)
        (entdel ent)
      )
    )
  )
  (prin1)
)
Message 9 of 17

braudpat
Mentor
Mentor

Hello Bruno @CADaSchtroumpf 

 

BRAVO !!

 

The Health, Bye, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 10 of 17

АлексЮстасу
Advisor
Advisor

Hi, Bruno,

 

While looked in Map/Civil - XD or OD saved all!

(I can't check in the base AutoCAD yet).


But one small wish is that other properties of polylines are preserved. Not only layers, but also colors, linetypes, weights, etc.

 


-- Alexander, private person, pacifist, english only with translator 🙂 --

Object-modeling _ odclass-odedit.com _ Help

0 Likes
Message 11 of 17

CADaSchtroumpf
Advisor
Advisor
Accepted solution

Hi Alexander,

For transfert also this properties

 

(vl-load-com)
(defun def_bulg_pl (ls lb flag_closed / ls lb rad a l_new)
  (if (not (zerop flag_closed)) (setq ls (append ls (list (car ls)))))
  (while (cadr ls)
    (if (zerop (car lb))
      (setq l_new (append l_new (list (car ls))))
      (progn
        (setq
          rad (/ (distance (car ls) (cadr ls)) (sin (* 2.0 (atan (abs (car lb))))) 2.0)
          a (- (/ pi 2.0) (- pi (* 2.0 (atan (abs (car lb))))))
        )
        (if (< a 0.0) (setq a (- (* 2.0 pi) a)))
        (if (or (and (< (car lb) 0.0) (> (car lb) -1.0)) (> (car lb) 1.0))
          (setq l_new (append l_new (reverse (cdr (reverse (bulge_pts (polar (car ls) (- (angle (car ls) (cadr ls)) a) rad) (car ls) (cadr ls) rad (car lb)))))))
          (setq l_new (append l_new (reverse (cdr (reverse (bulge_pts (polar (car ls) (+ (angle (car ls) (cadr ls)) a) rad) (car ls) (cadr ls) rad (car lb)))))))
        )
      )
    )
    (setq ls (cdr ls) lb (cdr lb))
  )
  (append l_new (list (car ls)))
)
(defun bulge_pts (pt_cen pt_begin pt_end rad sens / inc ang nm p lst)
  (setq
    inc (angle pt_cen (if (< sens 0.0) pt_end pt_begin))
    ang (+ (* 2.0 pi) (angle pt_cen (if (< sens 0.0) pt_begin pt_end)))
    nm (1- (fix (/ (rem (- ang inc) (* 2.0 pi)) (/ (* pi 2.0) 36.0))))
    lst (list (polar pt_cen inc rad))
  )
  (repeat nm
    (setq
      inc (+ inc (/ (* pi 2.0) 36.0))
      p (polar pt_cen inc rad)
      lst (append lst (list p))
    )
  )
  (setq
    p (polar pt_cen ang rad)
    lst (append lst (list p))
  )
  (if (< sens 0.0) (reverse lst) lst)
)
(defun c:lwto3dpoly ( / js AcDoc Space n ent dxf_ent name_layer col typl scl wl xd_l closed lst l_bulg nwent tbldef lst_data obj)
  (princ "\n"\nSelect the light polylines to convert to 3D: ")
  (setq js
    (ssget
      (list
        (cons 0 "LWPOLYLINE")
        (cons 67 (if (eq (getvar "CVPORT") 2) 0 1))
        (cons 410 (if (eq (getvar "CVPORT") 2) "Model" (getvar "CTAB")))
      )
    )
  )
  (cond
    (js
      (setq
        AcDoc (vla-get-ActiveDocument (vlax-get-acad-object))
        Space
        (if (eq (getvar "CVPORT") 1)
          (vla-get-PaperSpace AcDoc)
          (vla-get-ModelSpace AcDoc)
        )
      )
      (repeat (setq n (sslength js))
        (setq
          ent (ssname js (setq n (1- n)))
          dxf_ent (entget ent '("*"))
          name_layer (cdr (assoc 8 dxf_ent))
          col (if (assoc 62 dxf_ent) (cdr (assoc 62 dxf_ent)) 256)
          typl (if (assoc 6 dxf_ent) (cdr (assoc 6 dxf_ent)) "BYLAYER")
          scl (if (assoc 48 dxf_ent) (cdr (assoc 48 dxf_ent)) 1.0)
          wl (if (assoc 370 dxf_ent) (cdr (assoc 370 dxf_ent)) -1)
          xd_l (assoc -3 dxf_ent)
          closed (boole 1 (cdr (assoc 70 dxf_ent)) 1)
          lst (mapcar '(lambda (x) (trans x ent 1)) (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 10)) dxf_ent)))
          l_bulg (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 42)) dxf_ent))
          lst (def_bulg_pl (mapcar '(lambda (x) (list (car x) (cadr x) (cdr (assoc 38 dxf_ent)))) lst) l_bulg closed)
        )
        (if
          (or
            (numberp (vl-string-search "Map 3D" (vla-get-caption (vlax-get-acad-object))))
            (numberp (vl-string-search "Civil 3D" (vla-get-caption (vlax-get-acad-object))))
          )
          (progn
            (cond
              ((listp (ade_odgettables ent))
                (foreach n_od (ade_odgettables ent)
                  (setq tbldef (ade_odtabledefn n_od))
                  (setq lst_data
                    (cons
                      (mapcar
                        '(lambda (fld / tmp_rec numrec)
                          (setq numrec (ade_odrecordqty ent n_od))
                          (cons
                            n_od
                            (while (not (zerop numrec))
                              (setq numrec (1- numrec))
                              (if (zerop numrec)
                                (if tmp_rec
                                  (cons fld (list (cons (ade_odgetfield ent n_od fld numrec) tmp_rec)))
                                  (cons fld (ade_odgetfield ent n_od fld numrec))
                                )
                                (setq tmp_rec (cons (ade_odgetfield ent n_od fld numrec) tmp_rec))
                              )
                            )
                          )
                        )
                        (mapcar 'cdar (cdaddr tbldef))
                      )
                      lst_data
                    )
                  )
                )
              )
              (T (setq lst_data nil))
            )
          )
          (setq lst_data nil)
        )
        (if (not (zerop closed)) (setq lst (reverse (cdr (reverse lst)))))
        (setq obj (vlax-invoke Space 'Add3dPoly (apply 'append lst)))
        (vla-put-Closed obj closed)
        (vla-put-Color obj col)
        (vla-put-Linetype obj typl)
        (vla-put-LinetypeScale obj scl)
        (vla-put-Lineweight obj wl)
        (setq nwent (entlast))
        (if xd_l
          (entmod (append (entget nwent) (list xd_l)))
        )
        (cond
          (lst_data
            (mapcar
              '(lambda (x / ct)
                (while (< (ade_odrecordqty nwent (caar x)) (ade_odrecordqty ent (caar x)))
                  (ade_odaddrecord nwent (caar x))
                )
                (foreach el (mapcar 'cdr x)
                  (if (listp (cdr el))
                    (progn
                      (setq ct -1)
                      (mapcar
                        '(lambda (y / )
                          (ade_odsetfield nwent (caar x) (car el) (setq ct (1+ ct)) y)
                        )
                        (cadr el)
                      )
                    )
                    (ade_odsetfield nwent (caar x) (car el) 0 (cdr el))
                  )
                )
              )
              lst_data
            )
            (setq lst_data nil)
          )
        )
        (vla-put-Layer (vlax-ename->vla-object nwent) name_layer)
        (entdel ent)
      )
    )
  )
  (prin1)
)

 

Message 12 of 17

braudpat
Mentor
Mentor

Hello @CADaSchtroumpf 

BRAVO !

The Health, Bye, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 13 of 17

Dinesh_K214bad
Observer
Observer

Can you please share the name of the lisp cuz after loading the lisp iam not sure what is the name of the list to be entered in the command Box

 

0 Likes
Message 14 of 17

Kent1Cooper
Consultant
Consultant

@Dinesh_K214bad wrote:

Can you please share the name of the lisp cuz after loading the lisp iam not sure what is the name of the list to be entered in the command Box


The command name is always the part that follows the C and colon in (defun C:ThisIsTheCommandName ....

In this case:

(defun c:lwto3dpoly

Kent Cooper, AIA
Message 15 of 17

Dinesh_K214bad
Observer
Observer
Hi buddy, iam not find the lips after loading this lisp but your previous lisp is working with the name u have mentioned. Please help me out cuz i have been trying to convert 2dpolyline to 3dlolyline without any change in object data and line properties
0 Likes
Message 16 of 17

Dinesh_K214bad
Observer
Observer

the lisp mentioned in MESSAGE 8 OF 15 is working perfectly but it is not retaining line type. Please help me out with the lisp

0 Likes
Message 17 of 17

Kent1Cooper
Consultant
Consultant

@Dinesh_K214bad wrote:

.... it is not retaining line type. ....


3D Polylines have never displayed non-continuous linetypes -- you can assign one, but it won't show that way.

Kent Cooper, AIA