Add Button to FEM Toolbar Tutorial: Difference between revisions

From FreeCAD Documentation
No edit summary
No edit summary
Line 1: Line 1:
<languages/>
<languages/>

<translate>
<translate>
<!--T:1-->
<!--T:1-->
Line 10: Line 11:
|Files=
|Files=
}}
}}
</translate>


'''Notes: Work In Progress'''
<!--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.


FEM workbench has toolbars and menus. This tutorial shows how to add a button onto the toolbars. It also show how to add a menuitem into the menu.
<!--T:3-->

The task can be split into five parts:
The task can be split into four 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).
* '''Create a new icon file'''.
</translate>
* '''Register the new icon file'''. In /Gui/Resources/Fem.qrc
== New Equation Type== <!--T:4-->
* '''Create a new command'''. In src/Mod/Fem/femcommands/commands.py
* '''Add the new command onto the workbench'''. In src/Mod/Fem/Gui/Workbench.cpp


== Create a new icon file ==
For a button, We need a icon file, here we use FEM_EquationFlow.svg. It has to be put into {{incode|/Gui/Resources/icons/}}.


== Register the new icon file ==
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


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/}}).
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-->
<translate>
<translate>
== Create a new command ==
In addition to those base classes we have to create a new command class that adds a flow equation to the selected solver object.
A new command class that adds a flow equation to the selected solver object.
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.
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.
</translate>
</translate>
Line 54: Line 55:


<translate>
<translate>
== 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.
The FEM workbench has several toolbars and menus. We will add the new command on to both the '''solve''' toolbar and the '''solve''' menu.



Revision as of 23:58, 3 August 2021

Other languages:
Tutorial
Topic
Add FEM Equation
Level
Time to complete
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 button onto the toolbars. It also show 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. In /Gui/Resources/Fem.qrc
  • Create a new command. In src/Mod/Fem/femcommands/commands.py
  • Add the new command onto the workbench. In src/Mod/Fem/Gui/Workbench.cpp

Create a new icon file

For a button, We need a icon file, here we use FEM_EquationFlow.svg. It has to be put into /Gui/Resources/icons/.

Register the new icon file

The new FEM_EquationFlow.svg icon has to be registered for the GUI-button with <file>icons/FEM_EquationFlow.svg</file> in Fem.qrc (in /Gui/Resources/).

Create a new command

A new command class that adds a flow equation to the selected solver object. Next, the command/equation has to be added to the 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.

class _EquationFlow(CommandManager):
    "The FEM_EquationFlow command definition"

    def __init__(self):
        super(_EquationFlow, self).__init__()
        self.menuetext = "Flow equation"
        self.tooltip = "Creates a FEM equation for flow"
        self.is_active = "with_solver_elmer"
        self.do_activated = "add_obj_on_gui_selobj_noset_edit"
...
FreeCADGui.addCommand(
    "FEM_EquationFlow",
    _EquationFlow()
)


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.

To add it to the solve toolbar, search for the following code snippet in /Gui/Workbench.cpp and add the new command to the rest of the equation commands.

 
     Gui::ToolBarItem* solve = new Gui::ToolBarItem(root);
     solve->setCommand("Solve");
     *solve << "FEM_SolverCalculixCxxtools"
           << "FEM_SolverCalculiX"
           << "FEM_SolverElmer"
           << "Separator"
           << "FEM_EquationElasticity"
           << "FEM_EquationElectrostatic"
+          << "FEM_EquationFlow"
           << "FEM_EquationFluxsolver"
           << "FEM_EquationElectricforce"
           << "FEM_EquationHeat"
           << "Separator"
           << "FEM_SolverControl"
           << "FEM_SolverRun";

To add the flow equation 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"
           << "Separator"
           << "FEM_EquationElasticity"
           << "FEM_EquationElectrostatic"
+          << "FEM_EquationFlow"
           << "FEM_EquationFluxsolver"
           << "FEM_EquationElectricforce"
           << "FEM_EquationHeat"
           << "Separator"
           << "FEM_SolverControl"
           << "FEM_SolverRun";