zoukankan      html  css  js  c++  java
  • 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

    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 menu
    
        def Activated(self):
            """This function is executed when the workbench is activated"""
            return
    
        def Deactivated(self):
            """This function is executed when the workbench is deactivated"""
            return
    
        def 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 menu
    
        def GetClassName(self):
            # this function is mandatory if this is a full python workbench
            return "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")
            return
    
        def 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 goes
                pass
            if reply == QtGui.QMessageBox.No:
                # this is where the code relevant to a 'No' answer goes
                pass
            return
    
        def 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日

  • 相关阅读:
    拒绝喝酒理由1
    动态调用事件,事件的方法
    c#发送图片
    c#截屏功能的实现
    devexpress中文讨论论坛
    解析javascript变量
    devexpress_PivotGrid说明
    python学习之老男孩python全栈第九期_day022知识点总结——初识面向对象
    python学习之老男孩python全栈第九期_day022作业
    员工信息表
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12563018.html
Copyright © 2011-2022 走看看