need help with an STL exporting routine

need help with an STL exporting routine

Anonymous
Not applicable
3,693 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,694 Views
23 Replies
Replies (23)
Message 2 of 24

Sea-Haven
Mentor
Mentor

This is a bit rough and does not do error checking on layers frozen etc. Next version.

(defun c:ahstlout ( / lst x lay layname)
(setq lst '())
(vlax-for lay (vla-get-Layers (vla-get-activedocument (vlax-get-acad-object)))
   (setq lst (cons  (vla-get-name lay) lst))
)

(repeat (setq x (length lst))
(setq layname (nth (setq x (- x 1)) lst))
(setvar 'clayer layname)
(command "-layer" "off" "*" "" "")
(command "stlout" "all" "" "Y" "m" (strcat "D:\\acadtemp\\" "test" layname))
(command "-layer" "on" "*" "" )
)
)
(c:ahstlout)

 

 

0 Likes
Message 3 of 24

Anonymous
Not applicable

Sea Haven, thanks so much for doing this!

 

I can't seem to get it to work, though.  When I load the .lsp, I get the following:

 

Command: ahstlout
-layer
Current layer: "0"
Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: off
Enter name list of layer(s) to turn off or <select objects>: * Really want layer "0" (the CURRENT layer) off? <N> Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: stlout
Select solids or watertight meshes: all 1 found

Select solids or watertight meshes: Create a binary STL file? [Yes/No] <Y>: Y

Command: m Unknown command "M". Press F1 for help.

Command: D:\acadtemp\test0 Unknown command "D:\ACADTEMP\TEST0". Press F1 for help.

Command: -layer
Current layer: "0"
Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: on
Enter name list of layer(s) to turn on: * Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: nil

 

 

Any ideas?  Thanks again!

0 Likes
Message 4 of 24

Sea-Haven
Mentor
Mentor

I need to build a don't do layer list and check for frozen off layers. Defpoint layer in particular. It should have worked for 0 layer I used Briscad to test will try on Autocad. Is the problem the fact that I put the answer in D:\acadtemp directory you need to change it to suit you.

 

In Briscad the stlout command asks for low, medium, high resolution will check for Autocad. try without the "M"

 

 

0 Likes
Message 5 of 24

Anonymous
Not applicable

Well my autocad normally defaults to C:\Users\User\Documents when I manually save the .STLs, so I'm guessing that would be the filepath to use.  I'll give it a shot with this path and removing the "M" as you've suggested.  Thanks again!

0 Likes
Message 6 of 24

Anonymous
Not applicable

Hmm, I just tried it with the aforementioned tweaks but it's still asking me for the .STL name when the Documents folder is opened instead of automatically naming the exported file and continuing on to the next layer.

0 Likes
Message 7 of 24

hmsilva
Mentor
Mentor

In "3D something" layers, do you have only "3D solids" or "watertight meshes" also?

 

Henrique

EESignature

0 Likes
Message 8 of 24

Anonymous
Not applicable

100% 3D solids.  No meshes of any kind, nor even any leftover 2D lines, arcs, text, circles, etc.  I totally purge the drawing of any unused layers, blocks and remaining "bits and pieces" before I start exporting.

0 Likes
Message 9 of 24

hmsilva
Mentor
Mentor

@Anonymous wrote:

100% 3D solids.  No meshes of any kind, nor even any leftover 2D lines, arcs, text, circles, etc.  I totally purge the drawing of any unused layers, blocks and remaining "bits and pieces" before I start exporting.


Will save in the current dwg folder.

As a 'demo'...

(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)))
    (if (wcmatch (setq name (vla-get-name lay)) "3D*")
      (setq laylst (cons name laylst))
    )
  )
  (if (/= laylst nil)
    (progn
      (setq oldfdia (getvar 'FILEDIA)
     base    (strcat (getvar 'DWGPREFIX) (vl-filename-base (getvar 'DWGNAME)))
      )
      (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)
)

Edited...

Hope this helps,
Henrique

EESignature

0 Likes
Message 10 of 24

Anonymous
Not applicable

Henrique, when I try to run that lisp I get the following:

 

Command: ; error: malformed list on input

 

 

0 Likes
Message 11 of 24

Sea-Haven
Mentor
Mentor

It worked for me I tested on Briscad had to add about low, med, high, will look at check for Autocad v's another cad.

 

also (if (wcmatch (setq name (strcase (vla-get-name lay))) "3D*") the strcase means it will work with 3d or 3D layer names.

0 Likes
Message 12 of 24

Anonymous
Not applicable

Ok when I load that lisp, it loads fine.  I then type "DEMO" and hit enter.  There's a brief half a second pause, as if autocad is doing something, then...nothing happens.  No .stl files are created anywhere on my computer, no layers are turned off or on, nothing.  Am I doing something wrong here?

0 Likes
Message 13 of 24

hmsilva
Mentor
Mentor

@Anonymous wrote:

Ok when I load that lisp, it loads fine.  I then type "DEMO" and hit enter.  There's a brief half a second pause, as if autocad is doing something, then...nothing happens.  No .stl files are created anywhere on my computer, no layers are turned off or on, nothing.  Am I doing something wrong here?


Post a sample DWG file.

The 'demo' should create a *.stl file for each 3D*layer in the dwg file...

 

Henrique

EESignature

0 Likes
Message 14 of 24

Anonymous
Not applicable

Wait a minute, it DOES work!  Wow, that's pretty amazing.  It converted an entire dwg with 25 layers in less than 15 seconds and with a single click!  Fantastic!

 

Two little changes if possible then I think this routine is exactly what I needed:

 

1.)  Can you make it so the .stl files are saved in this folder "C:\Users\User\Documents"

 

2.)  Can you remove the drawing name prefix from the .stl files?  So instead of "export test - 3D Glass.stl" the file is simply named "3D Glass.stl"

 

Thank you so much for this!!!!

 

 

 

0 Likes
Message 15 of 24

hmsilva
Mentor
Mentor

@Anonymous wrote:

Wait a minute, it DOES work!  Wow, that's pretty amazing.  It converted an entire dwg with 25 layers in less than 15 seconds and with a single click!  Fantastic!

 

Two little changes if possible then I think this routine is exactly what I needed:

 

1.)  Can you make it so the .stl files are saved in this folder "C:\Users\User\Documents"

 

2.)  Can you remove the drawing name prefix from the .stl files?  So instead of "export test - 3D Glass.stl" the file is simply named "3D Glass.stl"

 

Thank you so much for this!!!!

 

 

 


You're welcome.
'demo' revised to save the files in user documents folder, removed the prefix, and ad @Sea-Haven 's 'strcase' advice...

(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)))
    (if	(wcmatch (setq name (strcase (vla-get-name lay))) "3D*")
      (setq laylst (cons name laylst))
    )
  )
  (if (/= laylst nil)
    (progn
      (setq oldfdia (getvar 'FILEDIA)
	    base    (strcat "C:\\Users\\" (getenv "username") "\\Documents\\")
      )
      (setvar 'FILEDIA 0)
      (foreach layname laylst
	(if (setq ss (ssget "_X" (list (cons 8 layname) '(0 . "3DSOLID"))))
	  (if (findfile (setq newfilename (strcat base layname ".stl")))
	    (command "_stlout" ss "" "_Y" newfilename "_y")
	    (command "_stlout" ss "" "_Y" newfilename)
	  )
	)
      )
      (setvar 'FILEDIA oldfdia)
    )
  )
  (princ)
)

Hope this helps,
Henrique

EESignature

0 Likes
Message 16 of 24

Anonymous
Not applicable

And with that, it now works PERFECTLY.  You guys are awesome!!  This will save me THOUSANDS of clicks per month. 

 

THANK YOU SO MUCH!!!!!  🙂

0 Likes
Message 17 of 24

hmsilva
Mentor
Mentor

@Anonymous wrote:

And with that, it now works PERFECTLY.  You guys are awesome!!  This will save me THOUSANDS of clicks per month. 

 

THANK YOU SO MUCH!!!!!  🙂


You're welcome, @Anonymous  !
Glad I could help

Henrique

EESignature

0 Likes
Message 18 of 24

Anonymous
Not applicable

Well after a few weeks of using this lisp routine with no trouble whatsoever, suddenly it no longer works!  I have no idea what I did, either.  I didn't update windows 10, I'm still using acad 2014, and the procedures I used before are what I'm still currently using.

 

When I load this lisp, it tells me the routine was loaded successfully.  When I then attempt to use the command, acad pauses for a brief moment, maybe 1 second, then...nothing.  No errors reported, no string of text to be seen, even when I hit F2, and no STLs have been exported.  The command "runs" presumably but nothing actually happens.  I can't even copy/paste the error code because THERE'S NOTHING TO COPY.   The command line just goes back to "Command:" with nothing further to show.  I'm at a total loss for an explanation.

 

Anyone care to shed some light on this?  Other lisp routines still work fine, only this one is "dead" when I run the command. 

0 Likes
Message 19 of 24

Sea-Haven
Mentor
Mentor

You could load the code into VLIDE 1st step do the check code should not be any errors, no Autocad at moment else would explain more  but can do break on error and look at the code that is causing error. You can also inspect each variable and see what its value is.

0 Likes
Message 20 of 24

Anonymous
Not applicable
Accepted solution

I figured it out.  I was trying to export solids that WERE NOT named with the "3D" prefix.  Once I changed the layer names to have "3D" in front of them, everything works fine again.  lol

 

Thanks again guys!!

0 Likes