LinkSub: Difference between revisions

From FreeCAD Documentation
(A LinkSub 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.)
 
(Marked this version for translation)
 
Line 2: Line 2:
<translate>
<translate>


== Description ==
== Description == <!--T:10-->


<!--T:11-->
A [[LinkSub|LinkSub]] 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 [[LinkSub|LinkSub]] 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.


<!--T:12-->
A LinkSub is composed of a list of two arguments.
A LinkSub is composed of a list of two arguments.
</translate>
</translate>
Line 13: Line 15:
<translate>
<translate>


<!--T:13-->
The first argument is a reference to a [[App_DocumentObject|document object]], and the second argument 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.
The first argument is a reference to a [[App_DocumentObject|document object]], and the second argument 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.
</translate>
</translate>
Line 32: Line 35:
<translate>
<translate>


<!--T:14-->
It is possible to see that a [[LinkSubList|LinkSubList]] is a list of [[LinkSub|LinkSub]] structures.
It is possible to see that a [[LinkSubList|LinkSubList]] is a list of [[LinkSub|LinkSub]] structures.


== Scripting ==
== Scripting == <!--T:15-->


<!--T:16-->
{{Emphasis|See also:}} [[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]], [[LinkSubList|LinkSubList]].
{{Emphasis|See also:}} [[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]], [[LinkSubList|LinkSubList]].


<!--T:17-->
A new [[scripted_objects|scripted object]] can accept a LinkSub by adding the corresponding [[property|property]].
A new [[scripted_objects|scripted object]] can accept a LinkSub by adding the corresponding [[property|property]].


<!--T:18-->
It is important to recompute the objects before their subelements are used as input for other objects, otherwise an error may be produced as the geometry may not contain a valid [[Part_TopoShape|Shape]].
It is important to recompute the objects before their subelements are used as input for other objects, otherwise an error may be produced as the geometry may not contain a valid [[Part_TopoShape|Shape]].



Latest revision as of 07:17, 2 October 2020

Other languages:

Description

A LinkSub 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 LinkSub is composed of a list of two arguments.

sub = [argument1, argument2]

The first argument is a reference to a document object, and the second argument 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.

sub = [obj1, "Vertex1"]
sub = [obj2, "Face1"]
sub = [obj3, "Edge1"]

The second argument can itself be a list of strings, indicating various subelements of the same document object.

sub1 = [obj1, ["Vertex1", "Vertex2"]]
sub2 = [obj2, ["Face1", "Face3", "Face5"]]
sub3 = [obj3, ["Vertex1", "Face5", "Edge1", "Edge2"]]

It is possible to see that a LinkSubList is a list of LinkSub structures.

Scripting

See also: FreeCAD Scripting Basics, LinkSubList.

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

It is important to recompute the objects before their subelements are used as input for other objects, otherwise an error may be produced as the geometry may not contain a valid Shape.

doc = App.newDocument()

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

new_obj = doc.addObject("App::FeaturePython", "New")
new_obj.addProperty("App::PropertyLinkSub", "Geometry")
new_obj.Geometry = [cube, ["Vertex1", "Vertex2"]]

new_obj2 = doc.addObject("App::FeaturePython", "New")
new_obj2.addProperty("App::PropertyLinkSub", "Geometry")
new_obj2.Geometry = [cyl, "Edge1"]

doc.recompute()

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

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

>>> new_obj.Geometry[1]
['Vertex1', 'Vertex2']

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

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

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