get_selected_elements
Retrieve currently selected elements in Revit UI, providing detailed data like ID, name, and category in JSON-RPC 2.0 format, with built-in error handling.
Instructions
获取当前Revit UI中选择的元素,遵循JSON-RPC 2.0规范。
特性:
获取当前用户在Revit界面中选择的所有元素
返回元素的完整信息,包括ID、类别和名称
无需额外参数,直接反映当前UI状态
完善的错误处理机制
参数: ctx (Context): FastMCP上下文对象 method (str): JSON-RPC方法名,默认为"GetSelectedElements"
返回: dict: JSON-RPC 2.0格式的响应,结构为: 成功时: { "jsonrpc": "2.0", "result": [ { "elementId": "元素ID", "name": "元素名称", "familyName": "类别名称" }, ... ], "id": request_id } 失败时: { "jsonrpc": "2.0", "error": { "code": int, "message": str, "data": any }, "id": request_id }
示例: # 获取当前选择的元素 response = get_selected_elements(ctx)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| method | No | GetSelectedElements |
Implementation Reference
- xml_revit_mcp/server.py:50-55 (registration)get_selected_elements is included in the GENERAL_TOOLS list, which is used to register MCP tools.GENERAL_TOOLS = [ get_commands, execute_commands, call_func, find_elements, update_elements, delete_elements, parameter_elements, get_locations, move_elements, show_elements, active_view, get_selected_elements, link_dwg_and_activate_view, get_view_data ]
- xml_revit_mcp/server.py:133-147 (registration)The register_tools function registers all tools from the *_TOOLS lists (including GENERAL_TOOLS containing get_selected_elements) to the FastMCP server using server.tool() decorator.# 注册工具函数 def register_tools(server: FastMCP) -> None: """注册所有工具到MCP服务器""" # 注册建筑工具 for tool in ARCHITECTURAL_TOOLS: server.tool()(tool) # 注册MEP工具 for tool in MEP_TOOLS: server.tool()(tool) # 注册通用工具 for tool in GENERAL_TOOLS: server.tool()(tool)
- tests/GetSelectedElements.py:1-14 (helper)Test file demonstrating the RPC method 'GetSelectedElements' sent to Revit, which corresponds to the MCP tool get_selected_elements.# -*- coding: utf-8 -*- from _tcp import send_tcp_data # 构造 JSON-RPC 请求 json_rpc_request = { "jsonrpc": "2.0", "method": "GetSelectedElements", "params": {}, } # 发送更新元素数据 send_tcp_data(json_rpc_request)