1 // Created on: 1993-06-14
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <Bnd_Box.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22 #include <BRepAdaptor_Surface.hxx>
23 #include <Geom2d_Curve.hxx>
24 #include <Geom_Curve.hxx>
26 #include <Standard_NoSuchObject.hxx>
27 #include <Standard_ProgramError.hxx>
28 #include <TColgp_Array1OfPnt.hxx>
29 #include <TCollection_AsciiString.hxx>
32 #include <TopoDS_Compound.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopOpeBRepBuild_Builder.hxx>
38 #include <TopOpeBRepBuild_define.hxx>
39 #include <TopOpeBRepBuild_EdgeBuilder.hxx>
40 #include <TopOpeBRepBuild_FaceBuilder.hxx>
41 #include <TopOpeBRepBuild_GTopo.hxx>
42 #include <TopOpeBRepBuild_HBuilder.hxx>
43 #include <TopOpeBRepBuild_PaveSet.hxx>
44 #include <TopOpeBRepBuild_ShapeSet.hxx>
45 #include <TopOpeBRepBuild_ShellFaceSet.hxx>
46 #include <TopOpeBRepBuild_SolidBuilder.hxx>
47 #include <TopOpeBRepBuild_WireEdgeSet.hxx>
48 #include <TopOpeBRepDS_BuildTool.hxx>
49 #include <TopOpeBRepDS_CurveIterator.hxx>
50 #include <TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeListOfShapeOn1State.hxx>
51 #include <TopOpeBRepDS_HDataStructure.hxx>
52 #include <TopOpeBRepDS_ListOfShapeOn1State.hxx>
53 #include <TopOpeBRepDS_PointIterator.hxx>
54 #include <TopOpeBRepDS_SurfaceIterator.hxx>
55 #include <TopOpeBRepTool_2d.hxx>
56 #include <TopOpeBRepTool_FuseEdges.hxx>
57 #include <TopOpeBRepTool_ShapeExplorer.hxx>
58 #include <TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape.hxx>
60 //#include <BRepAdaptor_Curve2d.hxx>
62 extern Standard_Boolean TopOpeBRepBuild_GetcontextNOFE();
65 //=======================================================================
68 //=======================================================================
69 void TopOpeBRepBuild_Builder::End()
71 const TopOpeBRepDS_DataStructure& BDS = myDataStructure->DS();
73 // recodage de la continuite (edge,(f1,f2)) != C0 perdue lors
74 // du changement de surface-support d'une arete non decoupee
75 // d'une face tangente.
76 for (Standard_Integer i=1;i<=2;i++) {
77 TopoDS_Shape S; TopAbs_State sta=TopAbs_UNKNOWN;
78 if ( i==1 ) { S = myShape1; sta = myState1; }
79 else if ( i==2 ) { S = myShape2; sta = myState2; }
82 for (exs.Init(S,TopAbs_SHELL);exs.More();exs.Next()) {
83 // for (TopExp_Explorer exs(S,TopAbs_SHELL);exs.More();exs.Next()) {
84 const TopoDS_Shape& SH = exs.Current();
85 Standard_Boolean SHhassha = BDS.HasShape(SH);
86 if ( !SHhassha ) continue;
88 Standard_Boolean Fhassam = Standard_False;
90 for (exf.Init(SH,TopAbs_FACE);exf.More(); exf.Next()) {
91 // for (TopExp_Explorer exf(SH,TopAbs_FACE);exf.More(); exf.Next()) {
92 Fhassam = myDataStructure->HasSameDomain(exf.Current());
95 if ( !Fhassam ) continue;
97 TopTools_IndexedDataMapOfShapeListOfShape M;
98 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,M);
99 Standard_Integer nE = M.Extent();
100 for (Standard_Integer iE = 1; iE <= nE; iE++) {
101 const TopoDS_Edge& E = TopoDS::Edge(M.FindKey(iE));
102 if ( IsSplit(E,sta) ) continue;
103 const TopTools_ListOfShape& LF = M.FindFromIndex(iE);
104 if ( LF.Extent() < 2 ) continue;
106 // NYI : > 2 faces connexes par E : iterer sur tous les couples
107 TopTools_ListIteratorOfListOfShape itLF(LF);
108 const TopoDS_Face& F1 = TopoDS::Face(itLF.Value()); itLF.Next();
109 const TopoDS_Face& F2 = TopoDS::Face(itLF.Value());
110 GeomAbs_Shape C = BRep_Tool::Continuity(E,F1,F2);
111 if ( C == GeomAbs_C0 ) continue;
113 Standard_Boolean F1hassam = myDataStructure->HasSameDomain(F1);
114 Standard_Boolean F2hassam = myDataStructure->HasSameDomain(F2);
115 if ( !F1hassam && !F2hassam ) continue;
117 Standard_Boolean F1issplit = IsSplit(F1,sta);
118 Standard_Boolean F2issplit = IsSplit(F2,sta);
119 F1issplit &= (Splits(F1,sta).Extent() != 0);
120 F2issplit &= (Splits(F2,sta).Extent() != 0);
121 if ( !F1issplit && !F2issplit ) continue;
123 TopoDS_Face FF1=F1,FF2=F2;
124 for (Standard_Integer ii=1; ii<=2; ii++) {
125 if ((ii==1 && !F1issplit) || (ii==2 && !F2issplit)) continue;
129 Standard_Boolean f = Standard_False;
130 TopTools_ListIteratorOfListOfShape it;
131 for (it.Initialize(Splits(F,sta));it.More();it.Next()) {
132 const TopoDS_Shape& SF = it.Value();
133 if (SF.ShapeType() != TopAbs_FACE) continue;
135 for (ex.Init(SF,TopAbs_EDGE);ex.More();ex.Next()) {
136 if (ex.Current().IsSame(E)) {
137 if (ii==1) FF1 = TopoDS::Face(it.Value());
138 else FF2 = TopoDS::Face(it.Value());
139 f = Standard_True; break;
146 B.Continuity(E,FF1,FF2,C);
152 // M.A.J de la tolerance des vertex
154 // modified by NIZHNY-MKK Fri Oct 6 16:13:33 2000.BEGIN
155 TopTools_MapOfShape aMapOfNewEdges, aMapOfNewVertices;
156 TopTools_ListIteratorOfListOfShape anIt;
157 Standard_Integer iteratorofnewshape=0;
158 for(iteratorofnewshape=1; iteratorofnewshape <= myDataStructure->NbCurves(); iteratorofnewshape++) {
159 for(anIt.Initialize(NewEdges(iteratorofnewshape)); anIt.More(); anIt.Next()) {
160 aMapOfNewEdges.Add(anIt.Value());
163 for(iteratorofnewshape=1; iteratorofnewshape <= myDataStructure->NbPoints(); iteratorofnewshape++) {
164 aMapOfNewVertices.Add(NewVertex(iteratorofnewshape));
166 // modified by NIZHNY-MKK Fri Oct 6 16:13:36 2000.END
168 TopoDS_Compound R;BRep_Builder B;B.MakeCompound(R);
169 const TopTools_ListOfShape& lmergesha1 = Merged(myShape1,myState1);
170 TopTools_ListIteratorOfListOfShape it(lmergesha1); for(;it.More();it.Next()) B.Add(R,it.Value());
171 const TopTools_ListOfShape& LOES = Section();
173 // Standard_Integer nLOES = LOES.Extent();
176 TopTools_IndexedDataMapOfShapeListOfShape idmoelof; TopExp::MapShapesAndAncestors(R,TopAbs_EDGE,TopAbs_FACE,idmoelof);
177 TopTools_IndexedDataMapOfShapeListOfShape idmovloe; TopExp::MapShapesAndUniqueAncestors(R,TopAbs_VERTEX,TopAbs_EDGE,idmovloe);
178 TopTools_IndexedDataMapOfShapeListOfShape idmovloes; for (TopTools_ListIteratorOfListOfShape I(LOES);I.More();I.Next())
179 TopExp::MapShapesAndAncestors(I.Value(),TopAbs_VERTEX,TopAbs_EDGE,idmovloes);
180 Standard_Integer iv,nv = idmovloe.Extent();
181 for (iv=1;iv<=nv;iv++) {
182 Standard_Integer nP1 = 0;
183 const TopoDS_Vertex& V = TopoDS::Vertex(idmovloe.FindKey(iv));
184 Standard_Boolean isbe = idmovloes.Contains(V);
185 if ( !isbe ) continue;
187 const TopTools_ListOfShape& loe = idmovloe.FindFromIndex(iv);
190 // Standard_Integer nloe = loe.Extent();
192 TopTools_ListIteratorOfListOfShape iloe;
193 for (iloe.Initialize(loe);iloe.More();iloe.Next()) {
194 const TopoDS_Edge& E = TopoDS::Edge(iloe.Value());
195 const TopTools_ListOfShape& lof = idmoelof.FindFromKey(E);
196 Standard_Integer nlof = lof.Extent();
200 TColgp_Array1OfPnt TP(1,nP1);
201 Standard_Integer nP2 = 0;
202 for (iloe.Initialize(loe);iloe.More();iloe.Next()) {
203 const TopoDS_Edge& E = TopoDS::Edge(iloe.Value());
204 Standard_Real pv = BRep_Tool::Parameter(V,E);
206 Standard_Real f,l;Handle(Geom_Curve) C3D = BRep_Tool::Curve(E,f,l);
211 const TopTools_ListOfShape& lof = idmoelof.FindFromKey(E);
213 // Standard_Integer nlof = lof.Extent();
215 for (TopTools_ListIteratorOfListOfShape ilof(lof);ilof.More();ilof.Next()) {
216 const TopoDS_Face& F = TopoDS::Face(ilof.Value());
218 Standard_Boolean pcf = FC2D_HasCurveOnSurface(E,F);
219 Handle(Geom2d_Curve) C2D;
221 C2D = FC2D_CurveOnSurface(E,F,f,l,tolpc);
222 if (C2D.IsNull()) throw Standard_ProgramError("TopOpeBRepBuild_Builder::End 1");
223 Standard_Real tolE = BRep_Tool::Tolerance(E);
224 Standard_Real tol = Max(tolE,tolpc);
225 B.UpdateEdge(E,C2D,F,tol);
227 C2D = FC2D_CurveOnSurface(E,F,f,l,tolpc);
228 gp_Pnt2d P2 = C2D->Value(pv);
229 BRepAdaptor_Surface BAS(F,Standard_False);
230 Pv = BAS.Value(P2.X(),P2.Y());
232 // modified by NIZHNY-MKK Fri Sep 29 16:08:28 2000.BEGIN
233 if(aMapOfNewEdges.Contains(E)) {
234 Standard_Real anEdgeTol = BRep_Tool::Tolerance(E);
235 Standard_Real aFaceTol = BRep_Tool::Tolerance(F);
236 if(anEdgeTol < aFaceTol)
237 B.UpdateEdge(E, aFaceTol);
239 // modified by NIZHNY-MKK Fri Sep 29 16:08:32 2000.END
241 // modified by NIZHNY-MKK Fri Sep 29 16:54:08 2000.BEGIN
242 if(aMapOfNewVertices.Contains(V)) {
243 Standard_Real aVertexTol = BRep_Tool::Tolerance(V);
244 Standard_Real anEdgeTol = BRep_Tool::Tolerance(E);
245 if (aVertexTol < anEdgeTol)
246 B.UpdateVertex(V, anEdgeTol);
248 // modified by NIZHNY-MKK Fri Sep 29 16:54:12 2000.END
251 Standard_Real newtol = BRep_Tool::Tolerance(V);
253 gp_Pnt Pv = BRep_Tool::Pnt(V);
255 for (Standard_Integer i=1;i<=nP2;i++) {
256 const gp_Pnt& Pi = TP(i);
257 BOX.Update(Pi.X(),Pi.Y(),Pi.Z());
259 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
260 BOX.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
261 gp_Pnt P1(aXmin, aYmin, aZmin);
262 gp_Pnt P2(aXmax, aYmax, aZmax);
263 Standard_Real d = P1.Distance(P2);
266 std::cout<<"\npoint P"<<iv<<" "<<Pv.X()<<" "<<Pv.Y()<<" "<<Pv.Z()<<std::endl;
267 std::cout<<"TopOpeBRepBuild_Builder::End BOX newtol "<<newtol<<" -> "<<d<<std::endl;
270 B.UpdateVertex(V,newtol);
275 Standard_Boolean makeFE = Standard_True;
277 makeFE = !TopOpeBRepBuild_GetcontextNOFE();
281 // TopAbs_State state = myState1;
282 TopTools_ListOfShape& ls = ChangeMerged(myShape1,myState1);
283 for (TopTools_ListIteratorOfListOfShape itls(ls);itls.More();itls.Next()) {
284 TopoDS_Shape& SFE = itls.Value();
285 TopOpeBRepTool_FuseEdges FE(SFE);
287 // avoid fusing old edges
288 TopTools_IndexedMapOfShape mapOldEdges;
289 TopExp::MapShapes (myShape1, TopAbs_EDGE, mapOldEdges);
290 TopExp::MapShapes (myShape2, TopAbs_EDGE, mapOldEdges);
291 FE.AvoidEdges (mapOldEdges);
293 // Get List of edges that have been fused
294 TopTools_DataMapOfIntegerListOfShape mle;
297 Standard_Integer nle = mle.Extent();
302 TopTools_DataMapOfIntegerShape mre;
303 TopTools_DataMapOfShapeShape mlf;
307 // edit the split to remove to edges to be fused and put them into the Merged
310 UpdateSplitAndMerged(mle, mre, mlf, TopAbs_IN);
311 UpdateSplitAndMerged(mle, mre, mlf, TopAbs_OUT);
312 UpdateSplitAndMerged(mle, mre, mlf, TopAbs_ON);
321 //=======================================================================
322 //function : UpdateSplitAndMerged
323 //purpose : edit the split to remove to edges to be fused and put them into the Merged
324 //=======================================================================
326 void TopOpeBRepBuild_Builder::UpdateSplitAndMerged(const TopTools_DataMapOfIntegerListOfShape& mle,
327 const TopTools_DataMapOfIntegerShape& mre,
328 const TopTools_DataMapOfShapeShape& mlf,
329 const TopAbs_State state)
331 const TopOpeBRepDS_DataMapOfShapeListOfShapeOn1State& MapSplit = MSplit(state);
332 TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeListOfShapeOn1State it;
333 for (it.Initialize(MapSplit); it.More(); it.Next()) {
334 const TopoDS_Shape& e = it.Key();
336 // For each edge of the MapSplit
337 if ( e.ShapeType() == TopAbs_EDGE ) {
339 // get the list of splitted edges.
340 TopTools_ListOfShape& LstSplit = ChangeSplit(e,state);
342 // for each edge of the list of splitted edges
343 TopTools_ListIteratorOfListOfShape itSplitEdg;
344 itSplitEdg.Initialize(LstSplit);
345 while ( itSplitEdg.More()) {
346 const TopoDS_Shape& edgecur = itSplitEdg.Value();
348 // for each "packet" of edges to be fused
349 TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape itLstEdg;
350 itLstEdg.Initialize(mle);
351 Standard_Boolean Found = Standard_False;
352 while ( itLstEdg.More() && !Found) {
353 const Standard_Integer& iLst = itLstEdg.Key();
354 const TopTools_ListOfShape& LmapEdg = mle.Find(iLst);
356 // look for each edge of the list if it is in the map Split
357 TopTools_ListIteratorOfListOfShape itEdg;
358 itEdg.Initialize(LmapEdg);
359 while ( itEdg.More() && !Found ) {
360 const TopoDS_Shape& edgefuse = itEdg.Value();
361 if (edgecur.IsSame(edgefuse)) {
362 Found = Standard_True;
365 LstSplit.Remove(itSplitEdg);
367 // edit the list of merged
368 TopAbs_State stateMerged;
369 if (ShapeRank(e) == 1)
370 stateMerged = myState1;
372 stateMerged = myState2;
374 TopTools_ListOfShape LstMerged;
375 LstMerged.Append(mre(iLst));
376 ChangeMerged(e,stateMerged) = LstMerged;
392 // For each face of the MapSplit
393 else if ( e.ShapeType() == TopAbs_FACE ) {
394 // get the list of splitted faces.
395 TopTools_ListOfShape& LstSplit = ChangeSplit(e,state);
397 // for each face of the list of splitted faces
398 TopTools_ListIteratorOfListOfShape itSplitFac;
399 itSplitFac.Initialize(LstSplit);
400 while ( itSplitFac.More()) {
401 const TopoDS_Shape& facecur = itSplitFac.Value();
403 if (mlf.IsBound(facecur)) {
404 LstSplit.InsertBefore(mlf(facecur),itSplitFac);
405 LstSplit.Remove(itSplitFac);