-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitGui.py
More file actions
111 lines (92 loc) · 4.2 KB
/
Copy pathInitGui.py
File metadata and controls
111 lines (92 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import sys
import FreeCAD
import FreeCADGui
# FreeCAD 通过 exec() 加载 InitGui.py,此时 __file__ 不可用,Python 也不知道本插件
# 的其他模块在哪里。必须手动定位插件目录并加入 sys.path,
# 否则后续的 import 会报 ModuleNotFoundError。
_mod_dir = os.path.join(FreeCAD.getUserAppDataDir(), "Mod", "CadAgent")
if not os.path.isdir(_mod_dir):
# 用户目录下没有则回退到 FreeCAD 安装目录
_mod_dir = os.path.normpath(os.path.join(FreeCAD.getHomePath(), "Mod", "CadAgent"))
if _mod_dir not in sys.path:
sys.path.insert(0, _mod_dir)
# Store on FreeCAD for access inside method bodies (exec() scope trap:
# top-level names are invisible inside class methods).
FreeCAD._cadagent_dir = _mod_dir
# FreeCAD exec() 作用域陷阱:InitGui.py 被 exec() 加载时,文件内定义的顶层名称
# (函数、类)在其方法体中不可作为全局变量使用。方法体只能访问:
# 1. FreeCAD 预注入的名称(FreeCAD, FreeCADGui, Workbench 等)
# 2. self 及其属性
# 3. 方法体内局部 import 的模块
# 因此下方所有方法体内部都使用局部 import 来引入依赖。
class CadAgentWorkbench(Workbench):
MenuText = "CadAgent"
ToolTip = "AI-powered CAD Agent"
Icon = ""
def __init__(self):
import os
icon_path = os.path.join(
FreeCAD._cadagent_dir, "resources", "icons", "CadAgentWorkbench.svg"
)
if os.path.isfile(icon_path):
self.__class__.Icon = icon_path
def Initialize(self):
"""首次切换到此工作台时调用,注册工具栏按钮。"""
import os
import FreeCADGui
icon_dir = os.path.join(FreeCAD._cadagent_dir, "resources", "icons")
if os.path.isdir(icon_dir):
FreeCADGui.addIconPath(icon_dir)
self.appendToolbar("CadAgent", ["CadAgent_ShowPanel", "CadAgent_Settings"])
self.appendMenu("CadAgent", ["CadAgent_ShowPanel", "CadAgent_Settings"])
def Activated(self):
"""每次切换到此工作台时调用,创建并显示 Agent panel。"""
from PySide6 import QtCore
from ui.panel import AgentPanel
import FreeCADGui
if not hasattr(FreeCADGui, '_cadagent_panel') or FreeCADGui._cadagent_panel is None:
FreeCADGui._cadagent_panel = AgentPanel()
FreeCADGui.getMainWindow().addDockWidget(QtCore.Qt.RightDockWidgetArea, FreeCADGui._cadagent_panel)
FreeCADGui._cadagent_panel.show()
FreeCADGui._cadagent_panel.raise_()
def Deactivated(self):
"""切换离开此工作台时调用。"""
pass
# FreeCAD Command 协议:需要实现 GetResources / Activated / IsActive 三个方法。
# 工具栏按钮点击时触发 Activated。
class _ShowPanelCmd:
def GetResources(self):
return {
"MenuText": "CadAgent",
"ToolTip": "Open the CadAgent panel",
}
def Activated(self):
from PySide6 import QtCore
from ui.panel import AgentPanel
import FreeCADGui
if not hasattr(FreeCADGui, '_cadagent_panel') or FreeCADGui._cadagent_panel is None:
FreeCADGui._cadagent_panel = AgentPanel()
FreeCADGui.getMainWindow().addDockWidget(QtCore.Qt.RightDockWidgetArea, FreeCADGui._cadagent_panel)
FreeCADGui._cadagent_panel.show()
FreeCADGui._cadagent_panel.raise_()
def IsActive(self):
return True
class _SettingsCmd:
def GetResources(self):
return {
"MenuText": "CadAgent Settings",
"ToolTip": "Configure CadAgent API and agent parameters",
}
def Activated(self):
from ui.settings_dialog import SettingsDialog
import FreeCADGui
SettingsDialog(parent=FreeCADGui.getMainWindow()).exec()
def IsActive(self):
return True
# 注册命令和工作台到 FreeCAD GUI 系统,必须在文件末尾执行。
# addCommand 的第一个参数是命令 ID,与工作台 Initialize 中引用的一致。
FreeCADGui.addCommand("CadAgent_ShowPanel", _ShowPanelCmd())
FreeCADGui.addCommand("CadAgent_Settings", _SettingsCmd())
FreeCADGui.addWorkbench(CadAgentWorkbench())
FreeCAD.Console.PrintMessage("CadAgent workbench loaded.\n")