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