need help with an STL exporting routine

need help with an STL exporting routine

Anonymous
Not applicable
3,697 Views
23 Replies
Message 1 of 24

need help with an STL exporting routine

Anonymous
Not applicable

Hello all, I have an autolisp request with which maybe someone can provide assistance.


I do a lot of 3D solid modeling in Autocad 2014 and then export the solids in .stl format for use in my 3rd party rendering software. My workflow consists of creating the 3D solids in acad and keeping them all on their respective layers, then I isolate those layers and use the STLOUT command to export every 3D solid object by its layer. So basically I have layers named 3D Walls, 3D Window Frame, 3D Glass, etc., and all the solids on each of those layers are exported as .stl files. Since the STLOUT command automatically unions two or more solids when exporting, I can fairly quickly convert an entire autocad file into multiple .stl files for import into my other software to apply textures, lighting and render the final images.


The only problem with this system is it's rather tedious, since I have to manually isolate each layer, click STLOUT, select all the visible objects, click Y to the binary file question, then manually give the file to be exported a name and location, (usually 1.stl, 2.stl, 3.stl, etc. in the My Documents folder).


What I want to be able to do is to simply click a custom icon (or invoke an autolisp routine) that will do this automatically for me without any manual input.


I want the routine to


1.) Isolate each layer
2.) Export every visible solid on that layer to an .stl file with the same name as the layer that all these solids are on (so there'd be files in the My Documents folder called 3D Walls.stl, 3D Window Frame.stl, 3D Glass.stl, etc.)
3.) Continue this process for every layer in the drawing (usually 15-25 layers total)


I have a basic grasp of Autolisp and command macros, but obviously not enough to handle this myself.  Please bear in mind that each drawing has unique layer names based on the particular subject of the drawing (not everything I do is architectural in nature), so manually entering these layer names in the lisp routine isn't really feasible.  I want the lisp routine to read each layer in the drawing and perform the exporting duties without me having to tell it what to do. 

 

Can anyone lend a hand? Thanks in advance!

Accepted solutions (1)
3,698 Views
23 Replies
Replies (23)
Message 21 of 24

Mike_Ishman
Observer
Observer

Sea Haven, your routine works great and thanks for all your previous help with this.

I tweaked your routine a bit to not need any specific prefix in the layer name as well as not include the drawing file name in the .stl created file naming from the layer name.

Only additional change I would like to make is to not have a specified path to save the .stl file too, instead just have it put the files in the same directory as the drawing file. This I am not able to figure out.

Here is the code I tweaked:

(defun c:demo (/ *error* base layname laylst name newfilename oldfdia ss)
(vl-load-com)
(defun *error* (msg)
(if oldfdia
(setvar 'FILEDIA oldfdia)
)
(cond ((not msg))
((member msg '("Function cancelled" "quit / exit abort")))
((princ (strcat "\n** Error: " msg " ** ")))
)
(princ)
)
(vlax-for lay (vla-get-Layers (vla-get-activedocument (vlax-get-acad-object)))
(setq name (vla-get-name lay))
(setq laylst (cons name laylst))
)
(if (/= laylst nil)
(progn
(setq oldfdia (getvar 'FILEDIA)
base (strcat "C:\\Work\\23006929G1 US Army Corps - West Valley\\Ryan Troy\\")
)
(setvar 'FILEDIA 0)
(foreach layname laylst
(if (setq ss (ssget "_X" (list (cons 8 layname) '(0 . "3DSOLID"))))
(if (findfile (setq newfilename (strcat base "" layname)))
(command "_stlout" ss "" "_Y" newfilename "_y")
(command "_stlout" ss "" "_Y" newfilename)
)
)
)
(setvar 'FILEDIA oldfdia)
)
)
(princ)
)

0 Likes
Message 22 of 24

Sea-Haven
Mentor
Mentor

Your looking for (getvar 'dwgprefix).

0 Likes
Message 23 of 24

Mike_Ishman
Observer
Observer

I change code line (setq oldfdia (getvar 'FILEDIA) to (setq oldfdia (getvar 'dwgprefix).

When I run it returns "AutoCAD variable setting rejected: FILEDIA" and the path where I opened the file from.

 

0 Likes
Message 24 of 24

Mike_Ishman
Observer
Observer
I figured it out, here is the full code, thanks for your help.
********************************************************************************************************************
;; 07/02/2019 Originally written by Alan Houston
;; 09/11/2023 Updated by Mike Ishman
;; This routine does the following:
;; - creates .stl files from 3D solids that are in the drawing file
;; - names the .stl file based on layer name of the 3D solid
;; - writes the .stl file(s) to the directory path that the drawing file is opened from
********************************************************************************************************************
(defun c:demo (/ *error* base layname laylst name newfilename oldfdia ss)
(vl-load-com)
(defun *error* (msg)
(if oldfdia
(setvar 'FILEDIA oldfdia)
)
(cond ((not msg))
((member msg '("Function cancelled" "quit / exit abort")))
((princ (strcat "\n** Error: " msg " ** ")))
)
(princ)
)
(vlax-for lay (vla-get-Layers (vla-get-activedocument (vlax-get-acad-object)))
(setq name (vla-get-name lay))
(setq laylst (cons name laylst))
)
(if (/= laylst nil)
(progn
(setq oldfdia (getvar 'FILEDIA)
base (strcat (getvar 'DWGPREFIX))
)
(setvar 'FILEDIA 0)
(foreach layname laylst
(if (setq ss (ssget "_X" (list (cons 8 layname) '(0 . "3DSOLID"))))
(if (findfile (setq newfilename (strcat base "" layname)))
(command "_stlout" ss "" "_Y" newfilename "_y")
(command "_stlout" ss "" "_Y" newfilename)
)
)
)
(setvar 'FILEDIA oldfdia)
)
)
(princ)
)
0 Likes