linking between objects

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

linking between objects

Post by bernd »

I'm struggling with family members. It is like real live, family is great but could be difficult too ... :geek:

The objects I do link between have no shape!

childs are defined in the parent object:
If a FeaturePython has some child objects defined in a App::PropertyLinkList these childs could be displayed in the tree view by collecting them in the view provider of the FeaturePython by claimChildren(self)

parent is defined in child objects:
A FeaturePython has a parent object defined by a App::PropertyLink. How is it possible to display the child under the parent in tree view?


Another question. Which one is the preferred way of linking between object. Or when would I use which one? Are there even more ways of linking?

cheers bernd
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: linking between objects

Post by microelly2 »

Lets have a relation A depends on B

If A is a PythonFeature, then A simply can have a PropertyLink to B.
When B updates a recompute of A is forced.

But if A is not a PythonFeature you cannot add such a PropertyLink.

In this case I use that way:
I add a PropertyLink to B and write a special execute method for B.
This method recomputes A.
To avoid a recursive call I use a special trick.

In the animation workbench the animation nodes are the parents
the grand father is the manager
its childs are the placer
and placer have common parts (box,sphere ..) as children

So I have the tree structure
Manager
.. |___>Placer
..............|__>Sphere

but the dependency
Sphere depends on Placer
Placer depends on Manager

I use the same mechanism for proxy sketches.
https://www.youtube.com/watch?v=ITLj1MmP4ho
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: linking between objects

Post by wmayer »

parent is defined in child objects:
A FeaturePython has a parent object defined by a App::PropertyLink. How is it possible to display the child under the parent in tree view?
As long as a parent has no link to its children there is no problem as otherwise we have a cyclic dependency which is impossible to show in a tree view.

Usually a parent knows its children and inside the claimChildren list it adds objects that are part of its OutList. When doing it the other way around we have to use its InList instead. Here is some demo code:

Code: Select all

parent=App.ActiveDocument.addObject("App::FeaturePython","Parent")
child=App.ActiveDocument.addObject("App::FeaturePython","Child")
child.addProperty("App::PropertyLink","LinkToParent")

class ViewProviderParent:
  def __init__(self, vp):
    vp.Proxy=self
    self.Object = vp.Object
  def claimChildren(self):
    return self.Object.InList
  def __getstate__(self):
    return None


child.LinkToParent=parent
vp=ViewProviderParent(parent.ViewObject)
parent.Label = parent.Label # this is needed to trigger an update
Add a second children

Code: Select all

child2=App.ActiveDocument.addObject("App::FeaturePython","Child2")
child2.addProperty("App::PropertyLink","LinkToParent")
child2.LinkToParent=parent
parent.Label = parent.Label
Another question. Which one is the preferred way of linking between object. Or when would I use which one? Are there even more ways of linking?
Until now in all cases I know of a parent has links to its children and I don't know of any real use case where a child links its parent.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: linking between objects

Post by bernd »

great, thanks for all the hints ...
wmayer wrote: Wed Oct 04, 2017 1:08 pm
Another question. Which one is the preferred way of linking between object. Or when would I use which one? Are there even more ways of linking?
Until now in all cases I know of a parent has links to its children and I don't know of any real use case where a child links its parent.
I used it months ago in the nonlinear material to link to the material https://github.com/FreeCAD/FreeCAD/blob ... ear.py#L37 because I did not know any better.

Later for the mesh I linked from parent to child: https://github.com/FreeCAD/FreeCAD/blob ... py#L46-L56

Today I came across the linkage from child to parent and thought it might be not smart to link from child to parent but also though it might be a good idea to ask in the forum about this topic.

cheers bernd
x70
Posts: 14
Joined: Mon Oct 05, 2020 3:42 pm

Re: linking between objects

Post by x70 »

bernd wrote: Wed Oct 04, 2017 3:52 pm great, thanks for all the hints ...
wmayer wrote: Wed Oct 04, 2017 1:08 pm
Another question. Which one is the preferred way of linking between object. Or when would I use which one? Are there even more ways of linking?
Until now in all cases I know of a parent has links to its children and I don't know of any real use case where a child links its parent.
I used it months ago in the nonlinear material to link to the material https://github.com/FreeCAD/FreeCAD/blob ... ear.py#L37 because I did not know any better.

Later for the mesh I linked from parent to child: https://github.com/FreeCAD/FreeCAD/blob ... py#L46-L56

Today I came across the linkage from child to parent and thought it might be not smart to link from child to parent but also though it might be a good idea to ask in the forum about this topic.

cheers bernd
I copy your code as like:
parent=App.ActiveDocument.addObject("App::FeaturePython","Parent")
child=App.ActiveDocument.addObject("App::FeaturePython","Child")
child.addProperty("App::PropertyLink","LinkToParent")
child.LinkToParent=parent

but parent and child are in the same level,could you tell me how to make the child in the sub level of the parentObj?and how to make a muti level tree in the treeeview with featurepython?
thx!
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: linking between objects

Post by Kunda1 »

Please put code within the BBcode tags for better readability and also preserving indentation.thx
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply