1 // Copyright (C) 2014-2017 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<mailto:webmaster.salome@opencascade.com>
20 #ifndef ModelHighAPI_Dumper_H_
21 #define ModelHighAPI_Dumper_H_
23 #include "ModelHighAPI.h"
36 class GeomDataAPI_Dir;
37 class GeomDataAPI_Point;
38 class GeomDataAPI_Point2D;
40 class ModelAPI_Attribute;
41 class ModelAPI_AttributeBoolean;
42 class ModelAPI_AttributeDouble;
43 class ModelAPI_AttributeInteger;
44 class ModelAPI_AttributeRefAttr;
45 class ModelAPI_AttributeRefAttrList;
46 class ModelAPI_AttributeReference;
47 class ModelAPI_AttributeRefList;
48 class ModelAPI_AttributeSelection;
49 class ModelAPI_AttributeSelectionList;
50 class ModelAPI_AttributeString;
51 class ModelAPI_AttributeStringArray;
52 class ModelAPI_CompositeFeature;
53 class ModelAPI_Document;
54 class ModelAPI_Entity;
55 class ModelAPI_Feature;
56 class ModelAPI_Object;
57 class ModelAPI_Result;
59 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
60 typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
61 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
62 typedef std::shared_ptr<ModelAPI_Result> ResultPtr;
64 /**\class ModelHighAPI_Dumper
66 * \brief Dump engine for the model
68 class ModelHighAPI_Dumper
71 /// Default constructor
73 ModelHighAPI_Dumper();
75 /// Sets instance of a dumper
77 static void setInstance(ModelHighAPI_Dumper* theDumper);
79 /// Returns instance of a dumper
81 static ModelHighAPI_Dumper* getInstance();
84 virtual ~ModelHighAPI_Dumper() {}
86 /// Dump given document into the file
87 /// \return \c true, if succeed
89 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
91 /// Add module to list of imported modules
92 /// \param theModuleName name of the module to be imported
93 /// \param theObject name of the entity to be imported
94 /// from the module (if empty, while module will be imported)
96 void importModule(const std::string& theModuleName,
97 const std::string& theObject = std::string());
99 /// Returns name of specified entity
100 /// \param theEntity [in] named entity
101 /// \param theSaveNotDumped [in]
102 /// if \c true, the entity should be stored as not dumped (will be dumped automatically)
103 /// \param theUseEntityName [in]
104 /// if \c true, the entity name should be used "as is" without changing default name
105 /// \return name of the entity
107 const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
108 bool theUseEntityName = false);
110 /// Returns name of parent composite feature for specified entity
112 const std::string& parentName(const FeaturePtr& theFeature);
114 /// Dump parameter feature only
115 virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
116 /// Dump given feature
117 virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
119 /// Set a feature that should not be dumped anyway
121 void doNotDumpFeature(const FeaturePtr& theFeature)
122 { myFeaturesToSkip.insert(theFeature); }
124 /// Dump sub-feature name and color, without dumping feature creation.
125 /// Used for features which creates sub-features in their execute method.
126 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
127 /// \param theSubFeature [in] sub-feature
129 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
130 const FeaturePtr& theSubFeature);
132 /// Return name of getter for corresponding attribute
133 virtual std::string attributeGetter(const FeaturePtr& theFeature,
134 const std::string& theAttrName) const = 0;
136 /// Return name of wrapper feature
137 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
139 /// Save all dumps into specified file
141 bool exportTo(const std::string& theFileName);
145 ModelHighAPI_Dumper& operator<<(const char theChar);
148 ModelHighAPI_Dumper& operator<<(const char* theString);
151 ModelHighAPI_Dumper& operator<<(const std::string& theString);
154 ModelHighAPI_Dumper& operator<<(const bool theValue);
157 ModelHighAPI_Dumper& operator<<(const int theValue);
160 ModelHighAPI_Dumper& operator<<(const double theValue);
164 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
165 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
167 /// Dump GeomAPI_Pnt in the following form:
168 /// "GeomAPI_Pnt(X, Y, Z)"
170 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
172 /// "GeomAPI_Dir(X, Y, Z)"
174 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
176 /// Dump GeomDataAPI_Dir in the following form:
179 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
180 /// Dump GeomDataAPI_Point in the following form:
183 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
184 /// Dump GeomDataAPI_Point2D in the following form:
187 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
189 /// Dump AttributeBoolean
191 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
192 /// Dump AttributeInteger
194 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
195 /// Dump AttributeDouble
197 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
198 /// Dump AttributeString
200 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
201 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
203 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
207 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
210 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
211 operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
213 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
214 operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
216 /// Dump AttributeRefAttr
217 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
218 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
219 /// Dump AttributeRefAttrList as follows:
220 /// "[obj1, obj2, obj3, ...]"
221 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
222 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
223 /// Dump AttributeRefList as follows:
224 /// "[obj1, obj2, obj3, ...]"
225 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
226 operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
227 /// Dump AttributeSelection
228 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
229 operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
230 /// Dump AttributeSelectionList
231 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
232 operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
233 /// Dump AttributeReference
234 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
235 operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
236 /// Dump AttributeStringArray
237 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
238 operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
240 /// Clear dump buffer
242 void clear(bool bufferOnly = false);
243 /// clear list of not dumped entities
244 MODELHIGHAPI_EXPORT void clearNotDumped();
247 /// Dump "setName" command if last entity had user-defined name
248 MODELHIGHAPI_EXPORT void dumpEntitySetName();
251 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
252 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
254 /// Iterate all features in document and dump them into intermediate buffer
255 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
257 /// Dump composite feature and all it sub-features
258 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
259 bool isForce = false);
261 /// Iterate all features in composite feature and dump them into intermediate buffer
262 /// \param theComposite [in] parent composite feature
263 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
265 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
266 bool theDumpModelDo = false);
268 /// Check the entity is already dumped
269 bool isDumped(const EntityPtr& theEntity) const;
271 /// Stores names of results for the given feature
272 void saveResultNames(const FeaturePtr& theFeature);
274 /// Check the result feature has default color
275 bool isDefaultColor(const ResultPtr& theResult) const;
277 /// Check the result feature has default deflection
278 bool isDefaultDeflection(const ResultPtr& theResult) const;
282 std::string myCurrentName; ///< default name of current feature
283 std::string myUserName; ///< user-defined name
284 bool myIsDefault; ///< \c true if the name is default
285 bool myIsDumped; ///< shows that the names of the feature are already stored
289 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
290 : myCurrentName(theCurName),
291 myUserName(theUserName),
292 myIsDefault(theDefault),
297 typedef std::map<EntityPtr, EntityName> EntityNameMap;
298 typedef std::map<std::string, std::set<std::string> > ModulesMap;
299 typedef std::map<DocumentPtr, std::map<std::string, int> > NbFeaturesMap;
301 struct LastDumpedEntity {
302 EntityPtr myEntity; ///< last dumped entity
303 bool myUserName; ///< the entity hase user-defined name
304 /// results of this entity, which has user-defined names or specific colors
305 std::list<ResultPtr> myResults;
307 LastDumpedEntity(EntityPtr theEntity, bool theUserName,
308 const std::list<ResultPtr>& theResults)
309 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
312 typedef std::stack<LastDumpedEntity> DumpStack;
314 static ModelHighAPI_Dumper* mySelf;
316 std::ostringstream myDumpBuffer; ///< intermediate buffer to store dumping data
317 std::ostringstream myFullDump; ///< full buffer of dumped data
319 ModulesMap myModules; ///< modules and entities to be imported
320 EntityNameMap myNames; ///< names of the entities
321 DumpStack myEntitiesStack; ///< stack of dumped entities
323 NbFeaturesMap myFeatureCount; ///< number of features of each kind
325 /// features which should not be dumped (like coincidence and tangency created by tangent arc)
326 std::set<FeaturePtr> myFeaturesToSkip;
329 /// list of entities, used by other features but not dumped yet
330 std::set<EntityPtr> myNotDumpedEntities;
332 friend class SketchAPI_Sketch;