FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3298字,纯文字阅读大概需要5分钟。
内容图文
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
class MyWorkbench ( Workbench ): MenuText = "My Workbench1" ToolTip = "A description of my workbench" Icon = """paste here the contents of a 16x16 xpm icon"""def Initialize(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 menudef Activated(self): """This function is executed when the workbench is activated"""returndef Deactivated(self): """This function is executed when the workbench is deactivated"""returndef ContextMenu(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 menudef GetClassName(self): # this function is mandatory if this is a full python workbenchreturn"Gui::PythonWorkbench" Gui.addWorkbench(MyWorkbench()) Caesar卢尚宇 2020年3月24日
LSY.py
import FreeCAD import FreeCADGui from PySide import QtGui, QtCore class MySecondCommand: def GetResources(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") returndef IsActive(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."""return True FreeCADGui.addCommand(‘MySecondCommand‘, MySecondCommand()) class MySecondCommand1: def GetResources(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 goespassif reply == QtGui.QMessageBox.No: # this is where the code relevant to a ‘No‘ answer goespassreturndef IsActive(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."""return True 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
内容总结
以上是互联网集市为您收集整理的FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令全部内容,希望文章能够帮你解决FreeCAD二次开发-创建Python工作台,添加菜单栏和工具条,FreeCAD命令所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。