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 /// Set a feature that should not be dumped anyway
108 void doNotDumpFeature(const FeaturePtr& theFeature)
109 { myFeaturesToSkip.insert(theFeature); }
111 /// Dump sub-feature name and color, without dumping feature creation.
112 /// Used for features which creates sub-features in their execute method.
113 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
114 /// \param theSubFeature [in] sub-feature
116 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
117 const FeaturePtr& theSubFeature);
119 /// Return name of getter for corresponding attribute
120 virtual std::string attributeGetter(const FeaturePtr& theFeature,
121 const std::string& theAttrName) const = 0;
123 /// Return name of wrapper feature
124 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
126 /// Save all dumps into specified file
128 bool exportTo(const std::string& theFileName);
132 ModelHighAPI_Dumper& operator<<(const char theChar);
135 ModelHighAPI_Dumper& operator<<(const char* theString);
138 ModelHighAPI_Dumper& operator<<(const std::string& theString);
141 ModelHighAPI_Dumper& operator<<(const bool theValue);
144 ModelHighAPI_Dumper& operator<<(const int theValue);
147 ModelHighAPI_Dumper& operator<<(const double theValue);
151 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
152 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
154 /// Dump GeomAPI_Pnt in the following form:
155 /// "GeomAPI_Pnt(X, Y, Z)"
157 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
159 /// "GeomAPI_Dir(X, Y, Z)"
161 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
163 /// Dump GeomDataAPI_Dir in the following form:
166 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
167 /// Dump GeomDataAPI_Point in the following form:
170 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
171 /// Dump GeomDataAPI_Point2D in the following form:
174 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
176 /// Dump AttributeBoolean
178 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
179 /// Dump AttributeInteger
181 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
182 /// Dump AttributeDouble
184 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
185 /// Dump AttributeString
187 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
188 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
190 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
194 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
197 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
198 operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
200 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
201 operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
203 /// Dump AttributeRefAttr
204 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
205 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
206 /// Dump AttributeRefAttrList as follows:
207 /// "[obj1, obj2, obj3, ...]"
208 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
209 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
210 /// Dump AttributeRefList as follows:
211 /// "[obj1, obj2, obj3, ...]"
212 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
213 operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
214 /// Dump AttributeSelection
215 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
216 operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
217 /// Dump AttributeSelectionList
218 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
219 operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
220 /// Dump AttributeReference
221 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
222 operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
223 /// Dump AttributeStringArray
224 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
225 operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
227 /// Clear dump buffer
229 void clear(bool bufferOnly = false);
230 /// clear list of not dumped entities
231 MODELHIGHAPI_EXPORT void clearNotDumped();
234 /// Dump "setName" command if last entity had user-defined name
235 MODELHIGHAPI_EXPORT void dumpEntitySetName();
238 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
239 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
241 /// Iterate all features in document and dump them into intermediate buffer
242 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
244 /// Dump composite feature and all it sub-features
245 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
246 bool isForce = false);
248 /// Iterate all features in composite feature and dump them into intermediate buffer
249 /// \param theComposite [in] parent composite feature
250 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
252 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
253 bool theDumpModelDo = false);
255 /// Check the entity is already dumped
256 bool isDumped(const EntityPtr& theEntity) const;
258 /// Stores names of results for the given feature
259 void saveResultNames(const FeaturePtr& theFeature);
261 /// Check the result feature has default color
262 bool isDefaultColor(const ResultPtr& theResult) const;
264 /// Check the result feature has default deflection
265 bool isDefaultDeflection(const ResultPtr& theResult) const;
269 std::string myCurrentName; ///< default name of current feature
270 std::string myUserName; ///< user-defined name
271 bool myIsDefault; ///< \c true if the name is default
272 bool myIsDumped; ///< shows that the names of the feature are already stored
276 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
277 : myCurrentName(theCurName),
278 myUserName(theUserName),
279 myIsDefault(theDefault),
284 typedef std::map<EntityPtr, EntityName> EntityNameMap;
285 typedef std::map<std::string, std::set<std::string> > ModulesMap;
286 typedef std::map<DocumentPtr, std::map<std::string, int> > NbFeaturesMap;
288 struct LastDumpedEntity {
289 EntityPtr myEntity; ///< last dumped entity
290 bool myUserName; ///< the entity hase user-defined name
291 /// results of this entity, which has user-defined names or specific colors
292 std::list<ResultPtr> myResults;
294 LastDumpedEntity(EntityPtr theEntity, bool theUserName,
295 const std::list<ResultPtr>& theResults)
296 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
299 typedef std::stack<LastDumpedEntity> DumpStack;
301 static ModelHighAPI_Dumper* mySelf;
303 std::ostringstream myDumpBuffer; ///< intermediate buffer to store dumping data
304 std::ostringstream myFullDump; ///< full buffer of dumped data
306 ModulesMap myModules; ///< modules and entities to be imported
307 EntityNameMap myNames; ///< names of the entities
308 DumpStack myEntitiesStack; ///< stack of dumped entities
310 NbFeaturesMap myFeatureCount; ///< number of features of each kind
312 /// features which should not be dumped (like coincidence and tangency created by tangent arc)
313 std::set<FeaturePtr> myFeaturesToSkip;
316 /// list of entities, used by other features but not dumped yet
317 std::set<EntityPtr> myNotDumpedEntities;
319 friend class SketchAPI_Sketch;