Import CSG files from OpenSCAD.

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!
Post Reply
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Import CSG files from OpenSCAD.

Post by keithsloan52 »

I have started work on importing CSG files from OpenSCAD. I see that the DXF importer has a DXF parser and the SVG importer uses an XML parser. Any suggestions on a parser for CSG files or is it a case of write one oneself. I don't want to reinvent the wheel so open to suggestions before I start.

A typical CSG file being
group() {
group() {
difference() {
sphere($fn = 0, $fa = 12, $fs = 2, r = 25);
group() {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
cylinder($fn = 0, $fa = 12, $fs = 2, h = 62.5, r1 = 12.5, r2 = 12.5, center = true);
}
}
group() {
multmatrix([[1, 0, 0, 0], [0, 6.12303e-017, -1, 0], [0, 1, 6.12303e-017, 0], [0, 0, 0, 1]]) {
cylinder($fn = 0, $fa = 12, $fs = 2, h = 62.5, r1 = 12.5, r2 = 12.5, center = true);
}
}
group() {
multmatrix([[6.12303e-017, 0, 1, 0], [0, 1, 0, 0], [-1, 0, 6.12303e-017, 0], [0, 0, 0, 1]]) {
cylinder($fn = 0, $fa = 12, $fs = 2, h = 62.5, r1 = 12.5, r2 = 12.5, center = true);
}
}
}
}
}
wmayer
Founder
Posts: 20202
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Import CSG files from OpenSCAD.

Post by wmayer »

How does the structure of a CSG file look like?
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Import CSG files from OpenSCAD.

Post by keithsloan52 »

I edited the original post to include an example
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Import CSG files from OpenSCAD.

Post by shoogen »

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

Re: Import CSG files from OpenSCAD.

Post by keithsloan52 »

Thats the OpenSCAD language. I am going to try and import CSG file which is not the same thing.
I am also looking for something to help parse the CSG without having to write from scratch.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Import CSG files from OpenSCAD.

Post by shoogen »

keithsloan52 wrote: Thats the OpenSCAD language. I am going to try and import CSG file which is not the same thing.
I'm sorry I don't see the difference.
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Import CSG files from OpenSCAD.

Post by keithsloan52 »

shoogen wrote:
keithsloan52 wrote: Thats the OpenSCAD language. I am going to try and import CSG file which is not the same thing.
I'm sorry I don't see the difference.
Try installing OpenSCAD then open a scad file and then export the same file to a CSG file. Do this for the example
files for instance. I think for very simple stuff it might be the same.

The scad file is written in the OpenSCAD language. The CSG file is different it does not contain functions and loads
of language stuff. It just enough to define the objects as combinations of primitive 3d objects. Hopefully a lesser
task than having to deal with the whole language.
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Import CSG files from OpenSCAD.

Post by keithsloan52 »

I just did a test and exported one of the examples as a CSG file. Sure enough it is different from the original.
But if I change the file extension to scad. It will load as a SCAD file and produce the same result.

So I think it fair to say CSG is a subset without some of the language features like functions etc
keithsloan52
Veteran
Posts: 2753
Joined: Mon Feb 27, 2012 5:31 pm

Re: Import CSG files from OpenSCAD.

Post by keithsloan52 »

Found this web page on various parsers for python http://wiki.python.org/moin/LanguageParsing

Any suggestions for one that might be suitable for passing the CSG files?
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Import CSG files from OpenSCAD.

Post by shoogen »

keithsloan52 wrote:So I think it fair to say CSG is a subset without some of the language features like functions etc
it seems to bee a dump of thier in memory tree structure. As there is practicaly no documentaion on this subset, we might have to get through every 'toString()' Method in OpenSCAD.
  • cgaladv.cc
    color.cc
    csgops.cc
    expr.cc
    import.cc
    linearextrude.cc
    node.cc
    node.cc
    primitives.cc
    projection.cc
    render.cc
    rotateextrude.cc
    surface.cc
    transform.cc
    value.cc
Last edited by shoogen on Wed Feb 29, 2012 7:09 am, edited 1 time in total.
Post Reply