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