python工作台_FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令
FreeCAD作為一款基于OpenCasCAD內(nèi)核的開源CAD軟件,可以在GitHub上下載源代碼。閱讀源代碼,有助于我們學(xué)習(xí)CAD軟件架構(gòu),了解底層幾何算法。
由博主Caesar盧尚宇自學(xué)整理(純粹出于對(duì)三維CAD軟件開發(fā)的熱愛)
內(nèi)容出自FreeCAD官方社區(qū)https://wiki.freecadweb.org/Workbench_creation
可以把FreeCAD理解成一個(gè)QT界面的容器,每次FreeCAD啟動(dòng)時(shí)都去Mod文件夾里讀取所有工作臺(tái)。
我們在Mod里添加自己的工作臺(tái)文件夾,里面放三個(gè)文件。
Init.py這個(gè)是FreeCAD啟動(dòng)的時(shí)候執(zhí)行的,不與界面交互的,后臺(tái)執(zhí)行。(一般做界面工具開發(fā),這個(gè)文件為空就行了)
InitGui.py這個(gè)是FreeCAD啟動(dòng)的時(shí)候執(zhí)行的,與界面交互的代碼。
LSY.py這個(gè)是我們存放命令功能的文件。
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): #點(diǎn)擊按鈕執(zhí)行的動(dòng)作
"""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): #點(diǎn)擊按鈕執(zhí)行的動(dòng)作
"""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日
這兩個(gè)文件里的代碼,也是從社區(qū)里找到的。但是!原封不動(dòng)的復(fù)制下來去做,會(huì)出錯(cuò)。一定要修改它的代碼。我試了兩個(gè)小時(shí),在找到一些有問題的地方。(使用的話,直接復(fù)制我上面的代碼就行了,我修改過了)
第一處:
第二處:
第三處:
演示:
附加參考資料https://www.jianshu.com/p/8a0a2b0e4aea
Caesar盧尚宇
2020年3月24日
原文:https://www.cnblogs.com/nxopen2018/p/12563018.html
總結(jié)
以上是生活随笔為你收集整理的python工作台_FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm32c8t6之跑马灯程序配置
- 下一篇: c#日期转换周几_Java时间与日期