Add Button to FEM Toolbar Tutorial: Difference between revisions

From FreeCAD Documentation
mNo edit summary
Line 41: Line 41:
A new command class has to be added to the {{incode|src/Mod/Fem/femcommands/commands.py}} module.
A new command class has to be added to the {{incode|src/Mod/Fem/femcommands/commands.py}} module.


Just copy an existing command and adjust the icon, menu text and tool-tip in __init__(self).
Just copy/paste an existing command, then adjust the icon, menu textm and tool-tip in {{incode|__init__(self)}}
</translate>
</translate>
{{code|code=
<pre>
class _testButton(CommandManager):
class _testButton(CommandManager):
"The FEM_testButton command definition"
"The FEM_testButton command definition"
Line 53: Line 54:
self.is_active = "always"
self.is_active = "always"
#self.do_activated = "add_obj_on_gui_selobj_noset_edit"
#self.do_activated = "add_obj_on_gui_selobj_noset_edit"
}}
</pre>
'''Don't forget''' to register the command at the bottom of the module file by using the addCommand(...) method.
'''Important''': Don't forget to register the command at the bottom of the module file by using the {{incode|addCommand(...)}} method.


{{code|code=
<pre>
FreeCADGui.addCommand(
FreeCADGui.addCommand(
"FEM_testButton",
"FEM_testButton",
_testButton()
_testButton()
)
)
}}
</pre>

Please see the discussion in the forum at https://forum.freecadweb.org/viewtopic.php?f=18&t=46693&start=10#p402004 if icons are involved.
'''Note''': Please see the [https://forum.freecadweb.org/viewtopic.php?f=18&t=46693&start=10#p402004 discussion thread] in the forum if icons are involved.


<translate>
<translate>

Revision as of 03:05, 4 August 2021

Other languages:
Tutorial
Topic
FEM
Level
Advanced
Time to complete
60 min
Authors
JohnWang
FreeCAD version
0.19
Example files
See also
None

Notes: Work In Progress

FEM workbench has toolbars and menus. This tutorial shows how to add a test button onto the toolbars. It also shows how to add a menuitem into the menu.

The task can be split into four parts:

  • Create a new icon file.
  • Register the new icon file. Make change at src/Mod/Fem//Gui/Resources/Fem.qrc
  • Create a new command class. Make change at src/Mod/Fem/femcommands/commands.py
  • Add the new command onto the workbench. Make change at src/Mod/Fem/Gui/Workbench.cpp

Create a new icon file

For a button, we need an icon file. You can use any of your favorite tools to create it, but it must be in SVG format. Here we use FEM_testButton.svg file as an example.

It must be placed into: src/Mod/Fem/Gui/Resources/icons/

Register the new icon file

The new SVG icon file has to be registered for the GUI-button by inserting it into src/Mod/Fem/Gui/Resources/Fem.qrc:

<file>icons/FEM_testButton.svg</file>

Create a new command class

A new command class has to be added to the src/Mod/Fem/femcommands/commands.py module.

Just copy/paste an existing command, then adjust the icon, menu textm and tool-tip in __init__(self)

class _testButton(CommandManager):
    "The FEM_testButton command definition"

    def __init__(self):
        super(_testButton, self).__init__()
        self.menuetext = "test Button"
        self.tooltip = "This is a test button"
        self.is_active = "always"
        #self.do_activated = "add_obj_on_gui_selobj_noset_edit"

Important: Don't forget to register the command at the bottom of the module file by using the addCommand(...) method.

FreeCADGui.addCommand(
    "FEM_testButton",
    _testButton()
)

Note: Please see the discussion thread in the forum if icons are involved.


Add the new command onto the workbench

The FEM workbench has several toolbars and menus. We will add the new command on to both the solve toolbar and the solve menu.

Search for the following code snippet in /Gui/Workbench.cpp and add the new command.

 
     Gui::ToolBarItem* solve = new Gui::ToolBarItem(root);
     solve->setCommand("Solve");
     *solve << "FEM_SolverCalculixCxxtools"
           << "FEM_SolverCalculiX"
           << "FEM_SolverElmer"
+          << "FEM_testButton"
           << "Separator"

To add the command to the solve menu of the FEM workbench, search for the following code snippet in Workbench.cpp.

 
    Gui::MenuItem* solve = new Gui::MenuItem;
    root->insertItem(item, solve);
    solve->setCommand("&Solve");
    *solve << "FEM_SolverCalculixCxxtools"
           << "FEM_SolverCalculiX"
           << "FEM_SolverElmer"
           << "FEM_SolverZ88"
+          << "FEM_testButton"
           << "Separator"

You have just added an test button onto FEM workbench toolbar and menu. Now, you can compile your own FreeCAD and test your new button.