mapteksdk.project.selection module
Provides an abstraction for working with a selection of objects.
- class Selection(initial=None)
Bases:
Sequence
Represents a selection or put simply a sequence of objects.
The selection will only contain a given object once.
An application maintains a special selection, known as the active selection, which is the selection highlighted in views and the browser, and under direct control of the user.
This means it will either be a copy of the active selection, a subset of it or an independent collection of objects.
- Parameters:
initial (list[ObjectID] | None) –
- classmethod active_selection()
Construct a Selection object from the active selection.
The returned selection object contains the active selection when it was constructed. It will not change what objects are in it when the active selection is changed.
- Return type:
Self
- append(item)
Append an object to the end of this selection.
If the object is already in the selection it will not be added and no error is raised.
- Parameters:
item (ObjectID | DataObject) – The object to append.
Examples
Add the object from the context to a selection.
>> from mapteksdk.project import Project >> from mapteksdk.context_menu import context_object_id >> from mapteksdk.context_menu import NoContextInformationError >> project = Project() >> selection = project.get_selected() >> try: … selection.append(context_object_id()) >> except NoContextInformationError: … pass # There is no object to add (no context).
- clear()
Remove all items from this selection.
- extend(iterable)
Extend this selection by appending elements from the iterable.
If an object is already in the selection then it will not be appended and will be skipped over and subsequent objects will be added if they are not already in the selection.
- where(*object_types)
Return a selection where the type of the object matches given types.
If multiple types are specified, this will filter the selection down to objects which are any of the specified types.
It does not require that the object is all of the types because it will either be impossible to match them all or it will only match the most derived type.
This does not change the current selection.
There is no need to call “where(Topology, EdgeChain)” as that is the same as calling “where(EdgeChain)”.
- Parameters:
*object_types (list[type]) – Variable number of object types to filter by.
- Returns:
A new selection which only contains objects that match at least one of the given types.
- Return type:
Examples
Find all the selected polygons.
>> from mapteksdk.project import Project >> from mapteksdk.data import Polygon >> project = Project() >> selection = project.get_selected() >> polygons = selection.where(Polygon) >> print(f’There were {len(polygons)} out of {len(selection)}.’)
Find all the selected polygons and polylines.
>> from mapteksdk.project import Project >> from mapteksdk.data import Polygon, Polyline >> project = Project() >> selection = project.get_selected() >> polygons_and_lines = selection.where(Polygon, Polyline) >> print(f’There were {len(polygons_and_lines)} out of {len(selection)}.’)
- where_not(*object_types)
Return a selection containing all objects not of the specified types.
When more than one type is given, it will exclude any object if it matches any of the given types.
This does not change the current selection.
- Parameters:
*object_types (list[type]) – Variable number of object types to filter by.
- Returns:
A new selection which only contains objects that are not of the given types.
- Return type:
Examples
Exclude any containers from the selection.
>> from mapteksdk.project import Project >> from mapteksdk.data import VisualContainer >> project = Project() >> selection = project.get_selected() >> non_containers = selection.where_not(VisualContainer) >> print(f’There were {len(non_containers)} out of {len(selection)}.’)
- partition(*object_types, with_remainder=False)
Partition the selection by the given types into separate selections.
The same object can appear in multiple selections if one of the object types are related to another
If a single type is given and with_remainder is False then you would be better off using where() instead. The specific case in which using partition() would be useful is if the list of types is variable, for example they are conditional or user defined.
- Parameters:
*object_types (list[type]) – Variable number of object types to partition by.
with_remainder – If True, an additional selection will be included in the results with the remaining objects in the selection that didn’t match any of the object types given.
- Returns:
A selection for each of the given object types, with objects of the given type. If with_remainder is True then an additional selection is included at the end containing the objects which didn’t match any given object types.
- Return type:
tuple
Examples
Find all the selected polygons and polylines.
>> from mapteksdk.project import Project >> from mapteksdk.data import Polygon, Polyline >> project = Project() >> selection = project.get_selected() >> polygons, lines = selection.partition(Polygon, Polyline) >> print(f’There were {len(polygons)} polygons out of {len(selection)}.’) >> print(f’There were {len(lines)} polylines out of {len(selection)}.’)
- property roots: Selection
Return the root objects of this selection.
The root objects in this selection are the objects whose parents are not part of this selection.
This is useful when the individual objects in a container aren’t needed because the function handles containers themselves.
For example if the selection contains three objects: - /cad - /cad/loop - /cad/line - /scrapbook/surface The root objects would be /cad and /scrapbook/surface.
Examples
Recycle objects in the active selection.
>> from mapteksdk.project import Project >> project = Project() >> selection = project.get_selected() >> for object_to_recycle in selection.roots: … project.recycle(object_to_recycle)
If you have selected the four objects mentioned above, then this will ensure the recycle bin contains a cad container (with the loop and line inside it) and surface from /scrapbook. If you were to use selection rather than selection.roots then it would recycle each individual object so the recycle bin would have cad, loop, line and surface with no nesting/hierarchy.
- count(value) integer -- return number of occurrences of value
- index(value[, start[, stop]]) integer -- return first index of value.
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.