1 // Copyright (C) 2014-2019 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
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;
39 class GeomDataAPI_Point2DArray;
41 class ModelAPI_Attribute;
42 class ModelAPI_AttributeBoolean;
43 class ModelAPI_AttributeDouble;
44 class ModelAPI_AttributeDoubleArray;
45 class ModelAPI_AttributeIntArray;
46 class ModelAPI_AttributeInteger;
47 class ModelAPI_AttributeRefAttr;
48 class ModelAPI_AttributeRefAttrList;
49 class ModelAPI_AttributeReference;
50 class ModelAPI_AttributeRefList;
51 class ModelAPI_AttributeSelection;
52 class ModelAPI_AttributeSelectionList;
53 class ModelAPI_AttributeString;
54 class ModelAPI_AttributeStringArray;
55 class ModelAPI_CompositeFeature;
56 class ModelAPI_Document;
57 class ModelAPI_Entity;
58 class ModelAPI_Feature;
59 class ModelAPI_Folder;
60 class ModelAPI_Object;
61 class ModelAPI_Result;
63 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
64 typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
65 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
66 typedef std::shared_ptr<ModelAPI_Folder> FolderPtr;
67 typedef std::shared_ptr<ModelAPI_Result> ResultPtr;
69 typedef std::set<std::string> ModulesSet;
70 typedef std::map<DocumentPtr, std::map<std::string, std::pair<int, int> > > NbFeaturesMap;
72 /**\class ModelHighAPI_Dumper
74 * \brief Dump engine for the model
76 * The dumper can be customized by the set of storages (DumpStorage), for example to identify
77 * dumped selected objects by their geometrical properties. By default, the dump is executed to
78 * store original names of the selected shapes.
80 class ModelHighAPI_Dumper
83 /** \class DumpStorage
85 * \brief Storage for the dumped data. Process selection attributes to be dumped by naming.
91 MODELHIGHAPI_EXPORT DumpStorage(const DumpStorage& theOther);
92 MODELHIGHAPI_EXPORT const DumpStorage& operator=(const DumpStorage& theOther);
94 void setFilenameSuffix(const std::string& theSuffix) { myFilenameSuffix = theSuffix; }
97 std::ostringstream& buffer() { return myDumpBuffer; }
98 std::ostringstream& fullDump() { return myFullDump; }
100 MODELHIGHAPI_EXPORT virtual void reserveBuffer();
101 MODELHIGHAPI_EXPORT virtual void restoreReservedBuffer();
104 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
107 virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules);
110 std::string myFilenameSuffix;
111 std::ostringstream myDumpBuffer;
112 std::ostringstream myFullDump;
114 std::stack<std::string> myDumpBufferHideout;
116 friend class ModelHighAPI_Dumper;
118 typedef std::shared_ptr<DumpStorage> DumpStoragePtr;
120 /** \class DumpStorageGeom
121 * \ingroup CPPHighAPI
122 * \brief Process selection attributes to be dumped by geometric properties.
124 class DumpStorageGeom : public DumpStorage
128 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
131 /** \class DumpStorageWeak
132 * \ingroup CPPHighAPI
133 * \brief Process selection attributes to be dumped by weak naming.
135 class DumpStorageWeak : public DumpStorage
139 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
143 /// Default constructor
145 ModelHighAPI_Dumper();
147 /// Sets instance of a dumper
149 static void setInstance(ModelHighAPI_Dumper* theDumper);
151 /// Returns instance of a dumper
153 static ModelHighAPI_Dumper* getInstance();
157 virtual ~ModelHighAPI_Dumper();
159 /// Add custom storage to collect corresponding dump
161 void addCustomStorage(const DumpStoragePtr& theStorage);
162 /// Clear custom storages list
164 void clearCustomStorage();
166 /// Dump given document into the file
167 /// \return \c true, if succeed
169 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
171 /// Add module to list of imported modules
172 /// \param theModuleName name of the module to be imported
174 void importModule(const std::string& theModuleName);
176 /// Returns name of specified entity
177 /// \param theEntity [in] named entity
178 /// \param theSaveNotDumped [in]
179 /// if \c true, the entity should be stored as not dumped (will be dumped automatically)
180 /// \param theUseEntityName [in]
181 /// if \c true, the entity name should be used "as is" without changing default name
182 /// \return name of the entity
184 const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
185 bool theUseEntityName = false);
187 /// Returns name of parent composite feature for specified entity
189 const std::string& parentName(const FeaturePtr& theFeature);
191 /// Dump parameter feature only
192 virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
193 /// Dump given feature
194 virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
196 virtual void dumpFolder(const FolderPtr& theFolder) = 0;
198 /// Set feature postponed until all its dependencies are not dumped.
199 /// The name of the feature is stored anyway.
201 void postpone(const EntityPtr& theEntity);
203 /// Set a feature that should not be dumped anyway
205 void doNotDumpFeature(const FeaturePtr& theFeature)
206 { myFeaturesToSkip.insert(theFeature); }
208 /// Dump sub-feature name and color, without dumping feature creation.
209 /// Used for features which creates sub-features in their execute method.
210 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
211 /// \param theSubFeature [in] sub-feature
213 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
214 const FeaturePtr& theSubFeature);
216 /// Return name of getter for corresponding attribute
217 virtual std::string attributeGetter(const FeaturePtr& theFeature,
218 const std::string& theAttrName) const = 0;
220 /// Return name of wrapper feature
221 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
225 ModelHighAPI_Dumper& operator<<(const char theChar);
228 ModelHighAPI_Dumper& operator<<(const char* theString);
231 ModelHighAPI_Dumper& operator<<(const std::string& theString);
234 ModelHighAPI_Dumper& operator<<(const bool theValue);
237 ModelHighAPI_Dumper& operator<<(const int theValue);
240 ModelHighAPI_Dumper& operator<<(const double theValue);
244 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
245 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
247 /// Dump GeomAPI_Pnt in the following form:
248 /// "GeomAPI_Pnt(X, Y, Z)"
250 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
252 /// "GeomAPI_Dir(X, Y, Z)"
254 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
256 /// Dump GeomDataAPI_Dir in the following form:
259 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
260 /// Dump GeomDataAPI_Point in the following form:
263 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
264 /// Dump GeomDataAPI_Point2D in the following form:
267 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
268 /// Dump GeomDataAPI_Point2DArray as a list of 2D points
270 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2DArray>& thePointArray);
272 /// Dump AttributeBoolean
274 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
275 /// Dump AttributeInteger
277 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
278 /// Dump AttributeIntArray
280 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeIntArray>& theArray);
281 /// Dump AttributeDouble
283 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
284 /// Dump AttributeDoubleArray
286 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDoubleArray>& theArray);
287 /// Dump AttributeString
289 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
290 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
292 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
296 ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
300 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
302 /// Dump a list of results
304 ModelHighAPI_Dumper& operator<<(const std::list<ResultPtr>& theResults);
307 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
308 operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
310 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
311 operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
313 /// Dump AttributeRefAttr
314 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
315 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
316 /// Dump AttributeRefAttrList as follows:
317 /// "[obj1, obj2, obj3, ...]"
318 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
319 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
320 /// Dump AttributeRefList as follows:
321 /// "[obj1, obj2, obj3, ...]"
322 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
323 operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
324 /// Dump AttributeSelection
325 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
326 operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
327 /// Dump AttributeSelectionList
328 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
329 operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
330 /// Dump AttributeReference
331 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
332 operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
333 /// Dump AttributeStringArray
334 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
335 operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
337 /// clear list of not dumped entities
338 MODELHIGHAPI_EXPORT void clearNotDumped();
340 /// Check the entity is already dumped
342 bool isDumped(const EntityPtr& theEntity) const;
343 /// Check theRefAttr is already dumped
345 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
346 /// Check all objects in theRefList are already dumped
348 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
351 /// Dump "setName" command if last entity had user-defined name
352 MODELHIGHAPI_EXPORT void dumpEntitySetName();
355 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
356 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
358 /// Iterate all features in document and dump them into intermediate buffer
359 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
361 /// Dump composite feature and all it sub-features
362 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
363 bool isForce = false,
364 bool isDumpModelDo = true);
366 /// Iterate all features in composite feature and dump them into intermediate buffer
367 /// \param theComposite [in] parent composite feature
368 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
370 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
371 bool theDumpModelDo = false);
373 /// Stores names of results for the given feature
374 void saveResultNames(const FeaturePtr& theFeature);
376 /// Check the result feature has default color
377 bool isDefaultColor(const ResultPtr& theResult) const;
379 /// Check the result feature has default deflection
380 bool isDefaultDeflection(const ResultPtr& theResult) const;
382 /// Check the result feature has default transparency
383 bool isDefaultTransparency(const ResultPtr& theResult) const;
385 /// Dump postponed entities
386 void dumpPostponed(bool theDumpFolders = false);
390 std::string myCurrentName; ///< default name of current feature
391 std::string myUserName; ///< user-defined name
392 bool myIsDefault; ///< \c true if the name is default
393 bool myIsDumped; ///< shows that the names of the feature are already stored
397 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
398 : myCurrentName(theCurName),
399 myUserName(theUserName),
400 myIsDefault(theDefault),
404 typedef std::map<EntityPtr, EntityName> EntityNameMap;
406 struct LastDumpedEntity {
407 EntityPtr myEntity; ///< last dumped entity
408 bool myUserName; ///< the entity hase user-defined name
409 /// results of this entity, which has user-defined names or specific colors
410 std::list<ResultPtr> myResults;
412 LastDumpedEntity(EntityPtr theEntity, bool theUserName,
413 const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
414 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
417 typedef std::stack<LastDumpedEntity> DumpStack;
419 static ModelHighAPI_Dumper* mySelf;
421 class DumpStorageBuffer;
422 DumpStorageBuffer* myDumpStorage; ///< storage of dumped data
424 ModulesSet myModules; ///< modules and entities to be imported
425 EntityNameMap myNames; ///< names of the entities
426 DumpStack myEntitiesStack; ///< stack of dumped entities
428 NbFeaturesMap myFeatureCount; ///< number of features of each kind
430 /// features which should not be dumped (like coincidence and tangency created by tangent arc)
431 std::set<FeaturePtr> myFeaturesToSkip;
433 std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
434 bool myDumpPostponedInProgress; ///< processing postponed is in progress
437 /// list of entities, used by other features but not dumped yet
438 std::set<EntityPtr> myNotDumpedEntities;
440 friend class SketchAPI_Sketch;
441 friend class ModelHighAPI_Folder;