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 /// Dump sub-feature name and color, without dumping feature creation.
102 /// Used for features which creates sub-features in their execute method.
103 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
104 /// \param theSubFeature [in] sub-feature
106 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
107 const FeaturePtr& theSubFeature);
109 /// Return name of getter for corresponding attribute
110 virtual std::string attributeGetter(const FeaturePtr& theFeature,
111 const std::string& theAttrName) const = 0;
113 /// Return name of wrapper feature
114 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
116 /// Save all dumps into specified file
118 bool exportTo(const std::string& theFileName);
122 ModelHighAPI_Dumper& operator<<(const char theChar);
125 ModelHighAPI_Dumper& operator<<(const char* theString);
128 ModelHighAPI_Dumper& operator<<(const std::string& theString);
131 ModelHighAPI_Dumper& operator<<(const bool theValue);
134 ModelHighAPI_Dumper& operator<<(const int theValue);
137 ModelHighAPI_Dumper& operator<<(const double theValue);
141 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
142 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
144 /// Dump GeomAPI_Pnt in the following form:
145 /// "GeomAPI_Pnt(X, Y, Z)"
147 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
149 /// "GeomAPI_Dir(X, Y, Z)"
151 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
153 /// Dump GeomDataAPI_Dir in the following form:
156 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
157 /// Dump GeomDataAPI_Point in the following form:
160 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
161 /// Dump GeomDataAPI_Point2D in the following form:
164 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
166 /// Dump AttributeBoolean
168 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
169 /// Dump AttributeInteger
171 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
172 /// Dump AttributeDouble
174 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
175 /// Dump AttributeString
177 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
178 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
180 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
184 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
188 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
191 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
193 /// Dump AttributeRefAttr
195 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
196 /// Dump AttributeRefAttrList as follows:
197 /// "[obj1, obj2, obj3, ...]"
199 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
200 /// Dump AttributeRefList as follows:
201 /// "[obj1, obj2, obj3, ...]"
203 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
204 /// Dump AttributeSelection
206 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
207 /// Dump AttributeSelectionList
209 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
210 /// Dump AttributeReference
212 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
214 /// Clear dump buffer
216 void clear(bool bufferOnly = false);
217 /// clear list of not dumped entities
218 MODELHIGHAPI_EXPORT void clearNotDumped();
221 /// Dump "setName" command if last entity had user-defined name
222 MODELHIGHAPI_EXPORT void dumpEntitySetName();
225 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
226 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
228 /// Iterate all features in document and dump them into intermediate buffer
229 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
231 /// Dump composite feature and all it sub-features
232 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce = false);
234 /// Iterate all features in composite feature and dump them into intermediate buffer
235 /// \param theComposite [in] parent composite feature
236 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
238 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool theDumpModelDo = false);
240 /// Check the entity is already dumped
241 bool isDumped(const EntityPtr& theEntity) const;
243 /// Stores names of results for the given feature
244 void saveResultNames(const FeaturePtr& theFeature);
246 /// Check the result feature has default color
247 bool isDefaultColor(const ResultPtr& theResult) const;
249 /// Check the result feature has default deflection
250 bool isDefaultDeflection(const ResultPtr& theResult) const;
254 std::string myCurrentName; ///< default name of current feature
255 std::string myUserName; ///< user-defined name
256 bool myIsDefault; ///< \c true if the name is default
260 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
261 : myCurrentName(theCurName), myUserName(theUserName), myIsDefault(theDefault)
265 typedef std::map<EntityPtr, EntityName> EntityNameMap;
266 typedef std::map<std::string, std::set<std::string> > ModulesMap;
267 typedef std::map<DocumentPtr, std::map<std::string, int> > NbFeaturesMap;
269 struct LastDumpedEntity {
270 EntityPtr myEntity; ///< last dumped entity
271 bool myUserName; ///< the entity hase user-defined name
272 std::list<ResultPtr> myResults; ///< results of this entity, which has user-defined names or specific colors
274 LastDumpedEntity(EntityPtr theEntity, bool theUserName, const std::list<ResultPtr>& theResults)
275 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
278 typedef std::stack<LastDumpedEntity> DumpStack;
280 static ModelHighAPI_Dumper* mySelf;
282 std::ostringstream myDumpBuffer; ///< intermediate buffer to store dumping data
283 std::ostringstream myFullDump; ///< full buffer of dumped data
285 ModulesMap myModules; ///< modules and entities to be imported
286 EntityNameMap myNames; ///< names of the entities
287 DumpStack myEntitiesStack; ///< stack of dumped entities
289 NbFeaturesMap myFeatureCount; ///< number of features of each kind
292 std::set<EntityPtr> myNotDumpedEntities; ///< list of entities, used by other features but not dumped yet
294 friend class SketchAPI_Sketch;