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