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_CompositeFeature;
39 class ModelAPI_Document;
40 class ModelAPI_Entity;
41 class ModelAPI_Feature;
42 class ModelAPI_Object;
43 class ModelAPI_Result;
45 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
46 typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
47 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
48 typedef std::shared_ptr<ModelAPI_Result> ResultPtr;
50 /**\class ModelHighAPI_Dumper
52 * \brief Dump engine for the model
54 class ModelHighAPI_Dumper
57 /// Default constructor
59 ModelHighAPI_Dumper();
61 /// Sets instance of a dumper
63 static void setInstance(ModelHighAPI_Dumper* theDumper);
65 /// Returns instance of a dumper
67 static ModelHighAPI_Dumper* getInstance();
70 virtual ~ModelHighAPI_Dumper() {}
72 /// Dump given document into the file
73 /// \return \c true, if succeed
75 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
77 /// Add module to list of imported modules
78 /// \param theModuleName name of the module to be imported
79 /// \param theObject name of the entity to be imported from the module (if empty, while module will be imported)
81 void importModule(const std::string& theModuleName,
82 const std::string& theObject = std::string());
84 /// Returns name of specified entity
85 /// \param theEntity [in] named entity
86 /// \param theSaveNotDumped [in] if \c true, the entity should be stored as not dumped (will be dumped automatically)
87 /// \param theUseEntityName [in] if \c true, the entity name should be used "as is" without changing default name
88 /// \return name of the entity
90 const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true, bool theUseEntityName = false);
92 /// Returns name of parent composite feature for specified entity
94 const std::string& parentName(const FeaturePtr& theFeature);
96 /// Dump parameter feature only
97 virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
98 /// Dump given feature
99 virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
101 /// Return name of getter for corresponding attribute
102 virtual std::string attributeGetter(const FeaturePtr& theFeature,
103 const std::string& theAttrName) const = 0;
105 /// Save all dumps into specified file
107 bool exportTo(const std::string& theFileName);
111 ModelHighAPI_Dumper& operator<<(const char theChar);
114 ModelHighAPI_Dumper& operator<<(const char* theString);
117 ModelHighAPI_Dumper& operator<<(const std::string& theString);
120 ModelHighAPI_Dumper& operator<<(const bool theValue);
123 ModelHighAPI_Dumper& operator<<(const int theValue);
126 ModelHighAPI_Dumper& operator<<(const double theValue);
130 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
131 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
133 /// Dump GeomAPI_Pnt in the following form:
134 /// "GeomAPI_Pnt(X, Y, Z)"
136 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
138 /// "GeomAPI_Dir(X, Y, Z)"
140 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
142 /// Dump GeomDataAPI_Dir in the following form:
145 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
146 /// Dump GeomDataAPI_Point in the following form:
149 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
150 /// Dump GeomDataAPI_Point2D in the following form:
153 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
155 /// Dump AttributeBoolean
157 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
158 /// Dump AttributeInteger
160 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
161 /// Dump AttributeDouble
163 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
164 /// Dump AttributeString
166 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
167 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
169 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
173 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
177 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
180 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
182 /// Dump AttributeRefAttr
184 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
185 /// Dump AttributeRefAttrList as follows:
186 /// "[obj1, obj2, obj3, ...]"
188 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
189 /// Dump AttributeRefList as follows:
190 /// "[obj1, obj2, obj3, ...]"
192 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
193 /// Dump AttributeSelection
195 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
196 /// Dump AttributeSelectionList
198 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
199 /// Dump AttributeReference
201 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
203 /// Clear dump buffer
205 void clear(bool bufferOnly = false);
206 /// clear list of not dumped entities
207 MODELHIGHAPI_EXPORT void clearNotDumped();
210 /// Dump "setName" command if last entity had user-defined name
211 MODELHIGHAPI_EXPORT void dumpEntitySetName();
214 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
215 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
217 /// Iterate all features in document and dump them into intermediate buffer
218 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
220 /// Dump composite feature and all it sub-features
221 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce = false);
223 /// Iterate all features in composite feature and dump them into intermediate buffer
224 /// \param theComposite [in] parent composite feature
225 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
227 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool theDumpModelDo = false);
229 /// Check the entity is already dumped
230 bool isDumped(const EntityPtr& theEntity) const;
232 /// Stores names of results for the given feature
233 void saveResultNames(const FeaturePtr& theFeature);
236 typedef std::map<EntityPtr, std::pair<std::string, std::string> > EntityNameMap;
237 typedef std::map<std::string, std::set<std::string> > ModulesMap;
238 typedef std::map<DocumentPtr, std::map<std::string, int> > NbFeaturesMap;
240 struct LastDumpedEntity {
241 EntityPtr myEntity; // last dumped entity
242 bool myUserName; // the entity hase user-defined name
243 std::list<ResultPtr> myResultsWithName; // results of this entity, which has user-defined names
245 LastDumpedEntity(EntityPtr theEntity, bool theUserName, const std::list<ResultPtr>& theResults)
246 : myEntity(theEntity), myUserName(theUserName), myResultsWithName(theResults)
249 typedef std::stack<LastDumpedEntity> DumpStack;
251 static ModelHighAPI_Dumper* mySelf;
253 std::ostringstream myDumpBuffer; ///< intermediate buffer to store dumping data
254 std::ostringstream myFullDump; ///< full buffer of dumped data
256 ModulesMap myModules; ///< modules and entities to be imported
257 EntityNameMap myNames; ///< names of the entities
258 DumpStack myEntitiesStack; ///< stack of dumped entities
260 NbFeaturesMap myFeatureCount; ///< number of features of each kind
263 std::set<EntityPtr> myNotDumpedEntities; ///< list of entities, used by other features but not dumped yet
265 friend class SketchAPI_Sketch;