Discussion:
[OpenSCAD] OpenSCAD output of BREP file format.
KeithSloan52
8 years ago
Permalink
Hi back in December a user named kintel posted the following in the FreeCAD
forum
https://forum.freecadweb.org/viewtopic.php?f=10&t=18934

"Hi all,

OpenSCAD maintainer here.

I'm doing some work on the .csg export from OpenSCAD, and I've heard from a
few places that there are some constructs that doesn't map well to FreeCAD
(hulls, minkowski, text, ...?). What I'm planning to implement is a very
minimal version of the .csg file format, which includes _only_ union,
difference, intersection, polyhedron and polygon nodes.
This might be a bit extreme in terms of FreeCAD import, but I'm interested
in feedback on _which_ nodes you'd like to see or not like to see in .csg
files, and ideas for how to specify this e.g. on the cmd-line.

-Marius"

This got me thinking and wondering if OpenSCAD could be enhanced to output
BREP file format as native to OCC.

The BREP file format is used by CAD Exchanger see
https://forum.freecadweb.org/viewtopic.php?f=9&t=22227

and whilst I would be sad to see all the work I and Sebastian Hogan did to
develop the FreeCAD importer for OpenSCAD it would make for a better synergy
between the two applications.

Thanks



--
View this message in context: http://forum.openscad.org/OpenSCAD-output-of-BREP-file-format-tp21443.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
TLC123
8 years ago
Permalink




--
View this message in context: http://forum.openscad.org/OpenSCAD-output-of-BREP-file-format-tp21443p21444.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Carsten Arnholm
8 years ago
Permalink
Post by TLC123
http://youtu.be/sXbRT439vRI
This illustrates that s Constructive Solid Geometry (CSG) model as in
OpenSCAD is rather (i.e. very) different from a BREP model as in
OpenCasCade(OCC) and similar systems. It is not just a question of
physical file formats, the basic concepts are not even close. OpenSCAD
is mesh based and the final geometry/topology is implicit, in OCC this
is not so.

Therefore it is really only the basic primitives (cube,sphere,cylinder)
that can be properly translated to brep. You can of course convert a
polyhedron as a brep also, but this will always reveal its mesh origin
and thus be an 'alien' in the brep world.

linear_extrude and rotate_extrude of 2d profiles should be doable,
especially if the profile is defined as a 2d booleans of basic circle,
square etc. I don't know if OCC has 2d booleans though.

Carsten Arnholm
KeithSloan52
8 years ago
Permalink
Yes OpenSCAD works with meshes and FreeCAD works with BREP which are not the
same thing. I maybe wrong but I don't see anything in the OpenSCAD language
that prerequisites Meshes its just that OpenSCAD has decided to implement
things as meshes. (Okay I think there might be the odd thing that requires a
mesh, but not in the main )

If you look at what FreeCAD does when you open a SCAD or CSG file it does a
reasonable job of importing things to BREP format. ( I am biased as one of
the original developers of the FreeCAD OpenSCAD importer ) At the moment it
has to use Meshes with hull, minkowski requests and a few others, but I
don't see why the output of hull or minkowski request could not be a BREF
shape as opposed to a mesh.

I am not an OpenSCAD developer, but the original enquiry in the FreeCAD
forum was from an OpenSCAD developer who was enquiring about changing CSG
import/export. I would say that rather than change the CSG I would look at
BREP.

Maybe it would require OpenSCAD to work internally with both BREP and Mesh
objects and maybe that is too much effort, but would be a great improvement
in my view.




--
View this message in context: http://forum.openscad.org/OpenSCAD-output-of-BREP-file-format-tp21443p21449.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
doug moen
8 years ago
Permalink
Hi Keith.

You said "I don't see why the output of hull or minkowski request could not
be a BREF shape as opposed to a mesh.", and "Maybe it would require
OpenSCAD to work internally with both BREP and Mesh objects".

In effect, you are asking for an implementation of Convex Hull and
Minkowski Sum that works on BREP instead of meshes. That sounds difficult,
like a research project. I don't think it is the job of the OpenSCAD
project to implement this. This is something that belongs in OpenCascade.
If somebody adds these operations to OpenCascade, and FreeCAD adds support
for hull and minkowski using the OpenCascade implementation, then FreeCAD
can import OpenSCAD CSG trees that contain hull and minkowski nodes.

However, I don't think this is going to happen any time soon. It sounds
like a lot of work. In my opinion, a cheaper and easier way to improve the
quality of the OpenSCAD import mechanism in FreeCAD is to add more high
level information to the OpenSCAD CSG tree, to guide the translation from
CSG to FreeCAD.

I have a fairly simple proposal for how to do this.

We introduce a new kind of module, called a "primitive". A primitive is
exactly like a module, except that it preserves its identity in the CSG
tree, when you export a model as a *.csg file.

We add a new keyword, "primitive". For example,

primitive prism(n, h=1, r=1, center=false)
cylinder($fn=n, h, r, center);
prism(6);

The CSG tree for this program will contain
prism(n=6, h=1, r=1, center=false)
instead of containing a call to cylinder.

Once this feature has been added to OpenSCAD, the FreeCAD developers
responsible for OpenSCAD import can provide an OpenSCAD library of
primitives that are supported by FreeCAD. Users can improve the quality of
OpenSCAD import in FreeCAD by using these primitives in their OpenSCAD
model.

Doug Moen.
...
KeithSloan52
8 years ago
Permalink
Hi Doug

"To improve the quality of the OpenSCAD import mechanism in FreeCAD is to
add more high level information to the OpenSCAD CSG tree, to guide the
translation from CSG to FreeCAD."

Don't really see how your example helps. Yes it would be useful to
distinguish between an n sided prism and a cylinder, but I think the current
method of the FreeCAD importer to treat prisms with n sides more than a
setting as a cylinder works reasonably well. I am also not sure the OpenSCAD
programmer has the option to specify one or the other,

More of an issue is use of hull and minkowski statements that cause elements
to be created as meshes.

Hope you are not going to introduce changes to CSG without the option to
produce the existing format, as otherwise you are going to break the
existing parser in the FreeCAD importer



--
View this message in context: http://forum.openscad.org/OpenSCAD-output-of-BREP-file-format-tp21443p21451.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
doug moen
8 years ago
Permalink
I agree the "prism" example is trivial: I picked it as a simple way to
illustrate the syntax and mechanism.

What's needed is big, high level primitives that encapsulate OpenSCAD
idioms that need to be translated into an entirely different set of FreeCAD
primitives.

Minkowski sum is often used to implement 3D offsetting in OpenSCAD. FreeCAD
has a built-in 3D offset command. So, define a 3D offset primitive that's
implemented using Minkowski sum in OpenSCAD, but which translates into the
FreeCAD 3D offset command.

Convex hull is sometimes used in an idiom called "hull chaining", which is
used to create polygonal approximations of curved objects. These same
curved objects can probably be represented exactly in FreeCAD. So, define
some high level primitives that directly construct parametrically curved
objects.

Parkinbot created a spline package for OpenSCAD, which can be used with
sweep() to construct polyhedrons that approximate arbitrary curved shapes.
So, define some high level BREP spline primitives that map easily to
FreeCAD, but which are implemented in OpenSCAD using splines, sweeps and
polyhedron. The benefit is that, once imported into FreeCAD, the shapes
will be represented exactly, rather than as polyhedral approximations.
...
Marius Kintel
8 years ago
Permalink
Hi Keith,

See here for the current state of a pure polyhedron CSG export: https://github.com/openscad/openscad/pull/1882

FYI: The motivation to export cleaner CSG files came from talks with mesh processing researchers who wanted to avoid dealing with more complex primitives and transformations; i.e. it’s driven by a need for more mesh-based geometry.
One of the next steps I wanted to explore was, as Doug suggested, to allow users to provide a list of acceptable node types in a CSG export. This could also benefit the FreeCAD importer as you’d no longer have to perform extra steps to render out some of the nodes not supported by the importer.

It’s well outside of our current scope to support transforming files to 3rd party file formats like OCC BREP, but we’re happy to accept contributions to that effect. Even if this work would come as a separate executable with other dependencies it could benefit from being run with the OpenSCAD test suite to be able to pick up changes in the CSG file format as OpenSCAD moves forward.

In related news, there is also work underway to package the OpenSCAD parser and evaluator into a separate component. This is still early, but here’s a PR taking steps in that direction: https://github.com/openscad/openscad/pull/1743. By using such a component, software like the FreeCAD importer could be able to also pick up parametrization of objects in OpenSCAD and convert that to the corresponding parametrization concepts of FreeCAD itself. This is likely a decent bulk of work though, but I’m sure there are other interesting things this could be used for.

Cheers,

-Marius
KeithSloan52
8 years ago
Permalink
Post by Marius Kintel
Hi Keith,
https://github.com/openscad/openscad/pull/1882
For me the link is broken, but if it means everything get treated a
polyhedron, that will not help with FreeCAD as the rational for importing is
to be able to do normal 3D CAD on things
...
So what happens to the not supported nodes? Ignored? Not sure that would
help at all.

As one of the two original developers of the FreeCAD importer ( Sebastian
Hoogen the other no longer has time for importCSG) I would urge you NOT to
change the CSG export. Or at least have the option to export in the current
way ideally as the default.





--
View this message in context: http://forum.openscad.org/OpenSCAD-output-of-BREP-file-format-tp21443p21454.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Marius Kintel
8 years ago
Permalink
Post by KeithSloan52
So what happens to the not supported nodes? Ignored? Not sure that would
help at all.
Unsupported nodes would in this proposal simply be rendered out as explicit polyhedrons.
Post by KeithSloan52
I would urge you NOT to change the CSG export. Or at least have the option to export in the current
way ideally as the default.
Backward compatibility is, as always, in our mind, but as OpenSCAD is an evolving software, I wouldn’t bank on this always staying the same (unless you simply keep using the existing version of software). There has been some discussions on how to version the file format, which would allow 3rd party software to detect if they’ve implemented support for the files being processed.

As an example of a recent change, rotate_extrude() now supports an “angle” parameter. I’m not sure if that has yet been added to the FreeCAD importer.

-Marius
KeithSloan52
8 years ago
Permalink
Post by Marius Kintel
Post by KeithSloan52
So what happens to the not supported nodes? Ignored? Not sure that would
help at all.
Unsupported nodes would in this proposal simply be rendered out as explicit polyhedrons.
That would be very useful. The important think from a FreeCAD importer point
of view would be that such options could be turned on/off via command line
switch(s) as FreeCAD calls the command line version of OpenSCAD under the
covers
Post by Marius Kintel
As an example of a recent change, rotate_extrude() now supports an “angle”
parameter. I’m not sure if that has yet been added to the FreeCAD
importer.
I will look into that when I can find the time.

Not sure how many FreeCAD users use the OpenSCAD importer. I know Sebastian
Hoogen was of the opinion that not enough people used it to worry about it.
Maybe it would be useful to have a survey in the FreeCAD forum (if that is
possible)



--
View this message in context: http://forum.openscad.org/OpenSCAD-output-of-BREP-file-format-tp21443p21465.html
Sent from the OpenSCAD mailing list archive at Nabble.com.