How to get an object from a body?

Need help, or want to share a macro? Post here!
user1234
Posts: 123
Joined: Mon Jul 11, 2016 5:08 pm

How to get an object from a body?

Postby user1234 » Thu Jan 17, 2019 9:08 pm

Hello!

Sorry for begging for help but i am dumb.

I want to script a rename tool (labels only) for bodys and parts and their objects and features. And have two questions.

Here the inner of a body:

Code: Select all

    <Extensions Count="1">
        <Extension type="App::OriginGroupExtension" name="OriginGroupExtension">
        </Extension>
    </Extensions>
    <Properties Count="8">
        <Property name="BaseFeature" type="App::PropertyLink">
            <Link value=""/>
        </Property>
        <Property name="ExpressionEngine" type="App::PropertyExpressionEngine">
            <ExpressionEngine count="0">
            </ExpressionEngine>
        </Property>
        <Property name="Group" type="App::PropertyLinkList">
            <LinkList count="17">
                <Link value="Sketch052"/>
                <Link value="DatumPlane010"/>
                <Link value="DatumLine003"/>
                <Link value="Revolution024"/>
                <Link value="Sketch053"/>
                <Link value="Pocket010"/>
                <Link value="Sketch054"/>
                <Link value="Groove008"/>
                <Link value="PolarPattern015"/>
                <Link value="Chamfer050"/>
                <Link value="Fillet039"/>
                <Link value="Fillet040"/>
                <Link value="Chamfer051"/>
                <Link value="Chamfer009"/>
                <Link value="Fillet041"/>
                <Link value="Chamfer061"/>
                <Link value="Fillet054"/>
            </LinkList>
        </Property>
        <Property name="Label" type="App::PropertyString">
            <String value="SpindleShaftBase"/>
        </Property>
        <Property name="Origin" type="App::PropertyLink">
            <Link value="Origin063"/>
        </Property>
        <Property name="Placement" type="App::PropertyPlacement">
            <PropertyPlacement Px="0" Py="0" Pz="0" Q0="0" Q1="0" Q2="0" Q3="1" A="0" Ox="0" Oy="0" Oz="1"/>
        </Property>
        <Property name="Shape" type="Part::PropertyPartShape">
        </Property>
        <Property name="Tip" type="App::PropertyLink">
            <Link value="Fillet054"/>
        </Property>
    </Properties>


First question , i get a list property when i mark the body and with

Code: Select all

sel = FreeCADGui.Selection.getSelection()
for obj in sel:
    a = obj.getPropertyByName("Group")
    try:
        App.Console.PrintMessage(str(a) + "\n")
    except:
        App.Console.PrintMessage("error" + "\n" + "\n" + "\n" + "\n")
But how i can get an object from the "App::PropertyLinkList"? I tried many variations of the syntax but with no success.



Second question, why get i the list 18 times back when i mark the body with

Code: Select all

sel = FreeCADGui.Selection.getSelection()
for obj in sel:
    a = obj.getPropertyByName("Group")
    try:
         for x in a:
            App.Console.PrintMessage(str(a) + "\n")
    except:
        App.Console.PrintMessage("error" + "\n" + "\n" + "\n" + "\n")
? I expect only the list one time, because i mark only one body.



Sorry for the dumb questions but i am new in python and scripting.

for informations

Code: Select all

OS: Debian GNU/Linux buster/sid
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15650 (Git)
Build type: Release
Branch: master
Hash: c9b4f4ba6779c9ccddb69fba57eee1b5acfd4083
Python version: 3.7.2
Qt version: 5.11.3
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedStates (en_US)
Many thanks for possible help!

Greetings
user
Attachments
SpindleShaft.FCStd
(164.19 KiB) Downloaded 3 times
01.png
01.png (57.35 KiB) Viewed 186 times
wandererfan
Posts: 2583
Joined: Tue Nov 06, 2012 5:42 pm

Re: How to get an object from a body?

Postby wandererfan » Fri Jan 18, 2019 2:40 pm

user1234 wrote:
Thu Jan 17, 2019 9:08 pm
First question , i get a list property when i mark the body and with
<<snip>>
But how i can get an object from the "App::PropertyLinkList"? I tried many variations of the syntax but with no success.
Try this:

Code: Select all

sel = FreeCADGui.Selection.getSelection()
for obj in sel:
    linkList = obj.getPropertyByName("Group")
    for linkObj in linkList:
        App.Console.PrintMessage(linkObj.Name + " " + str(linkObj.TypeId) + "\n")
user1234 wrote:
Thu Jan 17, 2019 9:08 pm
Second question, why get i the list 18 times back when i mark the body with
Because you're printing the whole list once for each entry in the list!

wf
user1234
Posts: 123
Joined: Mon Jul 11, 2016 5:08 pm

Re: How to get an object from a body?

Postby user1234 » Sat Jan 19, 2019 9:03 pm

Hello!
wandererfan wrote:
Fri Jan 18, 2019 2:40 pm
Try this:
Yes!!! Many thanks!
Hope i get the rest of macro from now on without help! (and show it when it is finished, still much to learn).

Sorry for the late reply.

Greetings
user