python工作台_FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令
FreeCAD作為一款基于OpenCasCAD內核的開源CAD軟件,可以在GitHub上下載源代碼。閱讀源代碼,有助于我們學習CAD軟件架構,了解底層幾何算法。
由博主Caesar盧尚宇自學整理(純粹出于對三維CAD軟件開發的熱愛)
內容出自FreeCAD官方社區https://wiki.freecadweb.org/Workbench_creation
可以把FreeCAD理解成一個QT界面的容器,每次FreeCAD啟動時都去Mod文件夾里讀取所有工作臺。
我們在Mod里添加自己的工作臺文件夾,里面放三個文件。
Init.py這個是FreeCAD啟動的時候執行的,不與界面交互的,后臺執行。(一般做界面工具開發,這個文件為空就行了)
InitGui.py這個是FreeCAD啟動的時候執行的,與界面交互的代碼。
LSY.py這個是我們存放命令功能的文件。
InitGui.py
classMyWorkbench ( Workbench ):
MenuText= "My Workbench1"ToolTip= "A description of my workbench"Icon= """paste here the contents of a 16x16 xpm icon"""
defInitialize(self):"""This function is executed when FreeCAD starts"""
import LSY #import here all the needed files that create your FreeCAD commands
self.list = [‘MySecondCommand‘, ‘MySecondCommand1‘] #A list of command names created in the line above
self.appendToolbar(‘My Commands‘,self.list) #creates a new toolbar with your commands
self.appendMenu(‘My New Menu‘,self.list) #creates a new menu
self.appendMenu(["An existing Menu", "My submenu"], self.list) #appends a submenu to an existing menu
defActivated(self):"""This function is executed when the workbench is activated"""
return
defDeactivated(self):"""This function is executed when the workbench is deactivated"""
return
defContextMenu(self, recipient):"""This is executed whenever the user right-clicks on screen"""
#"recipient" will be either "view" or "tree"
self.appendContextMenu("My commands", self.list) #add commands to the context menu
defGetClassName(self):#this function is mandatory if this is a full python workbench
return "Gui::PythonWorkbench"Gui.addWorkbench(MyWorkbench())
Caesar盧尚宇
2020年3月24日
LSY.py
importFreeCADimportFreeCADGuifrom PySide importQtGui, QtCoreclassMySecondCommand:defGetResources(self):return {‘Pixmap‘: ‘freecad‘, ‘MenuText‘: ‘show Message1‘, ‘ToolTip‘: ‘Print show Message1‘}def Activated(self): #點擊按鈕執行的動作
"""Do something here"""reply= QtGui.QMessageBox.information(None,"","Houston, we have a problem")return
defIsActive(self):"""Here you can define if the command must be active or not (greyed) if certain conditions
are met or not. This function is optional."""
returnTrue
FreeCADGui.addCommand(‘MySecondCommand‘, MySecondCommand())classMySecondCommand1:defGetResources(self):return {‘Pixmap‘: ‘freecad‘, ‘MenuText‘: ‘show Message2‘, ‘ToolTip‘: ‘Print show Message2‘}def Activated(self): #點擊按鈕執行的動作
"""Do something here"""reply= QtGui.QMessageBox.question(None, "", "This is your chance to answer, what do you think?",QtGui.QMessageBox.Yes |QtGui.QMessageBox.No, QtGui.QMessageBox.No)if reply ==QtGui.QMessageBox.Yes:#this is where the code relevant to a ‘Yes‘ answer goes
pass
if reply ==QtGui.QMessageBox.No:#this is where the code relevant to a ‘No‘ answer goes
pass
return
defIsActive(self):"""Here you can define if the command must be active or not (greyed) if certain conditions
are met or not. This function is optional."""
returnTrue
FreeCADGui.addCommand(‘MySecondCommand1‘, MySecondCommand1())
Caesar盧尚宇
2020年3月24日
這兩個文件里的代碼,也是從社區里找到的。但是!原封不動的復制下來去做,會出錯。一定要修改它的代碼。我試了兩個小時,在找到一些有問題的地方。(使用的話,直接復制我上面的代碼就行了,我修改過了)
第一處:
第二處:
第三處:
演示:
附加參考資料https://www.jianshu.com/p/8a0a2b0e4aea
Caesar盧尚宇
2020年3月24日
原文:https://www.cnblogs.com/nxopen2018/p/12563018.html
總結
以上是生活随笔為你收集整理的python工作台_FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm32c8t6之跑马灯程序配置
- 下一篇: python读取并可视化npy格式的深度