Creating STEP with colour

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Batteries Included
Posts: 5
Joined: Sat Jul 09, 2016 7:32 pm

Creating STEP with colour

Post by Batteries Included »

I am using FreeCAD to build STEP files for library of PCB components that will be available free on the web.

I would really like to control the colour of different parts of the output. For example chip pins grey, chip bodies black. Capacitors different colour from resistors.

The scripts will be run from FreeCADCmd, not from the GUI. Here is what I have so far, all I need is a way of passing some color or material info through to the step export code.

So far what I have read suggests its only possible via the GUI, but that seems against the ethos of FreeCAD, and compared to the FreeCADCmd the GUI is slow, compute intensive, and I am running this headless on a machine in the cloud all of which makes me really keen to find a way to work without the GUI.

Any tips, suggestions, other lines of enquiry welcome.

Code: Select all

import Part
import sys
import Draft

PartNumber = 'F981A336MSAAS1'

# Dimensions
BodyLengthAvg = 2.05
BodyWidthAvg = 1.3
HeightMax = 0.9
PinWidth1Avg = 0.5
PinWidth2Avg = 0.5

myDocument = App.ActiveDocument 
myDocument = App.newDocument()
doc=App.newDocument(PartNumber)  

term1 = FreeCAD.ActiveDocument.addObject("Part::Feature","term1") 
body = FreeCAD.ActiveDocument.addObject("Part::Feature","body") 
term2 = FreeCAD.ActiveDocument.addObject("Part::Feature","term2") 

from FreeCAD import Base

term1p = Part.makeBox(PinWidth1Avg,HeightMax,BodyWidthAvg)
term1.Shape = term1p 

bodyp = Part.makeBox(BodyLengthAvg-PinWidth1Avg-PinWidth2Avg,HeightMax,BodyWidthAvg)
bodyp.translate(Base.Vector(PinWidth1Avg,0,0))
body.Shape = bodyp 

term2p = Part.makeBox(PinWidth2Avg,HeightMax,BodyWidthAvg)
term2p.translate(Base.Vector(BodyLengthAvg-PinWidth2Avg,0,0))
term2.Shape = term2p 

doc.recompute()

topObjects=[]
for obj in FreeCAD.ActiveDocument.Objects:
	topObjects.append(obj)

from os.path import expanduser
home = expanduser("~")

Part.export(topObjects,home+'/t.stp')
Part.export(topObjects,home+'/t.stl')
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Creating STEP with colour

Post by wmayer »

It is also possible to export STEP models with color information from the command line. Just append your script by this block

Code: Select all

# export STEP with colors
topObjectsColor=[]
for obj in FreeCAD.ActiveDocument.Objects:
	numfaces = len(obj.Shape.Faces)
	rgb = (1.0,1.0,0.0) # yellow
	colors = [rgb for i in range(numfaces)]
	topObjectsColor.append((obj, colors))

import Import
Import.export(topObjectsColor, home+'/tcolor.stp')
It's up to you to decide which object should get which color. Important is also that the color list is as long as the number of faces of a shape.
Batteries Included
Posts: 5
Joined: Sat Jul 09, 2016 7:32 pm

Re: Creating STEP with colour

Post by Batteries Included »

Brilliant!

Thank you so much. This lets us do exactly what we wanted :D

Jeremy.
Shack
Posts: 35
Joined: Thu Jun 22, 2017 6:19 pm

Re: Creating STEP with colour

Post by Shack »

Hi Jeremy

Have you seen these scripts for generating PCB components?
https://github.com/easyw/kicad-3d-models-in-freecad
Most of the work are by @easyw
They out a step file and a scaled WRL file with material properties for sexy rendering.
You can find the output of the scripts here:
https://github.com/KiCad/kicad-packages3D
Which I believe is the biggest accesible pcb library (with a friendly license)
Currently 2000+ models :)
Batteries Included
Posts: 5
Joined: Sat Jul 09, 2016 7:32 pm

Re: Creating STEP with colour

Post by Batteries Included »

Yeh, seen that one. We are offering an alternative, also free to end users, but with a much larger set of parts. In addition to KiCAD support we have DesignSpark, Eagle, CADSTAR, PADS, Target 3000, xDX Designer and others.

We have a wizard where you can build the more common types of components in your browser from a handful of key dimensions and a list of pin names. For the oddballs that don't fit the wizards we have a free request service.

We don't have 3D for everything yet but its quite widespread. Where we do 3D its in STEP, STL and WRL.

The current challenge I am working on is to upgrade the web preview from STL to something that supports colour.
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: Creating STEP with colour

Post by easyw-fc »

Batteries Included wrote: Sat Sep 23, 2017 12:09 pm Yeh, seen that one. We are offering an alternative, also free to end users, but with a much larger set of parts.
Which is the license of use of your free library parts?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Creating STEP with colour

Post by keithsloan52 »

wmayer wrote: Sat Aug 19, 2017 9:28 am It is also possible to export STEP models with color information from the command line. Just append your script by this block

Code: Select all

# export STEP with colors
topObjectsColor=[]
for obj in FreeCAD.ActiveDocument.Objects:
	numfaces = len(obj.Shape.Faces)
	rgb = (1.0,1.0,0.0) # yellow
	colors = [rgb for i in range(numfaces)]
	topObjectsColor.append((obj, colors))

import Import
Import.export(topObjectsColor, home+'/tcolor.stp')
It's up to you to decide which object should get which color. Important is also that the color list is as long as the number of faces of a shape.
Does a straight STEP ( AP214 format) export also export Colour information ?
Under the impression that it may not so tried to adapt the above to a macro

Code: Select all

import Import
expObjCol=[]
for obj in App.ActiveDocument.Objects :
     if obj.Visibility == True :
        if hasattr(obj,'ViewObject') :
            if obj.ViewObject is not None :
                 if hasattr(obj.ViewObject,'ShapeColor'):
                    print('Export : '+obj.Name)
                    col = obj.ViewObject.ShapeColor
                    colors = [ col for i in range(len(obj.Shape.Faces))]
                    expObjCol.append((obj,colors))
print('Write step file')
Import.export(expObjCol,"/tmp/test.stp")
It is not creating tmp/test.stp

Code: Select all

OS: macOS 10.15
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24276 (Git)
Build type: Release
Branch: (HEAD detached at 0.19.1)
Hash: a88db11e0a908f6e38f92bfc5187b13ebe470438
Python version: 3.8.8
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.4.0
Locale: C/Default (C)
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Creating STEP with colour

Post by keithsloan52 »

Okay latest attempt but still not creating the step file

Code: Select all

mport Import
expObjCol=[]
for obj in App.ActiveDocument.Objects :
     if obj.Visibility == True :
        if hasattr(obj,'ViewObject') :
            if obj.ViewObject is not None :
                 if hasattr(obj.ViewObject,'ShapeColor'):
                    print('Export : '+obj.Name)
                    numFaces = len(obj.Shape.Faces)
                    col = obj.ViewObject.ShapeColor[0:3]
                    print(col)
                    colors = [ col for i in range(numFaces)]
                    expObjCol.append((obj,colors))
print(expObjCol)
print('Write step file')
Import.export(expObjCol,"/Users/keithsloan/Library/Preferences/FreeCAD/Macro/test.stp")
cregenschein
Posts: 19
Joined: Wed Aug 25, 2021 7:36 pm

Re: Creating STEP with colour

Post by cregenschein »

Hi, came across this thread as I also wanted to colorize some faces from a console application. But the approaches shown here do not seem to work for me either. Is there any update or alternative to change the colors of faces using FreeCAD python on the non-gui context?
~ cregenschein
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Creating STEP with colour

Post by keithsloan52 »

cregenschein wrote: Sun Aug 29, 2021 4:07 pm Hi, came across this thread as I also wanted to colorize some faces from a console application. But the approaches shown here do not seem to work for me either. Is there any update or alternative to change the colors of faces using FreeCAD python on the non-gui context?
~ cregenschein
Might be wrong but an objects colour is set in the Objects ViewObject and I think ViewObject is a Gui thing, so you maybe out of luck.
Post Reply