2

I am building a service to convert STEP files to STL files for user viewing. This conversion is just so the user can view the file uploaded to the server. It is not used to modify the file.

However, the tested conversion libraries can not efficiently convert from STEP to STL.

I first tried FreeCAD, and made the following forum post: https://forum.freecad.org/viewtopic.php?t=79333

How to install FreeCAD for scripting: https://forum.freecad.org/viewtopic.php?t=79327

For FreeCAD, the code goes as follows:

Part.open(fileNameFrom) # STEP File on server
Mesh.export( FreeCAD.getDocument("Unnamed").findObjects(), fileNameOut) # STL file on server

However, FreeCAD's process is so inefficient that a 22 MB STEP file outputs as a 366 MB STL file after two minutes of conversion. Even smaller-sized 500 KB files still produce an 18 MB file output, with no documentation specifying less resolution to reduce file size.

The next coding library I tried was cadquery. The syntax and setup for a server is simplistic, as this library can be directly installed from PIP, unlike FreeCAD.

import cadquery as cq
afile = cq.importers.importStep(fileNameFrom)
cq.exporters.export(afile,fileNameOut)

Cad Query initially performed decent, producing an 8 MB output STL file for the same 500 KB STEP test file. However, the 22 MB STEP file was converted to a 386 MB STL file.

The STEP files were not complex enough to yield such file sizes. This is the 22 MB STEP file:

Circuit Board 22 MB STEP File

Here is the 500 KB STEP file:

Door Hinge 500 KB STEP File

What libraries can I try that can easily convert STEP to STL without large file sizes? It is preferred that the library is in Python or Node. However, Java or C++ are also acceptable; our team can work with such servers. I hypothesize C++ based libraries would have the best processing speed.

M. Nicol
  • 73
  • 1
  • 5
  • 1
    Post a [mre] including sample files. – relent95 Jul 13 '23 at 03:08
  • 1
    Mostly a STEP file is larger than a binary STL file. – relent95 Jul 13 '23 at 03:14
  • @relent95 I made this post knowing FreeCAD could not be the solution, as others on the forums had previously mentioned how inefficient FreeCAD's conversion is. Over the past two weeks of communication on the FreeCAD forums, no one in the community could provide an example of a reduction in file size. This post was designed to be open-ended for any conversion software. A minimal reproducible example is difficult (although I linked to one in the post) because FreeCAD can not be installed via PIP. – M. Nicol Jul 13 '23 at 03:50
  • 1
    You should provide a sample STEP file which converts to a larger binary STL file. – relent95 Jul 13 '23 at 08:03
  • https://drive.google.com/file/d/1V1noEyDhaXHIQDCn-aEP9Iw60VpARLi7/view?usp=sharing – M. Nicol Jul 14 '23 at 03:14
  • 1
    That link requires an access priviledge. – relent95 Jul 14 '23 at 06:39
  • I have fixed the access privilege issues. – M. Nicol Jul 14 '23 at 13:57

2 Answers2

1

Have a look at DG Kernel: https://dynoinsight.com/ProDown.htm. ModelTest::SetGeometryTypeEx() in http://dynoinsight.com/Prod/Examples/InterfaceTestsVC.zip demonstrates generation of different density meshes defined by deviation. It is C++.

Regards

Nick
  • 26
  • 2
  • The program can specify deviations that alter the file size by looking through the code. Does this library have stable enough versions for production usage? Is the forum community active? How fast is the computation? – M. Nicol Jul 12 '23 at 16:49
  • 1
    DInsigh has been around since 2001. There are many commercial apps around the world based on it. Core of this particular functionality is based on the widely adopted Open Cascade engine integrated into DGK. DGK overcomes one significant limitation in OCCT that its mesh generated only per face. DGK's mesh is watertight. The forum is active. Support is very responsive. – Nick Jul 13 '23 at 22:48
  • 1
    DGK is as fast as it can be. I would say it is close to practically possible speed. STEP load takes about 90% of the time. – Nick Jul 13 '23 at 23:28
  • What file types can it work with? – M. Nicol Jul 14 '23 at 13:53
  • 1
    The list: https://dynoinsight.com/Help/V7_1/DGKC/Models/DataExchange/Export.aspx. AutoCAD formats are supported "on demand". – Nick Jul 15 '23 at 22:30
  • In the file you linked to download, is that a full application with a GUI or just the minimum setup required for the file conversion? Can any of that code be removed and still have access to just file conversion? – M. Nicol Jul 15 '23 at 23:52
  • 1
    The InterfaceTestsVC.zip is a quick test and an example of programming the functionality. Only few lines are needed. It is a empty MCF GUI app. It does not really need GUI. We can send you a command line example. Pls get in touch via https://dynoinsight.com/Contact.aspx. The app needs DGK runtime to be installed. – Nick Jul 17 '23 at 01:38
1

It's because your STEP file contains analytical surfaces like a cylinder, NURBS, etc. The STL file needs meshes(triangles) that are calculated by a tessellation process, and it usually results in a larger file.

Freecad uses OCCT(Open Cascade) and it seems you are using an older version of OCCT. You can use pythonocc-core binding, which provides OCCT verion 7.7.0 via Conda currently.

This is an example of the conversion.

from OCC.Extend.DataExchange import *

shape = read_step_file('circuit.step')
write_stl_file(shape, 'circuit.stl', mode='binary',
    linear_deflection=0.1, angular_deflection=0.5)

In my system, the circuit.step of size 29MB is converted into a STL file of size 29MB. You can tune the tessellation process with deflection options. See the OCCT reference.

relent95
  • 3,703
  • 1
  • 14
  • 17
  • Thank you, what file conversion types does this work for? Currently, we are focused on STEP to STL, but the ability to do STL, OBJ, and STEP is a feature down the line. We understand that this may require a combination of different libraries. – M. Nicol Jul 14 '23 at 18:42
  • 1
    OCCT supports various formats like STEP, XCAF, IEGS, VRML, OBJ, etc. See [DataExchange](https://dev.opencascade.org/doc/occt-7.7.0/refman/html/module_dataexchange.html) and [TKRWMesh](https://dev.opencascade.org/doc/occt-7.7.0/refman/html/toolkit_tkrwmesh.html). Currently, pythonocc-core does not provide high level API for OBJ files, but you can use low level API such as the ```OCC.Core.RWObj.RWObj_CafReader```, ```OCC.Core.RWObj.RWObj_CafWriter```, etc. – relent95 Jul 15 '23 at 06:17
  • can the write_stl_file be configured to go to S3? – M. Nicol Aug 17 '23 at 17:48
  • I also tried conda install pythonocc-core and pip install pythonocc-core and the package does not exist. – M. Nicol Aug 17 '23 at 18:02
  • @M.Nicol, you should run ```conda install -c conda-forge pythonocc-core```. – relent95 Aug 18 '23 at 00:32
  • @M.Nicol, the ```write_stl_file()``` and underlying C++ ```StlAPI_Writer::Write()``` expect a file path. So for writing into S3 directly, you need to use a file system emulator such as [S3fs-fuse](https://github.com/s3fs-fuse/s3fs-fuse). – relent95 Aug 18 '23 at 00:47
  • Running the conda install command took 20 minutes to complete, and attempting to import "OCC.Extends [...]" results in "No module named OCC" error. For the read_step_file, can it read from a URL? The files are saved on an AWS bucket with public read. – M. Nicol Aug 19 '23 at 18:55
  • @M.Nicol, see [Download/install binaries for Linux/OSX/Windows](https://github.com/tpaviot/pythonocc-core#downloadinstall-binaries-for-linuxosxwindows). You need to create a virtual environment. And for S3, I already said it's impossible to read from a URL directly. – relent95 Aug 21 '23 at 05:04