LinkSubList

From FreeCAD Documentation
Revision as of 19:22, 29 September 2020 by Renatorivo (talk | contribs) (Marked this version for translation)
Other languages:

Description

A LinkSubList is a data structure that is used as input to various functions and objects; its purpose is to pass a subobject or subelement (vertex, edge, or face) from an object to another object that will use or transform that geometry.

A LinkSubList is composed of a list of tuples.

sublist = [tuple1, tuple2, tuple3, ...]

Each tuple contains at least two elements; the first element is a reference to an object, and the second element is a text string indicating the internal name of the subelement. This name has a number starting with one, and up to the total number of those subelements.

tuple1 = (obj1, "Vertex1")
tuple2 = (obj2, "Face1")
tuple3 = (obj3, "Edge1")

The second element of the tuple can itself be a list of strings, indicating various subelements at the same time.

tuple1 = (obj1, ["Vertex1", "Vertex2"])
tuple2 = (obj2, ["Face1", "Face3", "Face5"])
tuple3 = (obj3, ["Vertex1", "Face5", "Edge1", "Edge2"])

Scripting

See also: FreeCAD Scripting Basics.

A new scripted object can accept a LinkSubList by adding the corresponding property.

doc = App.newDocument()

cube = doc.addObject("Part::Box", "Cube")
cyl = doc.addObject("Part::Cylinder", "Cylinder")

new_obj = doc.addObject("App::FeaturePython", "New")

new_obj.addProperty("App::PropertyLinkSubList", "Geometry")
new_obj.Geometry = [(cube, ["Face1", "Face2"]),
                    (cyl, "Edge1")]

The subelement can then be extracted from the assigned property, and can be manipulated to do something.

>>> new_obj.Geometry
[(<Part::PartFeature>, ('Vertex1', 'Vertex2')), (<Part::PartFeature>, ('Edge1',))]

>>> new_obj.Geometry[0]
(<Part::PartFeature>, ('Vertex1', 'Vertex2'))

>>> new_obj.Geometry[0][1]
('Vertex1', 'Vertex2')

>>> new_obj.Geometry[0][1][1]
'Vertex2'

>>> new_obj.Geometry[0][1][1].strip("Vertex")
'2'

>>> int(new_obj.Geometry[0][1][1].strip("Vertex"))
2