1 # Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 """Package for dumping purposes.
33 instance.__call__ = lambda: instance
37 ## @ingroup CPPHighAPI
38 # Collect information about features that may be dumped and stores the model as a Python script
40 class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
44 ModelHighAPI.ModelHighAPI_Dumper.__init__(self)
45 ModelHighAPI.ModelHighAPI_Dumper.setInstance(self)
46 self.collectFeatures()
49 ## Collect feature wrappers, which allow dumping (have method dump)
50 def collectFeatures(self):
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
59 ## Create wrapper for a given feature and dump it
60 def dumpFeature(self, theFeature, theForce):
62 aFeatureKind = theFeature.getKind()
63 if aFeatureKind in self.myFeatures:
64 # Dump only feature created by user (in history).
65 # Also dump Export and RemoveResults features (hard-coded here in order not to change the data model).
66 # For all other features, just keep their name.
67 if theForce or theFeature.isInHistory() or aFeatureKind=="Export" or aFeatureKind=="RemoveResults":
68 aDumper = self.myFeatures[aFeatureKind](theFeature)
69 # Dump comment for the operation before the dumping of the feature to improve the readability of a script.
70 if self.dumpCommentBeforeFeature(theFeature):
71 self.__print__("\n### Create " + theFeature.getKind())
77 # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint.
78 # In case of theFeature is not a constraint, it will not be dumped.
79 self.name(theFeature, False, True, True)
80 aDumper = self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature)
81 if aDumper is not None:
84 ## Create wrapper for a folder and dump it
85 def dumpFolder(self, theFolder):
86 if theFolder.ID() in self.myFeatures:
87 self.myFeatures[theFolder.ID()](theFolder).dump(self)
89 ## Dump all parameters
90 def dumpParameter(self, theFeature):
91 aFeatureKind = theFeature.getKind()
92 if aFeatureKind == "Parameter" and aFeatureKind in self.myFeatures:
93 self.myFeatures[aFeatureKind](theFeature).dump(self)
95 ## Return getter for specified attribute
96 def attributeGetter(self, theFeature, theAttrName):
97 aFeatureKind = theFeature.getKind()
98 if aFeatureKind in self.myFeatures:
99 return self.myFeatures[aFeatureKind](theFeature).attributeGetter(theAttrName)
102 ## Return name of wrapper feature
103 def featureWrapper(self, theFeature):
104 aFeatureKind = theFeature.getKind()
105 if aFeatureKind in self.myWrapperNames:
106 return self.myWrapperNames[aFeatureKind]
109 ## Exports the dumped variables names to the SHAPERSTUDY
110 def exportVariable(self, theEntry, theVarName):
112 if self.myEngine == None:
113 import SHAPERSTUDY_utils
114 aComponent = SHAPERSTUDY_utils.findOrCreateComponent()
115 self.myEngine = SHAPERSTUDY_utils.getEngine()
116 if self.myEngine != 1:
117 self.myEngine.StoreVariableName(theEntry, theVarName)
119 self.myEngine = 1 # invalid access
123 dumper = DumpAssistant