Need help, or want to share a macro? Post here!
wangnick
Posts: 19 Joined: Thu Nov 03, 2022 9:27 am
Post
by wangnick » Wed Nov 23, 2022 3:29 pm
Dear all,
I'd like to create labels flat on all the faces of a selected shape. Sorry if this has been answered before but I could not find any article on this. My attempt is currently as following:
Code: Select all
doc = App.ActiveDocument
obj, = Gui.Selection.getSelection()
shp = obj.Shape
txtl = []
for i,f in enumerate(shp.Faces):
pl = FreeCAD.Placement()
pl.Base = f.CenterOfMass
txt = Draft.make_text([f.ShapeType+str(i)],placement=pl)
txt.ViewObject.FontSize = 4
txt.ViewObject.Justification = "Center"
txtl.append(txt)
cmpd = doc.addObject("Part::Compound","FaceLabels")
cmpd.Links = txtl
for txt in txtl:
txt.ViewObject.Visibility = True
This results in:
labelfaces.png (228.29 KiB) Viewed 702 times
But only "Face203" is properly positioned. How do I massage the Placement such that all the texts get positioned flat on their faces? I tried in vain to understand the Euler angles involved ...
Kind regards,
Sebastian
Attachments
goldberglabeltest.FCStd
(169.37 KiB) Downloaded 21 times
Roy_043
Veteran
Posts: 7716 Joined: Thu Dec 27, 2018 12:28 pm
Post
by Roy_043 » Wed Nov 23, 2022 4:55 pm
Code: Select all
import FreeCAD as App
import Draft
txtl = []
for i, f in enumerate(shp.Faces):
pl = FreeCAD.Placement()
pl.Base = f.CenterOfMass
normal = f.normalAt(0,0)
pl.Rotation = App.Rotation(normal, normal, normal, "ZXY")
txt = Draft.make_text([f.ShapeType+str(i)],placement=pl)
txt.ViewObject.FontSize = 4
txt.ViewObject.Justification = "Center"
txtl.append(txt)
cmpd = doc.addObject("Part::Compound","FaceLabels")
cmpd.Links = txtl
for txt in txtl:
txt.ViewObject.Visibility = True
edwilliams16
Veteran
Posts: 2721 Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:
Post
by edwilliams16 » Wed Nov 23, 2022 7:56 pm
@Roy_043
Code: Select all
pl.Rotation = App.Rotation(normal, normal, normal, "ZXY")
That is obscure! It's really an error, but apparently it reverts to
Code: Select all
pl.Rotation(App.Vector(0, 0, 1), normal)
which I guess is the only reasonable result.
Roy_043
Veteran
Posts: 7716 Joined: Thu Dec 27, 2018 12:28 pm
Post
by Roy_043 » Thu Nov 24, 2022 8:57 am
Thanks, that does make more sense.
wangnick
Posts: 19 Joined: Thu Nov 03, 2022 9:27 am
Post
by wangnick » Sat Nov 26, 2022 7:45 pm
Roy_043 wrote: ↑ Thu Nov 24, 2022 8:57 am
Thanks, that does make more sense.
Ok, got it done:
Code: Select all
doc = App.ActiveDocument
obj, = Gui.Selection.getSelection()
shp = obj.Shape
import Draft
import math
txtl = []
for i,f in enumerate(shp.Faces):
pl = App.Placement()
pl.Base = f.CenterOfMass
pl.Rotation = App.Rotation(App.Vector(0,0,1),f.normalAt(0,0))
txt = Draft.make_text("Face%d"%(i+1),placement=pl)
txt.ViewObject.FontSize = math.sqrt(f.Area)/5
txt.ViewObject.Justification = "Center"
txtl.append(txt)
cmpd = doc.addObject("Part::Compound","Faces")
cmpd.Links = txtl
for txt in txtl:
txt.ViewObject.Visibility = True
if False:
txtl = []
for i,f in enumerate(shp.Faces):
for j,e in enumerate(f.Edges):
pl = App.Placement()
pl.Base = (f.CenterOfMass+2*e.CenterOfMass)/3
pl.Rotation = App.Rotation(App.Vector(0,0,1),f.normalAt(0,0))
txt = Draft.make_text("FE%d"%(j+1),placement=pl)
txt.ViewObject.FontSize = math.sqrt(f.Area)/5
txt.ViewObject.Justification = "Center"
txtl.append(txt)
cmpd = doc.addObject("Part::Compound","FaceEdges")
cmpd.Links = txtl
for txt in txtl:
txt.ViewObject.Visibility = True
labelfaces02.png (249.16 KiB) Viewed 447 times
Btw, which tools do YOU use when developing Python macros in FreeCAD e.g. to inspect Shapes?
Kind regards,
Sebastian