b311480e |
1 | // Copyright (c) 1999-2012 OPEN CASCADE SAS |
2 | // |
3 | // The content of this file is subject to the Open CASCADE Technology Public |
4 | // License Version 6.5 (the "License"). You may not use the content of this file |
5 | // except in compliance with the License. Please obtain a copy of the License |
6 | // at http://www.opencascade.org and read it completely before using this file. |
7 | // |
8 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
9 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
10 | // |
11 | // The Original Code and all software distributed under the License is |
12 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
13 | // Initial Developer hereby disclaims all such warranties, including without |
14 | // limitation, any warranties of merchantability, fitness for a particular |
15 | // purpose or non-infringement. Please see the License for the specific terms |
16 | // and conditions governing the rights and limitations under the License. |
17 | |
7fd59977 |
18 | #include <ShapeUpgrade_RemoveLocations.ixx> |
19 | #include <TopAbs_ShapeEnum.hxx> |
20 | #include <TopLoc_Location.hxx> |
21 | #include <TopoDS_Face.hxx> |
22 | #include <TopoDS.hxx> |
23 | #include <Geom_Surface.hxx> |
24 | #include <BRep_Builder.hxx> |
25 | #include <TopoDS_Shape.hxx> |
26 | #include <TopoDS_Edge.hxx> |
27 | #include <TopoDS_Vertex.hxx> |
28 | #include <TopExp.hxx> |
29 | #include <Geom_Curve.hxx> |
30 | #include <BRep_Tool.hxx> |
31 | #include <gp_Pnt.hxx> |
32 | #include <TopoDS_Iterator.hxx> |
33 | #include <TopTools_DataMapOfShapeShape.hxx> |
34 | #include <Geom2d_Curve.hxx> |
35 | #include <Geom_Geometry.hxx> |
36 | #include <gp_Trsf.hxx> |
b311480e |
37 | |
7fd59977 |
38 | #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx> |
39 | #include <BRep_TEdge.hxx> |
40 | #include <BRep_GCurve.hxx> |
41 | #include <BRep_CurveRepresentation.hxx> |
42 | #include <TColStd_ListIteratorOfListOfTransient.hxx> |
43 | #include <TopExp_Explorer.hxx> |
44 | #include <TColStd_ListOfTransient.hxx> |
45 | //#include <ShapeUpgrade_DataMapOfShapeListOfTransient.hxx> |
46 | #include <ShapeBuild_Edge.hxx> |
47 | #include <Geom_Plane.hxx> |
b311480e |
48 | |
7fd59977 |
49 | //======================================================================= |
50 | //function : ShapeUpgrade_RemoveLocations |
51 | //purpose : |
52 | //======================================================================= |
53 | |
54 | ShapeUpgrade_RemoveLocations::ShapeUpgrade_RemoveLocations() |
55 | { |
56 | myLevelRemoving = TopAbs_SHAPE; |
57 | } |
58 | |
59 | //======================================================================= |
60 | //function : Remove |
61 | //purpose : |
62 | //======================================================================= |
63 | |
64 | Standard_Boolean ShapeUpgrade_RemoveLocations::Remove(const TopoDS_Shape& theShape) |
65 | { |
66 | TopoDS_Shape aShape = theShape; |
67 | myShape = aShape; |
68 | TopAbs_ShapeEnum shtype = theShape.ShapeType(); |
69 | Standard_Boolean isRemoveLoc = ((shtype != TopAbs_COMPOUND && myLevelRemoving == TopAbs_SHAPE) || |
70 | ((Standard_Integer)myLevelRemoving <= ((Standard_Integer)shtype))); |
71 | TopoDS_Shape S; |
72 | Standard_Boolean isDone = MakeNewShape(theShape,S,myShape,isRemoveLoc); |
73 | |
74 | return isDone; |
75 | |
76 | } |
77 | |
78 | //======================================================================= |
79 | //function : RebuildShape |
80 | //purpose : |
81 | //======================================================================= |
82 | static Standard_Boolean RebuildShape(const TopoDS_Face& theFace, TopoDS_Face& theNewFace) |
83 | { |
84 | BRep_Builder aB; |
85 | TopLoc_Location aLoc; |
86 | Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace,aLoc); |
87 | Standard_Boolean isRebuild = Standard_False; |
88 | if(!aLoc.IsIdentity()) { |
89 | Handle(Geom_Surface) anewSurf =Handle(Geom_Surface)::DownCast( aSurf->Transformed(aLoc.Transformation())); |
90 | aB.MakeFace(theNewFace,anewSurf,BRep_Tool::Tolerance(theFace)); |
91 | isRebuild = Standard_True; |
92 | } |
93 | return isRebuild; |
94 | } |
95 | //======================================================================= |
96 | //function : RebuildShape |
97 | //purpose : |
98 | //======================================================================= |
99 | static Standard_Boolean RebuildShape(const TopoDS_Edge& theEdge, TopoDS_Edge& theNewEdge, |
100 | const TopoDS_Face& theFace, TopoDS_Face& theNewFace, |
101 | Standard_Boolean isBound) |
102 | { |
103 | Standard_Boolean isRebuild = Standard_False; |
104 | BRep_Builder aB; |
105 | if(!isBound) { |
106 | Handle(Geom_Curve) C3d; |
107 | TopLoc_Location aLoc; |
108 | Standard_Real First3d,Last3d; |
109 | C3d = BRep_Tool::Curve( theEdge,aLoc,First3d,Last3d); |
110 | aB.MakeEdge(theNewEdge); |
111 | if(!C3d.IsNull()) { |
112 | if(!aLoc.IsIdentity()) { |
113 | Handle(Geom_Curve) anewC3d = Handle(Geom_Curve)::DownCast(C3d->Transformed(aLoc.Transformation())); |
114 | C3d = anewC3d; |
115 | } |
116 | |
117 | aB.UpdateEdge(theNewEdge,C3d,BRep_Tool::Tolerance(theEdge)); |
118 | aB.Range(theNewEdge,First3d,Last3d); |
119 | } |
120 | theNewEdge.Orientation(theEdge.Orientation()); |
121 | if(BRep_Tool::Degenerated(theEdge)) |
122 | aB.Degenerated(theNewEdge,Standard_True); |
123 | isRebuild = Standard_True; |
124 | } |
125 | if(!theFace.IsNull()) { |
126 | Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace); |
127 | if(!aSurf->IsKind(STANDARD_TYPE(Geom_Plane))) { |
128 | Handle(Geom2d_Curve) c2d,c2d1; |
129 | Standard_Real First2d,Last2d; |
130 | |
131 | c2d= BRep_Tool::CurveOnSurface( theEdge,theFace,First2d,Last2d); |
132 | if(BRep_Tool::IsClosed(theEdge,theFace)) { |
133 | if(!BRep_Tool::IsClosed(theNewEdge,theNewFace)) { |
134 | TopoDS_Edge tmpE = TopoDS::Edge(theEdge.Reversed()); |
135 | c2d1= BRep_Tool::CurveOnSurface(tmpE,theFace,First2d,Last2d); |
136 | TopAbs_Orientation OrEdge = theNewEdge.Orientation(); |
137 | |
982a90fc |
138 | if(theFace.Orientation() == TopAbs_REVERSED) |
7fd59977 |
139 | OrEdge = ( OrEdge == TopAbs_FORWARD ? TopAbs_REVERSED : TopAbs_FORWARD); |
140 | |
141 | if(OrEdge == TopAbs_FORWARD) |
142 | aB.UpdateEdge(theNewEdge,c2d, c2d1,theNewFace,0); |
143 | else aB.UpdateEdge(theNewEdge,c2d1, c2d,theNewFace,0); |
144 | |
145 | } |
146 | } |
147 | else |
148 | aB.UpdateEdge(theNewEdge,c2d,theNewFace,0); |
149 | |
150 | if(!c2d.IsNull() || !c2d1.IsNull()) |
151 | aB.Range(theNewEdge,theNewFace,First2d,Last2d); |
152 | } |
153 | } |
154 | return isRebuild; |
155 | } |
156 | //======================================================================= |
157 | //function : RebuildShape |
158 | //purpose : |
159 | //======================================================================= |
160 | static Standard_Boolean RebuildShape(const TopoDS_Vertex& theVertex, TopoDS_Vertex& theNewVertex) |
161 | { |
162 | BRep_Builder aB; |
163 | aB.MakeVertex(theNewVertex); |
164 | theNewVertex.Orientation( theVertex.Orientation()); |
165 | gp_Pnt p1 = BRep_Tool::Pnt( theVertex); |
166 | aB.UpdateVertex(theNewVertex,p1,BRep_Tool::Tolerance( theVertex)); |
167 | return Standard_True; |
168 | } |
169 | //======================================================================= |
170 | //function : MakeNewShape |
171 | //purpose : |
172 | //======================================================================= |
173 | |
174 | Standard_Boolean ShapeUpgrade_RemoveLocations::MakeNewShape(const TopoDS_Shape& theShape, |
175 | const TopoDS_Shape& theAncShape, |
176 | TopoDS_Shape& theNewShape, |
177 | const Standard_Boolean theRemoveLoc) |
178 | { |
179 | Standard_Boolean isDone = Standard_False; |
180 | TopoDS_Shape aNewShape; |
181 | TopAbs_ShapeEnum shtype = theShape.ShapeType(); |
182 | BRep_Builder aB; |
183 | TopoDS_Shape aShape = theShape; |
184 | if(!theRemoveLoc && !theShape.Location().IsIdentity()) { |
185 | TopLoc_Location nulLoc; |
186 | aShape.Location(nulLoc); |
187 | } |
188 | Standard_Boolean isBound = myMapNewShapes.IsBound(aShape); |
189 | if(isBound) { |
190 | aNewShape= myMapNewShapes.Find(aShape); |
191 | aNewShape.Orientation(theShape.Orientation()); |
192 | if(!theRemoveLoc && !theShape.Location().IsIdentity()) { |
193 | TopLoc_Location aL = theShape.Location(); |
194 | aNewShape.Location(aL); |
195 | } |
196 | if(shtype != TopAbs_EDGE) { |
197 | |
198 | theNewShape = aNewShape; |
199 | |
200 | return Standard_True; |
201 | } |
202 | } |
203 | |
204 | |
205 | Standard_Boolean isRemoveLoc = theRemoveLoc; |
206 | if(!theRemoveLoc) { |
207 | isRemoveLoc = ((shtype != TopAbs_COMPOUND && myLevelRemoving == TopAbs_SHAPE) || |
208 | ((Standard_Integer)myLevelRemoving <= ((Standard_Integer)shtype))); |
209 | } |
210 | |
211 | Standard_Boolean aRebuild = Standard_False; |
212 | TopoDS_Shape anAncShape = theAncShape; |
213 | if(shtype == TopAbs_FACE) |
214 | anAncShape = aShape; |
215 | if(isRemoveLoc && (!aShape.Location().IsIdentity() || shtype == TopAbs_EDGE || shtype == TopAbs_FACE )) { |
216 | |
217 | //Rebuild geometry for shape with location. |
218 | if(shtype == TopAbs_FACE) { |
219 | TopoDS_Face anewFace; |
220 | TopoDS_Face oldFace = TopoDS::Face(aShape); |
221 | aRebuild = RebuildShape(oldFace,anewFace); |
222 | if(aRebuild) { |
223 | aNewShape = anewFace; |
224 | myMapNewShapes.Bind(oldFace,aNewShape); |
225 | } |
226 | |
227 | } |
228 | else if(shtype == TopAbs_EDGE) { |
229 | TopoDS_Edge oldEdge = TopoDS::Edge(aShape); |
230 | TopoDS_Edge anewEdge; |
231 | TopoDS_Face F,newFace; |
232 | |
233 | if(!anAncShape.IsNull()) { |
234 | F = TopoDS::Face(anAncShape); |
235 | newFace = F; |
236 | if(myMapNewShapes.IsBound(F)) |
237 | newFace = TopoDS::Face(myMapNewShapes.Find(F)); |
238 | } |
239 | if(isBound) |
240 | anewEdge = TopoDS::Edge(aNewShape); |
241 | aRebuild = RebuildShape(oldEdge,anewEdge,F,newFace,isBound); |
242 | aNewShape = anewEdge; |
243 | |
244 | } |
245 | else if(shtype == TopAbs_VERTEX) { |
246 | TopoDS_Vertex aVnew; |
247 | TopoDS_Vertex aV = TopoDS::Vertex(aShape); |
248 | aRebuild = RebuildShape(aV,aVnew); |
249 | if(aRebuild) |
250 | aNewShape = aVnew; |
251 | } |
252 | } |
253 | isDone = aRebuild; |
254 | |
255 | //Removing location from sub-shapes in dependance of LevelRemoving and re-building shape. |
256 | |
257 | if(!isBound) { |
258 | if(!aRebuild) |
259 | aNewShape = theShape.EmptyCopied(); |
260 | TopLoc_Location oldLoc,nullloc; |
261 | oldLoc = theShape.Location(); |
262 | if(!oldLoc.IsIdentity()) |
263 | aNewShape.Location(nullloc); |
264 | TopAbs_Orientation orient = theShape.Orientation(); |
265 | aNewShape.Orientation(TopAbs_FORWARD); |
266 | TopoDS_Iterator aIt(aShape,Standard_False,isRemoveLoc); |
267 | for( ; aIt.More(); aIt.Next()) { |
268 | TopoDS_Shape subshape = aIt.Value(); |
269 | TopoDS_Shape anewsubshape; |
270 | Standard_Boolean isDoneSubShape = MakeNewShape(subshape,anAncShape,anewsubshape,isRemoveLoc); |
271 | isDone = (isDone || isDoneSubShape); |
272 | aB.Add(aNewShape,anewsubshape); |
273 | |
274 | } |
275 | if(isDone) |
276 | aNewShape.Orientation(orient); |
277 | else aNewShape = aShape; |
278 | myMapNewShapes.Bind(aShape,aNewShape); |
279 | if(!theRemoveLoc && !oldLoc.IsIdentity()) |
280 | aNewShape.Location(oldLoc); |
281 | |
282 | } |
283 | theNewShape = aNewShape; |
284 | |
285 | |
286 | return (isDone || isBound); |
287 | } |
288 | |