Creating STEP with colour

Need help, or want to share a macro? Post here!
Batteries Included
Posts: 5
Joined: Sat Jul 09, 2016 7:32 pm

Creating STEP with colour

Postby Batteries Included » Wed Jul 19, 2017 6:26 pm

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
Site Admin
Posts: 14585
Joined: Thu Feb 19, 2009 10:32 am

Re: Creating STEP with colour

Postby wmayer » 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.
Batteries Included
Posts: 5
Joined: Sat Jul 09, 2016 7:32 pm

Re: Creating STEP with colour

Postby Batteries Included » Sat Aug 19, 2017 2:46 pm

Brilliant!

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

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

Re: Creating STEP with colour

Postby Shack » Fri Sep 22, 2017 10:01 pm

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

Postby Batteries Included » 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. 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
Posts: 2611
Joined: Thu Jul 09, 2015 9:34 am

Re: Creating STEP with colour

Postby easyw-fc » Sat Sep 23, 2017 12:43 pm

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?