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