PropertyLink: InList et OutList

From FreeCAD Documentation
Revision as of 20:13, 7 July 2021 by David69 (talk | contribs) (Created page with "Voici des propriétés similaires permettant de relier les fonctions de différents documents. C'est la partie essentielle pour les assemblages. * PropertyXLink * PropertyXLin...")
Other languages:

Voir Properties avant de lire cette section.

PropertyLink

En plus des Propriétés scalaires d'une fonction, les fonctions elles-mêmes contiennent des pointeurs les unes vers les autres. Ces pointeurs définissent un Graphe orienté acyclique qui détermine l'ensemble et l'ordre des objets qui sont recalculés en réponse à la modification d'un objet. Seules les caractéristiques qui dépendent d'une fonction modifiée sont recalculées.

Les dépendances sont exprimées par une classe spéciale de types de propriétés, à savoir les PropertyLink :

  • PropertyLink : permet à une caractéristique de se lier à une autre fonction unique dans le même document.
  • PropertyLinkList : permet à un élément de lier plusieurs éléments.
  • PropertyLinkSub : permet à un élément de lier un élément unique et de référencer des sous-éléments supplémentaires. Exemple : Si vous voulez modéliser une poche pour l'esquisse voulue, il est important de savoir sur quel sous-élément (par exemple, Face6) de la fonction liée elle doit être mappée.
  • PropertyLinkSubList : cela permet à une fonction de se lier à plusieurs sous-éléments de plusieurs fonctions.

Voici des propriétés similaires permettant de relier les fonctions de différents documents. C'est la partie essentielle pour les assemblages.

  • PropertyXLink
  • PropertyXLinkSub
  • PropertyXLinkSubList
  • PropertyXLinkList
  • PropertyXLinkContainer

Example

Consider a class BoxDimension that provides basic dimensions for another class Box. We would like a Box object to be recomputed whenever its associated BoxDimension is changed:

import FreeCAD
import Part

class BoxDimension:
  def __init__(self, obj):
    obj.addProperty("App::PropertyLength", "Length").Length = 1
    obj.addProperty("App::PropertyLength", "Width").Width = 1
    obj.addProperty("App::PropertyLength", "Height").Height = 1
    obj.Proxy = self

class Box:
  def __init__(self, obj):
    obj.addProperty("App::PropertyLink", "Dimensions").Dimensions = None
    obj.Proxy = self
  def execute(self, obj):
    if obj.Dimensions is None:
      return
    l = obj.Dimensions.Length
    w = obj.Dimensions.Width
    h = obj.Dimensions.Height
    obj.Shape = Part.makeBox(l, w, h)

Note that it is a Box object that contains the PropertyLink to the BoxDimension object. Usage is as follows:

doc = App.newDocument()
dim = doc.addObject("App::FeaturePython", "BoxDimension")
box = doc.addObject("Part::FeaturePython", "Box")
dim_proxy = BoxDimension(dim)
box_proxy = Box(box)
box.ViewObject.Proxy = None
box.Dimensions = dim

dim.Length = 5
dim.Width = 3
dim.Height = 7
doc.recompute()

Because our box depends on the dim object, it will be recomputed.

InList and OutList

PropertyLink objects can be accessed using a Python property using the name that they are registered with using .addObject(). However there is another way. Every feature has a pair of lazily-generated lists called InList and OutList that describe the outgoing and incoming edges of the DAG, respectively:

  • An InList is a list of all features that depend upon the current object. So, dim.InList will be a list containing our box object.
  • Similarly, an OutList is a list of all features that are depended upon the current object. That is, box.OutList will be a list containing our dim object.

Note that InList and OutList have nothing to do with the tree view of the document model that is presented in the GUI. At any time, a parent in that tree view may contain children that are part of the InList, the OutList, or neither.