exporting geometry faces as STLs for optimization

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
ferris12
Posts: 14
Joined: Thu Sep 01, 2022 5:54 pm

exporting geometry faces as STLs for optimization

Post by ferris12 »

Hi there,

I am working to automate the process of CAD preparation of pumps for simulations as part of the optimization process. I have written some scripts that generate the geometry, but currently struggling to save the geometry parts as STLs in the designated folder. I have attached a script that generates the inlet pipe, and then attempts to save a part of the geometry as STL using Mesh module, but it gives an error (see screenshot attached).

Can someone please help me out here? Why is it not able to save the STL?

Also, when I would manually CAD the goemetry, the dimensions would get messed up when I exported them as STLs. So, I would mesh them up and scale the mesh by 0.001. Can someone please advise how this can be achieved in Python code?

I have attached the script.

OS: Windows 10 Version 2009
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24267 +148 (Git)
Build type: Release
Branch: Branch_0.19.4
Hash: 476ecf091941bead59b14e44afa6064d5a66afa3
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.3
Locale: English/United States (en_US)
Attachments
error_1.JPG
error_1.JPG (26.87 KiB) Viewed 755 times
inlet_pipe.py
(1.85 KiB) Downloaded 20 times
AKNV
User avatar
Roy_043
Veteran
Posts: 8544
Joined: Thu Dec 27, 2018 12:28 pm

Re: exporting geometry faces as STLs for optimization

Post by Roy_043 »

Try:

Code: Select all

		# revolve to get the CFD domain of inlet pipe
		CFD_domain_inletpipe = inlet_face.revolve(FreeCAD.Vector(0,0,0), FreeCAD.Vector(1,0,0), 360)
		obj = Part.show(CFD_domain_inletpipe.Faces[2])

		export_files = []
		export_files.append(obj)
		Mesh.export(export_files, export_dir + "roughwork.stl") 
ferris12
Posts: 14
Joined: Thu Sep 01, 2022 5:54 pm

Re: exporting geometry faces as STLs for optimization

Post by ferris12 »

I tried, but it doesn't seem to make any difference. I get the same error.
AKNV
User avatar
Roy_043
Veteran
Posts: 8544
Joined: Thu Dec 27, 2018 12:28 pm

Re: exporting geometry faces as STLs for optimization

Post by Roy_043 »

In my tests it does make a difference. If export_files contains a Face I get the same error. But the document object created from the face can be exported.

Code: Select all

>>> ### Begin command Std_SendToPythonConsole
>>> try:
>>>     del(doc,lnk,obj,shp,sub,subs)
>>> except Exception:
>>>     pass
>>> 
>>> doc = App.getDocument("Unnamed")
>>> obj = doc.getObject("Box")
>>> shp = obj.Shape
>>> sub = obj.getSubObject("Face2")
>>> ### End command Std_SendToPythonConsole

>>> face = sub.copy()
>>> export_files = []
>>> export_files.append(face)
>>> Mesh.export(export_files, "D:/test_face.stl")

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: None of the objects can be exported to a mesh file

>>> obj = Part.show(face)
>>> export_files = []
>>> export_files.append(obj)
>>> Mesh.export(export_files, "D:/test_doc_obj.stl")
jbi
Posts: 117
Joined: Sun Apr 24, 2016 3:28 pm

Re: exporting geometry faces as STLs for optimization

Post by jbi »

try replacing or comment out:

Code: Select all

Mesh.export(export_files, export_dir + "roughwork.stl")
with

Code: Select all

for i,fig in enumerate(export_files):
	fig.exportStl(export_dir + "roughwork_"+str(i)+".stl")
I think you even can specify the tesselation tolerance:

Code: Select all

fig.exportStl(export_dir + "roughwork_"+str(i)+".stl",0.02)
ferris12
Posts: 14
Joined: Thu Sep 01, 2022 5:54 pm

Re: exporting geometry faces as STLs for optimization

Post by ferris12 »

jbi wrote: Fri Dec 09, 2022 11:23 am try replacing or comment out:

Code: Select all

Mesh.export(export_files, export_dir + "roughwork.stl")
with

Code: Select all

for i,fig in enumerate(export_files):
	fig.exportStl(export_dir + "roughwork_"+str(i)+".stl")
I think you even can specify the tesselation tolerance:

Code: Select all

fig.exportStl(export_dir + "roughwork_"+str(i)+".stl",0.02)
Sorry for late reply. But this seems to have worked nicely. Thank you so much!!
AKNV
Post Reply