Salome HOME
Merge commit 'refs/tags/V9_2_0^{}'
[modules/shaper.git] / src / Selector / Selector_Algo.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 Selector_Algo_H_
22 #define Selector_Algo_H_
23
24 #include "Selector.h"
25
26 #include <Standard_GUID.hxx>
27 #include <TDF_Label.hxx>
28 #include <TDF_LabelList.hxx>
29 #include <TopoDS_Shape.hxx>
30
31 class Selector_NameGenerator;
32
33 /**\class Selector_Selector
34  * \ingroup DataModel
35  * \brief Base class for all kinds of selection algorithms.
36  */
37 class Selector_Algo
38 {
39   TopAbs_ShapeEnum myShapeType; ///< type of this shape
40
41   TDF_Label myLab; ///< label where this also may be located
42   TDF_Label myBaseDocumentLab; ///< an access-label of the document that may contain initial shapes
43   bool myGeometricalNaming; ///< flag that indicates that geometrical naming selection is enabled
44   bool myAlwaysGeometricalNaming; ///< to enable geometrical naming from beginning, at select
45   bool myUseNeighbors; ///< to use neighbors algorithms
46   bool myUseIntersections; ///< to use intersections algorithms
47
48 public:
49   /// Type of a selector algorithm: on this type depends what is stored in this label and how to
50   /// restore it on update.
51   enum Selector_Type {
52     SELTYPE_CONTAINER, ///< just a container of sub-elements, keeps the shape type of container
53     SELTYPE_INTERSECT, ///< sub-shape is intersection of higher level objects
54     SELTYPE_PRIMITIVE, ///< sub-shape found as a primitive on some label
55     SELTYPE_MODIFICATION, ///< modification of base shapes to the final label
56     SELTYPE_FILTER_BY_NEIGHBOR,  ///< identification by neighbor shapes in context
57     SELTYPE_WEAK_NAMING, ///< pure weak naming by weak index in context
58   };
59
60   /// Initializes the algorithm
61   SELECTOR_EXPORT Selector_Algo();
62
63   /// Initializes the selector structure on the label.
64   /// Stores the name data to restore after modification.
65   /// \param theContext whole shape that contains the selected sub-shape
66   /// \param theValue selected subshape
67   /// \param theGeometricalNaming treats selection with equal surfaces as one
68   /// \param theUseNeighbors enables searching algorithm by neighbors
69   /// \param theUseIntersections enables searching algorithm by intersection of higher level shapes
70   SELECTOR_EXPORT static Selector_Algo* select(
71     const TopoDS_Shape theContext, const TopoDS_Shape theValue,
72     const TDF_Label theAccess, const TDF_Label theBaseDocument,
73     const bool theGeometricalNaming = false,
74     const bool theUseNeighbors = true, const bool theUseIntersections = true,
75     const bool theAlwaysGeometricalNaming = false);
76
77   /// Stores the name to the label and sub-labels tree
78   SELECTOR_EXPORT virtual void store() = 0;
79
80   /// Restores the selected shape by the topological naming kept in the data structure
81   /// Returns true if it can restore structure correctly
82   SELECTOR_EXPORT virtual bool restore() = 0;
83
84   /// Restores the selected shape by the topological name string.
85   /// Returns not empty label of the context.
86   SELECTOR_EXPORT virtual TDF_Label restoreByName(std::string theName,
87     const TopAbs_ShapeEnum theShapeType, Selector_NameGenerator* theNameGenerator) = 0;
88
89   /// Updates the current shape by the stored topological name
90   SELECTOR_EXPORT virtual bool solve(const TopoDS_Shape& theContext) = 0;
91
92   /// Returns the naming name of the selection
93   SELECTOR_EXPORT virtual std::string name(Selector_NameGenerator* theNameGenerator) = 0;
94   /// Returns the current sub-shape value (null if can not resolve)
95   SELECTOR_EXPORT TopoDS_Shape value();
96   /// Restores sub-algorithm of a given type by the storage-label
97   SELECTOR_EXPORT static Selector_Algo* restoreByLab(TDF_Label theLab, TDF_Label theBaseDocLab);
98   /// Restores the selected sub-algorithm by the naming name.
99   /// Returns not empty label of the context.
100   SELECTOR_EXPORT static Selector_Algo* restoreByName(
101     TDF_Label theLab, TDF_Label theBaseDocLab, std::string theName,
102     const TopAbs_ShapeEnum theShapeType, const bool theGeomNaming,
103     Selector_NameGenerator* theNameGenerator, TDF_Label& theContextLab);
104
105   /// Returns true if the given shapes are based on the same geometry
106   static bool sameGeometry(const TopoDS_Shape theShape1, const TopoDS_Shape theShape2);
107
108   /// Creates a new selection algorithm for selection of all topology based on the same geometry
109   SELECTOR_EXPORT static Selector_Algo* relesectWithAllGeometry(
110     Selector_Algo* theOldAlgo, const TopoDS_Shape theContext);
111   /// Sets geometrical naming flag to true
112   void setGeometricalNaming()
113   {myGeometricalNaming = true;}
114
115 protected:
116   /// Returns label where this algorithm is attached to, or just an access label to the document
117   const TDF_Label& label() const
118   {return myLab;}
119   /// Stores the array of references to theLab: references to elements of ref-list, then the last
120   void storeBaseArray(const TDF_LabelList& theRef, const TDF_Label& theLast);
121   /// Restores references to the labels: references to elements of ref-list, then the last
122   bool restoreBaseArray(TDF_LabelList& theRef, TDF_Label& theLast);
123   /// Stores result of selection at the given label
124   void store(const TopoDS_Shape theShape);
125   /// Returns an access-label of the document that may contain initial shapes
126   const TDF_Label& baseDocument() const
127   {return myBaseDocumentLab;}
128   /// Returns the geometrical naming flag
129   bool geometricalNaming() const
130   {return myGeometricalNaming;}
131   /// Returns always geometrical naming flag
132   bool alwaysGeometricalNaming() const
133   {return myAlwaysGeometricalNaming;}
134   /// Returns use neighbors flag
135   bool useNeighbors() const
136   {return myUseNeighbors;}
137   /// Returns use intersections flag
138   bool useIntersections() const
139   {return myUseIntersections;}
140   /// Returns GUID for the weak index (integer attribute) of the sub-shape
141   static const Standard_GUID& weakID()
142   {
143     static const Standard_GUID kWEAK_INDEX("e9373a61-cabc-4ee8-aabf-aea47c62ed87");
144     return kWEAK_INDEX;
145   }
146   /// Returns GUID for the type of the shape, stored in case it is intersection or container
147   static const Standard_GUID& shapeTypeID()
148   {
149     static const Standard_GUID kSHAPE_TYPE("864b3267-cb9d-4107-bf58-c3ce1775b171");
150     return kSHAPE_TYPE;
151   }
152   /// string identifier of the weak name in modification or intersection types of algorithm
153   static const std::string& weakNameID()
154   {
155     static const std::string kWEAK_NAME_IDENTIFIER = "weak_name_";
156     return kWEAK_NAME_IDENTIFIER;
157   }
158   /// string identifier of the pure weak name
159   static const std::string& pureWeakNameID()
160   {
161     static const std::string kPURE_WEAK_NAME_IDENTIFIER = "_weak_name_";
162     return kPURE_WEAK_NAME_IDENTIFIER;
163   }
164   /// Stores the type of an algorithm in the data tree (in myLab)
165   void storeType(const Selector_Type theType);
166
167   /// Searches the newer version of the shape in the document if the base shape does not
168   /// belong to context. Returns it in theResult (if any). Returns true is theResult is changed.
169   bool findNewVersion(const TopoDS_Shape& theContext, TopoDS_Shape& theResult) const;
170 };
171
172 #endif