0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / TopOpeBRepBuild / TopOpeBRepBuild_FREGU.cxx
1 // Created on: 1996-03-07
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1996-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
18 #include <BRep_Builder.hxx>
19 #include <BRep_Tool.hxx>
20 #include <gp_Pnt.hxx>
21 #include <Standard_NoSuchObject.hxx>
22 #include <Standard_ProgramError.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TopExp.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopoDS.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Face.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Vertex.hxx>
31 #include <TopoDS_Wire.hxx>
32 #include <TopOpeBRepBuild_Builder.hxx>
33 #include <TopOpeBRepBuild_EdgeBuilder.hxx>
34 #include <TopOpeBRepBuild_FaceBuilder.hxx>
35 #include <TopOpeBRepBuild_GTopo.hxx>
36 #include <TopOpeBRepBuild_HBuilder.hxx>
37 #include <TopOpeBRepBuild_PaveSet.hxx>
38 #include <TopOpeBRepBuild_ShapeSet.hxx>
39 #include <TopOpeBRepBuild_ShellFaceSet.hxx>
40 #include <TopOpeBRepBuild_SolidBuilder.hxx>
41 #include <TopOpeBRepBuild_WireEdgeSet.hxx>
42 #include <TopOpeBRepBuild_WireToFace.hxx>
43 #include <TopOpeBRepDS_BuildTool.hxx>
44 #include <TopOpeBRepDS_CurveIterator.hxx>
45 #include <TopOpeBRepDS_define.hxx>
46 #include <TopOpeBRepDS_HDataStructure.hxx>
47 #include <TopOpeBRepDS_PointIterator.hxx>
48 #include <TopOpeBRepDS_SurfaceIterator.hxx>
49 #include <TopOpeBRepTool.hxx>
50 #include <TopOpeBRepTool_ShapeExplorer.hxx>
51
52 #ifdef OCCT_DEBUG
53 extern Standard_Boolean TopOpeBRepBuild_GetcontextNOREGUFA();
54 extern Standard_Boolean TopOpeBRepBuild_GetcontextREGUXPU();
55 extern Standard_Boolean TopOpeBRepBuild_GettraceSAVFREGU();
56 void debregufa(const Standard_Integer /*iF*/) {}
57 #endif
58
59 #ifdef DRAW
60 #include <DBRep.hxx>
61 #endif
62
63 #define M_FORWARD(O)  (O == TopAbs_FORWARD)
64 #define M_REVERSED(O) (O == TopAbs_REVERSED)
65
66 //=======================================================================
67 //function : RegularizeFaces
68 //purpose  : 
69 //=======================================================================
70 void TopOpeBRepBuild_Builder::RegularizeFaces
71 (const TopoDS_Shape& FF,const TopTools_ListOfShape& lnewFace,TopTools_ListOfShape& LOF)
72 {
73   LOF.Clear();
74   myMemoSplit.Clear();
75
76   TopTools_ListIteratorOfListOfShape itl(lnewFace);  
77   for (;itl.More();itl.Next()) {
78     const TopoDS_Shape& newFace = itl.Value();
79     TopTools_ListOfShape newFaceLOF;
80     RegularizeFace(FF,newFace,newFaceLOF);
81 #ifdef OCCT_DEBUG
82 //    Standard_Integer nnewFaceLOF = newFaceLOF.Extent(); // DEB
83 #endif
84     LOF.Append(newFaceLOF);
85   }
86 #ifdef OCCT_DEBUG
87 //  Standard_Integer nLOF = LOF.Extent(); // DEB
88 #endif
89
90   Standard_Integer nr = myMemoSplit.Extent();
91   if (nr == 0 ) return;
92
93   // lfsdFF = faces SameDomain de FF
94   TopTools_ListOfShape lfsdFF,lfsdFF1,lfsdFF2;
95   GFindSamDom(FF,lfsdFF1,lfsdFF2);
96   lfsdFF.Append(lfsdFF1);
97   lfsdFF.Append(lfsdFF2);
98   
99   TopTools_ListIteratorOfListOfShape itlfsdFF(lfsdFF);
100   for (; itlfsdFF.More(); itlfsdFF.Next()) {
101     const TopoDS_Shape& fsdFF = itlfsdFF.Value();
102     // au moins une arete de FF dont le Split() est lui meme Split()
103     TopExp_Explorer x;
104     for (x.Init(fsdFF,TopAbs_EDGE);x.More();x.Next()) {
105 //    for (TopExp_Explorer x(fsdFF,TopAbs_EDGE);x.More();x.Next()) {
106       const TopoDS_Shape& e = x.Current();
107 #ifdef OCCT_DEBUG
108 //      Standard_Integer ie = myDataStructure->Shape(e); //DEB
109 //      Standard_Boolean issect = myDataStructure->DS().IsSectionEdge(TopoDS::Edge(e));
110 #endif
111
112       Standard_Integer ranke = GShapeRank(e);
113       TopAbs_State staeope = (ranke==1) ? myState1 : myState2;
114       
115       for (Standard_Integer iiista = 1; iiista <= 2; iiista++ ) {
116         
117         TopAbs_State stae = staeope;
118         if (iiista == 2) stae = TopAbs_ON;
119         
120         Standard_Boolean issplite = IsSplit(e,stae);
121         if (!issplite) continue;
122         
123         TopTools_ListOfShape& lspe = ChangeSplit(e,stae);
124 #ifdef OCCT_DEBUG
125 //      Standard_Integer nlspe = lspe.Extent(); // DEB
126 #endif
127         TopTools_ListOfShape newlspe;
128         for (TopTools_ListIteratorOfListOfShape itl1(lspe);itl1.More();itl1.Next()) {
129           const TopoDS_Shape& esp = itl1.Value();
130           Standard_Boolean espmemo = myMemoSplit.Contains(esp);
131           if (!espmemo) newlspe.Append(esp);
132           else {
133             const TopTools_ListOfShape& lspesp = Splits(esp,stae);
134             GCopyList(lspesp,newlspe);
135           }
136         }       
137         lspe.Clear();
138         GCopyList(newlspe,lspe);
139
140       } // iiista
141     } // explorer (fsdFF,TopAbs_EDGE)
142   } // itlfsdFF.More()
143 } // RegularizeFaces
144
145 /*static void FUN_setInternal(TopoDS_Face& F)
146 {
147   TopExp_Explorer ex(F,TopAbs_EDGE);
148   TopTools_MapOfShape meF,meR,meI;
149   for (; ex.More(); ex.Next()){
150     const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
151     TopAbs_Orientation oE = E.Orientation();
152     Standard_Boolean isclo = BRep_Tool::IsClosed(E,F); // E has 2d rep on F
153     if (isclo) continue; 
154     Standard_Boolean isb = Standard_False; // the edge is FOR + REV in F
155     if      (M_FORWARD(oE))  {meF.Add(E); isb = meR.Contains(E);}
156     else if (M_REVERSED(oE)) {meR.Add(E); isb = meF.Contains(E);}
157     if (isb) meI.Add(E.Oriented(TopAbs_INTERNAL));
158   }  
159
160   BRep_Builder BB;
161   TopTools_MapIteratorOfMapOfShape it(meI);
162   for (; it.More(); it.Next()){
163     const TopoDS_Edge& E = TopoDS::Edge(it.Key());
164     BB.Remove(F,E.Oriented(TopAbs_FORWARD));
165     BB.Remove(F,E.Oriented(TopAbs_REVERSED));
166     BB.Add(F,E);
167   }
168 }*/
169
170 //=======================================================================
171 //function : RegularizeFace
172 //purpose  : 
173 //=======================================================================
174 void TopOpeBRepBuild_Builder::RegularizeFace
175 (const TopoDS_Shape& FF,const TopoDS_Shape& anewFace,TopTools_ListOfShape& LOF)
176 {
177   LOF.Clear();
178   const TopoDS_Face& newFace = TopoDS::Face(anewFace);
179   Standard_Boolean toregu = Standard_True;
180   Standard_Boolean usewtof = Standard_True;
181   
182 #ifdef OCCT_DEBUG
183   Standard_Integer iF;Standard_Boolean tSPSFF=GtraceSPS(FF,iF);
184 //  Standard_Boolean savfregu = TopOpeBRepBuild_GettraceSAVFREGU();
185   if (TopOpeBRepBuild_GetcontextNOREGUFA()) toregu = Standard_False;
186   if (TopOpeBRepBuild_GetcontextREGUXPU()) usewtof = Standard_False;
187   if (tSPSFF) debregufa(iF);
188 #endif
189
190   // If the same edge appears FOR+REV in the resulting face and
191   // whereas it's not a closing edge, set it as INTERNAL instead.
192   // FRA60275(iF=4) + PRO16297 
193 //  FUN_setInternal(newFace);
194
195   if (!toregu) {
196     LOF.Append(newFace);
197     return;
198   }
199   
200   TopTools_DataMapOfShapeListOfShape ownw; // OldWires --> NewWires;
201   Standard_Boolean rw = Standard_False;
202   Standard_Boolean rf = Standard_False;
203   myESplits.Clear();
204   
205   rw = TopOpeBRepTool::RegularizeWires(newFace,ownw,myESplits);
206   
207   if ( !rw ) {
208     LOF.Append(newFace);
209     return;
210   }      
211   
212   TopTools_ListOfShape newfaces;
213   if (usewtof) {
214     TopOpeBRepBuild_WireToFace wtof;
215     TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itownw(ownw);
216     for (; itownw.More(); itownw.Next()) {
217       const TopTools_ListOfShape& lw = itownw.Value();
218
219       // xpu200798 : cto902D4 f14ou
220       // recall ownw = {(ow = old wire, lnw = {list of new wires descendant of old wire}}
221       // if lnw is empty, then ow is kept unchanged.
222       Standard_Boolean kept = lw.IsEmpty();
223       if (kept) {
224         const TopoDS_Wire& ow = TopoDS::Wire(itownw.Key());
225         wtof.AddWire(ow);
226       }
227       for(TopTools_ListIteratorOfListOfShape iw(lw);iw.More();iw.Next()) {
228         const TopoDS_Wire& w = TopoDS::Wire(iw.Value());
229         wtof.AddWire(w);
230       }
231     }
232     wtof.MakeFaces(newFace,newfaces);
233 #ifdef OCCT_DEBUG
234 //    Standard_Integer nnewfaces = newfaces.Extent(); // DEB
235 #endif
236     rf = (newfaces.Extent() != 0);
237   }
238   else {
239     rf = TopOpeBRepTool::RegularizeFace(newFace,ownw,newfaces);
240   }
241   
242   if (!rf) {
243     LOF.Append(newFace);
244     return;
245   }
246   
247 #ifdef OCCT_DEBUG
248   if (tSPSFF) { cout<<"RegularizeFace "<<iF<<endl; debregufa(iF); }
249 #endif
250   
251   // LOF = nouvelles faces regularisees de newFace
252   TopTools_ListIteratorOfListOfShape itlnf(newfaces);
253   for (; itlnf.More(); itlnf.Next()) 
254     LOF.Append(TopoDS::Face(itlnf.Value()));
255   
256   // mise a jour des aretes decoupees
257   // Edge(FF) = {E}, E-->Split(E) = {E'}, E'-->myESplits(E') = {E''}
258
259   TopTools_MapOfShape menf; // menf = aretes de newFace
260   TopExp_Explorer x;
261   for (x.Init(newFace,TopAbs_EDGE);x.More();x.Next()) {
262     const TopoDS_Shape& E = x.Current();
263     menf.Add(E);
264   }
265   
266   // lfsdFF = faces SameDomain de FF
267   TopTools_ListOfShape lfsdFF,lfsdFF1,lfsdFF2;
268   GFindSamDom(FF,lfsdFF1,lfsdFF2);
269   lfsdFF.Append(lfsdFF1);
270   lfsdFF.Append(lfsdFF2);
271   
272   TopTools_ListIteratorOfListOfShape itlfsdFF(lfsdFF);
273   for (; itlfsdFF.More(); itlfsdFF.Next()) {
274     const TopoDS_Shape& fsdFF = itlfsdFF.Value();
275
276 #ifdef OCCT_DEBUG
277     Standard_Integer ifsdFF=0;Standard_Boolean tSPSfsdFF=GtraceSPS(fsdFF,ifsdFF);
278     if (tSPSfsdFF) debregufa(ifsdFF);
279 #endif    
280
281     Standard_Integer rankfsdFF = GShapeRank(fsdFF);
282     TopAbs_State stafsdFF = (rankfsdFF == 1) ? myState1 : myState2;
283 #ifdef OCCT_DEBUG
284 //    Standard_Boolean issplitfsdFF = IsSplit(fsdFF,stafsdFF);
285 #endif
286
287 /*#ifdef OCCT_DEBUG
288     const TopTools_ListOfShape& lspfsdFF = Splits(fsdFF,stafsdFF);
289     Standard_Integer nlspfsdFF = lspfsdFF.Extent();
290 #endif*/
291     
292     // iteration sur les aretes de fsdFF
293     for (x.Init(fsdFF,TopAbs_EDGE);x.More();x.Next()) {
294
295       //fsdFFe : 1 edge de fsdFF = 1 face SameDomain de FF
296       const TopoDS_Shape& fsdFFe = x.Current(); 
297
298 #ifdef OCCT_DEBUG
299       Standard_Integer ifsdFFe = 0;Standard_Boolean tSPSfsdFFe=GtraceSPS(fsdFFe,ifsdFFe);
300       if (tSPSfsdFFe) debregufa(ifsdFFe);
301 #endif    
302       
303       // a priori, on ne peut avoir plus de deux etats splites
304       // sur l'arete , soit (IN + ON) , soit (OUT + ON) 
305       for (Standard_Integer iiista = 1; iiista <= 2; iiista++ ) {
306         TopAbs_State stafsdFFe = stafsdFF;
307         if (iiista == 2) stafsdFFe = TopAbs_ON;
308         
309         TopTools_ListOfShape& lspfsdFFe = ChangeSplit(fsdFFe,stafsdFFe);
310 #ifdef OCCT_DEBUG
311 //      Standard_Boolean issplitfsdFFe = IsSplit(fsdFFe,stafsdFFe);
312 //      Standard_Integer nlspfsdFFe = lspfsdFFe.Extent();
313 #endif    
314           
315         for (TopTools_ListIteratorOfListOfShape it(lspfsdFFe);it.More();it.Next()) {
316           
317           // fsdFFe (Cf supra E) a ete splittee, espfdsFFe = arete splittee de fsdFFe
318           
319           const TopoDS_Shape& espfsdFFe = it.Value();
320           Standard_Boolean inmenf = menf.Contains(espfsdFFe);
321           if (!inmenf) continue;
322           
323           // fsdFFe (Cf supra E) a ete splittee, espfdsFFe = arete splittee de fsdFFe
324           // espfsdFFe est une arete de Split(fsdFFe) ET figure dans newFace (Cf supra E')
325           
326           Standard_Boolean resplitloc = myESplits.IsBound(espfsdFFe);
327           if (resplitloc) {
328             
329             // fsdFFe (Cf supra E) a ete splittee, espfdsFFe = arete splittee de fsdFFe
330             // espfsdFFe est une arete de Split(fsdFFe) ET figure dans newFace (Cf supra E')
331             // espfsdFFe de newFace a ete redecoupee par RegularizeWires
332             
333             // son decoupage lresplit est stocke dans la DS du Builder
334             const TopTools_ListOfShape& lresplit = myESplits.Find(espfsdFFe); //Cf supra E''
335             
336             // on memorise que espfsdFFe est redecoupee ...
337             myMemoSplit.Add(espfsdFFe);
338             
339             // on stocke le nouveau decoupage de espfsdFFe dans la DS du builder ...
340             TopTools_ListOfShape& lsp = ChangeSplit(espfsdFFe,stafsdFFe);  
341             GCopyList(lresplit,lsp);
342           }
343         } // it.More
344       } // iiista
345     } // explore(fsdFF,TopAbs_EDGE)
346   } // itlfsdFF.More()
347
348 #ifdef DRAW
349   if (tSPSFF) debregufa(iF);
350   if (tSPSFF && savfregu) {
351     TCollection_AsciiString str("fregu"); str = str + iF;
352     DBRep::Set(str.ToCString(),newFace);
353     cout<<"newFace "<<str<<" built on face "<<iF<<" saved"<<endl;
354   }
355 #endif
356   
357 } // RegularizeFace