Salome HOME
Refactoring of the Selector package: split selection algorithms to separated classes.
[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 Selector_Algo* restoreByLab(TDF_Label theLab);
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, Selector_NameGenerator* theNameGenerator,
103     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
112 protected:
113   /// Returns label where this algorithm is attached to, or just an access label to the document
114   const TDF_Label& label() const
115   {return myLab;}
116   /// Stores the array of references to theLab: references to elements of ref-list, then the last
117   void storeBaseArray(const TDF_LabelList& theRef, const TDF_Label& theLast);
118   /// Restores references to the labels: references to elements of ref-list, then the last
119   bool restoreBaseArray(TDF_LabelList& theRef, TDF_Label& theLast);
120   /// Stores result of selection at the given label
121   void store(const TopoDS_Shape theShape);
122   /// Returns an access-label of the document that may contain initial shapes
123   const TDF_Label& baseDocument() const
124   {return myBaseDocumentLab;}
125   /// Returns the geometrical naming flag
126   bool geometricalNaming() const
127   {return myGeometricalNaming;}
128   /// Returns always geometrical naming flag
129   bool alwaysGeometricalNaming() const
130   {return myAlwaysGeometricalNaming;}
131   /// Returns use neighbors flag
132   bool useNeighbors() const
133   {return myUseNeighbors;}
134   /// Returns use intersections flag
135   bool useIntersections() const
136   {return myUseIntersections;}
137   /// Returns GUID for the weak index (integer attribute) of the sub-shape
138   static const Standard_GUID& weakID()
139   {
140     static const Standard_GUID kWEAK_INDEX("e9373a61-cabc-4ee8-aabf-aea47c62ed87");
141     return kWEAK_INDEX;
142   }
143   /// Returns GUID for the type of the shape, stored in case it is intersection or container
144   static const Standard_GUID& shapeTypeID()
145   {
146     static const Standard_GUID kSHAPE_TYPE("864b3267-cb9d-4107-bf58-c3ce1775b171");
147     return kSHAPE_TYPE;
148   }
149   /// string identifier of the weak name in modification or intersection types of algorithm
150   static const std::string& weakNameID()
151   {
152     static const std::string kWEAK_NAME_IDENTIFIER = "weak_name_";
153     return kWEAK_NAME_IDENTIFIER;
154   }
155   /// string identifier of the pure weak name
156   static const std::string& pureWeakNameID()
157   {
158     static const std::string kPURE_WEAK_NAME_IDENTIFIER = "_weak_name_";
159     return kPURE_WEAK_NAME_IDENTIFIER;
160   }
161   /// Stores the type of an algorithm in the data tree (in myLab)
162   void storeType(const Selector_Type theType);
163 };
164
165 #endif