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;
116 /** \class DumpStorageGeom
117 * \ingroup CPPHighAPI
118 * \brief Process selection attributes to be dumped by geometric properties.
120 class DumpStorageGeom : public DumpStorage
124 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
127 /** \class DumpStorageWeak
128 * \ingroup CPPHighAPI
129 * \brief Process selection attributes to be dumped by weak naming.
131 class DumpStorageWeak : public DumpStorage
135 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
139 /// Default constructor
141 ModelHighAPI_Dumper();
143 /// Sets instance of a dumper
145 static void setInstance(ModelHighAPI_Dumper* theDumper);
147 /// Returns instance of a dumper
149 static ModelHighAPI_Dumper* getInstance();
153 virtual ~ModelHighAPI_Dumper();
155 /// Add custom storage to collect corresponding dump
157 void addCustomStorage(const DumpStorage& theStorage);
158 /// Clear custom storages list
160 void clearCustomStorage();
162 /// Dump given document into the file
163 /// \return \c true, if succeed
165 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
167 /// Add module to list of imported modules
168 /// \param theModuleName name of the module to be imported
170 void importModule(const std::string& theModuleName);
172 /// Returns name of specified entity
173 /// \param theEntity [in] named entity
174 /// \param theSaveNotDumped [in]
175 /// if \c true, the entity should be stored as not dumped (will be dumped automatically)
176 /// \param theUseEntityName [in]
177 /// if \c true, the entity name should be used "as is" without changing default name
178 /// \return name of the entity
180 const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
181 bool theUseEntityName = false);
183 /// Returns name of parent composite feature for specified entity
185 const std::string& parentName(const FeaturePtr& theFeature);
187 /// Dump parameter feature only
188 virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
189 /// Dump given feature
190 virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
192 virtual void dumpFolder(const FolderPtr& theFolder) = 0;
194 /// Set feature postponed until all its dependencies are not dumped.
195 /// The name of the feature is stored anyway.
197 void postpone(const EntityPtr& theEntity);
199 /// Set a feature that should not be dumped anyway
201 void doNotDumpFeature(const FeaturePtr& theFeature)
202 { myFeaturesToSkip.insert(theFeature); }
204 /// Dump sub-feature name and color, without dumping feature creation.
205 /// Used for features which creates sub-features in their execute method.
206 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
207 /// \param theSubFeature [in] sub-feature
209 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
210 const FeaturePtr& theSubFeature);
212 /// Return name of getter for corresponding attribute
213 virtual std::string attributeGetter(const FeaturePtr& theFeature,
214 const std::string& theAttrName) const = 0;
216 /// Return name of wrapper feature
217 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
221 ModelHighAPI_Dumper& operator<<(const char theChar);
224 ModelHighAPI_Dumper& operator<<(const char* theString);
227 ModelHighAPI_Dumper& operator<<(const std::string& theString);
230 ModelHighAPI_Dumper& operator<<(const bool theValue);
233 ModelHighAPI_Dumper& operator<<(const int theValue);
236 ModelHighAPI_Dumper& operator<<(const double theValue);
240 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
241 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
243 /// Dump GeomAPI_Pnt in the following form:
244 /// "GeomAPI_Pnt(X, Y, Z)"
246 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
248 /// "GeomAPI_Dir(X, Y, Z)"
250 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
252 /// Dump GeomDataAPI_Dir in the following form:
255 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
256 /// Dump GeomDataAPI_Point in the following form:
259 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
260 /// Dump GeomDataAPI_Point2D in the following form:
263 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
265 /// Dump AttributeBoolean
267 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
268 /// Dump AttributeInteger
270 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
271 /// Dump AttributeDouble
273 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
274 /// Dump AttributeString
276 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
277 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
279 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
283 ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
287 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
290 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
291 operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
293 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
294 operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
296 /// Dump AttributeRefAttr
297 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
298 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
299 /// Dump AttributeRefAttrList as follows:
300 /// "[obj1, obj2, obj3, ...]"
301 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
302 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
303 /// Dump AttributeRefList as follows:
304 /// "[obj1, obj2, obj3, ...]"
305 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
306 operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
307 /// Dump AttributeSelection
308 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
309 operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
310 /// Dump AttributeSelectionList
311 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
312 operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
313 /// Dump AttributeReference
314 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
315 operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
316 /// Dump AttributeStringArray
317 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
318 operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
320 /// clear list of not dumped entities
321 MODELHIGHAPI_EXPORT void clearNotDumped();
323 /// Check the entity is already dumped
325 bool isDumped(const EntityPtr& theEntity) const;
326 /// Check theRefAttr is already dumped
328 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
329 /// Check all objects in theRefList are already dumped
331 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
334 /// Dump "setName" command if last entity had user-defined name
335 MODELHIGHAPI_EXPORT void dumpEntitySetName();
338 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
339 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
341 /// Iterate all features in document and dump them into intermediate buffer
342 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
344 /// Dump composite feature and all it sub-features
345 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
346 bool isForce = false);
348 /// Iterate all features in composite feature and dump them into intermediate buffer
349 /// \param theComposite [in] parent composite feature
350 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
352 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
353 bool theDumpModelDo = false);
355 /// Stores names of results for the given feature
356 void saveResultNames(const FeaturePtr& theFeature);
358 /// Check the result feature has default color
359 bool isDefaultColor(const ResultPtr& theResult) const;
361 /// Check the result feature has default deflection
362 bool isDefaultDeflection(const ResultPtr& theResult) const;
364 /// Check the result feature has default transparency
365 bool isDefaultTransparency(const ResultPtr& theResult) const;
367 /// Dump postponed entities
368 void dumpPostponed(bool theDumpFolders = false);
372 std::string myCurrentName; ///< default name of current feature
373 std::string myUserName; ///< user-defined name
374 bool myIsDefault; ///< \c true if the name is default
375 bool myIsDumped; ///< shows that the names of the feature are already stored
379 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
380 : myCurrentName(theCurName),
381 myUserName(theUserName),
382 myIsDefault(theDefault),
386 typedef std::map<EntityPtr, EntityName> EntityNameMap;
388 struct LastDumpedEntity {
389 EntityPtr myEntity; ///< last dumped entity
390 bool myUserName; ///< the entity hase user-defined name
391 /// results of this entity, which has user-defined names or specific colors
392 std::list<ResultPtr> myResults;
394 LastDumpedEntity(EntityPtr theEntity, bool theUserName,
395 const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
396 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
399 typedef std::stack<LastDumpedEntity> DumpStack;
401 static ModelHighAPI_Dumper* mySelf;
403 class DumpStorageBuffer;
404 DumpStorageBuffer* myDumpStorage; ///< storage of dumped data
406 ModulesSet myModules; ///< modules and entities to be imported
407 EntityNameMap myNames; ///< names of the entities
408 DumpStack myEntitiesStack; ///< stack of dumped entities
410 NbFeaturesMap myFeatureCount; ///< number of features of each kind
412 /// features which should not be dumped (like coincidence and tangency created by tangent arc)
413 std::set<FeaturePtr> myFeaturesToSkip;
415 std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
416 bool myDumpPostponedInProgress; ///< processing postponed is in progress
419 /// list of entities, used by other features but not dumped yet
420 std::set<EntityPtr> myNotDumpedEntities;
422 friend class SketchAPI_Sketch;
423 friend class ModelHighAPI_Folder;