File size: 5,900 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# ***************************************************************************
# *   Copyright (c) 2025 Stefan Tröger <stefantroeger@gmx.net>              *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

__title__ = "FreeCAD visualization registry"
__author__ = "Stefan Tröger"
__url__ = "https://www.freecad.org"

## @package post_visualization
#  \ingroup FEM
#  \brief A registry to collect visualizations for use in menus

# Note: This file is imported from FreeCAD App files. Do not import any FreeCADGui
#       directly to support cmd line use.

import copy
from dataclasses import dataclass

from PySide import QtCore

import FreeCAD


# Registry to handle visualization commands
# #########################################

_registry = {}


@dataclass
class _Extraction:

    name: str
    icon: str
    dimension: str
    extracttype: str
    module: str
    factory: str


@dataclass
class _Visualization:

    name: str
    icon: str
    module: str
    factory: str
    extractions: list[_Extraction]


# Register a visualization by type, icon and factory function
def register_visualization(visualization_type, icon, module, factory):
    if visualization_type in _registry:
        raise ValueError("Visualization type already registered")

    _registry[visualization_type] = _Visualization(visualization_type, icon, module, factory, [])


def register_extractor(
    visualization_type, extraction_type, icon, dimension, etype, module, factory
):

    if not visualization_type in _registry:
        raise ValueError("visualization not registered yet")

    extraction = _Extraction(extraction_type, icon, dimension, etype, module, factory)
    _registry[visualization_type].extractions.append(extraction)


def get_registered_visualizations():
    return copy.deepcopy(_registry)


def _to_command_name(name):
    return "FEM_PostVisualization" + name


class _VisualizationGroupCommand:

    def GetCommands(self):
        visus = _registry.keys()
        cmds = [_to_command_name(v) for v in visus]
        return cmds

    def GetDefaultCommand(self):
        return 0

    def GetResources(self):
        return {
            "MenuText": QtCore.QT_TRANSLATE_NOOP("FEM", "Data Visualizations"),
            "ToolTip": QtCore.QT_TRANSLATE_NOOP(
                "FEM", "Different visualizations to show post processing data in"
            ),
        }

    def IsActive(self):
        if not FreeCAD.ActiveDocument:
            return False

        import FemGui

        return bool(FemGui.getActiveAnalysis())


class _VisualizationCommand:

    def __init__(self, visualization_type):
        self._visualization_type = visualization_type

    def GetResources(self):

        cmd = _to_command_name(self._visualization_type)
        vis = _registry[self._visualization_type]
        tooltip = f"Create a {self._visualization_type} post processing data visualization"

        return {
            "Pixmap": vis.icon,
            "MenuText": QtCore.QT_TRANSLATE_NOOP(cmd, "Create {}".format(self._visualization_type)),
            "Accel": "",
            "ToolTip": QtCore.QT_TRANSLATE_NOOP(cmd, tooltip),
            "CmdType": "AlterDoc",
        }

    def IsActive(self):
        # active analysis available
        if not FreeCAD.ActiveDocument:
            return False

        import FemGui

        return bool(FemGui.getActiveAnalysis())

    def Activated(self):
        import FreeCADGui

        vis = _registry[self._visualization_type]
        FreeCAD.ActiveDocument.openTransaction(f"Create {vis.name}")

        FreeCADGui.addModule(vis.module)
        FreeCADGui.addModule("FemGui")

        FreeCADGui.doCommand(f"obj = {vis.module}.{vis.factory}(FreeCAD.ActiveDocument)")
        FreeCADGui.doCommand(f"FemGui.getActiveAnalysis().addObject(obj)")

        FreeCADGui.Selection.clearSelection()
        FreeCADGui.doCommand("FreeCADGui.ActiveDocument.setEdit(obj)")


def setup_commands(toplevel_name):
    # creates all visualization commands and registers them. The
    # toplevel group command will have the name provided to this function.

    import FreeCADGui

    # first all visualization and extraction commands
    for vis in _registry:
        FreeCADGui.addCommand(_to_command_name(vis), _VisualizationCommand(vis))

    # build the group command!
    FreeCADGui.addCommand("FEM_PostVisualization", _VisualizationGroupCommand())