The selection_tools.py module#
Summary#
List the entities of a given type in the active design. |
|
Create an empty selection builder in the product backend. |
|
Query the design for entities matching a property and store them in a selection builder. |
|
Narrow the entities already held in a selection builder to those matching a condition. |
|
Order the entities in a selection builder by a numeric property (ascending). |
|
Group the entities in a selection builder by a shared property. |
|
Extend a selection with additional entities related to the ones already selected. |
|
Create a named selection containing geometry entities. |
Description#
MCP selection tools for PyAnsys Geometry-MCP.
Each function decorated with @app.tool() is exposed as an MCP tool.
The ctx parameter provides access to the app context (and thus
to the live product instance and command history).
The query/filter/order/group/extend tools all take an entity_type of
"body", "face", or "edge" and dispatch to the matching typed
selection on the selection builder. Not every criterion applies to every
entity type. The tools report the supported criteria when given a bad
combination.
How to add a tool#
Define a Python function with type-annotated parameters.
Add a NumPy-style docstring. The summary line becomes the tool description visible to the AI assistant.
Decorate it with
@app.tool().Access the product instance:
app_context = ctx.fastmcp._lifespan_result instance = app_context.product_instance
Notes#
Import this module in __main__.py to register all tools
with the server before it starts.
Module detail#
- selection_tools.list_entities(ctx: fastmcp.server.Context, entity_type: Literal['body', 'component', 'face', 'edge', 'design_curves', 'datum_points', 'datum_planes', 'datum_lines', 'coordinate_systems'], body_id: str | None = None, component_id: str | None = None, name: str | None = None) str#
List the entities of a given type in the active design.
This is the discovery tool. Use it to find the exact body, component, face, and edge IDs that other tools expect as input, with body paths and component names for context. Body and component names need not be unique, so other tools take IDs.
Users refer to bodies by name. To turn a body name into its ID, call this tool with
entity_type="body"andnameset to that name. If more than one body matches, use the component path to tell them apart, or ask the user which one they mean.Behavior per
entity_type:"body":id= id> path=for every body, e.g.path>/ name> id=0:12 path=MainComponent/Cylinder. Passnameto list only bodies with that name, andcomponent_idto restrict the listing to one component and its children."component": the component hierarchy as an indented tree ofid=lines. Component names need not be unique, so use the ID to refer to a component.id> name= name> "face":id=for every face ofid> surface_type= body_id. Use it whenever a face ID is needed, or to answer questions about how many faces a body has or which faces are planes, cylinders, cones, and so on."edge":id=for every edge ofid> curve_type= body_id. Use it whenever an edge ID is needed, or to answer questions about how many edges a body has or which edges are lines, circles, arcs, and so on."design_curves","datum_points","datum_planes","datum_lines", and"coordinate_systems":id=for live objects throughout the design, including nested components. Design curves include construction lines left by modeling operations; they are separate from body edges. Passid> path= path>/ namefor an exact case-insensitive name match orcomponent_idto restrict the listing to a component and its children. Use these IDs with the PyAnsys Geometry API throughrun_python; selection-builder tools accept only bodies, faces, and edges.
Parameters#
- ctxContext
MCP request context (injected automatically).
- entity_typestr
Kind of entity to select. Options are
"body","component","face","edge","design_curves","datum_points","datum_planes","datum_lines", and"coordinate_systems".- body_idstr, default: None
ID of the body to inspect. Required for
"face"and"edge", ignored otherwise. List the bodies first to discover valid IDs.- component_idstr, default: None
ID of a component to restrict body, design curve, datum object, or coordinate system listings to, together with its children. List the components first to discover IDs. Ignored for component, face, and edge listings.
- namestr, default: None
Entity name to look up; only objects with exactly this name (ignoring case) are listed. Applies to bodies, design curves, datum objects, and coordinate systems. Ignored for components, faces, and edges.
Returns#
- str
Newline-separated listing, or an error message.
- selection_tools.create_selection_builder(ctx: fastmcp.server.Context) str#
Create an empty selection builder in the product backend.
Use a selection builder when a selection request needs multiple query or refinement steps. Start with
select_entities, refine withfilter_entitiesor add related entities withextend_selection, and useorder_entitiesorgroup_entitieswhen ranking or grouping is useful. Save the final set withcreate_named_selection. If the requested entities are already known by ID and only need a label, callcreate_named_selectiondirectly without a builder. Resolve body names to IDs withlist_entitiesfirst.Call this tool only to start a brand-new selection. If a selection builder already exists from earlier in this conversation, reuse its handle in the other selection tools instead of calling it again.
Parameters#
- ctxContext
MCP request context (injected automatically).
Returns#
- str
Selection handle (opaque ID string) that can be used in subsequent selection tools, or an error message if creation fails.
- selection_tools.select_entities(ctx: fastmcp.server.Context, selection_handle: str, entity_type: EntityType, criteria: Literal['all', 'visible', 'solid', 'surface', 'named_selection', 'name', 'volume', 'surface_area', 'area', 'length', 'x_location', 'y_location', 'z_location', 'color'], name: str | None = None, named_selection: str | None = None, min_value: float | None = None, max_value: float | None = None, color: str | None = None, range_type: Literal['intersect', 'contain'] = 'intersect', body_ids: list[str] | None = None) list[str] | str#
Query the design for entities matching a property and store them in a selection builder.
Query the design for the starting set of bodies, faces, or edges in a selection workflow. The result replaces the builder’s current selection for that entity type. Queries search the whole design. To select only faces or edges of specific bodies, such as “the circular edges of the plate”, pass those bodies’ IDs as
body_ids. Follow withfilter_entitiesto narrow the set,extend_selectionto add related entities, orcreate_named_selectionto save the final set. Pass onecriteriavalue with its one or more corresponding arguments. Available criteria depend onentity_type.Criteria by
entity_type:"body":all,visible,solid,surface,named_selection,name,volume,surface_area,x_location,y_location,z_location,color"face":all,visible,named_selection,area,x_location,y_location,z_location,color"edge":all,visible,named_selection,length,x_location,y_location,z_location
Parameters#
- ctxContext
MCP request context (injected automatically).
- selection_handlestr
Handle of the selection builder to use for this query. Reuse the handle from the most recent
create_selection_buildercall in this conversation rather than creating a new one.- entity_typestr
Kind of entity to select. Options are
"body","face", and"edge".- criteriastr
Selection rule to apply:
"all"Every entity of that type in the design. No extra arguments are needed.
"visible"All currently visible entities. No extra arguments are needed.
"solid"Solid (volumetric) bodies. Bodies only.
"surface"Surface (sheet) bodies. Bodies only.
"named_selection"Entities belonging to a named selection group. Requires
named_selection."name"All bodies whose name matches, ignoring case, including bodies sharing a name. Bodies only. Requires
name."volume"Bodies whose volume falls in [
min_value,max_value] (m³).min_valuedefaults to 0.max_valueis optional."surface_area"Bodies whose surface area falls in [
min_value,max_value] (m²).min_valuedefaults to 0.max_valueis optional."area"Faces whose area falls in [
min_value,max_value] (m²).min_valuedefaults to 0.max_valueis optional."length"Edges whose length falls in [
min_value,max_value] (m).min_valuedefaults to 0.max_valueis optional."x_location","y_location","z_location"Entities whose location range on that axis intersects or is contained within [
min_value,max_value] (m), according torange_type. Requires at least one ofmin_valueormax_value."color"Bodies or faces with a specific display color. Requires
color.
- namestr, default: None
Body name for the
"name"criterion.- named_selectionstr, default: None
Named selection label for the
"named_selection"criterion.- min_valuefloat, default: None
Lower bound (inclusive) for range-based criteria, in SI units: m for
lengthand the location criteria, m² forareaandsurface_area, m³ forvolume. Convert millimeter values first, for example 2000 mm² is0.002.- max_valuefloat, default: None
Upper bound (inclusive) for range-based criteria, in the same SI units as
min_value. PassNoneto leave the upper bound open.- colorstr, default: None
Hex color string for the
"color"criterion, such as"#FF0000".- range_typestr, default:
"intersect" Whether a location criterion intersects or contains the specified range. Options are
"intersect"and"contain".- body_idslist of str, default: None
Keep only faces or edges that belong to these bodies. Faces and edges only. Look up a body named by the user with
list_entities(entity_type="body", name=...).
Returns#
- list of str | str
Body, face, or edge IDs for every match. If no entities match, an empty list is returned.
If the selection handle is invalid, the criterion does not apply to the entity type, or required arguments are missing, a string error message is returned.
Examples#
- All solid bodies:
select_entities(entity_type="body", criteria="solid")- All faces of the design:
select_entities(entity_type="face", criteria="all")- Edges longer than 0.01 m:
select_entities(entity_type="edge", criteria="length", min_value=0.01)- Bodies matching a Z-location range starting at 0.05 m:
select_entities(entity_type="body", criteria="z_location", min_value=0.05)- Circular edges of one body, after looking up its ID with
list_entities: select_entities(entity_type="edge", criteria="all", body_ids=[plate_id])followed byfilter_entities(entity_type="edge", criteria="curve_type", curve_type="circle")
- selection_tools.filter_entities(ctx: fastmcp.server.Context, selection_handle: str, entity_type: EntityType, criteria: Literal['volume', 'surface_area', 'area', 'perimeter', 'length', 'face_count', 'edge_count', 'loop_count', 'surface_count', 'curve_count', 'color', 'name', 'containing_surface_type', 'containing_curve_type', 'curve_type', 'is_surface', 'is_solid'], name: str | None = None, find_max: bool | None = None, find_min: bool | None = None, min_value: float | None = None, max_value: float | None = None, surface_type: Literal['plane', 'cylinder', 'cone', 'torus', 'sphere', 'nurbs', 'procedural', 'spline'] | None = None, curve_type: Literal['line', 'circle', 'ellipse', 'nurbs', 'procedural', 'helix', 'spline'] | None = None, color: str | None = None) list[str] | str#
Narrow the entities already held in a selection builder to those matching a condition.
Unlike
select_entities, which queries the whole design, this tool only keeps a subset of the current selection. It does not search the design for new entities. Populate the builder withselect_entitiesor another selection operation first. Useextend_selectionwhen the request is to add related entities. Usecreate_named_selectionto save the resulting set.Criteria by
entity_type:"body":volume,surface_area,face_count,edge_count,loop_count,surface_count,curve_count,color,name,containing_surface_type,containing_curve_type,is_surface,is_solid"face":area,perimeter,edge_count,loop_count,curve_count,color,containing_curve_type"edge":length,curve_type
Parameters#
- ctxContext
MCP request context (injected automatically).
- selection_handlestr
Handle of the selection builder to use for this query. Reuse the handle from the most recent
create_selection_buildercall in this conversation rather than creating a new one.- entity_typestr
Kind of entity to filter. Options are
"body","face", and"edge".- criteriastr
Filter rule to apply:
"volume","surface_area","area","perimeter","length"Keep entities whose measurement falls in [
min_value,max_value], or the single largest/smallest whenfind_max/find_minisTrue.min_valuedefaults to 0.max_valueis optional."face_count","edge_count","loop_count"Keep entities with a count in [
min_value,max_value], or the largest/smallest whenfind_max/find_minisTrue.min_valuedefaults to 0.max_valueis optional."surface_count"Keep bodies by how many surfaces of
surface_typethey contain. Requiressurface_type.min_valuedefaults to 0.max_valueis optional."curve_count"Keep bodies or faces by how many curves of
curve_typethey contain. Requirescurve_type.min_valuedefaults to 0.max_valueis optional."color"Keep bodies or faces with a specific display color. Requires
color."name"Keep all bodies whose name matches, ignoring case, including bodies sharing a name. Bodies only. Requires
name."containing_surface_type"Keep bodies containing a surface of
surface_type. Bodies only."containing_curve_type"Keep bodies or faces containing a curve of
curve_type."curve_type"Keep edges of the given
curve_type. Edges only."is_surface"/"is_solid"Keep only surface or only solid bodies. Bodies only.
- namestr, default: None
Body name for the
"name"criterion.- find_maxbool, default: False
Keep only the entity with the maximum value of the criterion.
- find_minbool, default: False
Keep only the entity with the minimum value of the criterion.
- min_valuefloat, default: None
Lower bound (inclusive) for range-based criteria. Measurements are in SI units: m for
lengthandperimeter, m² forareaandsurface_area, m³ forvolume. Convert millimeter values first, for example 2000 mm² is0.002. Count criteria take plain integers.- max_valuefloat, default: None
Upper bound (inclusive) for range-based criteria, in the same units as
min_value.- surface_typestr, default: None
Surface type for the
"surface_count"and"containing_surface_type"criteria.- curve_typestr, default: None
Curve type for the
"curve_count","containing_curve_type", and"curve_type"criteria.- colorstr, default: None
Hex color string for the
"color"criterion, e.g."#FF0000".
Returns#
- list of str | str
Body, face, or edge IDs that survive the filter. If no entities survive the filter, an empty list is returned.
If the selection handle is invalid, the criterion does not apply to the entity type, or required arguments are missing, a string error message is returned.
Examples#
- Keep only bodies with volume of at least 100 m³ (drops the smaller ones):
filter_entities(entity_type="body", criteria="volume", min_value=100)- Keep only the single largest face by area:
filter_entities(entity_type="face", criteria="area", find_max=True)- Keep only solid bodies:
filter_entities(entity_type="body", criteria="is_solid")
- selection_tools.order_entities(ctx: fastmcp.server.Context, selection_handle: str, entity_type: EntityType, criteria: Literal['volume', 'surface_area', 'area', 'perimeter', 'length', 'face_count', 'edge_count', 'loop_count', 'surface_count', 'curve_count'], curve_type: Literal['line', 'circle', 'ellipse', 'nurbs', 'procedural', 'helix', 'spline'] | None = None) list[str] | str#
Order the entities in a selection builder by a numeric property (ascending).
This ranks the entities already in the builder. It does not discover, add, or remove entities. Use
filter_entitiesto narrow the set,extend_selectionto add related entities, orgroup_entitiesto partition it by a shared property.Criteria by
entity_type:"body":volume,surface_area,face_count,edge_count,loop_count,surface_count,curve_count"face":area,perimeter,edge_count,loop_count,curve_count(requirescurve_type)"edge":length
Parameters#
- ctxContext
MCP request context (injected automatically).
- selection_handlestr
Handle of the selection builder to use for this query. Reuse the handle from the most recent
create_selection_buildercall in this conversation rather than creating a new one.- entity_typestr
Kind of entity to order. Options are
"body","face", and"edge".- criteriastr
Property to sort by, smallest first.
- curve_typestr, default: None
Curve type for the face
"curve_count"criterion.
Returns#
- list of str | str
Body, face, or edge IDs in ascending order of the chosen property. If the selection is empty, an empty list is returned.
If the selection handle is invalid, the criterion does not apply to the entity type, or required arguments are missing, a string error message is returned.
Examples#
- Bodies sorted by volume (smallest first):
order_entities(entity_type="body", criteria="volume")- Faces sorted by area:
order_entities(entity_type="face", criteria="area")
- selection_tools.group_entities(ctx: fastmcp.server.Context, selection_handle: str, entity_type: EntityType, criteria: Literal['volume', 'surface_area', 'area', 'perimeter', 'face_count', 'edge_count', 'loop_count', 'color', 'name', 'body', 'coincident', 'curve_type']) list[list[str]] | str#
Group the entities in a selection builder by a shared property.
Each returned group contains entities that are equal in the chosen property. Grouping applies only to entities already in the builder. It does not search the design for additional matches. Use
filter_entitiesto narrow the selection orextend_selectionto add related entities.Criteria by
entity_type:"body":volume,surface_area,face_count,edge_count,loop_count,color,name"face":area,perimeter,body(group faces by owning body),edge_count,loop_count,color,coincident"edge":curve_type
Backend compatibility: face
coincidentgrouping has been reported as unimplemented on CoreServiceCORE_WINDOWS27.1.0. Do not use that combination on the affected backend. Other grouping properties, such asareaorbody, do not identify coincident faces; use them only when they match the request. If no supported criterion satisfies the request, report the limitation rather than substituting a different property.Parameters#
- ctxContext
MCP request context (injected automatically).
- selection_handlestr
Handle of the selection builder to use for this query. Reuse the handle from the most recent
create_selection_buildercall in this conversation rather than creating a new one.- entity_typestr
Kind of entity to group. Options are
"body","face", and"edge".- criteriastr
Property to group by.
Returns#
- list of list of str | str
Each inner list holds the body, face, or edge IDs of one group.
If the selection handle is invalid or the criterion does not apply to the entity type, a string error message is returned.
Examples#
- Group all bodies by color:
group_entities(entity_type="body", criteria="color")- Group faces by the body that owns them:
group_entities(entity_type="face", criteria="body")
- selection_tools.extend_selection(ctx: fastmcp.server.Context, selection_handle: str, entity_type: EntityType, criteria: Literal['volume', 'surface_area', 'area', 'face_count', 'edge_count', 'loop_count', 'color', 'name', 'nearby', 'coincident', 'coaxial', 'connected', 'tangent_chain'], scope: Literal['all', 'visible', 'body'] = 'all', distance: float | None = None) list[str] | str#
Extend a selection with additional entities related to the ones already selected.
The current selection is kept and matching entities from the specified scope are added to it. Use this when a request asks to include entities related to an existing selection. Use
filter_entitiesinstead when it asks to keep only matching entities, andcreate_named_selectionwhen it asks to save the final selection under a name.Criteria by
entity_type:"body":volume,surface_area,face_count,edge_count,color,name,nearby"face":area,edge_count,loop_count,color,coincident,coaxial"edge":nearby,connected,tangent_chain,coaxial
Backend compatibility: body
nearbyis unsupported on CoreService backends, regardless ofdistance, including zero. Facecoincidentand edgenearby/connectedhave also been reported as unimplemented on CoreServiceCORE_WINDOWS27.1.0. Do not use these combinations on the affected backend or retry them with different distances. These restrictions take precedence over the general criterion guidance below. Property-based criteria are not substitutes for contact, coincidence, or connectivity. If no supported criterion satisfies the request, report the limitation rather than claiming an equivalent selection.Parameters#
- ctxContext
MCP request context (injected automatically).
- selection_handlestr
Handle of the selection builder to use for this query. Reuse the handle from the most recent
create_selection_buildercall in this conversation rather than creating a new one.- entity_typestr
Kind of entity to extend. Options are
"body","face", and"edge".- criteriastr
Rule used to find additional entities. Property criteria such as
"volume"or"color"add entities sharing that property with any entity already selected."nearby"adds entities withindistance."coincident","coaxial","connected", and"tangent_chain"add entities related by topology or geometry. Bodies have no dedicated “touching”/contact criterion: for bodies in direct contact, use"nearby"with a near-zerodistance(e.g.0.0) rather than omitting it.- scopestr, default:
"all" Which entities to search when extending:
"all","visible","body".- distancefloat, default: None
Maximum proximity distance in metres for the
"nearby"criterion.
Returns#
- list of str | str
Body, face, or edge IDs in the extended selection.
If the selection handle is invalid, the criterion does not apply to the entity type, or required arguments are missing, a string error message is returned.
Examples#
- Add bodies that share the same color:
extend_selection(entity_type="body", criteria="color")- Add edges tangent to the selected ones:
extend_selection(entity_type="edge", criteria="tangent_chain")
- selection_tools.create_named_selection(ctx: fastmcp.server.Context, name: str, selection_handle: str | None = None, entity_types: list[EntityType] | None = None, body_ids: list[str] | None = None, face_ids: list[str] | None = None, edge_ids: list[str] | None = None, beam_ids: list[str] | None = None, design_point_ids: list[str] | None = None, component_ids: list[str] | None = None, vertex_ids: list[str] | None = None, datum_plane_ids: list[str] | None = None, coordinate_system_ids: list[str] | None = None, datum_point_ids: list[str] | None = None) str#
Create a named selection containing geometry entities.
A named selection is a labelled group of entities that can later be referenced by name (for example, for meshing or boundary conditions). Use this tool to save the final result of a selection workflow or call it directly when the requested entities are already known by ID. Resolve body names to IDs with
list_entitiesfirst. It stores the supplied entities. It does not discover or refine them. At least one ofselection_handleor an entity list must be provided.Typical workflows:
Use
list_entitieswithentity_type="body"or"component"to find body or component IDs. Then useentity_type="face"or"edge"on a body to obtain the face and edge IDs to pass here.Build a selection with
create_selection_builderfollowed byselect_entities/filter_entities/order_entities/group_entities/extend_selection. Then pass that builder’sselection_handlehere to save its currently selected bodies, faces, and edges as a named selection (for example, “save the bodies/faces I selected as …”). Useentity_typesto limit which of those types are saved. The IDs those tools return can also be passed directly asbody_ids,face_ids, oredge_ids.
Parameters#
- ctxContext
MCP request context (injected automatically).
- namestr
Label for the named selection.
- selection_handlestr, default: None
Handle of a selection builder (from
create_selection_builder) whose currently selected bodies, faces, and edges should be included. Use this when the user refers to entities already picked/selected rather than naming them explicitly.- entity_typeslist of str, default: None
Entity types to include from
selection_handle:"body","face", and/or"edge". If omitted, all entity types present in the selection are included. Requested types that the selection does not contain are ignored; an error is returned only if none of them are present. This does not restrict entities supplied by the explicit ID arguments.- body_idslist of str, default: None
Body IDs to include. Look up a body named by the user with
list_entities(entity_type="body", name=...).- face_idslist of str, default: None
Face IDs to include. Use
list_entitiesto discover face IDs of a body.- edge_idslist of str, default: None
Edge IDs to include. Use
list_entitiesto discover edge IDs of a body.- beam_idslist of str, default: None
Beam IDs to include.
- design_point_idslist of str, default: None
Design point IDs to include.
- component_idslist of str, default: None
Component IDs to include. Use
list_entitieswithentity_type="component"to discover component IDs.- vertex_idslist of str, default: None
Vertex IDs to include (from
body.vertices).- datum_plane_idslist of str, default: None
Datum plane IDs to include.
- coordinate_system_idslist of str, default: None
Coordinate system IDs to include.
- datum_point_idslist of str, default: None
Datum point IDs to include.
Returns#
- str
Confirmation message, or an error message.
- selection_tools.logger#
- selection_tools.EntityType#