Salome HOME
Debug of move to the end of history
[modules/shaper.git] / src / Model / Model_AttributeSelection.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_AttributeSelection_H_
22 #define Model_AttributeSelection_H_
23
24 #include "Model.h"
25 #include "Model_AttributeReference.h"
26 #include <ModelAPI_AttributeSelection.h>
27 #include <Selector_NameGenerator.h>
28 #include <TDF_LabelMap.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopTools_ListOfShape.hxx>
31
32 class Model_AttributeSelectionList;
33 class Model_Document;
34
35 /**\class Model_AttributeSelection
36  * \ingroup DataModel
37  * \brief Attribute that contains reference to the sub-shape of some result, the selected shape.
38  */
39 class Model_AttributeSelection : public ModelAPI_AttributeSelection,
40                                  public Selector_NameGenerator
41 {
42   Model_AttributeReference myRef;  ///< The reference functionality reusage
43   /// temporarily storages to avoid keeping in the data structure if not needed
44   ResultPtr myTmpContext;
45   /// temporarily storages to avoid keeping in the data structure if not needed
46   std::shared_ptr<GeomAPI_Shape> myTmpSubShape;
47   /// temporarily storages to avoid keeping in the data structure if not needed
48   CenterType myTmpCenterType;
49   /// Reference to the partent attribute, if any (to split selection compounds in issue 1799)
50   Model_AttributeSelectionList* myParent;
51
52   std::shared_ptr<Model_Document> myRestoreDocument; // current document to restore by name
53 public:
54   /// Defines the result and its selected sub-shape
55   /// \param theContext object where the sub-shape was selected
56   /// \param theSubShape selected sub-shape (if null, the whole context is selected)
57   /// \param theTemporarily if it is true, do not store and name the added in the data framework
58   ///           (used to remove immideately, without the following updates)
59   /// \returns true if attribute was updated
60   MODEL_EXPORT virtual bool setValue(
61     const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
62     const bool theTemporarily = false);
63
64   /// Same as SetValue, but it takes an edge (on circular or elliptical curve)
65   /// and stores the vertex of the central point (for ellipse the first or the second focus point)
66   MODEL_EXPORT virtual void setValueCenter(
67     const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
68     const CenterType theCenterType,
69     const bool theTemporarily = false);
70
71   /// Makes this selection attribute selects the same as in theSource selection
72   MODEL_EXPORT virtual void selectValue(
73     const std::shared_ptr<ModelAPI_AttributeSelection>& theSource);
74
75   /// Reset temporary stored values
76   virtual void removeTemporaryValues();
77
78   /// Returns the selected subshape
79   MODEL_EXPORT virtual std::shared_ptr<GeomAPI_Shape> value();
80
81   /// Returns the context of the selection (the whole shape owner)
82   MODEL_EXPORT virtual ResultPtr context();
83
84   /// Returns the context of the selection if the whole feature was selected
85   MODEL_EXPORT virtual FeaturePtr contextFeature();
86   /// Returns the context of the selection : result or feature
87   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> contextObject();
88
89   /// Sets the feature object
90   MODEL_EXPORT virtual void setObject(const std::shared_ptr<ModelAPI_Object>& theObject);
91
92   /// Updates the underlied selection due to the changes in the referenced objects
93   /// \returns false if update is failed
94   MODEL_EXPORT virtual bool update();
95
96   /// Returns a textual string of the selection
97   /// \param theDefaultValue a name, which is returned if the naming name can not be obtained
98   MODEL_EXPORT virtual std::string namingName(const std::string& theDefaultValue = "");
99
100   /// Returns an Id of the selection
101   /// NOTE: This method has been added for temporary export of groups towards old GEOM
102   /// It should then be removed when a direct use of objects from SHAPER
103   /// will be possible from SMESH module of SALOME.
104   MODEL_EXPORT virtual int Id();
105
106   /// Defines the sub-shape by Id
107   /// NOTE: This method is opposite to Id() method.
108   MODEL_EXPORT virtual void setId(int theID);
109
110   /// Selects (i.e. creates Naming data structure) of sub-shape specifed by textual name
111   MODEL_EXPORT virtual void selectSubShape(const std::string& theType,
112                                            const std::string& theSubShapeName);
113
114   /// Selects sub-shape by its inner point
115   MODEL_EXPORT virtual void selectSubShape(const std::string& theType,
116                                            const std::shared_ptr<GeomAPI_Pnt>& thePoint);
117   /// Selects sub-shape by weak naming index
118   MODEL_EXPORT virtual void selectSubShape(const std::string& theType,
119     const std::string& theContextName, const int theIndex);
120
121   /// Returns true if attribute was  initialized by some value
122   MODEL_EXPORT virtual bool isInitialized();
123
124   /// Returns true if recomute of selection become impossible
125   MODEL_EXPORT virtual bool isInvalid();
126
127   /// Updates the arguments of selection if something was affected by creation
128   /// or reorder of features upper in the history line (issue #1757)
129   MODEL_EXPORT virtual void updateInHistory();
130
131   /// Returns true if the name was stored using weak naming principle
132   MODEL_EXPORT virtual bool isWeakNaming();
133
134   // Implementation of the name generator method from the Selector package
135   // This method returns the context name by the label of the sub-selected shape
136   MODEL_EXPORT virtual std::string contextName(const TDF_Label theSelectionLab) override;
137
138   /// This method restores by the context and value name the context label and
139   /// sub-label where the value is. Returns true if it is valid.
140   MODEL_EXPORT virtual bool restoreContext(std::string theName,
141     TDF_Label& theContext, TDF_Label& theValue) override;
142
143   /// Returns true if the first result is newer than the second one in the tree of features
144   MODEL_EXPORT virtual bool isLater(const TDF_Label theResult1, const TDF_Label theResult2)
145     const override;
146
147 protected:
148   /// Objects are created for features automatically
149   MODEL_EXPORT Model_AttributeSelection(TDF_Label& theLabel);
150
151   /// Returns the selected subshape, internal method that works without knowledge
152   /// about special selection of circle and ellipse focuses (for that the public value
153   /// method calls this and makes additional processing).
154   /// Returns theType type of the center, or NOT_CENTER if it is not.
155   std::shared_ptr<GeomAPI_Shape> internalValue(CenterType& theType);
156
157
158   /// Performs the selection for the body result (TNaming Selection)
159
160   /// Performs the selection for the body result (TNaming selection)
161   virtual void selectBody(
162     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape);
163
164   /// Performs the selection for the part result (selection by name of body result inside of part)
165   /// \param theContext the result - owner of the selection
166   /// \param theSubShape selected shape
167   /// \param theUpdate flag that shows that it must be just update, theShape is null
168   /// \returns true if eveything is selected correctly
169   virtual bool selectPart(
170     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
171     const bool theUpdate = false);
172
173   /// Returns the label where TNaming_Selection results are stored
174   /// Note: there must be no attributes stored at the same label because Selector clears this lab
175   TDF_Label selectionLabel();
176
177   /// Sets the ID of the attribute in Data (called from Data): here it is used for myRef ID setting
178   MODEL_EXPORT virtual void setID(const std::string theID);
179
180   /// Returns the name by context. Adds the part name if the context is located in other document
181   std::string contextName(const ResultPtr& theContext) const;
182
183   /// Sets the parent attribute
184   void setParent(Model_AttributeSelectionList* theParent);
185
186   /// Splits theNewShape into sub-shapes of theType type (for the list parent of this attribute)
187   void split(ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType);
188
189   /// When group position is updated, searches the new context and new values
190   bool searchNewContext(std::shared_ptr<Model_Document> theDoc, const TopoDS_Shape theContShape,
191                         ResultPtr theContext, TopoDS_Shape theValShape, TDF_Label theAccessLabel,
192                         std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes);
193
194   /// computes theShapes list - shapes that were generated/modified/deleted the theValShape
195   /// during creation from new to old context
196   void computeValues(ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
197     TopTools_ListOfShape& theShapes);
198
199   friend class Model_Data;
200   friend class Model_AttributeSelectionList;
201 };
202
203 #endif