freecad二开 xmlrpc接口api qtgui

FreeCAD.ConfigGet("UserAppData") 文件夹下创建mod文件夹

mod文件夹底下创建插件文件夹my_server:

freecad_server.py:

python 复制代码
from xmlrpc.server import SimpleXMLRPCServer
import FreeCADGui
import FreeCAD

import queue
from PySide2.QtCore import QTimer
import threading
# 定义一个类来封装API方法



# GUI task queue
rpc_request_queue = queue.Queue()
rpc_response_queue = queue.Queue()


def process_gui_tasks():
    while not rpc_request_queue.empty():
       
        task = rpc_request_queue.get()
        res = task()
        if res is not None:
            rpc_response_queue.put(res)
    QTimer.singleShot(500, process_gui_tasks)

class FreeCADRPC:
    """RPC server for FreeCAD"""

    def ping(self):
        return True

    def create_document(self, name="New_Document"):
    
       
        rpc_request_queue.put(lambda: self._create_document_gui(name))
        res = rpc_response_queue.get()
        if res is True:
            return {"success": True, "document_name": name}
        else:
            return {"success": False, "error": res}
        
    def _create_document_gui(self, name):
        
        doc = FreeCAD.newDocument(name)
        doc.recompute()
        print(f"Document '{name}' created via RPC.\n")
        return True
def start_rpc_server(host="localhost", port=8000):
   

    print(f"Starting RPC server at {host}:{port}...")
    rpc_server_instance = SimpleXMLRPCServer(
        (host, port), allow_none=True, logRequests=False
    )
    rpc_server_instance.register_instance(FreeCADRPC())

    def server_loop():
        
        rpc_server_instance.serve_forever()

    rpc_server_thread = threading.Thread(target=server_loop, daemon=True)
    rpc_server_thread.start()

    QTimer.singleShot(500, process_gui_tasks)

    return f"RPC Server started at {host}:{port}."

class StartRPCServerCommand:
    def GetResources(self):
        return {"MenuText": "なんで好きか――?"}

    def Activated(self):
        msg = start_rpc_server()

    def IsActive(self):
        return True
FreeCADGui.addCommand("a_Server", StartRPCServerCommand())

如果不在gui里加入服务器操作的话,比如说直接调用freecad.newdocument() 会在闪退前出现以下报错

有试过在init.py里面直接跑,不行,还是只能加在任务栏上

test.py:可以用这个测试服务有没有装上

python 复制代码
import xmlrpc.client

# 连接到服务器
proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")

# 调用函数
result = proxy.create_document()

如果想接mcp:

python 复制代码
from mcp.server.fastmcp import FastMCP


mcp = FastMCP("Demo")
import xmlrpc.client

# 连接到服务器
proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")


@mcp.tool()
def create_document():
    """
        创建一个freecad的基础文档。

    参数:不需要参数
        

    返回:
        函数执行结果

    示例:
        >>> proxy.create_document()
    """
    return  proxy.create_document()


if __name__ == "__main__":
     mcp.run()
 
python 复制代码
{
  "mcpServers": {
    "freecad_mcp": {
      "autoApprove": [],
      "disabled": false,
      "timeout": 60,
      "command": "/home/chen/anaconda3/envs/freecad_mcp/bin/python",
      "args": [
        "/home/chen/snap/freecad/common/Mod/freecad_mcp_my/mcp_server.py"
      ],
      "transportType": "stdio"
    }}

outpu

他这个只能调用基础的工作台,我想给他连到别的工作台

gitee:sato77711/freecad_mcp_addon可以直接下载后丢入FreeCAD.ConfigGet("UserAppData") 文件夹下创建的mod文件夹

参考:https://wiki.freecad.org/Workbench_creation/zh-hant

FreeCAD二次开发-基于PyQT对话框与FC交互的开发_51CTO博客_freecad二次开发

代码来自:
freecad-mcp · PyPI

相关推荐
智者知已应修善业5 小时前
【proteus中lm339电压滞回比较器达到三角波转换成方波】2023-4-13
驱动开发·经验分享·笔记·硬件架构·proteus·硬件工程
恒拓高科WorkPlus8 小时前
私有化视频会议的核心价值在于安全与自主可控
经验分享
其实秋天的枫8 小时前
2025年12月大学英语六级真题及答案电子版pdf三套全
经验分享·pdf
其实秋天的枫9 小时前
【专四】英语专业四级TEM4历年真题及答案电子版PDF(2009-2025年)
经验分享
其实秋天的枫11 小时前
2025年12月英语六级真题及答案解析完整版(第一、二、三套全PDF)
经验分享·算法
中屹指纹浏览器11 小时前
2026指纹浏览器性能瓶颈分析与优化技巧
经验分享·笔记
优化控制仿真模型11 小时前
2026年最新驾考科目一考试题库2309道全。电子版pdf
经验分享·算法·pdf
上海曼博生物医药科技有限公司12 小时前
Biolaminin 521在MSCs培养中的应用:从原代扩增到iMSC分化的关键基质解析【曼博生物】
经验分享·业界资讯·细胞培养·biolamina·ln521·层粘连蛋白
探索宇宙真理.12 小时前
SiYuan SQL漏洞 | CVE-2026-29073复现&研究
数据库·经验分享·sql·eureka·安全漏洞·siyuan
吉哥机顶盒刷机14 小时前
海信IP820N机顶盒的刷机教程
经验分享·刷机