1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
3 // File: ModelHighAPI_Dumper.h
4 // Created: 1 August 2016
5 // Author: Artem ZHIDKOV
7 #ifndef ModelHighAPI_Dumper_H_
8 #define ModelHighAPI_Dumper_H_
10 #include "ModelHighAPI.h"
23 class GeomDataAPI_Dir;
24 class GeomDataAPI_Point;
25 class GeomDataAPI_Point2D;
27 class ModelAPI_Attribute;
28 class ModelAPI_AttributeBoolean;
29 class ModelAPI_AttributeDouble;
30 class ModelAPI_AttributeInteger;
31 class ModelAPI_AttributeRefAttr;
32 class ModelAPI_AttributeRefAttrList;
33 class ModelAPI_AttributeReference;
34 class ModelAPI_AttributeRefList;
35 class ModelAPI_AttributeSelection;
36 class ModelAPI_AttributeSelectionList;
37 class ModelAPI_AttributeString;
38 class ModelAPI_AttributeStringArray;
39 class ModelAPI_CompositeFeature;
40 class ModelAPI_Document;
41 class ModelAPI_Entity;
42 class ModelAPI_Feature;
43 class ModelAPI_Object;
44 class ModelAPI_Result;
46 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
47 typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
48 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
49 typedef std::shared_ptr<ModelAPI_Result> ResultPtr;
51 /**\class ModelHighAPI_Dumper
53 * \brief Dump engine for the model
55 class ModelHighAPI_Dumper
58 /// Default constructor
60 ModelHighAPI_Dumper();
62 /// Sets instance of a dumper
64 static void setInstance(ModelHighAPI_Dumper* theDumper);
66 /// Returns instance of a dumper
68 static ModelHighAPI_Dumper* getInstance();
71 virtual ~ModelHighAPI_Dumper() {}
73 /// Dump given document into the file
74 /// \return \c true, if succeed
76 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
78 /// Add module to list of imported modules
79 /// \param theModuleName name of the module to be imported
80 /// \param theObject name of the entity to be imported
81 /// from the module (if empty, while module will be imported)
83 void importModule(const std::string& theModuleName,
84 const std::string& theObject = std::string());
86 /// Returns name of specified entity
87 /// \param theEntity [in] named entity
88 /// \param theSaveNotDumped [in]
89 /// if \c true, the entity should be stored as not dumped (will be dumped automatically)
90 /// \param theUseEntityName [in]
91 /// if \c true, the entity name should be used "as is" without changing default name
92 /// \return name of the entity
94 const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
95 bool theUseEntityName = false);
97 /// Returns name of parent composite feature for specified entity
99 const std::string& parentName(const FeaturePtr& theFeature);
101 /// Dump parameter feature only
102 virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
103 /// Dump given feature
104 virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
106 /// Dump sub-feature name and color, without dumping feature creation.
107 /// Used for features which creates sub-features in their execute method.
108 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
109 /// \param theSubFeature [in] sub-feature
111 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
112 const FeaturePtr& theSubFeature);
114 /// Return name of getter for corresponding attribute
115 virtual std::string attributeGetter(const FeaturePtr& theFeature,
116 const std::string& theAttrName) const = 0;
118 /// Return name of wrapper feature
119 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
121 /// Save all dumps into specified file
123 bool exportTo(const std::string& theFileName);
127 ModelHighAPI_Dumper& operator<<(const char theChar);
130 ModelHighAPI_Dumper& operator<<(const char* theString);
133 ModelHighAPI_Dumper& operator<<(const std::string& theString);
136 ModelHighAPI_Dumper& operator<<(const bool theValue);
139 ModelHighAPI_Dumper& operator<<(const int theValue);
142 ModelHighAPI_Dumper& operator<<(const double theValue);
146 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
147 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
149 /// Dump GeomAPI_Pnt in the following form:
150 /// "GeomAPI_Pnt(X, Y, Z)"
152 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
154 /// "GeomAPI_Dir(X, Y, Z)"
156 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
158 /// Dump GeomDataAPI_Dir in the following form:
161 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
162 /// Dump GeomDataAPI_Point in the following form:
165 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
166 /// Dump GeomDataAPI_Point2D in the following form:
169 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
171 /// Dump AttributeBoolean
173 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
174 /// Dump AttributeInteger
176 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
177 /// Dump AttributeDouble
179 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
180 /// Dump AttributeString
182 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
183 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
185 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
189 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
192 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
193 operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
195 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
196 operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
198 /// Dump AttributeRefAttr
199 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
200 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
201 /// Dump AttributeRefAttrList as follows:
202 /// "[obj1, obj2, obj3, ...]"
203 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
204 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
205 /// Dump AttributeRefList as follows:
206 /// "[obj1, obj2, obj3, ...]"
207 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
208 operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
209 /// Dump AttributeSelection
210 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
211 operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
212 /// Dump AttributeSelectionList
213 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
214 operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
215 /// Dump AttributeReference
216 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
217 operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
218 /// Dump AttributeStringArray
219 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
220 operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
222 /// Clear dump buffer
224 void clear(bool bufferOnly = false);
225 /// clear list of not dumped entities
226 MODELHIGHAPI_EXPORT void clearNotDumped();
229 /// Dump "setName" command if last entity had user-defined name
230 MODELHIGHAPI_EXPORT void dumpEntitySetName();
233 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
234 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
236 /// Iterate all features in document and dump them into intermediate buffer
237 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
239 /// Dump composite feature and all it sub-features
240 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
241 bool isForce = false);
243 /// Iterate all features in composite feature and dump them into intermediate buffer
244 /// \param theComposite [in] parent composite feature
245 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
247 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
248 bool theDumpModelDo = false);
250 /// Check the entity is already dumped
251 bool isDumped(const EntityPtr& theEntity) const;
253 /// Stores names of results for the given feature
254 void saveResultNames(const FeaturePtr& theFeature);
256 /// Check the result feature has default color
257 bool isDefaultColor(const ResultPtr& theResult) const;
259 /// Check the result feature has default deflection
260 bool isDefaultDeflection(const ResultPtr& theResult) const;
264 std::string myCurrentName; ///< default name of current feature
265 std::string myUserName; ///< user-defined name
266 bool myIsDefault; ///< \c true if the name is default
267 bool myIsDumped; ///< shows that the names of the feature are already stored
271 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
272 : myCurrentName(theCurName),
273 myUserName(theUserName),
274 myIsDefault(theDefault),
279 typedef std::map<EntityPtr, EntityName> EntityNameMap;
280 typedef std::map<std::string, std::set<std::string> > ModulesMap;
281 typedef std::map<DocumentPtr, std::map<std::string, int> > NbFeaturesMap;
283 struct LastDumpedEntity {
284 EntityPtr myEntity; ///< last dumped entity
285 bool myUserName; ///< the entity hase user-defined name
286 /// results of this entity, which has user-defined names or specific colors
287 std::list<ResultPtr> myResults;
289 LastDumpedEntity(EntityPtr theEntity, bool theUserName,
290 const std::list<ResultPtr>& theResults)
291 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
294 typedef std::stack<LastDumpedEntity> DumpStack;
296 static ModelHighAPI_Dumper* mySelf;
298 std::ostringstream myDumpBuffer; ///< intermediate buffer to store dumping data
299 std::ostringstream myFullDump; ///< full buffer of dumped data
301 ModulesMap myModules; ///< modules and entities to be imported
302 EntityNameMap myNames; ///< names of the entities
303 DumpStack myEntitiesStack; ///< stack of dumped entities
305 NbFeaturesMap myFeatureCount; ///< number of features of each kind
308 /// list of entities, used by other features but not dumped yet
309 std::set<EntityPtr> myNotDumpedEntities;
311 friend class SketchAPI_Sketch;