利用FreeCAD的插件开发功能可以定制一些针对特定应用的宏,比如图形程序转机械手语言这样针对特定场所使用的功能,下面将简述一下基于FreeCAD0.19版本的开发macro插件的流程。
准备工作:
**安装FreeCAD0.19版本
提示:注意下文都是安装缺省安装目录来介绍,如安装到非缺省目录请自行调整文档中的路径。
**安装vscode 本文将基于vscode作为调试工具进行介绍。
提示:你不需要额外安装python,freecad里面已经集成
技能要求:熟悉python 语言, 熟悉pyqt。
开发步骤:
1 ui 设计
进入目录C:\Program Files\FreeCAD 0.19\bin\Scripts,运行pyside2-designer.exe
按照QTdesigner的方式规划ui,比如规划了下面这个测试界面:
将其保存为hello.ui
2 在命令行下运行转换程序将hello.ui转换成为python文件。
你需要进入C:\Program Files\FreeCAD 0.19\bin\Scripts目录并运行该目录下面的pyside2-uic.exe程序
.\pyside2-uic.exe D:\hello.ui -o D:\hello.py
3 修改hello.py:
增加全局变量:
global count ; count=0
增加按键回调:
def on_PButton_clicked(self):
global count
count+=1
self.label_result.setText(str(count))
在setupUi函数里面增加按键回调关联:
self.pushButton.clicked.connect(self.on_PButton_clicked)
在末尾追加启动函数:
d = QtGui.QWidget()
d.ui = Ui_Dialog()
d.ui.setupUi(d)
d.show()
将生成的文件复制到FreeCAD的数据目录下面:
C:\Users\Administrator\AppData\Roaming\FreeCAD\Macro
然后通过FreeCAD 的菜单macro->macros可以看到该文件:
查看宏列表可以看到对应的文件。
提示:标准的freecad的宏是以FCMacro结尾,对于以py结尾的文件freecad也可以正常识别。
4 选中hello.py并点击execute:
点击“测试”按键
5 增加能够远程调试功能:
重新编辑hello.py,在代码前面添加进入调试的几行代码。
# debug
import ptvsd
print("Waiting for debugger attach")
# 5678 is the default attach port in the VS Code debug configurations
ptvsd.enable_attach(address=('localhost', 5678))
ptvsd.wait_for_attach()
#debug end
6 打开vscode,点击“运行”->“打开配置”:
增加配置选项:
{
"workbench.colorTheme": "Default Dark+",
"editor.minimap.enabled": false,
"workbench.editor.enablePreview": false,
"security.workspace.trust.untrustedFiles": "open",
"launch": {
"configurations": [
{
"name": "Python: Attacher",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "C:\\Users\\Administrator\\AppData\\Roaming\\FreeCAD\\Macro\\",
"remoteRoot": "C:\\Users\\Administrator\\AppData\\Roaming\\FreeCAD\\Macro\\"
}
]
},
]
}
}
6 开始调试 在freecad里面重新开始运行hello.py,这次程序并没有运行且freecad还会出现类似死机状态,这是在等待调试器连接,你需要切换到vscode 打开Hello.py 设置好断点后点击“运行”->“启动调试”
程序会开始运行并跳转到断点,即可进行调试和分析
提示:freeCAD集成了一个python 命令控制台,可以通过view->panels->python console,启用后窗口操作对应的python命令会在该处显示,可以用于参考命令。