Easy to generate file format for efficient FreeCAD import
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Easy to generate file format for efficient FreeCAD import
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?
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?
Re: Easy to generate file format for efficient FreeCAD impor
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.
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.
Re: Easy to generate file format for efficient FreeCAD impor
Yeah, it's the right place to ask.Apologies if this is not the right forum to ask.
What is the input data of your triangulation algorithm? A point cloud or a mathematical description of a surface?Currently I have a setup which triangulates the surface and writes it to an STL file.
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.However, some of my meshes are complex, and the conversion from STL to a 'shape' in FreeCAD is unnaceptable slow.
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.
Re: Easy to generate file format for efficient FreeCAD impor
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.
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.
Re: Easy to generate file format for efficient FreeCAD impor
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.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.
Could you upload an image of such a mesh and/or an STL file to get a first impression, please?
Re: Easy to generate file format for efficient FreeCAD impor
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.crobar wrote:I have developed an interface for CGAL from Matlab/Octave.
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
Re: Easy to generate file format for efficient FreeCAD impor
A plot of the shape before triangulation is shown below:
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.
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.
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.
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 (47.12 KiB) Viewed 4179 times
Re: Easy to generate file format for efficient FreeCAD impor
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?ulrich1a wrote: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.crobar wrote:I have developed an interface for CGAL from Matlab/Octave.
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 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.
Re: Easy to generate file format for efficient FreeCAD impor
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 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?
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.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.
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.
Re: Easy to generate file format for efficient FreeCAD impor
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.
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