]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Dumper.h
Salome HOME
265743dca07f39cebd4a0540dc25886ec1683bbb
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef ModelHighAPI_Dumper_H_
21 #define ModelHighAPI_Dumper_H_
22
23 #include "ModelHighAPI.h"
24
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <set>
29 #include <sstream>
30 #include <stack>
31 #include <string>
32
33 class GeomAPI_Pnt;
34 class GeomAPI_Dir;
35
36 class GeomDataAPI_Dir;
37 class GeomDataAPI_Point;
38 class GeomDataAPI_Point2D;
39
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;
59
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;
65
66 /**\class ModelHighAPI_Dumper
67  * \ingroup CPPHighAPI
68  * \brief Dump engine for the model
69  */
70 class ModelHighAPI_Dumper
71 {
72 public:
73   /// Default constructor
74   MODELHIGHAPI_EXPORT
75   ModelHighAPI_Dumper();
76
77   /// Sets instance of a dumper
78   MODELHIGHAPI_EXPORT
79   static void setInstance(ModelHighAPI_Dumper* theDumper);
80
81   /// Returns instance of a dumper
82   MODELHIGHAPI_EXPORT
83   static ModelHighAPI_Dumper* getInstance();
84
85   /// Destructor
86   virtual ~ModelHighAPI_Dumper() {}
87
88   /// Set/unset flag to dump selection attributes by geometrical properties:
89   /// inner point in the selected shape
90   void setSelectionByGeometry(bool theDumpByGeom = true)
91   { myGeometricalSelection = theDumpByGeom; }
92
93   /// Set/unset flag to dump selection attributes by weak naming
94   void setSelectionWeakNaming(bool theDumpByWeakNaming = true)
95   { myWeakNamingSelection = theDumpByWeakNaming; }
96
97   /// Dump given document into the file
98   /// \return \c true, if succeed
99   MODELHIGHAPI_EXPORT
100   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
101
102   /// Add module to list of imported modules
103   /// \param theModuleName  name of the module to be imported
104   MODELHIGHAPI_EXPORT
105   void importModule(const std::string& theModuleName);
106
107   /// Returns name of specified entity
108   /// \param theEntity        [in] named entity
109   /// \param theSaveNotDumped [in]
110   ///    if \c true, the entity should be stored as not dumped (will be dumped automatically)
111   /// \param theUseEntityName [in]
112   ///    if \c true, the entity name should be used "as is" without changing default name
113   /// \return name of the entity
114   MODELHIGHAPI_EXPORT
115   const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
116                           bool theUseEntityName = false);
117
118   /// Returns name of parent composite feature for specified entity
119   MODELHIGHAPI_EXPORT
120   const std::string& parentName(const FeaturePtr& theFeature);
121
122   /// Dump parameter feature only
123   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
124   /// Dump given feature
125   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
126   /// Dump folder
127   virtual void dumpFolder(const FolderPtr& theFolder) = 0;
128
129   /// Set feature postponed until all its dependencies are not dumped.
130   /// The name of the feature is stored anyway.
131   MODELHIGHAPI_EXPORT
132   void postpone(const EntityPtr& theEntity);
133
134   /// Set a feature that should not be dumped anyway
135   MODELHIGHAPI_EXPORT
136   void doNotDumpFeature(const FeaturePtr& theFeature)
137   { myFeaturesToSkip.insert(theFeature); }
138
139   /// Dump sub-feature name and color, without dumping feature creation.
140   /// Used for features which creates sub-features in their execute method.
141   /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
142   /// \param theSubFeature    [in] sub-feature
143   MODELHIGHAPI_EXPORT
144   void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
145                                   const FeaturePtr& theSubFeature);
146
147   /// Return name of getter for corresponding attribute
148   virtual std::string attributeGetter(const FeaturePtr& theFeature,
149                                       const std::string& theAttrName) const = 0;
150
151   /// Return name of wrapper feature
152   virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
153
154   /// Save all dumps into specified file
155   MODELHIGHAPI_EXPORT
156   bool exportTo(const std::string& theFileName);
157
158   /// Dump character
159   MODELHIGHAPI_EXPORT
160   ModelHighAPI_Dumper& operator<<(const char theChar);
161   /// Dump string
162   MODELHIGHAPI_EXPORT
163   ModelHighAPI_Dumper& operator<<(const char* theString);
164   /// Dump string
165   MODELHIGHAPI_EXPORT
166   ModelHighAPI_Dumper& operator<<(const std::string& theString);
167   /// Dump boolean
168   MODELHIGHAPI_EXPORT
169   ModelHighAPI_Dumper& operator<<(const bool theValue);
170   /// Dump integer
171   MODELHIGHAPI_EXPORT
172   ModelHighAPI_Dumper& operator<<(const int theValue);
173   /// Dump real
174   MODELHIGHAPI_EXPORT
175   ModelHighAPI_Dumper& operator<<(const double theValue);
176   /// Dump std::endl
177   friend
178   MODELHIGHAPI_EXPORT
179   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
180                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
181
182   /// Dump GeomAPI_Pnt in the following form:
183   /// "GeomAPI_Pnt(X, Y, Z)"
184   MODELHIGHAPI_EXPORT
185   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
186   /// Dump GeomAPI_Dir
187   /// "GeomAPI_Dir(X, Y, Z)"
188   MODELHIGHAPI_EXPORT
189   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
190
191   /// Dump GeomDataAPI_Dir in the following form:
192   /// "X, Y, Z"
193   MODELHIGHAPI_EXPORT
194   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
195   /// Dump GeomDataAPI_Point in the following form:
196   /// "X, Y, Z"
197   MODELHIGHAPI_EXPORT
198   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
199   /// Dump GeomDataAPI_Point2D in the following form:
200   /// "X, Y"
201   MODELHIGHAPI_EXPORT
202   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
203
204   /// Dump AttributeBoolean
205   MODELHIGHAPI_EXPORT
206   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
207   /// Dump AttributeInteger
208   MODELHIGHAPI_EXPORT
209   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
210   /// Dump AttributeDouble
211   MODELHIGHAPI_EXPORT
212   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
213   /// Dump AttributeString
214   MODELHIGHAPI_EXPORT
215   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
216   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
217   MODELHIGHAPI_EXPORT
218   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
219
220   /// Dump folder
221   MODELHIGHAPI_EXPORT
222   ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
223
224   /// Dump result
225   MODELHIGHAPI_EXPORT
226   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
227
228   /// Dump Attribute
229   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
230     operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
231   /// Dump Object
232   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
233     operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
234
235   /// Dump AttributeRefAttr
236   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
237     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
238   /// Dump AttributeRefAttrList as follows:
239   /// "[obj1, obj2, obj3, ...]"
240   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
241     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
242   /// Dump AttributeRefList as follows:
243   /// "[obj1, obj2, obj3, ...]"
244   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
245     operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
246   /// Dump AttributeSelection
247   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
248     operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
249   /// Dump AttributeSelectionList
250   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
251     operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
252   /// Dump AttributeReference
253   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
254   operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
255   /// Dump AttributeStringArray
256   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
257     operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
258
259   /// Clear dump buffer
260   MODELHIGHAPI_EXPORT
261   void clear(bool bufferOnly = false);
262   /// clear list of not dumped entities
263   MODELHIGHAPI_EXPORT void clearNotDumped();
264
265   /// Check the entity is already dumped
266   MODELHIGHAPI_EXPORT
267   bool isDumped(const EntityPtr& theEntity) const;
268   /// Check theRefAttr is already dumped
269   MODELHIGHAPI_EXPORT
270   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
271   /// Check all objects in theRefList are already dumped
272   MODELHIGHAPI_EXPORT
273   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
274
275 protected:
276   /// Dump "setName" command if last entity had user-defined name
277   MODELHIGHAPI_EXPORT void dumpEntitySetName();
278
279 private:
280   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
281   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
282
283   /// Iterate all features in document and dump them into intermediate buffer
284   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
285
286   /// Dump composite feature and all it sub-features
287   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
288                bool isForce = false);
289
290   /// Iterate all features in composite feature and dump them into intermediate buffer
291   /// \param theComposite   [in] parent composite feature
292   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
293   MODELHIGHAPI_EXPORT
294   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
295                    bool theDumpModelDo = false);
296
297   /// Stores names of results for the given feature
298   void saveResultNames(const FeaturePtr& theFeature);
299
300   /// Check the result feature has default color
301   bool isDefaultColor(const ResultPtr& theResult) const;
302
303   /// Check the result feature has default deflection
304   bool isDefaultDeflection(const ResultPtr& theResult) const;
305
306   /// Check the result feature has default transparency
307   bool isDefaultTransparency(const ResultPtr& theResult) const;
308
309   /// Dump postponed entities
310   void dumpPostponed(bool theDumpFolders = false);
311
312 private:
313   struct EntityName {
314     std::string myCurrentName; ///< default name of current feature
315     std::string myUserName;    ///< user-defined name
316     bool        myIsDefault;   ///< \c true if the name is default
317     bool        myIsDumped;    ///< shows that the names of the feature are already stored
318
319     EntityName() {}
320
321     EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
322       : myCurrentName(theCurName),
323         myUserName(theUserName),
324         myIsDefault(theDefault),
325         myIsDumped(false)
326     {}
327   };
328
329   typedef std::map<EntityPtr, EntityName>                                      EntityNameMap;
330   typedef std::set<std::string>                                                ModulesSet;
331   typedef std::map<DocumentPtr, std::map<std::string, std::pair<int, int> > >  NbFeaturesMap;
332
333   struct LastDumpedEntity {
334     EntityPtr            myEntity;   ///< last dumped entity
335     bool                 myUserName; ///< the entity hase user-defined name
336     /// results of this entity, which has user-defined names or specific colors
337     std::list<ResultPtr> myResults;
338
339     LastDumpedEntity(EntityPtr theEntity, bool theUserName,
340       const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
341       : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
342     {}
343   };
344   typedef std::stack<LastDumpedEntity>                              DumpStack;
345
346   static ModelHighAPI_Dumper* mySelf;
347
348   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
349   std::ostringstream  myFullDump;           ///< full buffer of dumped data
350
351   ModulesSet          myModules;            ///< modules and entities to be imported
352   EntityNameMap       myNames;              ///< names of the entities
353   DumpStack           myEntitiesStack;      ///< stack of dumped entities
354
355   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
356
357   /// features which should not be dumped (like coincidence and tangency created by tangent arc)
358   std::set<FeaturePtr> myFeaturesToSkip;
359
360   std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
361   bool myDumpPostponedInProgress; ///< processing postponed is in progress
362
363   bool myGeometricalSelection; ///< dump selection not by naming, but by coordinates of inner point
364   bool myWeakNamingSelection; ///< dump selection by weak naming
365
366 protected:
367   /// list of entities, used by other features but not dumped yet
368   std::set<EntityPtr> myNotDumpedEntities;
369
370   friend class SketchAPI_Sketch;
371   friend class ModelHighAPI_Folder;
372 };
373
374 #endif