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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef ModelHighAPI_Dumper_H_
22 #define ModelHighAPI_Dumper_H_
24 #include "ModelHighAPI.h"
37 class GeomDataAPI_Dir;
38 class GeomDataAPI_Point;
39 class GeomDataAPI_Point2D;
41 class ModelAPI_Attribute;
42 class ModelAPI_AttributeBoolean;
43 class ModelAPI_AttributeDouble;
44 class ModelAPI_AttributeInteger;
45 class ModelAPI_AttributeRefAttr;
46 class ModelAPI_AttributeRefAttrList;
47 class ModelAPI_AttributeReference;
48 class ModelAPI_AttributeRefList;
49 class ModelAPI_AttributeSelection;
50 class ModelAPI_AttributeSelectionList;
51 class ModelAPI_AttributeString;
52 class ModelAPI_AttributeStringArray;
53 class ModelAPI_CompositeFeature;
54 class ModelAPI_Document;
55 class ModelAPI_Entity;
56 class ModelAPI_Feature;
57 class ModelAPI_Folder;
58 class ModelAPI_Object;
59 class ModelAPI_Result;
61 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
62 typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
63 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
64 typedef std::shared_ptr<ModelAPI_Folder> FolderPtr;
65 typedef std::shared_ptr<ModelAPI_Result> ResultPtr;
67 /**\class ModelHighAPI_Dumper
69 * \brief Dump engine for the model
71 class ModelHighAPI_Dumper
74 /// Default constructor
76 ModelHighAPI_Dumper();
78 /// Sets instance of a dumper
80 static void setInstance(ModelHighAPI_Dumper* theDumper);
82 /// Returns instance of a dumper
84 static ModelHighAPI_Dumper* getInstance();
87 virtual ~ModelHighAPI_Dumper() {}
89 /// Set/unset flag to dump selection attributes by geometrical properties:
90 /// inner point in the selected shape
91 void setSelectionByGeometry(bool theDumpByGeom = true)
92 { myGeometricalSelection = theDumpByGeom; }
94 /// Set/unset flag to dump selection attributes by weak naming
95 void setSelectionWeakNaming(bool theDumpByWeakNaming = true)
96 { myWeakNamingSelection = theDumpByWeakNaming; }
98 /// Dump given document into the file
99 /// \return \c true, if succeed
101 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
103 /// Add module to list of imported modules
104 /// \param theModuleName name of the module to be imported
105 /// \param theObject name of the entity to be imported
106 /// from the module (if empty, while module will be imported)
108 void importModule(const std::string& theModuleName,
109 const std::string& theObject = std::string());
111 /// Returns name of specified entity
112 /// \param theEntity [in] named entity
113 /// \param theSaveNotDumped [in]
114 /// if \c true, the entity should be stored as not dumped (will be dumped automatically)
115 /// \param theUseEntityName [in]
116 /// if \c true, the entity name should be used "as is" without changing default name
117 /// \return name of the entity
119 const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
120 bool theUseEntityName = false);
122 /// Returns name of parent composite feature for specified entity
124 const std::string& parentName(const FeaturePtr& theFeature);
126 /// Dump parameter feature only
127 virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
128 /// Dump given feature
129 virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
131 virtual void dumpFolder(const FolderPtr& theFolder) = 0;
133 /// Set feature postponed until all its dependencies are not dumped.
134 /// The name of the feature is stored anyway.
136 void postpone(const EntityPtr& theEntity);
138 /// Set a feature that should not be dumped anyway
140 void doNotDumpFeature(const FeaturePtr& theFeature)
141 { myFeaturesToSkip.insert(theFeature); }
143 /// Dump sub-feature name and color, without dumping feature creation.
144 /// Used for features which creates sub-features in their execute method.
145 /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
146 /// \param theSubFeature [in] sub-feature
148 void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
149 const FeaturePtr& theSubFeature);
151 /// Return name of getter for corresponding attribute
152 virtual std::string attributeGetter(const FeaturePtr& theFeature,
153 const std::string& theAttrName) const = 0;
155 /// Return name of wrapper feature
156 virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
158 /// Save all dumps into specified file
160 bool exportTo(const std::string& theFileName);
164 ModelHighAPI_Dumper& operator<<(const char theChar);
167 ModelHighAPI_Dumper& operator<<(const char* theString);
170 ModelHighAPI_Dumper& operator<<(const std::string& theString);
173 ModelHighAPI_Dumper& operator<<(const bool theValue);
176 ModelHighAPI_Dumper& operator<<(const int theValue);
179 ModelHighAPI_Dumper& operator<<(const double theValue);
183 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
184 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
186 /// Dump GeomAPI_Pnt in the following form:
187 /// "GeomAPI_Pnt(X, Y, Z)"
189 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
191 /// "GeomAPI_Dir(X, Y, Z)"
193 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
195 /// Dump GeomDataAPI_Dir in the following form:
198 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
199 /// Dump GeomDataAPI_Point in the following form:
202 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
203 /// Dump GeomDataAPI_Point2D in the following form:
206 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
208 /// Dump AttributeBoolean
210 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
211 /// Dump AttributeInteger
213 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
214 /// Dump AttributeDouble
216 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
217 /// Dump AttributeString
219 ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
220 /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
222 ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
226 ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
230 ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
233 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
234 operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
236 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
237 operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
239 /// Dump AttributeRefAttr
240 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
241 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
242 /// Dump AttributeRefAttrList as follows:
243 /// "[obj1, obj2, obj3, ...]"
244 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
245 operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
246 /// Dump AttributeRefList as follows:
247 /// "[obj1, obj2, obj3, ...]"
248 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
249 operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
250 /// Dump AttributeSelection
251 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
252 operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
253 /// Dump AttributeSelectionList
254 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
255 operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
256 /// Dump AttributeReference
257 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
258 operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
259 /// Dump AttributeStringArray
260 MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
261 operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
263 /// Clear dump buffer
265 void clear(bool bufferOnly = false);
266 /// clear list of not dumped entities
267 MODELHIGHAPI_EXPORT void clearNotDumped();
269 /// Check the entity is already dumped
271 bool isDumped(const EntityPtr& theEntity) const;
272 /// Check theRefAttr is already dumped
274 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
275 /// Check all objects in theRefList are already dumped
277 bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
280 /// Dump "setName" command if last entity had user-defined name
281 MODELHIGHAPI_EXPORT void dumpEntitySetName();
284 ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
285 const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
287 /// Iterate all features in document and dump them into intermediate buffer
288 bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
290 /// Dump composite feature and all it sub-features
291 bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
292 bool isForce = false);
294 /// Iterate all features in composite feature and dump them into intermediate buffer
295 /// \param theComposite [in] parent composite feature
296 /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
298 bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
299 bool theDumpModelDo = false);
301 /// Stores names of results for the given feature
302 void saveResultNames(const FeaturePtr& theFeature);
304 /// Check the result feature has default color
305 bool isDefaultColor(const ResultPtr& theResult) const;
307 /// Check the result feature has default deflection
308 bool isDefaultDeflection(const ResultPtr& theResult) const;
310 /// Check the result feature has default transparency
311 bool isDefaultTransparency(const ResultPtr& theResult) const;
313 /// Dump postponed entities
314 void dumpPostponed(bool theDumpFolders = false);
318 std::string myCurrentName; ///< default name of current feature
319 std::string myUserName; ///< user-defined name
320 bool myIsDefault; ///< \c true if the name is default
321 bool myIsDumped; ///< shows that the names of the feature are already stored
325 EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
326 : myCurrentName(theCurName),
327 myUserName(theUserName),
328 myIsDefault(theDefault),
333 typedef std::map<EntityPtr, EntityName> EntityNameMap;
334 typedef std::map<std::string, std::set<std::string> > ModulesMap;
335 typedef std::map<DocumentPtr, std::map<std::string, std::pair<int, int> > > NbFeaturesMap;
337 struct LastDumpedEntity {
338 EntityPtr myEntity; ///< last dumped entity
339 bool myUserName; ///< the entity hase user-defined name
340 /// results of this entity, which has user-defined names or specific colors
341 std::list<ResultPtr> myResults;
343 LastDumpedEntity(EntityPtr theEntity, bool theUserName,
344 const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
345 : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
348 typedef std::stack<LastDumpedEntity> DumpStack;
350 static ModelHighAPI_Dumper* mySelf;
352 std::ostringstream myDumpBuffer; ///< intermediate buffer to store dumping data
353 std::ostringstream myFullDump; ///< full buffer of dumped data
355 ModulesMap myModules; ///< modules and entities to be imported
356 EntityNameMap myNames; ///< names of the entities
357 DumpStack myEntitiesStack; ///< stack of dumped entities
359 NbFeaturesMap myFeatureCount; ///< number of features of each kind
361 /// features which should not be dumped (like coincidence and tangency created by tangent arc)
362 std::set<FeaturePtr> myFeaturesToSkip;
364 std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
365 bool myDumpPostponedInProgress; ///< processing postponed is in progress
367 bool myGeometricalSelection; ///< dump selection not by naming, but by coordinates of inner point
368 bool myWeakNamingSelection; ///< dump selection by weak naming
371 /// list of entities, used by other features but not dumped yet
372 std::set<EntityPtr> myNotDumpedEntities;
374 friend class SketchAPI_Sketch;
375 friend class ModelHighAPI_Folder;