Salome HOME
d32cb19baa56eab81cea2f4bddf32c8dbe1d562d
[modules/shaper.git] / src / PythonAPI / model / dump / DumpAssistant.py
1 """Package for dumping purposes.
2 """
3
4 import ModelHighAPI
5
6 import ModelAPI
7 import SketchAPI
8
9 import sys
10 import inspect
11
12 def singleton(cls):
13     instance = cls()
14     instance.__call__ = lambda: instance
15     return instance
16
17
18 ## @ingroup CPPHighAPI
19 #  Collect information about features that may be dumped and stores the model as a Python script
20 @singleton
21 class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
22
23     ## Constructor
24     def __init__(self):
25         ModelHighAPI.ModelHighAPI_Dumper.__init__(self)
26         ModelHighAPI.ModelHighAPI_Dumper.setInstance(self)
27         self.collectFeatures()
28
29     ## Collect feature wrappers, which allow dumping (have method dump)
30     def collectFeatures(self):
31         self.myFeatures = {}
32         self.myWrapperNames = {}
33         for aModule in sys.modules:
34             for aName, anObj in inspect.getmembers(sys.modules[aModule], inspect.isclass):
35                 if issubclass(anObj, ModelHighAPI.ModelHighAPI_Interface) and hasattr(anObj, "ID") and anObj.dump != ModelHighAPI.ModelHighAPI_Interface.dump:
36                     self.myFeatures[anObj.ID()] = anObj
37                     self.myWrapperNames[anObj.ID()] = aName
38
39     ## Create wrapper for a given feature and dump it
40     def dumpFeature(self, theFeature, theForce):
41         aFeatureKind = theFeature.getKind()
42         if aFeatureKind in self.myFeatures:
43             # Dump only feature created by user (in history).
44             # For all other features, just keep their name.
45             if theForce or theFeature.isInHistory():
46                 self.myFeatures[aFeatureKind](theFeature).dump(self)
47             else:
48                 self.name(theFeature)
49                 self.clearNotDumped()
50         else:
51             # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint.
52             # In case of theFeature is not a constraint, it will not be dumped.
53             self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature).dump(self)
54
55     ## Dump all parameters
56     def dumpParameter(self, theFeature):
57         aFeatureKind = theFeature.getKind()
58         if aFeatureKind == "Parameter" and aFeatureKind in self.myFeatures:
59             self.myFeatures[aFeatureKind](theFeature).dump(self)
60
61     ## Return getter for specified attribute
62     def attributeGetter(self, theFeature, theAttrName):
63         aFeatureKind = theFeature.getKind()
64         if aFeatureKind in self.myFeatures:
65             return self.myFeatures[aFeatureKind](theFeature).attributeGetter(theAttrName)
66         return std_string()
67
68     ## Return name of wrapper feature
69     def featureWrapper(self, theFeature):
70         aFeatureKind = theFeature.getKind()
71         if aFeatureKind in self.myWrapperNames:
72             return self.myWrapperNames[aFeatureKind]
73         return std_string()
74
75 # Instance of dumper
76 dumper = DumpAssistant