Salome HOME
updated copyright message
[modules/shaper.git] / src / PythonAPI / model / dump / DumpAssistant.py
1 # Copyright (C) 2014-2023  CEA, EDF
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """Package for dumping purposes.
21 """
22
23 import ModelHighAPI
24
25 import ModelAPI
26 import SketchAPI
27
28 import sys
29 import inspect
30
31 def singleton(cls):
32     instance = cls()
33     instance.__call__ = lambda: instance
34     return instance
35
36
37 ## @ingroup CPPHighAPI
38 #  Collect information about features that may be dumped and stores the model as a Python script
39 @singleton
40 class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
41
42     ## Constructor
43     def __init__(self):
44         ModelHighAPI.ModelHighAPI_Dumper.__init__(self)
45         ModelHighAPI.ModelHighAPI_Dumper.setInstance(self)
46         self.collectFeatures()
47         self.myEngine = None
48
49     ## Collect feature wrappers, which allow dumping (have method dump)
50     def collectFeatures(self):
51         self.myFeatures = {}
52         self.myWrapperNames = {}
53         # Fixing [bos #26531] [CEA] Shaper tests fail with SEGFAULT
54         # Use copy of sys.modules to avoid exception "dictionary changed size during iteration"
55         sys_modules = sys.modules.copy()
56         for aModule in sys_modules:
57             for aName, anObj in inspect.getmembers(sys_modules[aModule], inspect.isclass):
58                 if issubclass(anObj, ModelHighAPI.ModelHighAPI_Interface) and hasattr(anObj, "ID") and anObj.dump != ModelHighAPI.ModelHighAPI_Interface.dump:
59                     self.myFeatures[anObj.ID()] = anObj
60                     self.myWrapperNames[anObj.ID()] = aName
61
62     ## Create wrapper for a given feature and dump it
63     def dumpFeature(self, theFeature, theForce):
64         aDumper = None
65         aFeatureKind = theFeature.getKind()
66         if aFeatureKind in self.myFeatures:
67             # Dump only feature created by user (in history).
68             # Also dump Export and RemoveResults features (hard-coded here in order not to change the data model).
69             # For all other features, just keep their name.
70             if theForce or theFeature.isInHistory() or aFeatureKind=="Export" or aFeatureKind=="RemoveResults":
71                 aDumper = self.myFeatures[aFeatureKind](theFeature)
72                 # Dump comment for the operation before the dumping of the feature to improve the readability of a script.
73                 if self.dumpCommentBeforeFeature(theFeature):
74                     self.__print__("\n### Create " + theFeature.getKind())
75                     self.newline()
76             else:
77                 self.name(theFeature)
78                 self.clearNotDumped()
79         else:
80             # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint.
81             # In case of theFeature is not a constraint, it will not be dumped.
82             self.name(theFeature, False, True, True)
83             aDumper = self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature)
84         if aDumper is not None:
85             aDumper.dump(self)
86
87     ## Create wrapper for a folder and dump it
88     def dumpFolder(self, theFolder):
89         if theFolder.ID() in self.myFeatures:
90             self.myFeatures[theFolder.ID()](theFolder).dump(self)
91
92     ## Dump all parameters
93     def dumpParameter(self, theFeature):
94         aFeatureKind = theFeature.getKind()
95         if aFeatureKind == "Parameter" and aFeatureKind in self.myFeatures:
96             self.myFeatures[aFeatureKind](theFeature).dump(self)
97
98     ## Return getter for specified attribute
99     def attributeGetter(self, theFeature, theAttrName):
100         aFeatureKind = theFeature.getKind()
101         if aFeatureKind in self.myFeatures:
102             return self.myFeatures[aFeatureKind](theFeature).attributeGetter(theAttrName)
103         return std_string()
104
105     ## Return name of wrapper feature
106     def featureWrapper(self, theFeature):
107         aFeatureKind = theFeature.getKind()
108         if aFeatureKind in self.myWrapperNames:
109             return self.myWrapperNames[aFeatureKind]
110         return std_string()
111
112     ## Exports the dumped variables names to the SHAPERSTUDY
113     def exportVariable(self, theEntry, theVarName):
114         try:
115           if self.myEngine == None:
116             import SHAPERSTUDY_utils
117             aComponent = SHAPERSTUDY_utils.findOrCreateComponent()
118             self.myEngine = SHAPERSTUDY_utils.getEngine()
119           if self.myEngine != 1:
120             self.myEngine.StoreVariableName(theEntry, theVarName)
121         except:
122           self.myEngine = 1 # invalid access
123         pass
124
125 # Instance of dumper
126 dumper = DumpAssistant