Macro to Create Clock Face

Need help, or want to share a macro? Post here!
User avatar
cblt2l
Posts: 155
Joined: Sat May 15, 2010 3:59 am

Macro to Create Clock Face

Postby cblt2l » Sat Nov 23, 2013 5:34 pm

I plan on 3D printing some clocks faces, but don't have a way to position the numbers in a circular pattern (other than manually placing them). I initially tried a polar array but couldn't find a way to keep the numbers horizontal so I created this script. I'm new to python and freecad macros in general so it may be a bit rough. Thanks to danfalck for helping me out on the irc channel.

I would like to make some improvements so if anyone has ideas or tips please let me know.

1) Make the feature parametric so I can change the radius, thickness, etc in the data box instead of rerunning the macro.
2) Delete the shapestring features after the extrusion is made. - Fixed, thanks danielfalck

Code: Select all

import Draft
import Part
from math import cos, sin, radians

# Default Values
radius = 35
size = 10.0
angle = 30
thickness = 2

shapes = []
label_list = []
# Create the numbers
for x in xrange(1, 13):
   pos = angle * x - 90
   x_val = radius * cos(-radians(pos))
   y_val = radius * sin(-radians(pos))
   print(x_val)
   print(y_val)
   ss=Draft.makeShapeString(String=str(x),FontFile="/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",Size=size,Tracking=0)
   centroid = ss.Shape.BoundBox.Center
   plm=FreeCAD.Placement()
   plm.Base=FreeCAD.Vector(x_val - centroid[0],y_val - centroid[1],0)
   ss.Placement=plm
   shapes.append(ss.Shape)
   label_list.append(ss.Label)

# Create the Solid
comp = Part.makeCompound(shapes)
extrude = comp.extrude(App.Base.Vector(0,0,thickness))
Part.show(extrude)

#remove the shapestrings
for l in label_list:
    App.getDocument("Unnamed").removeObject(l)
clock-test.png
clock-test.png (27.98 KiB) Viewed 3795 times
clock-test.fcstd
(115.27 KiB) Downloaded 172 times
Last edited by cblt2l on Mon Nov 25, 2013 12:24 am, edited 1 time in total.
User avatar
NormandC
Posts: 18534
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: Macro to Create Clock Face

Postby NormandC » Sat Nov 23, 2013 6:56 pm

Very nice! :)

This may be similar to the python function wandererfan has programmed, it's not merged into the master code branch yet. viewtopic.php?f=17&t=5007
danielfalck
Posts: 395
Joined: Fri Oct 07, 2011 8:58 pm
Location: Beaverton,Oregon, USA
Contact:

Re: Macro to Create Clock Face

Postby danielfalck » Sat Nov 23, 2013 7:38 pm

Very nice indeed. Thanks for showing this.
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: Macro to Create Clock Face

Postby quick61 » Sat Nov 23, 2013 10:12 pm

Like the others have commented, it's a shiny little script.

As to your question:
cblt2l wrote:2) Delete the shapestring features after the extrusion is made.


You can always use - App.getDocument("Unnamed").removeObject("ShapeString") ~ App.getDocument("Unnamed").removeObject("ShapeString011") - but that seems to be a very sloppy and amateurish way to go about it. It should be something like - for obj in FreeCADGui.Selection.getSelection(): - then the "remove ShapeString" but that's one of the parts about FreeCAD/Python that flat out confounds me. How in the world do you get Python to loop through each one of the ShapeStrings and remove them?

Anyway, here's your script with something that works, though it's total crap and a grand display of my lack of Python skills. But it does remove the ShapeStrings. :roll: :oops: :?

Code: Select all

import Draft
from math import cos, sin, radians

# Default Values
radius = 35
size = 10.0
angle = 30
thickness = 2

shapes = []

# Create the numbers
for x in xrange(1, 13):
   pos = angle * x - 90
   x_val = radius * cos(-radians(pos))
   y_val = radius * sin(-radians(pos))
   print(x_val)
   print(y_val)
   ss=Draft.makeShapeString(String=str(x),FontFile="/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",Size=size,Tracking=0)
   centroid = ss.Shape.BoundBox.Center
   plm=FreeCAD.Placement()
   plm.Base=FreeCAD.Vector(x_val - centroid[0],y_val - centroid[1],0)
#   plm.Rotation.Q=(0.0,-0.0,-0.0,1.0)
   ss.Placement=plm
#   ss.Support=None
   shapes.append(ss.Shape)

# Create the Solid
comp = Part.makeCompound(shapes)
extrude = comp.extrude(App.Base.Vector(0,0,thickness))
Part.show(extrude)

#for obj in FreeCADGui.Selection.getSelection():#This should be the start of only a line or two, 
   #App.getDocument("Unnamed").removeObject("ShapeString")#but below is all I can get to work.
App.getDocument("Unnamed").removeObject("ShapeString")
App.getDocument("Unnamed").removeObject("ShapeString001")
App.getDocument("Unnamed").removeObject("ShapeString002")
App.getDocument("Unnamed").removeObject("ShapeString003")
App.getDocument("Unnamed").removeObject("ShapeString004")
App.getDocument("Unnamed").removeObject("ShapeString005")
App.getDocument("Unnamed").removeObject("ShapeString006")
App.getDocument("Unnamed").removeObject("ShapeString007")
App.getDocument("Unnamed").removeObject("ShapeString008")
App.getDocument("Unnamed").removeObject("ShapeString009")
App.getDocument("Unnamed").removeObject("ShapeString010")
App.getDocument("Unnamed").removeObject("ShapeString011")
This post made with 0.0% Micro$oft products - GOT LINUX?
danielfalck
Posts: 395
Joined: Fri Oct 07, 2011 8:58 pm
Location: Beaverton,Oregon, USA
Contact:

Re: Macro to Create Clock Face

Postby danielfalck » Sat Nov 23, 2013 11:16 pm

You can get the shapestring label from ss.Label and put it in a list. After you're done with the shapestrings, you can remove them.

Code: Select all

import Draft
from math import cos, sin, radians

# Default Values
radius = 35
size = 10.0
angle = 30
thickness = 2

shapes = []
label_list = []
# Create the numbers
for x in xrange(1, 13):
   pos = angle * x - 90
   x_val = radius * cos(-radians(pos))
   y_val = radius * sin(-radians(pos))
   print(x_val)
   print(y_val)
   ss=Draft.makeShapeString(String=str(x),FontFile="/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",Size=size,Tracking=0)
   centroid = ss.Shape.BoundBox.Center
   plm=FreeCAD.Placement()
   plm.Base=FreeCAD.Vector(x_val - centroid[0],y_val - centroid[1],0)
   ss.Placement=plm
   shapes.append(ss.Shape)
   label_list.append(ss.Label)

# Create the Solid
comp = Part.makeCompound(shapes)
extrude = comp.extrude(App.Base.Vector(0,0,thickness))
Part.show(extrude)

#remove the shapestrings
for l in label_list:
    App.getDocument("Unnamed").removeObject(l)
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: Macro to Create Clock Face

Postby quick61 » Sat Nov 23, 2013 11:49 pm

danielfalck wrote:You can get the shapestring label from ss.Label and put it in a list. After you're done with the shapestrings, you can remove them.
I knew there was a much better way to do that. Just have no clue as to even how to go look for it. This way one can do things like make Fuzzy faces, (12, 3, 6, 9), 12 hr and 24 hr without having to edit the ShapeString removal. Can't speak for the OP, but I do thank you.

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
wandererfan
Posts: 3139
Joined: Tue Nov 06, 2012 5:42 pm

Re: Macro to Create Clock Face

Postby wandererfan » Sun Nov 24, 2013 2:26 pm

normandc wrote:This may be similar to the python function wandererfan has programmed, it's not merged into the master code branch yet. viewtopic.php?f=17&t=5007
ShapeArray spaces copies of a single shape along an arbitrary path. It doesn't have the ability to put 12 different shapes on the path (yet??).

Nice work, cblt2l.

wf
User avatar
cblt2l
Posts: 155
Joined: Sat May 15, 2010 3:59 am

Re: Macro to Create Clock Face

Postby cblt2l » Mon Nov 25, 2013 12:20 am

Thanks for the tips everyone :) . I updated the original post to include danielfalck's modification.

Here's the end product
clock-face-2.jpg
3d printed clock face
clock-face-2.jpg (396.54 KiB) Viewed 3716 times
danielfalck
Posts: 395
Joined: Fri Oct 07, 2011 8:58 pm
Location: Beaverton,Oregon, USA
Contact:

Re: Macro to Create Clock Face

Postby danielfalck » Mon Nov 25, 2013 1:17 am

Very cool! I'm sure those presents will be much appreciated.