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;
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_Folder;
57 class ModelAPI_Object;
58 class ModelAPI_Result;
60 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
61 typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
62 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
63 typedef std::shared_ptr<ModelAPI_Folder> FolderPtr;
64 typedef std::shared_ptr<ModelAPI_Result> ResultPtr;
66 typedef std::set<std::string> ModulesSet;
67 typedef std::map<DocumentPtr, std::map<std::string, std::pair<int, int> > > NbFeaturesMap;
69 /**\class ModelHighAPI_Dumper
71 * \brief Dump engine for the model
73 * The dumper can be customized by the set of storages (DumpStorage), for example to identify
74 * dumped selected objects by their geometrical properties. By default, the dump is executed to
75 * store original names of the selected shapes.
77 class ModelHighAPI_Dumper
80 /** \class DumpStorage
82 * \brief Storage for the dumped data. Process selection attributes to be dumped by naming.
88 MODELHIGHAPI_EXPORT DumpStorage(const DumpStorage& theOther);
89 MODELHIGHAPI_EXPORT const DumpStorage& operator=(const DumpStorage& theOther);
91 void setFilenameSuffix(const std::string& theSuffix) { myFilenameSuffix = theSuffix; }
94 std::ostringstream& buffer() { return myDumpBuffer; }
95 std::ostringstream& fullDump() { return myFullDump; }
97 MODELHIGHAPI_EXPORT virtual void reserveBuffer();
98 MODELHIGHAPI_EXPORT virtual void restoreReservedBuffer();
101 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
104 virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules);
107 std::string myFilenameSuffix;
108 std::ostringstream myDumpBuffer;
109 std::ostringstream myFullDump;
111 std::stack<std::string> myDumpBufferHideout;
113 friend class ModelHighAPI_Dumper;
115 typedef std::shared_ptr<DumpStorage> DumpStoragePtr;
117 /** \class DumpStorageGeom
118 * \ingroup CPPHighAPI
119 * \brief Process selection attributes to be dumped by geometric properties.
121 class DumpStorageGeom : public DumpStorage
125 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
128 /** \class DumpStorageWeak
129 * \ingroup CPPHighAPI
130 * \brief Process selection attributes to be dumped by weak naming.
132 class DumpStorageWeak : public DumpStorage
136 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
140 /// Default constructor
142 ModelHighAPI_Dumper();
144 /// Sets instance of a dumper
146 static void setInstance(ModelHighAPI_Dumper* theDumper);
148 /// Returns instance of a dumper
150 static ModelHighAPI_Dumper* getInstance();
154 virtual ~ModelHighAPI_Dumper();
156 /// Add custom storage to collect corresponding dump
158 void addCustomStorage(const DumpStoragePtr& theStorage);
159 /// Clear custom storages list
161 void clearCustomStorage();
163 /// Dump given document into the file
164 /// \return \c true, if succeed
166 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
168 /// Add module to list of imported modules
169 /// \param theModuleName name of the module to be imported
171 void importModule(const std::string& theModuleName);
173 /// Returns name of specified entity
174 /// \param theEntity [in] named entity
175 /// \param theSaveNotDumped [in]
176 /// if \c true, the entity should be stored as not dumped (will be dumped automatically)
177 /// \param theUseEntityName [in]
178 /// if \c true, the entity name should be used "as is" without changing default name
179 /// \return name of the entity
181 const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
182 bool theUseEntityName = false);
184 /// Returns name of parent composite feature for specified entity
186 const std::string& parentName(const FeaturePtr& theFeature);
188 /// Dump parameter feature only
189 virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
190 /// Dump given feature
191 virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
193 virtual void dumpFolder(const FolderPtr& theFolder) = 0;
195 /// Set feature postponed until all its dependencies are not dumped.
196 /// The name of the feature is stored anyway.
198 void postpone(const EntityPtr& theEntity);
200 /// Set a feature that should not be dumped anyway
202 void doNotDumpFeature(const FeaturePtr& theFeature)
203 { myFeaturesToSkip.insert(theFeature); }
205 /// Dump sub-feature name and color, without dumping feature creation.
206 /// Used for features which creates sub-features in their execute method.
207 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
208 /// \param theSubFeature [in] sub-feature
210 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
211 const FeaturePtr& theSubFeature);
213 /// Return name of getter for corresponding attribute
214 virtual std::string attributeGetter(const FeaturePtr& theFeature,
215 const std::string& theAttrName) const = 0;
217 /// Return name of wrapper feature
218 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
222 ModelHighAPI_Dumper& operator<<(const char theChar);
225 ModelHighAPI_Dumper& operator<<(const char* theString);
228 ModelHighAPI_Dumper& operator<<(const std::string& theString);
231 ModelHighAPI_Dumper& operator<<(const bool theValue);
234 ModelHighAPI_Dumper& operator<<(const int theValue);
237 ModelHighAPI_Dumper& operator<<(const double theValue);
241 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
242 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
244 /// Dump GeomAPI_Pnt in the following form:
245 /// "GeomAPI_Pnt(X, Y, Z)"
247 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
249 /// "GeomAPI_Dir(X, Y, Z)"
251 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
253 /// Dump GeomDataAPI_Dir in the following form:
256 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
257 /// Dump GeomDataAPI_Point in the following form:
260 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
261 /// Dump GeomDataAPI_Point2D in the following form:
264 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
266 /// Dump AttributeBoolean
268 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
269 /// Dump AttributeInteger
271 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
272 /// Dump AttributeDouble
274 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
275 /// Dump AttributeString
277 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
278 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
280 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
284 ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
288 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
290 /// Dump a list of results
292 ModelHighAPI_Dumper& operator<<(const std::list<ResultPtr>& theResults);
295 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
296 operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
298 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
299 operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
301 /// Dump AttributeRefAttr
302 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
303 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
304 /// Dump AttributeRefAttrList as follows:
305 /// "[obj1, obj2, obj3, ...]"
306 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
307 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
308 /// Dump AttributeRefList as follows:
309 /// "[obj1, obj2, obj3, ...]"
310 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
311 operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
312 /// Dump AttributeSelection
313 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
314 operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
315 /// Dump AttributeSelectionList
316 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
317 operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
318 /// Dump AttributeReference
319 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
320 operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
321 /// Dump AttributeStringArray
322 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
323 operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
325 /// clear list of not dumped entities
326 MODELHIGHAPI_EXPORT void clearNotDumped();
328 /// Check the entity is already dumped
330 bool isDumped(const EntityPtr& theEntity) const;
331 /// Check theRefAttr is already dumped
333 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
334 /// Check all objects in theRefList are already dumped
336 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
339 /// Dump "setName" command if last entity had user-defined name
340 MODELHIGHAPI_EXPORT void dumpEntitySetName();
343 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
344 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
346 /// Iterate all features in document and dump them into intermediate buffer
347 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
349 /// Dump composite feature and all it sub-features
350 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
351 bool isForce = false);
353 /// Iterate all features in composite feature and dump them into intermediate buffer
354 /// \param theComposite [in] parent composite feature
355 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
357 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
358 bool theDumpModelDo = false);
360 /// Stores names of results for the given feature
361 void saveResultNames(const FeaturePtr& theFeature);
363 /// Check the result feature has default color
364 bool isDefaultColor(const ResultPtr& theResult) const;
366 /// Check the result feature has default deflection
367 bool isDefaultDeflection(const ResultPtr& theResult) const;
369 /// Check the result feature has default transparency
370 bool isDefaultTransparency(const ResultPtr& theResult) const;
372 /// Dump postponed entities
373 void dumpPostponed(bool theDumpFolders = false);
377 std::string myCurrentName; ///< default name of current feature
378 std::string myUserName; ///< user-defined name
379 bool myIsDefault; ///< \c true if the name is default
380 bool myIsDumped; ///< shows that the names of the feature are already stored
384 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
385 : myCurrentName(theCurName),
386 myUserName(theUserName),
387 myIsDefault(theDefault),
391 typedef std::map<EntityPtr, EntityName> EntityNameMap;
393 struct LastDumpedEntity {
394 EntityPtr myEntity; ///< last dumped entity
395 bool myUserName; ///< the entity hase user-defined name
396 /// results of this entity, which has user-defined names or specific colors
397 std::list<ResultPtr> myResults;
399 LastDumpedEntity(EntityPtr theEntity, bool theUserName,
400 const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
401 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
404 typedef std::stack<LastDumpedEntity> DumpStack;
406 static ModelHighAPI_Dumper* mySelf;
408 class DumpStorageBuffer;
409 DumpStorageBuffer* myDumpStorage; ///< storage of dumped data
411 ModulesSet myModules; ///< modules and entities to be imported
412 EntityNameMap myNames; ///< names of the entities
413 DumpStack myEntitiesStack; ///< stack of dumped entities
415 NbFeaturesMap myFeatureCount; ///< number of features of each kind
417 /// features which should not be dumped (like coincidence and tangency created by tangent arc)
418 std::set<FeaturePtr> myFeaturesToSkip;
420 std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
421 bool myDumpPostponedInProgress; ///< processing postponed is in progress
424 /// list of entities, used by other features but not dumped yet
425 std::set<EntityPtr> myNotDumpedEntities;
427 friend class SketchAPI_Sketch;
428 friend class ModelHighAPI_Folder;