Export FreeCAD to OpenSCAD CSG.

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Export FreeCAD to OpenSCAD CSG.

Post by keithsloan52 »

I have started an exporter for FreeCAD to OpenSCAD CSG format.

Its available at http://www.sloan-home.co.uk/Export/Export.html

Its no where near complete, but extra function will be added shortly. So keep checking back for updates.

If you have a particular requirement for new function then please contact by email which is in the comments at the start of the
python script.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by shoogen »

I think i would be nice to distinguish between csg and scad export. CSG is very restricted and you cannot expect tools (like the reprap software) to handle things that are not produced by OpenSCAD. For that reason i would not put comments into a CSG file.
On the other hand if a user chooses to the .scad file extension he may want a translate() and rotate() output rather than a multmatrix(). And things like arrays in FreeCAD can be converted to for-loops in Openscad. You can even use the 'module' meachnism to keep the file readable and editable.
I'd like to have a fallback to polyhedron export. So every solid object that is not recognized by the exporter, would be converted to a mesh and exported to a file or inline as a 'polyhedron'.(Later on, one can think of a similar system for faces, which would be exported as polygon.)
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by keithsloan52 »

shoogen I think you are getting way way ahead of the game.

The long term plan is to produce an export for CSG and also an export for SCAD, but thats a long term aim,
At the moment I intend to concentrate on export CSG

At the moment having an option to create the csg file with an extension of scad makes it easier for testing.
OpenSCAD only allows one to open a file with an extension of scad and I want to test that the exported csg file will load into OpenSCAD as well
as back into FreeCAD via importCSG. I don't want to have to fiddle with changing file extensions.

The point of the comment is to document that the file has been exported via the Exporter, rather than come from OpenSCAD.
It does not cause OpenSCAD a problem and it does not cause importCSG a problem unless you are using an early version.
I take you point about RepRap software. If it bothers you just delete the comment first or take out the line of python that outputs
the comment. But at this stage I need it so as to not get confused when testing, so it stays in my versions at least.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by shoogen »

I've found a way to do the polyhedron fallback

Code: Select all

def mesh2polyhedron(mesh):
    pointstr=','.join(['[%f,%f,%f]' % tuple(vec) for vec in mesh.Topology[0]])
    trianglestr=','.join(['[%d,%d,%d]' % tri for tri in mesh.Topology[1]])
    return 'polyhedron ( points = [%s], triangles = [%s]);' % (pointstr,triangestr)

def shape2polyhedron(shape):
    import MeshPart
    return mesh2polyhedron(MeshPart.meshFromShape(shape))

def process_object(csg,ob):
   (...)
    elif ob.isDerivedFrom('Part::Feature') :
        print "Part::Feature"
        mm = check_multmatrix(csg,ob,0,0,0)
        csg.write('%s\n' % shape2polyhedron(ob.Shape))
        if mm : csg.write("}\n")       
Please excuse the list comprehensions
Last edited by shoogen on Fri Mar 23, 2012 10:21 pm, edited 2 times in total.
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by keithsloan52 »

shoogen

Thanks I will incorporate it into the code good timing I am working on it at the moment (Other bits ) and just came back from lunch.

Keith
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by keithsloan52 »

shoogen I have added you code, couple of questions

1) Do you have a test file I can add to my set of test files.

2) Also I was going to add support for a Torus.
I drew a Torus is FreeCAD and then tried to export i.e. was going to look for its signatures as it were
It dropped into your code and produced an error message
in mesh3polyhron
pointstr=','.join(['[%d,%d,%d]' % vec for vec in mesh.Topology[0]])
%format a number is required not a Base.Vector

Maybe I should be processing if a torus and only dropping into your code if not.

Any comments
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by shoogen »

That was my fault please replace the first 'vec' with 'tuple(vec)'. I've edited my last post.
This 'fallback' should be at the very end, as it will match every Part::Feature object. Not just the base class but all derived classes.
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by keithsloan52 »

Okay I already put it as the last elif coming to the same conclusion.

Am I right that trianglestr=','.join(['[%d,%d,%d]' % tuple(tri) for tri in mesh.Topology[1]])
should also have the tuple like I added here.

Okay I have added code to detect Torus but don't know how to find out what object parameters are called.
I assume its got two radius so am Guessing maybe Radius1 and Radius2, but would really like a way to
print out what parameters an object has. Tried looking for source code in Part but did not find anything.

Do you know a way to copy and paste from report window to another program it lets me copy but gives no paste in notepad.
It would be better to be able to copy and past error messages rather than having to type them out again, with the risk of error

By the way the exported torus file loaded into OpenSCAD okay. Torus looked a bit rough at the edges, obviously would be better
in the case of actual Torus to process as a rotatedExtrusion, but looks like you code with work for the general situation
Thanks again
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by shoogen »

keithsloan52 wrote:should also have the tuple like I added here.
That won't hurt, but tri is allready a tuple.
keithsloan52 wrote:Okay I have added code to detect Torus but don't know how to find out what object parameters are called.
create one and look in the properties view or do

Code: Select all

>>> App.ActiveDocument.Torus.PropertiesList
['Angle1', 'Angle2', 'Angle3', 'Label', 'Placement', 'Pos', 'Radius1', 'Radius2', 'Shape']
keithsloan52 wrote:Do you know a way to copy and paste from report window to another program it lets me copy but gives no paste in notepad.
It would be better to be able to copy and past error messages rather than having to type them out again, with the risk of error
If copy from the context menu doesn't work, try saving it as a file (also from the context menu)
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Export FreeCAD to OpenSCAD CSG.

Post by keithsloan52 »

New version 0.1b uploaded. Thanks to shoogen for polyhedron support.
Post Reply