0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[occt.git] / src / BRepTools / BRepTools_ReShape.hxx
1 // Created on: 1998-06-03
2 // Created by: data exchange team
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _BRepTools_ReShape_HeaderFile
18 #define _BRepTools_ReShape_HeaderFile
19
20 #include <BRepTools_History.hxx>
21
22 #include <TopTools_DataMapOfShapeShape.hxx>
23 #include <TopTools_MapOfShape.hxx>
24 #include <Standard_Integer.hxx>
25 #include <Standard_Boolean.hxx>
26 #include <Standard_Transient.hxx>
27 #include <TopAbs_ShapeEnum.hxx>
28
29 class TopoDS_Shape;
30 class TopoDS_Vertex;
31
32 // resolve name collisions with X11 headers
33 #ifdef Status
34   #undef Status
35 #endif
36
37 class BRepTools_ReShape;
38 DEFINE_STANDARD_HANDLE(BRepTools_ReShape, Standard_Transient)
39
40 //! Rebuilds a Shape by making pre-defined substitutions on some
41 //! of its components
42 //!
43 //! In a first phase, it records requests to replace or remove
44 //! some individual shapes
45 //! For each shape, the last given request is recorded
46 //! Requests may be applied "Oriented" (i.e. only to an item with
47 //! the SAME orientation) or not (the orientation of replacing
48 //! shape is respectful of that of the original one)
49 //!
50 //! Then, these requests may be applied to any shape which may
51 //! contain one or more of these individual shapes
52 //!
53 //! Supports the 'BRepTools_History' history by method 'History'.
54 class BRepTools_ReShape : public Standard_Transient
55 {
56 public:
57   //! Returns an empty Reshape
58   Standard_EXPORT BRepTools_ReShape();
59   
60   //! Clears all substitutions requests
61   Standard_EXPORT virtual void Clear();
62   
63   //! Sets a request to Remove a Shape whatever the orientation
64   Standard_EXPORT virtual void Remove (const TopoDS_Shape& shape);
65   
66   //! Sets a request to Replace a Shape by a new one.
67   virtual void Replace (const TopoDS_Shape& shape, const TopoDS_Shape& newshape)
68   {
69     replace (shape, newshape, TReplacementKind_Modify);
70   }
71   
72   //! Merges the parts to the single product.
73   //! The first part is replaced by the product.
74   //! The other parts are removed.
75   //! The history of the merged shapes is presented by equal ways.
76   template<typename TCollection> void Merge(
77     const TCollection& theParts, const TopoDS_Shape& theProduct)
78   {
79     typename TCollection::Iterator aPIt(theParts);
80
81     if (aPIt.More())
82     {
83       replace(aPIt.Value(), theProduct, TReplacementKind_Merge_Main);
84
85       aPIt.Next();
86     }
87
88     const TReplacementKind aKind = TReplacementKind_Merge_Ordinary;
89     for (; aPIt.More(); aPIt.Next())
90     {
91       replace(aPIt.Value(), theProduct, aKind);
92     }
93   }
94
95   //! Tells if a shape is recorded for Replace/Remove
96   Standard_EXPORT virtual Standard_Boolean IsRecorded (const TopoDS_Shape& shape) const;
97   
98   //! Returns the new value for an individual shape
99   //! If not recorded, returns the original shape itself
100   //! If to be Removed, returns a Null Shape
101   //! Else, returns the replacing item
102   Standard_EXPORT virtual TopoDS_Shape Value (const TopoDS_Shape& shape) const;
103   
104   //! Returns a complete substitution status for a shape
105   //! 0  : not recorded,   <newsh> = original <shape>
106   //! < 0: to be removed,  <newsh> is NULL
107   //! > 0: to be replaced, <newsh> is a new item
108   //! If <last> is False, returns status and new shape recorded in
109   //! the map directly for the shape, if True and status > 0 then
110   //! recursively searches for the last status and new shape.
111   Standard_EXPORT virtual Standard_Integer Status (const TopoDS_Shape& shape, TopoDS_Shape& newsh, const Standard_Boolean last = Standard_False);
112
113   //! Applies the substitutions requests to a shape.
114   //!
115   //! <until> gives the level of type until which requests are taken
116   //! into account. For subshapes of the type <until> no rebuild
117   //! and futher exploring are done.
118   //!
119   //! NOTE: each subshape can be replaced by shape of the same type
120   //! or by shape containing only shapes of that type (for
121   //! example, TopoDS_Edge can be replaced by TopoDS_Edge,
122   //! TopoDS_Wire or TopoDS_Compound containing TopoDS_Edges).
123   //! If incompatible shape type is encountered, it is ignored
124   //! and flag FAIL1 is set in Status.
125   Standard_EXPORT virtual TopoDS_Shape Apply (const TopoDS_Shape& shape, const TopAbs_ShapeEnum until = TopAbs_SHAPE);
126
127   //! Returns (modifiable) the flag which defines whether Location of shape take into account
128   //! during replacing shapes.
129   virtual Standard_Boolean& ModeConsiderLocation()
130   {
131     return myConsiderLocation;
132   }
133
134   //! Returns modified copy of vertex if original one is not recorded or returns modified original vertex otherwise.
135   //@param theV - original vertex.
136   //@param theTol - new tolerance of vertex, optional.
137   Standard_EXPORT TopoDS_Vertex CopyVertex(const TopoDS_Vertex& theV,
138                                            const Standard_Real theTol = -1.0);
139
140   //! Returns modified copy of vertex if original one is not recorded or returns modified original vertex otherwise.
141   //@param theV - original vertex.
142   //@param theNewPos - new position for vertex copy.
143   //@param theTol - new tolerance of vertex.
144   Standard_EXPORT TopoDS_Vertex CopyVertex(const TopoDS_Vertex& theV,
145                                            const gp_Pnt& theNewPos,
146                                            const Standard_Real aTol);
147
148   //! Checks if shape has been recorded by reshaper as a value
149   //@param theShape is the given shape
150   Standard_EXPORT Standard_Boolean IsNewShape(const TopoDS_Shape& theShape) const;
151
152   //! Returns the history of the substituted shapes.
153   Standard_EXPORT Handle(BRepTools_History) History() const;
154
155   DEFINE_STANDARD_RTTIEXT(BRepTools_ReShape,Standard_Transient)
156
157 protected:
158   //! The kinds of the replacements.
159   enum TReplacementKind
160   {
161     TReplacementKind_Remove = 1,
162     TReplacementKind_Modify = 2,
163     TReplacementKind_Merge_Main = 4,
164     TReplacementKind_Merge_Ordinary = 8
165   };
166
167   //! Replaces the first shape by the second one
168   //! after the following reorientation.
169   //!
170   //! If the first shape has the reversed orientation
171   //! then the both shapes are reversed.
172   //! If the first shape has the internal or external orientation then: <br>
173   //! - the second shape is oriented forward (reversed) if it's orientation
174   //!   is equal (not equal) to the orientation of the first shape; <br>
175   //! - the first shape is oriented forward.
176   Standard_EXPORT virtual void replace (
177     const TopoDS_Shape& shape,
178     const TopoDS_Shape& newshape,
179     const TReplacementKind theKind);
180
181 private:
182   //! Returns 'true' if the kind of a replacement is an ordinary merging.
183   static Standard_Boolean isOrdinaryMerged(const TReplacementKind theKind)
184   {
185     return (theKind == TReplacementKind_Merge_Ordinary);
186   }
187
188   //! A replacement of an initial shape.
189   struct TReplacement
190   {
191   public:
192     //! The default constructor.
193     TReplacement() : myKind(TReplacementKind_Remove)
194     {
195     }
196
197     //! The initializing constructor.
198     TReplacement(
199         const TopoDS_Shape& theResult, const TReplacementKind theKind) :
200       myResult(theResult), myKind(theKind)
201     {
202     }
203
204     //! Returns the result of the replacement.
205     TopoDS_Shape Result() const
206     {
207       return (myKind != TReplacementKind_Merge_Ordinary) ?
208         myResult : TopoDS_Shape();
209     }
210
211     //! Returns the result of the relation.
212     const TopoDS_Shape& RelationResult() const
213     {
214       return myResult;
215     }
216
217     //! Returns the kind of the relation
218     //! between an initial shape and the result of the replacement.
219     BRepTools_History::TRelationType RelationKind() const
220     {
221       return (myKind == TReplacementKind_Remove) ?
222         BRepTools_History::TRelationType_Removed :
223         BRepTools_History::TRelationType_Modified;
224     }
225
226   private:
227     TopoDS_Shape myResult; //!< The result of the replacement.
228     TReplacementKind myKind; //!< The kind of the replacement.
229   };
230
231   typedef NCollection_DataMap<TopoDS_Shape, TReplacement,
232     TopTools_ShapeMapHasher> TShapeToReplacement;
233
234 private:
235   //! Maps each shape to its replacement.
236   //! If a shape is not bound to the map then the shape is replaced by itself.
237   TShapeToReplacement myShapeToReplacement;
238
239 protected:
240   TopTools_MapOfShape myNewShapes;
241   Standard_Integer myStatus;
242
243
244 private:
245
246
247   Standard_Boolean myConsiderLocation;
248
249
250 };
251
252
253
254
255
256
257
258 #endif // _BRepTools_ReShape_HeaderFile