Add Button to FEM Toolbar Tutorial: Difference between revisions

From FreeCAD Documentation
No edit summary
mNo edit summary
 
(53 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
<translate>
<translate>

<!--T:1-->
<!--T:1-->
{{TutorialInfo
{{TutorialInfo
|Topic= Add FEM Equation
|Topic=FEM
|Level=
|Level=Advanced
|Time=
|Time=60 min
|Author=[[User:JohnWang|JohnWang]]
|Author=[[User:JohnWang|JohnWang]]
|FCVersion=0.19
|FCVersion=0.19
Line 11: Line 12:
}}
}}


<!--T:2-->
== Introduction == <!--T:2-->
In this tutorial we are going to add the '''Flow''' equation to FreeCAD and implement support for elmer solver. Please make sure you have read and understood [[Extend_FEM_Module| Extend FEM Module]] before reading this tutorial.


<!--T:3-->
<!--T:3-->
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 menu item to a menu.
The task can be split into five parts:
* '''New equation type'''. This step must only be done if the equation doesn't exist in FreeCAD yet (as opposed to a equation that is already in FreeCAD but not supported by the target solver).


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


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


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

In this step we are going to modify the following files:
* src/Mod/Fem/femcommands/commands.py
* src/Mod/Fem/Gui/Workbench.cpp
* src/Mod/Fem/Gui/Resources/Fem.qrc
We also need to create an Icon file:
* src/Mod/Fem/Gui/Resources/icons/FEM_EquationFlow.svg


== Register the new icon file == <!--T:8-->
We need a FEM_EquationFlow.svg file and has to be put into {{incode|/Gui/Resources/icons/}}. The new FEM_EquationFlow.svg icon has to be registered for the GUI-button with {{incode|<file>icons/FEM_EquationFlow.svg</file>}} in {{incode|Fem.qrc}} (in {{incode|/Gui/Resources/}}).


<!--T:9-->
<!--T:9-->
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=
<file>icons/FEM_testButton.svg</file>
}}
<translate>
<translate>

In addition to those base classes we have to create a new command class that adds a flow equation to the selected solver object.
== Create a new command class == <!--T:10-->
Next, the command/equation has to be added to the {{incode|femcommands/commands.py}} module. Just copy an existing command and adjust the icon, menu text and tool-tip in __init__(self). '''Don't forget''' to register the command at the bottom of the module file by using the addCommand(...) method. 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.

<!--T:11-->
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 text and tool-tip in {{incode|__init__(self)}}:

</translate>
</translate>
{{code|code=
<pre>
class _EquationFlow(CommandManager):
class _testButton(CommandManager):
"The FEM_EquationFlow command definition"
"The FEM_testButton command definition"


def __init__(self):
def __init__(self):
super(_EquationFlow, self).__init__()
super(_testButton, self).__init__()
self.menuetext = "Flow equation"
self.menuetext = "test Button"
self.tooltip = "Creates a FEM equation for flow"
self.tooltip = "This is a test button"
self.is_active = "with_solver_elmer"
self.is_active = "always"
self.do_activated = "add_obj_on_gui_selobj_noset_edit"
#self.do_activated = "add_obj_on_gui_selobj_noset_edit"
}}
...
<translate>

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

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

<!--T:14-->
'''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-->


<!--T:16-->
We will add the new command to both the '''solve''' toolbar and the '''solve''' menu.


<!--T:17-->
<translate>
Search for the following code snippet in {{incode|/Gui/Workbench.cpp}} and add the new command:
The FEM workbench has several toolbars and menus. We will add the new command on to both the '''solve''' toolbar and the '''solve''' menu.


To add it to the '''solve''' toolbar, search for the following code snippet in {{incode|/Gui/Workbench.cpp}} and add the new command to the rest of the equation commands.
</translate>
</translate>
{{code|code=
<pre>
Gui::ToolBarItem* solve = new Gui::ToolBarItem(root);
Gui::ToolBarItem* solve = new Gui::ToolBarItem(root);
solve->setCommand("Solve");
solve->setCommand("Solve");
*solve << "FEM_SolverCalculixCxxtools"
*solve << "FEM_SolverCalculixCxxtools"
<< "FEM_SolverCalculiX"
<< "FEM_SolverCalculiX"
<< "FEM_SolverElmer"
<< "FEM_SolverElmer"
<< "Separator"
+ << "FEM_testButton"
<< "FEM_EquationElasticity"
<< "Separator"
}}
<< "FEM_EquationElectrostatic"
+ << "FEM_EquationFlow"
<< "FEM_EquationFluxsolver"
<< "FEM_EquationElectricforce"
<< "FEM_EquationHeat"
<< "Separator"
<< "FEM_SolverControl"
<< "FEM_SolverRun";
</pre>

<translate>
<translate>

To add the '''flow''' equation 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=
<pre>
Gui::MenuItem* solve = new Gui::MenuItem;
Gui::MenuItem* solve = new Gui::MenuItem;
root->insertItem(item, solve);
root->insertItem(item, solve);
Line 90: Line 112:
<< "FEM_SolverElmer"
<< "FEM_SolverElmer"
<< "FEM_SolverZ88"
<< "FEM_SolverZ88"
+ << "FEM_testButton"
<< "Separator"
<< "Separator"
}}
<< "FEM_EquationElasticity"
<translate>
<< "FEM_EquationElectrostatic"

+ << "FEM_EquationFlow"
<!--T:19-->
<< "FEM_EquationFluxsolver"
'''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.
<< "FEM_EquationElectricforce"

<< "FEM_EquationHeat"
== Related == <!--T:20-->
<< "Separator"

<< "FEM_SolverControl"
<!--T:21-->
<< "FEM_SolverRun";
* [[Extend_FEM_Module|Extend FEM Module]]
</pre>
* [[Onboarding_FEM_Devs|Onboarding FEM Devs]]


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

Latest revision as of 18:02, 12 January 2024

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 menu item 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