Author Topic: 3dpoly to 3dface (with 3 vertices or 4 vertices with the first and last equal)  (Read 997 times)

0 Members and 1 Guest are viewing this topic.

mariolino0099

  • Newt
  • Posts: 26
This AutoLISP code is used to convert closed 3D polylines into 3D faces. It will check a selection set of polylines and convert any polylines that are closed 3D polylines into 3D faces. In order for a polyline to be considered a 3D polyline, it must have at least four vertices (first and last equal). The code will output the number of 3D polylines that were converted and the number that were ignored.
But here are something wrong... can anyone help me ? Thanks

Code - Auto/Visual Lisp: [Select]
  1. (defun c:polytoface (/ ss cnt1 cnt2)
  2.   (setq ss (ssget '((0 . "POLYLINE") (-4 . "&") (70 . 8))))
  3.   (setq cnt1 0
  4.         cnt2 0
  5.   )
  6.   (if ss
  7.     (progn
  8.       (repeat (sslength ss)
  9.         (setq ent (entget (ssname ss (setq cnt1 (1+ cnt1)))))
  10.         (if (= (cdr (assoc 10 ent)) (cdr (assoc 11 (last ent))))
  11.           (if (= (- (length (cdr (assoc 10 ent)))) 3)
  12.             (progn
  13.               (command "._3dface"
  14.                        (cdr (assoc 10 ent))
  15.                        (cdr (assoc 20 ent))
  16.                        (cdr (assoc 30 ent))
  17.                        (cdr (assoc 40 ent))
  18.               )
  19.               (setq cnt2 (1+ cnt2))
  20.             )
  21.           )
  22.           (if (= (- (length (cdr (assoc 10 ent)))) 4)
  23.             (if (= (cdr (assoc 10 ent)) (nth 1 (cdr (assoc -1 ent))))
  24.               (progn
  25.                 (command "._3dface"
  26.                          (cdr (assoc 10 ent))
  27.                          (cdr (assoc 20 ent))
  28.                          (cdr (assoc 30 ent))
  29.                          (cdr (assoc 40 ent))
  30.                 )
  31.                 (setq cnt2 (1+ cnt2))
  32.               )
  33.             )
  34.           )
  35.         )
  36.       )
  37.       (princ (strcat "\n"
  38.                      (itoa cnt2)
  39.                      " 3dpoly converted, "
  40.                      (itoa (- cnt1 cnt2))
  41.                      " ignored."
  42.              )
  43.       )
  44.     )
  45.     (princ "\nNo 3dpoly found.")
  46.   )
  47.   (princ)
  48. )
  49.  
  50.  
  51.  

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Untested, but it should work...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:polytoface ( / vertlst ss ent pl cnt1 cnt2 )
  2.  
  3.   (defun vertlst ( poly / v vx pl )
  4.     (setq v poly)
  5.     (while (= (cdr (assoc 0 (setq vx (entget (setq v (entnext v)))))) "VERTEX")
  6.       (setq pl (cons (cdr (assoc 10 vx)) pl))
  7.     )
  8.     (reverse pl)
  9.   )
  10.  
  11.   (setq cnt1 -1
  12.         cnt2 0
  13.   )
  14.   (if (setq ss (ssget '((0 . "POLYLINE") (-4 . "<or") (70 . 8) (70 . 9) (-4 . "or>"))))
  15.     (progn
  16.       (repeat (sslength ss)
  17.         (setq ent (ssname ss (setq cnt1 (1+ cnt1))))
  18.         (setq pl (vertlst ent))
  19.         (if (= (length pl) 4)
  20.           (if (equal (car pl) (last pl) 1e-6)
  21.             (progn
  22.               (command "._3dface"
  23.                  "_non" (car pl)
  24.                  "_non" (cadr pl)
  25.                  "_non" (caddr pl)
  26.                  "_non" (cadddr pl)
  27.                  ""
  28.               )
  29.               (setq cnt2 (1+ cnt2))
  30.             )
  31.           )
  32.           (if (= (length pl) 3)
  33.             (progn
  34.               (command "._3dface"
  35.                  "_non" (car pl)
  36.                  "_non" (cadr pl)
  37.                  "_non" (caddr pl)
  38.                  "_non" (caddr pl)
  39.                  ""
  40.               )
  41.               (setq cnt2 (1+ cnt2))
  42.             )
  43.           )
  44.         )
  45.       )
  46.       (princ (strcat "\n"
  47.                      (itoa cnt2)
  48.                      " 3dpoly converted, "
  49.                      (itoa (- (1+ cnt1) cnt2))
  50.                      " ignored."
  51.              )
  52.       )
  53.     )
  54.     (princ "\nNo 3dpoly found.")
  55.   )
  56.   (princ)
  57. )
  58.  

HTH.
M.R.
« Last Edit: April 12, 2023, 02:57:55 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BIGAL

  • Swamp Rat
  • Posts: 1418
  • 40 + years of using Autocad
Have you looked into Triangulation lisp used in civil engineering to take points and make 3dface's for surface representations.

Do a google for Triangulation YMG.
A man who never made a mistake never made anything

mariolino0099

  • Newt
  • Posts: 26
many thanks for the response,
unfortunately it doesn't work..... I get this error:

Command: POLYTOFACE
Select objects: Specify opposite corner: 346 found
Select objects: ; error: bad argument type: lentityp ((-1 . <Entity name: 1280f370470>) (0 . "POLYLINE") (330 . <Entity name: 128112b0080>) (5 . "E0F") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDb3dPolyline") (66 . 1) (10 0.0 0.0 0.0) (70 . 8) (40 . 0.0) (41 . 0.0) (210 0.0 0.0 1.0) (71 . 0) (72 . 0) (73 . 0) (74 . 0) (75 . 0))



ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
many thanks for the response,
unfortunately it doesn't work..... I get this error:

Command: POLYTOFACE
Select objects: Specify opposite corner: 346 found
Select objects: ; error: bad argument type: lentityp ((-1 . <Entity name: 1280f370470>) (0 . "POLYLINE") (330 . <Entity name: 128112b0080>) (5 . "E0F") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDb3dPolyline") (66 . 1) (10 0.0 0.0 0.0) (70 . 8) (40 . 0.0) (41 . 0.0) (210 0.0 0.0 1.0) (71 . 0) (72 . 0) (73 . 0) (74 . 0) (75 . 0))

Try now...
IMHO it was cnt1 variable - should be starting -1 and (setq ent (entget (ssname ... should have been (setq ent (ssname ... and there should be "" after (command "_.3dface" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "")
« Last Edit: April 12, 2023, 08:22:39 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

mariolino0099

  • Newt
  • Posts: 26
many thanks for the response....
thanks for new code.... with this modification

Code - Auto/Visual Lisp: [Select]
  1.  
  2.   (setq cnt1 -1
  3.         cnt2 0
  4.   )

Code - Auto/Visual Lisp: [Select]
  1.             (progn
  2.               (command "._3dface"
  3.                  "_non" (car pl)
  4.                  "_non" (cadr pl)
  5.                  "_non" (caddr pl)
  6.                  "_non" (cadddr pl)
  7.                  ""
  8.               )
  9.  

unfortunately it doesn't work....
this error:
Command:  POLYTOFACE
Select objects: Specify opposite corner: 346 found
Select objects:  ; error: bad argument type: lentityp ((-1 . <Entity name: 20d8a53ece0>) (0 . "POLYLINE") (330 . <Entity name: 20db1273080>) (5 . "E16") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDb3dPolyline") (66 . 1) (10 0.0 0.0 0.0) (70 . 8) (40 . 0.0) (41 . 0.0) (210 0.0 0.0 1.0) (71 . 0) (72 . 0) (73 . 0) (74 . 0) (75 . 0))

PS: in previous post attached DXF for test conversion
« Last Edit: April 12, 2023, 08:44:50 AM by mariolino0099 »

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
@mariolino

This was an issue (setq ent (entget (ssname ss (setq cnt1 (1+ cnt1)))))
Should be : (setq ent (ssname ss (setq cnt1 (1+ cnt1))))

You have tested the code in the meantime I haven't changed that line...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

mariolino0099

  • Newt
  • Posts: 26
it works !!!!!
many thanks !!!!!  :smitten:

PS it's just a little slow....

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
PS it's just a little slow....

You could alternatively use (entmake) instead of command calls :

Code - Auto/Visual Lisp: [Select]
  1. (defun c:polytoface ( / vertlst ss ent pl cnt1 cnt2 )
  2.  
  3.   (defun vertlst ( poly / v vx pl )
  4.     (setq v poly)
  5.     (while (= (cdr (assoc 0 (setq vx (entget (setq v (entnext v)))))) "VERTEX")
  6.       (setq pl (cons (cdr (assoc 10 vx)) pl))
  7.     )
  8.     (reverse pl)
  9.   )
  10.  
  11.   (setq cnt1 -1
  12.         cnt2 0
  13.   )
  14.   (if (setq ss (ssget '((0 . "POLYLINE") (-4 . "<or") (70 . 8) (70 . 9) (-4 . "or>"))))
  15.     (progn
  16.       (repeat (sslength ss)
  17.         (setq ent (ssname ss (setq cnt1 (1+ cnt1))))
  18.         (setq pl (vertlst ent))
  19.         (if (= (length pl) 4)
  20.           (if (equal (car pl) (last pl) 1e-6)
  21.             (progn
  22.               (entmake
  23.                 (list
  24.                   (cons 0 "3DFACE")
  25.                   (cons 10 (car pl))
  26.                   (cons 11 (cadr pl))
  27.                   (cons 12 (caddr pl))
  28.                   (cons 13 (cadddr pl))
  29.                 )
  30.               )
  31.               (setq cnt2 (1+ cnt2))
  32.             )
  33.           )
  34.           (if (= (length pl) 3)
  35.             (progn
  36.               (entmake
  37.                 (list
  38.                   (cons 0 "3DFACE")
  39.                   (cons 10 (car pl))
  40.                   (cons 11 (cadr pl))
  41.                   (cons 12 (caddr pl))
  42.                   (cons 13 (caddr pl))
  43.                 )
  44.               )
  45.               (setq cnt2 (1+ cnt2))
  46.             )
  47.           )
  48.         )
  49.       )
  50.       (princ (strcat "\n"
  51.                      (itoa cnt2)
  52.                      " 3dpoly converted, "
  53.                      (itoa (- (1+ cnt1) cnt2))
  54.                      " ignored."
  55.              )
  56.       )
  57.     )
  58.     (princ "\nNo 3dpoly found.")
  59.   )
  60.   (princ)
  61. )
  62.  

HTH.
M.R.
« Last Edit: April 12, 2023, 02:57:36 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube