Add Button to FEM Toolbar Tutorial: Difference between revisions

From FreeCAD Documentation
m (Roy 043 moved page Add A Button In FEM Toolbar Tutorial to Add button to FEM toolbar tutorial: New page titles should use sentence case)
mNo edit summary
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
<translate>


<translate>
<!--T:1-->
<!--T:1-->
{{TutorialInfo
{{TutorialInfo
|Topic= FEM
|Topic=FEM
|Level= Advanced
|Level=Advanced
|Time= 60 min
|Time=60 min
|Author=[[User:JohnWang|JohnWang]]
|Author=[[User:JohnWang|JohnWang]]
|FCVersion=0.19
|FCVersion=0.19
|Files=
|Files=
}}
}}
</translate>


== Introduction == <!--T:2-->
'''Notes: Work In Progress'''


<!--T:3-->
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 FEM workbench has toolbars and menus. This tutorial shows how to add a test button to a toolbar. It also shows how to add a menuitem to a menu.


<!--T:4-->
The task can be split into four parts:
The task can be split into four parts:
* '''Create a new icon file'''.
* '''Create a new icon file'''.
* '''Register the new icon file'''. <small>Modification needed to {{incode|src/Mod/Fem//Gui/Resources/Fem.qrc}}</small>
* '''Register the new icon file'''. Modification needed to {{incode|src/Mod/Fem/Gui/Resources/Fem.qrc}}
* '''Create a new command class'''. <small>Modification needed to {{incode|src/Mod/Fem/femcommands/commands.py}}</small>
* '''Create a new command class'''. Modification needed to {{incode|src/Mod/Fem/femcommands/commands.py}}
* '''Add new command to workbench'''. <small>Modification needed to {{incode|src/Mod/Fem/Gui/Workbench.cpp}}</small>
* '''Add new command to workbench'''. Modification needed to {{incode|src/Mod/Fem/Gui/Workbench.cpp}}


== Create a new icon file ==
== Create a new icon file == <!--T:5-->


<!--T:6-->
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 {{FileName|FEM_testButton.svg}} file as an example.
For the button we need an icon file. You can use any of your favorite tools to create it, but it must be in the SVG format. Here we will use the {{FileName|FEM_testButton.svg}} file as an example.


<!--T:7-->
It must be placed into: {{incode|src/Mod/Fem/Gui/Resources/icons/}}
It must be placed in: {{incode|src/Mod/Fem/Gui/Resources/icons/}}.


== Register the new icon file ==
== Register the new icon file == <!--T:8-->


<!--T:9-->
The new SVG icon file has to be registered for the GUI-button by inserting it into {{incode|src/Mod/Fem/Gui/Resources/Fem.qrc}}:
The new SVG icon file has to be registered for the GUI-button by inserting it in {{incode|src/Mod/Fem/Gui/Resources/Fem.qrc}}:


</translate>
{{code|code=
{{code|code=
<file>icons/FEM_testButton.svg</file>
<file>icons/FEM_testButton.svg</file>
Line 38: Line 43:
<translate>
<translate>


== Create a new command class ==
== Create a new command class == <!--T:10-->

<!--T:11-->
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.


<!--T:12-->
Just copy/paste an existing command, then adjust the icon, menu textm and tool-tip in {{incode|__init__(self)}}
Just copy/paste an existing command, then adjust the icon, menu text and tool-tip in {{incode|__init__(self)}}:

</translate>
</translate>
{{code|code=
{{code|code=
Line 55: Line 63:
#self.do_activated = "add_obj_on_gui_selobj_noset_edit"
#self.do_activated = "add_obj_on_gui_selobj_noset_edit"
}}
}}
<translate>
'''Important''': Don't forget to register the command at the bottom of the module file by using the {{incode|addCommand(...)}} method.


<!--T:13-->
Don't forget to register the command at the bottom of the module file with the {{incode|addCommand(...)}} method:

</translate>
{{code|code=
{{code|code=
FreeCADGui.addCommand(
FreeCADGui.addCommand(
Line 63: Line 75:
)
)
}}
}}
<translate>


<!--T:14-->
'''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.
'''Note''': Please see this [https://forum.freecadweb.org/viewtopic.php?f=18&t=46693&start=10#p402004 discussion thread] in the forum if icons are involved.


== Add new command to workbench == <!--T:15-->
<translate>


<!--T:16-->
== Add new command to 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.
We will add the new command to both the '''solve''' toolbar and the '''solve''' menu.

<!--T:17-->
Search for the following code snippet in {{incode|/Gui/Workbench.cpp}} and add the new command:


Search for the following code snippet in {{incode|/Gui/Workbench.cpp}} and add the new command.
</translate>
</translate>
{{code|code=
{{code|code=
Line 82: Line 98:
<< "Separator"
<< "Separator"
}}
}}

<translate>
<translate>

To add the command to the '''solve''' menu of the FEM workbench, search for the following code snippet in {{incode|Workbench.cpp}}.
<!--T:18-->
To add the command to the '''solve''' menu of the FEM workbench, search for the following code snippet in {{incode|Workbench.cpp}}:

</translate>
</translate>
{{code|code=
{{code|code=
Line 97: Line 115:
<< "Separator"
<< "Separator"
}}
}}
<translate>


<!--T:19-->
'''Result''': You should have just successfully added a test button into FEM workbench toolbar and menu. Now, you [[Compiling|compile FreeCAD]] and test your new button.
'''Result''': You should have just successfully added a test button to a FEM workbench toolbar and menu. Now, you can [[Compiling|compile FreeCAD]] and test your new button.


== Related ==
== Related == <!--T:20-->


<!--T:21-->
* [[Extend FEM Module]]
* [[Onboarding FEM Devs]]
* [[Extend_FEM_Module|Extend FEM Module]]
* [[Onboarding_FEM_Devs|Onboarding FEM Devs]]



</translate>
[[Category:FEM{{#translation:}}]]
[[Category:FEM{{#translation:}}]]
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Developer Documentation{{#translation:}}]]

Revision as of 17:43, 20 November 2021

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

Introduction

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

The task can be split into four parts:

  • Create a new icon file.
  • Register the new icon file. Modification needed to src/Mod/Fem/Gui/Resources/Fem.qrc
  • Create a new command class. Modification needed to src/Mod/Fem/femcommands/commands.py
  • Add new command to workbench. Modification needed to src/Mod/Fem/Gui/Workbench.cpp

Create a new icon file

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

It must be placed in: 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 in 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 text 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"

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

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

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

Add new command to workbench

We will add the new command 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"

Result: You should have just successfully added a test button to a FEM workbench toolbar and menu. Now, you can compile FreeCAD and test your new button.

Related