]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Document.h
Salome HOME
Moving features to a folder has been implemented (Task 2.3. Ability to put consecutiv...
[modules/shaper.git] / src / Model / Model_Document.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 Model_Document_H_
22 #define Model_Document_H_
23
24 #include <Model.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultParameter.h>
29
30 #include <TDocStd_Document.hxx>
31 #include <map>
32 #include <set>
33
34 class Handle_Model_Document;
35 class Model_Objects;
36 class ModelAPI_AttributeSelectionList;
37
38 /**\class Model_Document
39  * \ingroup DataModel
40  * \brief Document for internal data structure of any object storage.
41  * Document contains all data that must be stored/retrived in the file.
42  * Also it provides acces to this data: open/save, transactions management etc.
43  */
44 class Model_Document : public ModelAPI_Document
45 {
46  public:
47   //! Returns the kind of the document: "PartSet", "Part", or something else.
48   //! This kind is used for feature buttons enable/disable depending on active document
49   //! (it uses workbench "document" identifier in XML configuration file for this)
50   MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
51
52   //! Loads the OCAF document from the file.
53   //! \param theDirName directory of the loaded file
54   //! \param theFileName a name of the file to load
55   //! \param theThis the common shared pointer to the document to manage with it later
56   //! \returns true if file was loaded successfully
57   MODEL_EXPORT virtual bool load(
58     const char* theDirName, const char* theFileName, DocumentPtr theThis);
59
60   //! Saves the OCAF document to the file.
61   //! \param theDirName directory where the document will be saved
62   //! \param theFileName a name of the document file to store
63   //! \param theResults the result full file names that were stored by "save"
64   //! \returns true if file was stored successfully
65   MODEL_EXPORT virtual bool save(
66     const char* theDirName, const char* theFileName, std::list<std::string>& theResults);
67
68   //! Removes document data
69   //! \param theForever if it is false, document is just hiden
70   //!                   (to keep possibility make it back on Undo/Redo)
71   MODEL_EXPORT virtual void close(const bool theForever = false);
72
73   //! Starts a new operation (opens a tansaction)
74   MODEL_EXPORT virtual void startOperation();
75   //! Finishes the previously started operation (closes the transaction)
76   //! \returns true if transaction in this document is not empty and really was performed
77   MODEL_EXPORT virtual bool finishOperation();
78   //! Aborts the operation
79   MODEL_EXPORT virtual void abortOperation();
80   //! Returns true if operation has been started, but not yet finished or aborted
81   MODEL_EXPORT virtual bool isOperation() const;
82   //! Returns true if document was modified (since creation/opening)
83   MODEL_EXPORT virtual bool isModified();
84
85   //! Returns True if there are available Undos
86   MODEL_EXPORT virtual bool canUndo();
87   //! Undoes last operation
88   MODEL_EXPORT virtual void undo();
89   //! Returns True if there are available Redos
90   MODEL_EXPORT virtual bool canRedo();
91   //! Redoes last operation
92   MODEL_EXPORT virtual void redo();
93
94   //! Adds to the document the new feature of the given feature id
95   //! \param theID creates feature and puts it in the document
96   //! \param theMakeCurrent to make current this new feature in this document
97   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID, const bool theMakeCurrent = true);
98
99   //! Return a list of features, which refers to the feature
100   //! \param theFeature a feature
101   //! \param theRefs a list of reference features
102   //! \param isSendError a flag whether the error message should be send
103   MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
104                                           std::set<FeaturePtr>& theRefs,
105                                           const bool isSendError = true);
106
107   //! Removes the feature from the document (with result)
108   //! It is necessary to flush REDISPLAY signal manually after this method because
109   //! the method sends it, but for the performance purpose does not flush it
110   //! \param theFeature a removed feature
111   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
112
113   //! Moves the feature to make it after the given one in the history.
114   MODEL_EXPORT virtual void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
115
116   //! Returns the first found object in the group by the object name
117   //! \param theGroupID group that contains an object
118   //! \param theName name of the object to search
119   //! \returns null if such object is not found
120   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> objectByName(
121     const std::string& theGroupID, const std::string& theName);
122
123   //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
124   //! \param theObject object of this document
125   //! \returns index started from zero, or -1 if object is invisible or belongs to another document
126   MODEL_EXPORT virtual const int index(std::shared_ptr<ModelAPI_Object> theObject);
127
128   //! Internal sub-document by ID
129   MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(int theDocID);
130
131   ///! Returns the id of the document
132   MODEL_EXPORT virtual const int id() const
133   {
134     return myID;
135   }
136
137   //! Returns the feature in the group by the index (started from zero)
138   //! \param theGroupID group that contains a feature
139   //! \param theIndex zero-based index of feature in the group
140   //! \param theAllowFolder take into account grouping feature by folders
141   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID,
142                                         const int theIndex,
143                                         const bool theAllowFolder = false);
144
145   //! Returns the number of features in the group
146   //! \param theGroupID group of objects
147   //! \param theAllowFolder take into account grouping feature by folders
148   MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theAllowFolder = false);
149
150   //! Returns the feature that is currently edited in this document, normally
151   //! this is the latest created feature
152   //! \param theVisible use visible features only: flag is true for Object Browser functionality
153   //! \returns null if next created feature must be the first
154   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> currentFeature(const bool theVisible);
155
156   //! Sets the current feature: all features below will be disabled, new features
157   //! will be appended after this one.
158   //! \param theCurrent the selected feature as current: below it everything becomes disabled
159   //! \param theVisible use visible features only: flag is true for Object Browser functionality
160   MODEL_EXPORT virtual void setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent,
161     const bool theVisible);
162
163   //! Makes the current feature one feature upper
164   MODEL_EXPORT virtual void setCurrentFeatureUp();
165
166   //! Returns the number of all features: in the history or not
167   MODEL_EXPORT virtual int numInternalFeatures();
168   //! Returns the feature by zero-based index: features in the history or not
169   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
170   //! Performs synchronization of transactions with the module document:
171   //! If some document is not active (by undo of activation) but in memory,
172   //! on activation the transactions must be synchronised because all redos performed
173   //! wihtout this participation
174   MODEL_EXPORT virtual void synchronizeTransactions();
175
176   //! Returns feature by the id of the feature (produced by the Data "featureId" method)
177   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> featureById(const int theId);
178
179
180   /// Creates a construction cresults
181   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
182       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
183   /// Creates a body results
184   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
185       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
186   /// Creates a part results
187   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
188       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
189   //! Copies a part result, keeping the reference to origin
190   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> copyPart(
191     const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
192     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
193   /// Creates a group result
194   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
195       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
196   /// Creates a field result
197   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultField> createField(
198       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
199   /// Creates a parameter result
200   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultParameter> createParameter(
201       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
202
203   //! Returns a feature by result (owner of result)
204   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
205     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
206
207   //! Creates a folder (group of the features in the object browser)
208   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> addFolder(
209       std::shared_ptr<ModelAPI_Feature> theAddBefore = std::shared_ptr<ModelAPI_Feature>());
210   //! Removes the folder from the document (all features in the folder will be kept).
211   MODEL_EXPORT virtual void removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder);
212   //! Search a folder above the list of features applicable to store them
213   //! (it means the list of features stored in the folder should be consequential)
214   //! \return Empty pointer if there is no applicable folder
215   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> findFolderAbove(
216       const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures);
217   //! Search a folder below the list of features applicable to store them
218   //! (it means the list of features stored in the folder should be consequential)
219   //! \return Empty pointer if there is no applicable folder
220   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> findFolderBelow(
221       const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures);
222   //! Add a list of features to the folder. The correctness of the adding is not performed
223   //! (such checks have been done in corresponding find.. method).
224   //! \return \c true if the movement is successfull
225   MODEL_EXPORT virtual bool moveToFolder(
226       const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
227       const std::shared_ptr<ModelAPI_Folder>& theFolder);
228   //! Remove features from the folder
229   //! \return \c true if the features have been moved out
230   MODEL_EXPORT virtual bool removeFromFolder(
231       const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures);
232
233   ///! Returns true if parametric updater need to execute feature on recomputartion
234   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
235   bool& executeFeatures() {return myExecuteFeatures;}
236
237   //! Registers the name of the shape for the topological naming needs
238   void addNamingName(const TDF_Label theLabel, std::string theName);
239   //! Updates the name of some object
240   void changeNamingName(std::string theOldName, const std::string theNewName,
241     const TDF_Label& theLabel);
242   //! Returns the label, keeper of the name  for the topological naming needs
243   TDF_Label findNamingName(std::string theName, ResultPtr theContext);
244   //! Returns the number of the name in the history relatively to the given object (by label).
245   //! Start from 1 (this object).
246   int numberOfNameInHistory(const ObjectPtr& theNameObject, const TDF_Label& theStartFrom);
247   //! Returns the result by name of the result (names of results must be unique, used for naming
248   //! selection by name.
249   ResultPtr findByName(std::string& theName, std::string& theSubShapeName, bool& theUniqueContext);
250
251   ///! Returns all features of the document including the hidden features which are not in
252   ///! history. Not very fast method, for calling once, not in big cycles.
253   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
254
255   //! Returns all objects of the document including the hidden features which are not in
256   //! history. Not very fast method, for calling once, not in big cycles.
257   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Object> > allObjects();
258
259   /// Returns the global identifier of the current transaction (needed for the update algo)
260   MODEL_EXPORT virtual int transactionID();
261   /// Increases the transaction ID
262   MODEL_EXPORT virtual void incrementTransactionID();
263   /// Decreases the transaction ID
264   MODEL_EXPORT virtual void decrementTransactionID();
265
266   /// Returns true if document is opened and valid
267   MODEL_EXPORT virtual bool isOpened();
268
269   /// Returns the last feature in the document (even not visible or disabled)
270   /// \returns null if there is no features
271   FeaturePtr lastFeature();
272
273   /// Returns the feature that produced the given face of the given result.
274   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> producedByFeature(
275     std::shared_ptr<ModelAPI_Result> theResult,
276     const std::shared_ptr<GeomAPI_Shape>& theShape);
277
278   /// Returns true if theLater is in history of features creation later than theCurrent
279   MODEL_EXPORT virtual bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
280
281   /// Just removes all features without touching the document data (to be able undo)
282   MODEL_EXPORT virtual void eraseAllFeatures();
283
284
285  protected:
286   //! Returns (creates if needed) the general label
287   TDF_Label generalLabel() const;
288
289   //! Creates new document with binary file format
290   Model_Document(const int theID, const std::string theKind);
291
292   //! Returns the internal OCCT document of this interface
293   Handle_TDocStd_Document document()
294   {
295     return myDoc;
296   }
297
298   //! performs compactification of all nested operations into one
299   //! \returns true if resulting transaction is not empty and can be undoed
300   void compactNested();
301
302   //! Returns all loaded sub documents
303   const std::set<int> subDocuments() const;
304
305   //! The implementation of undo: with or without recoursive calls in the sub-documents
306   void undoInternal(const bool theWithSubs, const bool theSynchronize);
307
308   //! Stores the Id of the current operation (normally is called for the root document)
309   void operationId(const std::string& theId);
310
311   //! Returns the list of Ids of the operations that can be undoed (called for the root document)
312   std::list<std::string> undoList() const;
313
314   //! Returns the list of Ids of the operations that can be redoed (called for the root document)
315   std::list<std::string> redoList() const;
316
317   //! Internally makes document know that feature was removed or added in history after creation
318   virtual void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
319   //! Internally makes document know that feature was removed or added in history after creation
320   virtual void updateHistory(const std::string theGroup);
321
322   //! Returns true if the document is root module document
323   bool isRoot() const;
324
325   //! Sets shared pointer to this
326   void setThis(DocumentPtr theDoc);
327
328   //! Returns the objects manager
329   Model_Objects* objects() {return myObjs;}
330
331   ///! Informs the document that it becomes active and some actions must be performed
332   virtual void setActive(const bool theFlag);
333   //! Returns true if this document is currently active
334   virtual bool isActive() const;
335
336   //! Returns the selection attribute that is used
337   //! for calculation of selection externally from the document
338   std::shared_ptr<ModelAPI_AttributeSelectionList> selectionInPartFeature();
339
340   /// Stores in the document boolean flags: states of the nodes in the object browser.
341   /// Normally is called outside of the transaction, just before "save".
342   virtual void storeNodesState(const std::list<bool>& theStates);
343
344   /// Returns the stored nodes states. Normally it is calles just after "open".
345   /// Appends the values to theStates list.
346   virtual void restoreNodesState(std::list<bool>& theStates) const;
347
348   /// Label that constains structures for selection of constructions of another document
349   TDF_Label extConstructionsLabel() const;
350
351   /// searches in this document feature that contains this label
352   FeaturePtr featureByLab(const TDF_Label& theLab);
353
354   /// returns true if theThis is later in the features trre and dependencies than theOther
355   bool isLaterByDep(FeaturePtr theThis, FeaturePtr theOther);
356
357   friend class Model_Application;
358   friend class Model_Session;
359   friend class Model_Update;
360   friend class Model_AttributeReference;
361   friend class Model_AttributeRefAttr;
362   friend class Model_AttributeRefList;
363   friend class Model_AttributeRefAttrList;
364   friend class Model_AttributeSelection;
365   friend class Model_ResultPart;
366   friend class Model_ResultCompSolid;
367   friend class Model_ResultConstruction;
368   friend class Model_SelectionNaming;
369   friend class DFBrowser;
370
371  private:
372   int myID;  ///< identifier of the document in the application
373   std::string myKind;  ///< kind of the document in the application
374   Handle_TDocStd_Document myDoc;  ///< OCAF document
375
376   Model_Objects *myObjs; ///< data manager of this document
377
378   //! counter value of transaction on the last "save" call, used for "IsModified" method
379   int myTransactionSave;
380   //! number of nested transactions performed (list becasue may be nested inside of nested)
381   //! the list is empty if not nested transaction is performed
382   std::list<int> myNestedNum;
383
384   //! Information related to the every user-transaction
385   struct Transaction {
386     int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
387     std::string myId; ///< user-identifier string of transaction
388     //! default constructor with default Id
389     Transaction(): myOCAFNum(0), myId("") {}
390   };
391
392   //! transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
393   //! in myDocument connected to this operation (may be zero for empty transaction)
394   std::list<Transaction> myTransactions;
395   //! list of info about transactions undone (first is oldest undone)
396   std::list<Transaction> myRedos;
397
398   //! Optimization for finding the shape-label by topological naming names
399   //! The name -> list of labels where this name is appeared (the last created name is last here)
400   std::map<std::string, std::list<TDF_Label> > myNamingNames;
401   //! If it is true, features are not executed on update (on abort, undo, redo)
402   bool myExecuteFeatures;
403
404   bool myIsActive; ///< flag that stores the active/not active state
405
406   //! The selection feature, if needed
407   FeaturePtr mySelectionFeature;
408 };
409
410 #endif