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日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值