]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAPI/model/dump/DumpAssistant.py
Salome HOME
Implement ExportToGEOM via exportToXAO. Fix the issue #2198.
[modules/shaper.git] / src / PythonAPI / model / dump / DumpAssistant.py
1 ## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """Package for dumping purposes.
22 """
23
24 import ModelHighAPI
25
26 import ModelAPI
27 import SketchAPI
28
29 import sys
30 import inspect
31
32 def singleton(cls):
33     instance = cls()
34     instance.__call__ = lambda: instance
35     return instance
36
37
38 ## @ingroup CPPHighAPI
39 #  Collect information about features that may be dumped and stores the model as a Python script
40 @singleton
41 class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
42
43     ## Constructor
44     def __init__(self):
45         ModelHighAPI.ModelHighAPI_Dumper.__init__(self)
46         ModelHighAPI.ModelHighAPI_Dumper.setInstance(self)
47         self.collectFeatures()
48
49     ## Collect feature wrappers, which allow dumping (have method dump)
50     def collectFeatures(self):
51         self.myFeatures = {}
52         self.myWrapperNames = {}
53         for aModule in sys.modules:
54             for aName, anObj in inspect.getmembers(sys.modules[aModule], inspect.isclass):
55                 if issubclass(anObj, ModelHighAPI.ModelHighAPI_Interface) and hasattr(anObj, "ID") and anObj.dump != ModelHighAPI.ModelHighAPI_Interface.dump:
56                     self.myFeatures[anObj.ID()] = anObj
57                     self.myWrapperNames[anObj.ID()] = aName
58
59     ## Create wrapper for a given feature and dump it
60     def dumpFeature(self, theFeature, theForce):
61         aFeatureKind = theFeature.getKind()
62         if aFeatureKind in self.myFeatures:
63             # Dump only feature created by user (in history).
64             # Also dump Export features (hard-coded here in order not to change the data model).
65             # For all other features, just keep their name.
66             if theForce or theFeature.isInHistory() or aFeatureKind=="Export":
67                 self.myFeatures[aFeatureKind](theFeature).dump(self)
68             else:
69                 self.name(theFeature)
70                 self.clearNotDumped()
71         else:
72             # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint.
73             # In case of theFeature is not a constraint, it will not be dumped.
74             self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature).dump(self)
75
76     ## Dump all parameters
77     def dumpParameter(self, theFeature):
78         aFeatureKind = theFeature.getKind()
79         if aFeatureKind == "Parameter" and aFeatureKind in self.myFeatures:
80             self.myFeatures[aFeatureKind](theFeature).dump(self)
81
82     ## Return getter for specified attribute
83     def attributeGetter(self, theFeature, theAttrName):
84         aFeatureKind = theFeature.getKind()
85         if aFeatureKind in self.myFeatures:
86             return self.myFeatures[aFeatureKind](theFeature).attributeGetter(theAttrName)
87         return std_string()
88
89     ## Return name of wrapper feature
90     def featureWrapper(self, theFeature):
91         aFeatureKind = theFeature.getKind()
92         if aFeatureKind in self.myWrapperNames:
93             return self.myWrapperNames[aFeatureKind]
94         return std_string()
95
96 # Instance of dumper
97 dumper = DumpAssistant