Get vertex object from name in PropertyLinkSub

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Get vertex object from name in PropertyLinkSub

Post by Aleks »

I am linking to a vertex in a Part::FeaturePython object using PropertyLinkSub.

Now I want to get the linked vertex in the execute method to move the box to the location of that linked vertex.

Code: Select all

obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)
# something like this:
# obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height, App.Vector(vertex.X, vertex.Y, vertex.Z))

Because PropertyLinkSub only stores the vertex name, it doesnt seem obvious to me how I can retrieve the vertex object to get its x, y and z.

I know that this is potentially unstable and might need to be reassigned, due to the changing topo names, but thats a tradeoff that I am willing to take.

Code: Select all

class Box():

    def __init__(self, obj):
        """
        Default constructor
        """

        self.Type = 'box'

        obj.Proxy = self

        obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = "Just a simple box"
        obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
        obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = 5.0
        obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = 10.0
        obj.addProperty("App::PropertyLinkSub", "LinkSub", "Subsection", "Description for tooltip") 

    def execute(self, obj):
        """
        Called on document recompute
        """

        obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
Roy_043
Veteran
Posts: 8580
Joined: Thu Dec 27, 2018 12:28 pm

Re: Get vertex object from name in PropertyLinkSub

Post by Roy_043 »

User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Get vertex object from name in PropertyLinkSub

Post by Aleks »

I need something like this:

Code: Select all

vertex = getVertex(<document_object>, <vertex_name>)
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Get vertex object from name in PropertyLinkSub

Post by TheMarkster »

PropertyLinkSub will contain both the document object and the subshape name. If this property contains, for example, vertex3 of Box002:

Code: Select all

vertex = obj.LinkSub[0].getSubObject(obj.LinkSub[1])
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Get vertex object from name in PropertyLinkSub

Post by Aleks »

TheMarkster wrote: Sat Apr 30, 2022 3:15 pm
Thank you, this worked for me. I made a small modification to directly return the first topo object and not a tuple of topo objects:

Code: Select all

vertex = obj.LinkSub[0].getSubObject(obj.LinkSub[1])[0]
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
Post Reply