LinkSub

From FreeCAD Documentation
Revision as of 07:41, 2 October 2020 by Renatorivo (talk | contribs) (Created page with "È possibile vedere che un LinkSubList è un elenco di strutture LinkSub.")
Other languages:

Descrizione

Un LinkSub è una struttura dati che viene utilizzata come input per varie funzioni e oggetti; il suo scopo è passare un sottooggetto o sottoelemento (vertice, bordo o faccia) da un oggetto a un altro oggetto che utilizzerà o trasformerà quella geometria.

Un LinkSub è composto da un elenco di due argomenti.

sub = [argument1, argument2]

Il primo argomento è un riferimento a un oggetto documento e il secondo argomento è una stringa di testo che indica il nome interno del sottoelemento. Questo nome ha un numero che inizia con uno e va fino al numero totale di tali sottoelementi.

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

Il secondo argomento può essere a sua volta un elenco di stringhe, che indica vari sottoelementi dello stesso oggetto documento.

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

È possibile vedere che un LinkSubList è un elenco di strutture LinkSub.

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