Skip to main content
Glama
neka-nat
by neka-nat

edit_object

Modify object properties in FreeCAD when initial creation fails. Specify document, object name, and desired properties to update the object and receive a success confirmation and screenshot.

Instructions

Edit an object in FreeCAD. This tool is used when the create_object tool cannot handle the object creation.

Args:
    doc_name: The name of the document to edit the object in.
    obj_name: The name of the object to edit.
    obj_properties: The properties of the object to edit.

Returns:
    A message indicating the success or failure of the object editing and a screenshot of the object.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
doc_nameYes
obj_nameYes
obj_propertiesYes

Implementation Reference

  • Primary MCP tool handler for 'edit_object'. Proxies the call to FreeCADConnection.edit_object, handles errors, formats response with success message and optional screenshot.
    @mcp.tool()
    def edit_object(
        ctx: Context, doc_name: str, obj_name: str, obj_properties: dict[str, Any]
    ) -> list[TextContent | ImageContent]:
        """Edit an object in FreeCAD.
        This tool is used when the `create_object` tool cannot handle the object creation.
    
        Args:
            doc_name: The name of the document to edit the object in.
            obj_name: The name of the object to edit.
            obj_properties: The properties of the object to edit.
    
        Returns:
            A message indicating the success or failure of the object editing and a screenshot of the object.
        """
        freecad = get_freecad_connection()
        try:
            res = freecad.edit_object(doc_name, obj_name, {"Properties": obj_properties})
            screenshot = freecad.get_active_screenshot()
    
            if res["success"]:
                response = [
                    TextContent(type="text", text=f"Object '{res['object_name']}' edited successfully"),
                ]
                return add_screenshot_if_available(response, screenshot)
            else:
                response = [
                    TextContent(type="text", text=f"Failed to edit object: {res['error']}"),
                ]
                return add_screenshot_if_available(response, screenshot)
        except Exception as e:
            logger.error(f"Failed to edit object: {str(e)}")
            return [
                TextContent(type="text", text=f"Failed to edit object: {str(e)}")
            ]
  • Proxy method in FreeCADConnection class that forwards the edit_object RPC call to the FreeCAD XMLRPC server.
    def edit_object(self, doc_name: str, obj_name: str, obj_data: dict[str, Any]) -> dict[str, Any]:
        return self.server.edit_object(doc_name, obj_name, obj_data)
  • XMLRPC server method for edit_object that queues the GUI-safe editing task and returns success/error response.
    def edit_object(self, doc_name: str, obj_name: str, properties: dict[str, Any]) -> dict[str, Any]:
        obj = Object(
            name=obj_name,
            properties=properties.get("Properties", {}),
        )
        rpc_request_queue.put(lambda: self._edit_object_gui(doc_name, obj))
        res = rpc_response_queue.get()
        if res is True:
            return {"success": True, "object_name": obj.name}
        else:
            return {"success": False, "error": res}
  • GUI-thread safe implementation that performs the actual property setting on FreeCAD objects, handling special cases like References and Placement.
    def _edit_object_gui(self, doc_name: str, obj: Object):
        doc = FreeCAD.getDocument(doc_name)
        if not doc:
            FreeCAD.Console.PrintError(f"Document '{doc_name}' not found.\n")
            return f"Document '{doc_name}' not found.\n"
    
        obj_ins = doc.getObject(obj.name)
        if not obj_ins:
            FreeCAD.Console.PrintError(f"Object '{obj.name}' not found in document '{doc_name}'.\n")
            return f"Object '{obj.name}' not found in document '{doc_name}'.\n"
    
        try:
            # For Fem::ConstraintFixed
            if hasattr(obj_ins, "References") and "References" in obj.properties:
                refs = []
                for ref_name, face in obj.properties["References"]:
                    ref_obj = doc.getObject(ref_name)
                    if ref_obj:
                        refs.append((ref_obj, face))
                    else:
                        raise ValueError(f"Referenced object '{ref_name}' not found.")
                obj_ins.References = refs
                FreeCAD.Console.PrintMessage(
                    f"References updated for '{obj.name}' in '{doc_name}'.\n"
                )
                # delete References from properties
                del obj.properties["References"]
            set_object_property(doc, obj_ins, obj.properties)
            doc.recompute()
            FreeCAD.Console.PrintMessage(f"Object '{obj.name}' updated via RPC.\n")
            return True
        except Exception as e:
            return str(e)
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/neka-nat/freecad-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server