File size: 7,853 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# ***************************************************************************
# *   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 post line plot"
__author__ = "Stefan Tröger"
__url__ = "https://www.freecad.org"

## @package post_histogram
#  \ingroup FEM
#  \brief Post processing plot displaying lines

import FreeCAD

from . import base_fempostextractors
from . import base_fempythonobject

_PropHelper = base_fempythonobject._PropHelper

from vtkmodules.vtkCommonCore import vtkDoubleArray
from vtkmodules.vtkCommonDataModel import vtkTable
from vtkmodules.vtkCommonExecutionModel import vtkStreamingDemandDrivenPipeline

from PySide.QtCore import QT_TRANSLATE_NOOP


class PostFieldData1D(base_fempostextractors.Extractor1D):
    """
    A post processing extraction of one dimensional field data
    """

    ExtractionType = "Field"

    def __init__(self, obj):
        super().__init__(obj)

    def _get_properties(self):
        prop = [
            _PropHelper(
                type="App::PropertyBool",
                name="ExtractFrames",
                group="Multiframe",
                doc=QT_TRANSLATE_NOOP(
                    "FEM", "Specify if the field shall be extracted for every available frame"
                ),
                value=False,
            ),
        ]
        return super()._get_properties() + prop

    def execute(self, obj):

        # on execution we populate the vtk table
        table = vtkTable()

        if not obj.Source:
            obj.Table = table
            return

        dataset = obj.Source.getDataSet()
        if not dataset:
            obj.Table = table
            return

        timesteps = []
        if obj.ExtractFrames:
            # check if we have timesteps
            info = obj.Source.getOutputAlgorithm().GetOutputInformation(0)
            if info.Has(vtkStreamingDemandDrivenPipeline.TIME_STEPS()):
                timesteps = info.Get(vtkStreamingDemandDrivenPipeline.TIME_STEPS())
            else:
                FreeCAD.Console.PrintWarning(
                    'No frames available in data, ignoring "ExtractFrames" property'
                )

        if not timesteps:
            # get the dataset and extract the correct array
            array = self._x_array_from_dataset(obj, dataset)
            if array.GetNumberOfComponents() > 1:
                array.SetName(obj.XField + " (" + obj.XComponent + ")")
            else:
                array.SetName(obj.XField)

            self._x_array_component_to_table(obj, array, table)

        else:
            algo = obj.Source.getOutputAlgorithm()
            for timestep in timesteps:
                algo.UpdateTimeStep(timestep)
                dataset = algo.GetOutputDataObject(0)
                array = self._x_array_from_dataset(obj, dataset)

                if array.GetNumberOfComponents() > 1:
                    array.SetName(f"{obj.XField} ({obj.XComponent}) - {timestep}")
                else:
                    array.SetName(f"{obj.XField} - {timestep}")
                self._x_array_component_to_table(obj, array, table)

        # set the final table
        obj.Table = table


class PostIndexOverFrames1D(base_fempostextractors.Extractor1D):
    """
    A post processing extraction of one dimensional index data
    """

    ExtractionType = "Index"

    def __init__(self, obj):
        super().__init__(obj)

    def _get_properties(self):
        prop = [
            _PropHelper(
                type="App::PropertyInteger",
                name="Index",
                group="X Data",
                doc=QT_TRANSLATE_NOOP(
                    "FEM", "Specify for which index the data should be extracted"
                ),
                value=0,
            ),
        ]
        return super()._get_properties() + prop

    def execute(self, obj):

        # on execution we populate the vtk table
        table = vtkTable()

        if not obj.Source:
            obj.Table = table
            return

        dataset = obj.Source.getDataSet()
        if not dataset:
            obj.Table = table
            return

        # check if we have timesteps
        timesteps = []
        info = obj.Source.getOutputAlgorithm().GetOutputInformation(0)
        if info.Has(vtkStreamingDemandDrivenPipeline.TIME_STEPS()):
            timesteps = info.Get(vtkStreamingDemandDrivenPipeline.TIME_STEPS())

        algo = obj.Source.getOutputAlgorithm()
        frame_array = vtkDoubleArray()
        idx = obj.Index

        if timesteps:
            setup = False
            for i, timestep in enumerate(timesteps):

                algo.UpdateTimeStep(timestep)
                dataset = algo.GetOutputDataObject(0)
                array = self._x_array_from_dataset(obj, dataset, copy=False)

                # safeguard for invalid access
                if idx < 0 or array.GetNumberOfTuples() - 1 < idx:
                    raise Exception(
                        f"Invalid index: {idx} is not in range 0 - {array.GetNumberOfTuples()-1}"
                    )

                if not setup:
                    frame_array.SetNumberOfComponents(array.GetNumberOfComponents())
                    frame_array.SetNumberOfTuples(len(timesteps))
                    setup = True

                frame_array.SetTuple(i, idx, array)
        else:
            algo.Update()
            dataset = algo.GetOutputDataObject(0)
            array = self._x_array_from_dataset(obj, dataset, copy=False)

            # safeguard for invalid access
            if idx < 0 or array.GetNumberOfTuples() - 1 < idx:
                raise Exception(
                    f"Invalid index: {idx} is not in range 0 - {array.GetNumberOfTuples()-1}"
                )

            frame_array.SetNumberOfComponents(array.GetNumberOfComponents())
            frame_array.SetNumberOfTuples(1)
            frame_array.SetTuple(0, idx, array)

        if frame_array.GetNumberOfComponents() > 1:
            frame_array.SetName(f"{obj.XField} ({obj.XComponent}) @Idx {obj.Index}")
        else:
            frame_array.SetName(f"{obj.XField} @Idx {obj.Index}")

        self._x_array_component_to_table(obj, frame_array, table)

        # set the final table
        obj.Table = table