need a lisp for multiple object STL export

need a lisp for multiple object STL export

tobissammet
Community Visitor Community Visitor
194 Views
11 Replies
Message 1 of 12

need a lisp for multiple object STL export

tobissammet
Community Visitor
Community Visitor

Good afternoon, I was wondering if someone could write a lisp routine for a task I often have to manually perform. I use solid modeling in Autocad and export the solids as an .stl file for various applications. Exporting .stl files from Autocad is a bit outdated in its functionality, and sometimes it wastes a ton of time for specific circumstances, such as needing to export many solid objects (that are not unioned as one item). My issue is I have many objects (on a shared layer) that I want to export as individual files but I don't want them all unioned prior to export. So instead of me having to manually click each solid object, then give it a filename, then export it to a specific folder (the default folder is simply the Documents folder in windows 10), then repeat this process for each object on that layer, is there a lisp routine that can automate this in a single click?


I would like for the lisp to:


1.) isolate a specific layer that I click (basically just the LAYISO command)
2.) randomly select a single 3D solid object on that isolated layer
3.) export that single 3D solid object as a binary .stl to the Documents folder with the filename of 1.stl
4.) delete that 3D solid object from the drawing
5.) repeat steps 2 thru 4, naming each successive .stl file 2.stl, 3.stl, 4.stl, etc. until all objects on that isolated layer have been exported and deleted.

 

The reason I want each solid object deleted is so I can quickly verify that ALL objects on that isolated layer were in fact affected by the lisp routine.  


This should result in the Documents folder containing potentially a few hundred .stl files, since many of my drawings require hundreds or even thousands of separate 3D objects that I'd like to export as individual files.


Would any of you lisp wizards care to take a stab at this? Thank you!

 

tobi

0 Likes
Accepted solutions (1)
195 Views
11 Replies
Replies (11)
Message 2 of 12

devitg
Advisor
Advisor

@tobissammet Please upload a sample dwg with some solids .

0 Likes
Message 3 of 12

tobissammet
Community Visitor
Community Visitor

Here you go!

0 Likes
Message 4 of 12

paullimapa
Mentor
Mentor

First confirm that the attached 1.stl which I exported from AutoCAD works for your setup


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 12

tobissammet
Community Visitor
Community Visitor

Yes, this works perfectly.

0 Likes
Message 6 of 12

BlackBox_
Advisor
Advisor
(vl-load-com)

(defun c:Quux (/ *error* cmdecho filedia ss f acDoc i e)

  (defun *error* (msg)
    (if cmdecho (setvar 'cmdecho cmdecho))
    (if filedia (setvar 'filedia filedia))
    (if acDoc (vla-endundomark acDoc))
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
    )
    (princ)
  )
    
  (if
    (and
      (princ
        "\nSelect a single 3DSOLID to export all on same layer to .STL: "
      )
      (setq cmdecho (getvar 'cmdecho))
      (setvar 'cmdecho 0)
      (setq filedia (getvar 'filedia))
      (setvar 'filedia 0)
      (setq ss (ssget ":S:E:L" '((0 . "3DSOLID"))))
      (setq ss (cdr (assoc 8 (entget (ssname ss 0)))))
      (setq ss (ssget "_x" (list '(0 . "3DSOLID") (cons 8 ss))))
      (setq f (getvar 'mydocumentsprefix))
      (princ "\nWorking... ")
    )
     (progn
       (vla-startundomark
         (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
       )
       (setq i 0)
       (repeat (sslength ss)
         (command "._stlout"
                  (setq e (ssname ss i))
                  ""
                  "_y"
                  (strcat f "\\" (itoa (setq i (1+ i))) ".stl")
         )
         (entdel e)
       )
       (startapp (strcat "explorer /n,/e,/select, " f "\\1.stl"))
       (princ "Done. \n")
     )
  )

  (*error* nil)
)

 

[Edit] - used @paullimapa 's (setq f (getvar 'mydocumentsprefix)) suggestion to make this more universal, as not all IT departments make (getvar 'username) same as (getvar 'loginname).


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

Message 7 of 12

tobissammet
Community Visitor
Community Visitor

I get "** Error: bad argument type: stringp nil **" after clicking an object when I try to run that...  Did I do something wrong?

0 Likes
Message 8 of 12

BlackBox_
Advisor
Advisor

@tobissammet wrote:

I get "** Error: bad argument type: stringp nil **" after clicking an object when I try to run that...  Did I do something wrong?


Sounds like it's not finding your Documents folder... what is the value of the 'f' variable?

 

[Edit] - if 'f' == Nil, then does (strcat "C:\\Users\\" (getvar 'username) "\\Documents") match the path to your actual Documents folder?


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 9 of 12

paullimapa
Mentor
Mentor
Accepted solution

you have two options:

1) replace the following line:

(getvar 'username)

with this:

getvar 'loginname)

Or replace all these lines:

      (setq f
             (findfile (strcat "C:\\Users\\"
                               (getvar 'username)
                               "\\Documents"
                       )
             )
      )

with this:

     (setq f (getvar 'MYDOCUMENTSPREFIX))

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 10 of 12

BlackBox_
Advisor
Advisor

Good idea; not all IT departments are the same when it comes to UserName vs LoginName. 


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

Message 11 of 12

tobissammet
Community Visitor
Community Visitor

It works!  Perfectly!

 

You guys are amazing with this stuff.  THANK YOU SO MUCH!!!!!!!!!  

 

 

Message 12 of 12

BlackBox_
Advisor
Advisor

@tobissammet wrote:

It works!  Perfectly!

 

You guys are amazing with this stuff.  THANK YOU SO MUCH!!!!!!!!!  

 

 


Happy to help! 🍺


If you found the code useful, please mark as solution. 

 

Cheers


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes