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