Skip to content

Instantly share code, notes, and snippets.

@slazav
Last active November 17, 2022 14:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slazav/4853bd36669bb9313ddb83f51ee1cb82 to your computer and use it in GitHub Desktop.
Save slazav/4853bd36669bb9313ddb83f51ee1cb82 to your computer and use it in GitHub Desktop.
Convert OpenSCAD files to STEP or IGES using FreeCAD python library
#!/usr/bin/python
# path to FreeCAD.so
FREECADPATH = '/usr/lib64/freecad/lib'
import sys
sys.path.append(FREECADPATH)
if len(sys.argv)<3:
print "Usage: sys.argv[0] <in_file> <out_file>"
sys.exit(1)
iname=sys.argv[1]
oname=sys.argv[2]
# support two export formats: step and iges.
# determin format from extension
if oname[-5:]==".iges":
type="iges"
elif oname[-5:]==".step":
type="step"
else:
print "Output file should have .step or .iges extension"
sys.exit(1)
import FreeCAD
import Part
# Openscad import settings according to
# https://forum.lulzbot.com/viewtopic.php?t=243
p=FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/OpenSCAD")
p.SetBool('useViewProviderTree',True)
p.SetBool('useMultmatrixFeature',True)
# For some reason conversion does not work with cylinders created from
# extruded 2d circles.
# So I set MaxFN large enough and use smaller $fn in my step files to
# export such cilinders as polygons.
# If you use only normal cylinders, no need to use so large number here.
p.SetInt('useMaxFN',50)
# This should read any type of file
FreeCAD.loadFile(iname)
# iterate through all objects
for o in App.ActiveDocument.Objects:
# find root object and export the shape
if len(o.InList)==0:
if type=="step": o.Shape.exportStep(oname)
elif type=="iges": o.Shape.exportIges(oname)
sys.exit(0)
print "Error: can't find any object"
sys.exit(1)
@KeithSloan
Copy link

I am the author of the FreeCAD OpenSCAD importer.
Do you have an example of cylinder from extruded 2d circles that would not easily convert?
Perhaps you could attach a file to an issue on https://github.com/KeithSloan/OpenSCAD
Thanks

@slazav
Copy link
Author

slazav commented Mar 14, 2020

I did this long time ago (May 2017), and do not remember details. At that time I did not manage to convert cylinders properly.
It was, for example, this file: http://slazav.mccme.ru/tmp/sbox2.scad

@KeithSloan
Copy link

Think the problem is setting the max number faces for polygon no longer works via the OpenSCAD preference setting screen.
If one changes it via Tools | Edit Parameters | Mod | OpenSCAD etc it works.

I have another workbench which also suffers from using the preference setting screen.

Need to investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment