Labelling Faces of Shape

Need help, or want to share a macro? Post here!
Post Reply
wangnick
Posts: 19
Joined: Thu Nov 03, 2022 9:27 am

Labelling Faces of Shape

Post by wangnick »

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
labelfaces.png (228.29 KiB) Viewed 420 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 10 times
User avatar
Roy_043
Veteran
Posts: 5882
Joined: Thu Dec 27, 2018 12:28 pm

Re: Labelling Faces of Shape

Post by Roy_043 »

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: 1650
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Labelling Faces of Shape

Post by edwilliams16 »

@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.
User avatar
Roy_043
Veteran
Posts: 5882
Joined: Thu Dec 27, 2018 12:28 pm

Re: Labelling Faces of Shape

Post by Roy_043 »

Thanks, that does make more sense.
wangnick
Posts: 19
Joined: Thu Nov 03, 2022 9:27 am

Re: Labelling Faces of Shape

Post by wangnick »

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
labelfaces02.png (249.16 KiB) Viewed 165 times
Btw, which tools do YOU use when developing Python macros in FreeCAD e.g. to inspect Shapes?

Kind regards,
Sebastian
User avatar
Roy_043
Veteran
Posts: 5882
Joined: Thu Dec 27, 2018 12:28 pm

Re: Labelling Faces of Shape

Post by Roy_043 »

My approach is rather primitive: I use Std SendToPythonConsole and then dig down using the Python console. So I'll type "sub." f.e. and then scroll through the list.

I should start using https://freecad.github.io/SourceDoc/.
Post Reply