Write/Read many parts as color .step files

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
JF2791
Posts: 19
Joined: Fri Jan 22, 2021 2:52 pm

Write/Read many parts as color .step files

Post by JF2791 »

The following script draws a blue and red box on the GUI, and writes the blue box to a monochrome .step file.

I would like:-

1) To write the blue box to a color .step file
2) Write both parts to a single color .step file.

I have third party color .step files that show as colored when opened using File -> Open in the GUI, but because
Part is monochrome, are monochrome when read in a script using Part.read ( StepPathname ). How can I read
them in as colored?

Thanks.

Code: Select all

import FreeCAD
import Part

from FreeCAD import Vector

import ImportGui

def SetPart ( App, Part, Name, Color ) :
  Obj                       = App.ActiveDocument.addObject ( "Part::Feature", Name )
  Obj.Shape                 = Part
  Obj.ViewObject.ShapeColor = Color
  return Obj

def Display () :
  doc = FreeCAD.newDocument()

  BlueBox = Part.makeBox ( 2, 3,   4 )
  RedBox  = Part.makeBox ( 1, 1.5, 2 )
  RedBox.translate ( Vector ( 4, 0, 0 ) )

  BlueObj = SetPart ( FreeCAD, BlueBox, "BlueBox", (0.0,0.0,1.0) )
  RedObj  = SetPart ( FreeCAD, RedBox,  "RedBox",  (1.0,0.0,0.0) )

  BlueObj.Shape.exportStep ( "BlueBox.step" ) # How do I export as a colored step file?

# How do I export both the Blue and Red boxes to the same colored step file?

  Gui.activeDocument().activeView().viewIsometric()
  Gui.activeDocument().activeView().setCameraType("Perspective")

  Gui.SendMsgToActiveView("ViewFit")

if __name__ == "__main__" : Display()
Post Reply