Easy to generate file format for efficient FreeCAD import

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!
crobar
Posts: 160
Joined: Fri Aug 29, 2014 1:26 pm

Easy to generate file format for efficient FreeCAD import

Post by crobar »

Hello,

Apologies if this is not the right forum to ask.

I have developed an interface for CGAL from Matlab/Octave. I would like to efficiently import the meshes created using CGAL to FreeCAD. Currently I have a setup which triangulates the surface and writes it to an STL file. However, some of my meshes are complex, and the conversion from STL to a 'shape' in FreeCAD is unnaceptable slow.

My question is therefore, is there a suitible file format to which I can write the CGAL mesh which will be more efficiently converted to a shape by FreeCAD? Ideally the format would either be very simple to generate, or have an easy to use, stable, mature C or C++ library for it's creation.

Alternatively, is there any other solution I'm overlooking for this?
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Easy to generate file format for efficient FreeCAD impor

Post by ickby »

There is none. It has nothing to do with the file format but with the fact that meshes are very low-level entitys and freecad shapes are high-level entitys with lot's of information attached. There is no way to generate the high-level information from the low-level stl. Therefore freecad creates a planar face for every mesh triangle when you try to convert mesh to shape. However, as a face in freecad is a complex entity it takes very long to create many and needs a huge amount of memory. And the more faces you have for one part the more unusable the mdoel becomes in the end.

A other possible way is to fit high-level geometry into the stl mesh, but that seems rather hard to do for complex shapes with more than a few surfaces to fit. And currently there are only very few tools available in freecad tha tcould be partially used for that, you can have a look at the reverse engineering workbench.
wmayer
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Easy to generate file format for efficient FreeCAD impor

Post by wmayer »

Apologies if this is not the right forum to ask.
Yeah, it's the right place to ask.
Currently I have a setup which triangulates the surface and writes it to an STL file.
What is the input data of your triangulation algorithm? A point cloud or a mathematical description of a surface?
However, some of my meshes are complex, and the conversion from STL to a 'shape' in FreeCAD is unnaceptable slow.
A mesh is only a set of triangles without any information about the higher-level surfaces. Thus, converting a mesh to a shape doesn't make much sense in most cases because it only creates the BREP structure but cannot reverse engineer the actual surfaces.

So back to my initial question: if your input data is a point cloud you can first triangulate it and then use some algorithms to approximate the segments step by step. Here you need some robust algorithms for plane fit, cylinder fit, cone fit, sphere fit and a general NURBS fit.

If your input data is already a higher-level surface then you shouldn't triangulate it because this way you lose the information which you can't get back easily or even not at all. Instead you should try to create directly the BREP structure. The CAD kernel OpenCascade (OCC) that we use offers a wide range of standard (surface) geometries like plane, cylinder, sphere, ..., Bezier and NURBS surfaces. Now I don't know what kind of surfaces you produce but for arbitrary surfaces (some complex parametric surfaces or so) you probably won't find a counterpart in OCC. In this case you must describe or approximate it as a NURBS surface.
crobar
Posts: 160
Joined: Fri Aug 29, 2014 1:26 pm

Re: Easy to generate file format for efficient FreeCAD impor

Post by crobar »

The input data is a set of vertices (x,y,z locations), and a set of faces represented by sets of indices of the vertices. The faces are polygons of any number of vertices. The set of faces can be assumed to form a closed surface (e.g. it might be created from an extruded polygon).

This surface mesh is indirectly created by the CGAL library, but I am working with it in Matlab. To create the STL I am forced to triangulate this surface, then generate the STL.

I am only creating STL as I happen to have a function which can write STL files.
wmayer
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Easy to generate file format for efficient FreeCAD impor

Post by wmayer »

The input data is a set of vertices (x,y,z locations), and a set of faces represented by sets of indices of the vertices.
So, this means it is already a polygon mesh and you triangulate it to get a triangle mesh in order to export it as STL.

Could you upload an image of such a mesh and/or an STL file to get a first impression, please?
ulrich1a
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: Easy to generate file format for efficient FreeCAD impor

Post by ulrich1a »

crobar wrote:I have developed an interface for CGAL from Matlab/Octave.
FreeCAD can use OpenSCAD scripts (Shoogen, please correct me, if I am wrong!) to convert them to FreeCAD-shapes. OpenSCAD uses also the CGAL library.

So I would thing, it will be easier to use directly your Matlab-commands and interpret them by an python script, to convert it into a FreeCAD-shape similar to the OpenSCAD workbench.

This approach has the advantage, to maintain the primery geometric data.

Ulrich
crobar
Posts: 160
Joined: Fri Aug 29, 2014 1:26 pm

Re: Easy to generate file format for efficient FreeCAD impor

Post by crobar »

A plot of the shape before triangulation is shown below:
extruded_part_matlab_untriangulated.png
extruded_part_matlab_untriangulated.png (16.69 KiB) Viewed 4178 times
It is extruded from the end polygon which has about 1000 vertices. This shape has about 1000 faces. Below is the part after triangulation, export to stl, and import to FreeCAD. It has many more faces.
The attachment triangulated_extruded_part_with_lines.png is no longer available
I have now experimented with OFF files, but it seems FreeCAD will not process faces with more than 4 vertiecs (an extruded 5-sided shape skips the end faces). If I export such a shape to OFF from Freecad it is converted to triangles. I have also attached the STL.
Attachments
test_problem2polygon_mfemm.stl
(202.82 KiB) Downloaded 51 times
triangulated_extruded_part_with_lines.png
triangulated_extruded_part_with_lines.png (47.12 KiB) Viewed 4178 times
crobar
Posts: 160
Joined: Fri Aug 29, 2014 1:26 pm

Re: Easy to generate file format for efficient FreeCAD impor

Post by crobar »

ulrich1a wrote:
crobar wrote:I have developed an interface for CGAL from Matlab/Octave.
FreeCAD can use OpenSCAD scripts (Shoogen, please correct me, if I am wrong!) to convert them to FreeCAD-shapes. OpenSCAD uses also the CGAL library.

So I would thing, it will be easier to use directly your Matlab-commands and interpret them by an python script, to convert it into a FreeCAD-shape similar to the OpenSCAD workbench.

This approach has the advantage, to maintain the primery geometric data.

Ulrich
I have seen mentioned in various places the OpenSCAD .csg file format, but can't find any details of it's format, maybe this would be a possible interchange format? Do you know where I could get information on this?

I created the Matlab interface to CGAL (actually based on someone elses python interface) because OpenSCAD didn't quite have the power I needed and doesn't interface nicely with my other code. I really use these shapes for finite element analysis, but the ability to export them to a CAD package would finish the toolchain off nicely.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Easy to generate file format for efficient FreeCAD impor

Post by shoogen »

crobar wrote:I have seen mentioned in various places the OpenSCAD .csg file format, but can't find any details of it's format, maybe this would be a possible interchange format? Do you know where I could get information on this?
It is a subset of the OpenSCAD language. There is no reference doucmentation. You would need to read the OpenSCAD source code. It is useful if you are dealing with CSG data. But if you deal with linear segments from the beginning there is no purpose in using the .csg file format or OpenSCAD in general.
crobar wrote:I created the Matlab interface to CGAL (actually based on someone elses python interface) because OpenSCAD didn't quite have the power I needed and doesn't interface nicely with my other code. I really use these shapes for finite element analysis, but the ability to export them to a CAD package would finish the toolchain off nicely.
If you are working with linear segments there is no point in importing the data into CAD program, besides using the present mesh interfaces. If I understand you correctly, you currently do not intend to use the model in FreeCAD. But there is no purpose in saving a STEP file which bears the same information as a STL file.
It might be possible to use spline approximation, to get a nice CAD model. But then you should consider that geomtry in the first place, right inside of Matlab. Which segments are, linear, are there circular or eliptic arcs or other conic sections, and are there segments that should approximated by spline interpolation.
wmayer
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Easy to generate file format for efficient FreeCAD impor

Post by wmayer »

Here is a Python macro that might be useful.

The steps:
1. Load your STL file
2. Cut the extrusion to get only the base
3. Run the macro to create a single face
4. Use the Part extrusion from FreeCAD (value: -0.2)

This happens in almost no time while the mesh-to-shape seems to take forever.

Improvements:
There is still place for improvements like replacing the circular part with a real arc of circle.
Attachments
test_problem2polygon_mfemm.fcstd
(460.64 KiB) Downloaded 59 times
Post Reply