Script Fodder: Cutting Text into a Pad

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
mermaldad
Posts: 29
Joined: Mon Feb 24, 2014 11:16 pm

Script Fodder: Cutting Text into a Pad

Post by mermaldad »

I have been using my Canstruction builds as a training ground for learning how to script objects in FreeCAD. One capability that has eluded me has been cutting text into the pads which are my can support/leveling layers. There is a nice tutorial on how to cut letters into the chamfer of a box using the GUI, but all my attempts to translate this to pad and script failed. So I finally worked everything out. Starting with a video tutorial, I figured out how to cut text into a pad using the GUI while recording it as a macro. I made a number of mistakes along the way, so the resulting script was messy. I went through the script, figuring out what each section was doing, removing all (I hope) the unnecessary commands, and playing with a number of things to test my understanding. The result was the script below. I'm posting it in hopes it will help someone looking to do something similar. Comments on the script content are welcome. If this helped you, please let me know!

Code: Select all

# -*- coding: utf-8 -*-

# Macro Begin +++++++++++++++++++++++++++++++++++++++++++++++++
import FreeCAD
import PartDesign
import PartDesignGui
#import Show.TempoVis
import Sketcher
import DraftTools
import Draft
import Part

doc_name = "Test"

# If there is an old version of the document, close it
try:
	App.getDocument(doc_name)
	App.closeDocument(doc_name)
except NameError:
	pass

# Create a new document, add a body and a sketch
doc = App.newDocument(doc_name)
doc.addObject('PartDesign::Body','Body')
sk = doc.Body.newObject('Sketcher::SketchObject','Sketch')
doc.Sketch.Support = (doc.XY_Plane, [''])
doc.Sketch.MapMode = 'FlatFace'
App.ActiveDocument.recompute()
#
# Add a closed figure to the sketch (in this case a rectangle)
sk.addGeometry(Part.LineSegment(App.Vector(-23.815701,12.635452,0),App.Vector(21.331200,12.706439,0)),False)
sk.addConstraint(Sketcher.Constraint('Horizontal',0)) 
sk.addGeometry(Part.LineSegment(App.Vector(21.331200,12.635452,0),App.Vector(21.331200,-9.512083,0)),False)
sk.addConstraint(Sketcher.Constraint('Coincident',1,1,0,2)) 
sk.addConstraint(Sketcher.Constraint('Vertical',1)) 
sk.addGeometry(Part.LineSegment(App.Vector(21.331200,-9.512083,0),App.Vector(-24.312599,-9.299126,0)),False)
sk.addConstraint(Sketcher.Constraint('Coincident',2,1,1,2)) 
sk.addConstraint(Sketcher.Constraint('Horizontal',2)) 
sk.addGeometry(Part.LineSegment(App.Vector(-24.312599,-9.512083,0),App.Vector(-23.815701,12.635452,0)),False)
sk.addConstraint(Sketcher.Constraint('Coincident',3,1,2,2)) 
sk.addConstraint(Sketcher.Constraint('Coincident',3,2,0,1)) 
sk.addConstraint(Sketcher.Constraint('Vertical',3)) 
sk.addConstraint(Sketcher.Constraint('Distance',1,22.147535)) 
sk.setDatum(8,App.Units.Quantity('30.000000 mm'))
sk.addConstraint(Sketcher.Constraint('Distance',0,45.643799)) 
sk.setDatum(9,App.Units.Quantity('60.000000 mm'))
App.getDocument('Test').recompute()

# Pad the sketch
doc.Body.newObject("PartDesign::Pad","Pad")
doc.Pad.Profile = doc.Sketch
doc.Pad.Length = 10.0
#App.ActiveDocument.recompute()
App.ActiveDocument.Pad.Length = 10.000000
App.ActiveDocument.Pad.Length2 = 100.000000
App.ActiveDocument.Pad.Type = 0
App.ActiveDocument.Pad.UpToFace = None
App.ActiveDocument.Pad.Reversed = 0
App.ActiveDocument.Pad.Midplane = 0
App.ActiveDocument.Pad.Offset = 0.000000
App.ActiveDocument.recompute()

# Create a shapestring
ss=Draft.makeShapeString(String=u"Hello",FontFile="/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",Size=10.0,Tracking=0.0)
plm=FreeCAD.Placement()
plm.Base=FreeCAD.Vector(-20.0,-10.0,0.0)
plm.Rotation.Q=(0.0,-0.0,-0.707106781187,0.707106781187)
ss.Placement=plm
ss.Support=FreeCAD.ActiveDocument.getObject("Pad")
Draft.autogroup(ss)
ss.Placement = App.Placement(App.Vector(-20,-10,0),App.Rotation(App.Vector(0,0,-1),0))
##ss.Size = '1 mm'
ss.Size = '15 mm'

# Extrude the shapestring as a solid
f = FreeCAD.getDocument('Test').addObject('Part::Extrusion', 'Extrude')
f = App.getDocument('Test').getObject('Extrude')
f.Base = App.getDocument('Test').getObject('ShapeString')
f.DirMode = "Normal"
f.DirLink = None
f.LengthFwd = 2.000000000000000
f.LengthRev = 0.000000000000000
f.Solid = True
f.Reversed = False
f.Symmetric = False
f.TaperAngle = 0.000000000000000
f.TaperAngleRev = 0.000000000000000
App.ActiveDocument.recompute()

# Move the extruded shapestring to the placement (Learned: Subsequent moves will be relative)
Draft.move([FreeCAD.ActiveDocument.Extrude],FreeCAD.Vector(-6.6,3.2,8.0),copy=False)
FreeCAD.ActiveDocument.recompute()

# Cut the extruded shapestring from the pad
doc.addObject("Part::Cut","Cut")
doc.Cut.Base = doc.Pad
doc.Cut.Tool = doc.Extrude
App.ActiveDocument.recompute()
Gui.getDocument("Test").getObject("ShapeString").Visibility=False # Learned: App.getDocument and Gui.getDocument are different!

# Macro End+++++++++++++++++++++++++++++++++++++++++++++++++
Post Reply