Exporting to "STEP with colors" from the CLI (python)

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
le_top
Posts: 20
Joined: Sat Apr 24, 2021 2:13 pm

Exporting to "STEP with colors" from the CLI (python)

Post by le_top »

I've been using FreeCAD to convert 'openscad' files to 'STEP' from the CLI using a python script and I fail to get a STEP with colors.

I thought that this was a limitation of FreeCAD, but when doing a similar operation from the GUI, the "Export STEP with colors" actually exports to a STEP with colors.

Using the FreeCAD GUI, I can open the following code saved as "cube.scad", select the cube, proceed with export "STEP with colors" to a file I called cube.step and then read that cube.step file in a new FreeCAD session where the cube will show up blue.

Code: Select all

color("blue")cube([10,10,10]);

Reading the cube.scad file using a Python script where the exportStep method is called on the Shape, generates a step file where this blue color is lost.

The python script in a simplified form for this example is like this:

Code: Select all

#!/usr/bin/python
import sys
import FreeCAD
import Part
# This should read any type of file
FreeCAD.loadFile("cube.scad")
outfilename="cube.step"

# iterate through all objects
for o in App.ActiveDocument.Objects:
  # find root object and export the shape
  if len(o.InList)==0:
    o.Shape.exportStep(outfilename)
    sys.exit(0)

print("Error: can't find any object")
sys.exit(1)

I suppose that there a better method of exporting the design in order to also export the colors as it is possible to do so from the GUI.
I've tried FreeCAD 0.18 and 0.19, on Windows 10.
Any hint?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by keithsloan52 »

le_top wrote: Sat Apr 24, 2021 2:55 pm I've been using FreeCAD to convert 'openscad' files to 'STEP' from the CLI using a python script and I fail to get a STEP with colors.

I thought that this was a limitation of FreeCAD, but when doing a similar operation from the GUI, the "Export STEP with colors" actually exports to a STEP with colors.

Using the FreeCAD GUI, I can open the following code saved as "cube.scad", select the cube, proceed with export "STEP with colors" to a file I called cube.step and then read that cube.step file in a new FreeCAD session where the cube will show up blue.

Code: Select all

color("blue")cube([10,10,10]);

Reading the cube.scad file using a Python script where the exportStep method is called on the Shape, generates a step file where this blue color is lost.

The python script in a simplified form for this example is like this:

Code: Select all

#!/usr/bin/python
import sys
import FreeCAD
import Part
# This should read any type of file
FreeCAD.loadFile("cube.scad")
outfilename="cube.step"

# iterate through all objects
for o in App.ActiveDocument.Objects:
  # find root object and export the shape
  if len(o.InList)==0:
    o.Shape.exportStep(outfilename)
    sys.exit(0)

print("Error: can't find any object")
sys.exit(1)

I suppose that there a better method of exporting the design in order to also export the colors as it is possible to do so from the GUI.
I've tried FreeCAD 0.18 and 0.19, on Windows 10.
Any hint?
Minor point but slightly more efficient to have
for o inApp.ActiveDocuments.RootObjects :
le_top
Posts: 20
Joined: Sat Apr 24, 2021 2:13 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by le_top »

keithsloan52 wrote: Mon Apr 26, 2021 3:15 pm Minor point but slightly more efficient to have
for o inApp.ActiveDocuments.RootObjects :
Thanks for the heads up, I changed it in my local copy and will propose a change on the github link above.

It doesnt turn the cube to blue in the step unfortunately :|
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by keithsloan52 »

Have you tried setting the legacy exporter option in FreeCAD preferences and experimenting with different STEP scheme, about I can think of.
le_top
Posts: 20
Joined: Sat Apr 24, 2021 2:13 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by le_top »

Reminder, this is about scripting the conversion. Converting a scad file to a STEP file using the GUI adds the colors to the output.

I wanted to make some more conversions, but I am having the same issue (of course).

I've tried many combinations of the options that I could find by reading the code.
  • I tried the Legacy option - the output changes, but still no color.
    I tried the different formats - no color.
    I tried reading a .STEP with the colors and writing it as a step file: the color is lost.
    I tried two different methods of writing the STEP.
    I tried many combinations of the options.
I've attached the files that I tested with, and updated a git gist with the script slightly changed (Linux style):
https://gist.github.com/d14274ac53b64f2 ... fdabed8f9e

When the script is called "convert_shape.py', nor "convert_shape.py cube.scad cube.step" nor "convert_shape.py cubeçwithcolor.step cube2.step" produce steps without color . To quickly check if there is a color specification, I used "grep COLO *.step" which reveals:

Code: Select all

cube_withcolor.step:#358 = FILL_AREA_STYLE_COLOUR('',#359);
cube_withcolor.step:#359 = DRAUGHTING_PRE_DEFINED_COLOUR('blue');
which is the source step in one of the two tests.

Any further suggestion is welcome.
Attachments
convert_shape.py
(3.67 KiB) Downloaded 51 times
cube_withcolor.step
(15.89 KiB) Downloaded 42 times
cube.scad
(31 Bytes) Downloaded 41 times
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by keithsloan52 »

Not your colour issue but looking at the program it says

Code: Select all

# For some reason conversion does not work with cylinders created from
# extruded 2d circles.
Okay would not be hard to create, but do you have a test script for this?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by keithsloan52 »

keithsloan52 wrote: Wed Nov 17, 2021 8:51 pm Not your colour issue but looking at the program it says

Code: Select all

# For some reason conversion does not work with cylinders created from
# extruded 2d circles.
Okay would not be hard to create, but do you have a test script for this?
Okay looks fine to me don't see the issue
ExtrudeCircle.scad
(84 Bytes) Downloaded 38 times
F851EFA4-61F3-44FC-81EC-B1A1075562E5.jpeg
F851EFA4-61F3-44FC-81EC-B1A1075562E5.jpeg (153.57 KiB) Viewed 2947 times
le_top
Posts: 20
Joined: Sat Apr 24, 2021 2:13 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by le_top »

Hi

The comment regarding the extruded cilinder issue is from the original gist:
https://gist.github.com/4853bd36669bb93 ... f51ee1cb82

It's potentially ok in a later version.
le_top
Posts: 20
Joined: Sat Apr 24, 2021 2:13 pm

Re: Exporting to "STEP with colors" from the CLI (python)

Post by le_top »

This can be made to work with FreeCAD, not with FreeCADCmd or python.

The updated script is:
convert_shape.py
(5.91 KiB) Downloaded 92 times
. (Note that my github gist may be more recent.)
Post Reply