0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / TopOpeBRepBuild / TopOpeBRepBuild_kpkole.cxx
1 // Created on: 1994-08-30
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1994-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 #include <TopOpeBRepBuild_Builder.jxx>
18
19 #include <TopAbs.hxx>
20 #include <TopoDS_Solid.hxx>
21 #include <TopoDS_Shell.hxx>
22 #include <TopoDS_Wire.hxx>
23 #include <TopoDS.hxx>
24 #include <TopExp.hxx>
25 #include <BRep_Builder.hxx>
26 #include <TopOpeBRepTool_ShapeExplorer.hxx>
27 #include <TopOpeBRepTool_EXPORT.hxx>
28 #include <TopOpeBRepDS_EXPORT.hxx>
29 #include <TopOpeBRepDS_connex.hxx>
30 #include <TopOpeBRepBuild_define.hxx>
31 #include <TopOpeBRepBuild_WireToFace.hxx>
32 #include <TopOpeBRepBuild_kpresu.hxx>
33 #include <Standard_ProgramError.hxx>
34 #include <TopOpeBRepDS_ShapeShapeInterference.hxx>
35
36 #ifdef OCCT_DEBUG
37 extern Standard_Boolean TopOpeBRepBuild_GettraceKPB();
38 void debiskole() {}
39 #endif
40
41 Standard_EXPORT Standard_Boolean FUNKP_KPiskolesh(const TopOpeBRepBuild_Builder& BU,const TopOpeBRepDS_DataStructure& BDS,const TopoDS_Shape& Sarg,TopTools_ListOfShape& lShsd,TopTools_ListOfShape& lfhsd);
42 Standard_EXPORT void FUNKP_KPmakefaces(const TopOpeBRepBuild_Builder& BU, const TopoDS_Shape& Fac1, const TopTools_ListOfShape& LF2,
43                                        const TopAbs_State Stfac1, const TopAbs_State Stfac2,
44                                        const Standard_Boolean R1, const Standard_Boolean R2,TopTools_ListOfShape& Lres);
45
46
47 //=======================================================================
48 //function : MergeKPartiskole
49 //purpose  : 
50 //=======================================================================
51
52 void TopOpeBRepBuild_Builder::MergeKPartiskole()
53 {
54 #ifdef OCCT_DEBUG
55   Standard_Boolean TKPB = TopOpeBRepBuild_GettraceKPB();
56   if (TKPB) KPreturn(myIsKPart);
57   debiskole();
58 #endif
59   
60   Standard_Integer ibid;
61   
62   if ( myIsKPart != 1 ) return;
63   
64   GMapShapes(myShape1,myShape2);
65   // NYI : on doit pouvoir faire l'economie du mapping GMapShapes(...)
66   // NYI en allant chercher l'indice 1,2 retourne par GShapeRank(S)
67   // NYI dans la DS. l'index est defini pour tous les shapes HasSameDomain
68   
69   TopTools_ListOfShape& lmergesha1 = ChangeMerged(myShape1,myState1);
70   ChangeMerged(myShape2,myState2);
71   
72   TopTools_ListOfShape lShsd1,lShsd2; // liste de solides HasSameDomain
73   TopTools_ListOfShape lfhsd1,lfhsd2; // liste de faces HasSameDomain
74   KPiskolesh(myShape1,lShsd1,lfhsd1);
75   KPiskolesh(myShape2,lShsd2,lfhsd2);
76   // traitement de tous les solides NYI
77   TopoDS_Shape sol1 = lShsd1.First();
78   TopoDS_Shape sol2 = lShsd2.First();
79   
80   ChangeMerged(sol1,myState1); 
81   ChangeMerged(sol2,myState2);
82   
83   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itm1;
84   itm1.Initialize(myKPMAPf1f2);
85   if ( ! itm1.More() ) return;
86   
87 #ifdef OCCT_DEBUG
88   if (TKPB) {
89     cout<<""<<endl;
90     for (; itm1.More();itm1.Next()) {
91       const TopoDS_Shape& f = itm1.Key();
92       Standard_Integer fi = myDataStructure->Shape(f);
93       cout<<"face "<<fi<<" : ";
94       const TopTools_ListOfShape& l = itm1.Value();
95       TopTools_ListIteratorOfListOfShape it(l);
96       for(; it.More(); it.Next()) {
97         const TopoDS_Shape& ff = it.Value();      
98         Standard_Integer ffi = myDataStructure->Shape(ff);
99         cout<<ffi<<" ";
100       }
101       cout<<endl;
102     }
103     itm1.Initialize(myKPMAPf1f2);
104   }
105 #endif
106   
107   TopTools_ListOfShape  LFIN;
108   TopTools_ListOfShape* plfIN = NULL;
109   const TopoDS_Shape* pfOU = NULL;
110   const TopoDS_Shape* pfIN = NULL;
111   
112   for (; itm1.More();itm1.Next()) {
113 //    const TopoDS_Shape& f = itm1.Key();
114 //   myDataStructure->Shape(f); //DEB
115     const TopTools_ListOfShape& los = itm1.Value();
116     Standard_Boolean emp = los.IsEmpty();
117     if (!emp) {
118       if (plfIN == NULL) plfIN = (TopTools_ListOfShape*)&itm1.Value();
119       if (pfOU == NULL) pfOU = &itm1.Key();
120       if (pfIN == NULL) pfIN = &plfIN->First();
121       for (TopTools_ListIteratorOfListOfShape it(los);it.More();it.Next()) LFIN.Append(it.Value());
122     }
123   }
124   
125   if ( plfIN==NULL) return;
126   if ( pfOU==NULL) return;
127   if ( pfIN==NULL) return;
128   
129 #ifdef OCCT_DEBUG
130   Standard_Integer ifOU; Standard_Boolean tSPS = GtraceSPS(*pfOU,ifOU);
131   if(tSPS || TKPB) { 
132 //    Standard_Integer iOU = myDataStructure->Shape(*pfOU);
133 //    Standard_Integer iIN = myDataStructure->Shape(*pfIN);
134     GdumpSHA(*pfOU, (char *) "MergeKPartiskole pfOU ");cout<<endl;
135     GdumpSAMDOM(LFIN, (char *) "LFIN : ");
136     debiskole();
137   }
138 #endif
139   
140   Standard_Integer rankpfOU = GShapeRank(*pfOU);
141   Standard_Integer rankpfIN = GShapeRank(*pfIN);
142   if ( rankpfOU != 1 && rankpfOU != 2 ) return;
143   if ( rankpfIN != 1 && rankpfIN != 2 ) return;
144   
145   // solfOU = solide dont la face *pfOU est OUT / faces LFIN
146   // solfIN = solide dont les faces *plfIN sont IN / face *pfOU
147   TopoDS_Shape solfOU;
148   if (rankpfOU == 1) solfOU = sol1;
149   else               solfOU = sol2; 
150   TopoDS_Shape solfIN;
151   if (rankpfIN == 1) solfIN = sol1;
152   else               solfIN = sol2; 
153   TopAbs_State stsolfOU = KPclasSS(solfOU,*pfOU, solfIN);
154   TopAbs_State stsolfIN = KPclasSS(solfIN,LFIN,solfOU);
155   TopAbs_State stfOU = TopAbs_OUT;
156   TopAbs_State stfIN = TopAbs_IN;
157   
158   TopAbs_State stsol1=TopAbs_UNKNOWN,stsol2=TopAbs_UNKNOWN;
159   TopAbs_State stfac1=TopAbs_UNKNOWN,stfac2=TopAbs_UNKNOWN;
160   TopoDS_Shape fac1,fac2;
161   if      (rankpfOU == 1 ) {
162     stsol1 = stsolfOU; stfac1 = stfOU; fac1 = *pfOU;
163     stsol2 = stsolfIN; stfac2 = stfIN; fac2 = *pfIN;
164   }
165   else if (rankpfOU == 2 ) {
166     stsol1 = stsolfIN; stfac1 = stfIN; fac1 = *pfIN;
167     stsol2 = stsolfOU; stfac2 = stfOU; fac2 = *pfOU;
168   }
169   
170   Standard_Integer ires,icla1,icla2;
171   KPiskoleanalyse(stfac1,stfac2,stsol1,stsol2,ires,icla1,icla2);
172   if (ires == RESUNDEF) return;
173   if (icla1 == SHEUNDEF || icla2 == SHEUNDEF) return;
174   
175   TopoDS_Shape she1; // she1 = shell accedant fac1
176   TopTools_IndexedDataMapOfShapeListOfShape Mfacshe1;
177   TopExp::MapShapesAndAncestors(sol1,TopAbs_FACE,TopAbs_SHELL,Mfacshe1);
178   const TopTools_ListOfShape& lshe1 = Mfacshe1.FindFromKey(fac1);
179   TopTools_ListIteratorOfListOfShape itlshe1(lshe1);
180   she1 = itlshe1.Value(); 
181   
182   TopoDS_Shape she2; // she2 = shell accedant fac2
183   TopTools_IndexedDataMapOfShapeListOfShape Mfacshe2;
184   TopExp::MapShapesAndAncestors(sol2,TopAbs_FACE,TopAbs_SHELL,Mfacshe2);
185   const TopTools_ListOfShape& lshe2 = Mfacshe2.FindFromKey(fac2);
186   TopTools_ListIteratorOfListOfShape itlshe2(lshe2);
187   she2 = itlshe2.Value();
188   
189   ChangeMerged(she1,myState1);
190   ChangeMerged(she2,myState2);
191   
192 #ifdef OCCT_DEBUG
193   if (TKPB) { cout<<"stsol1 ";TopAbs::Print(stsol1,cout); cout<<" "; }
194   if (TKPB) { cout<<"stsol2 ";TopAbs::Print(stsol2,cout); cout<<endl; }
195   debiskole();
196 #endif
197   
198   TopoDS_Shell newshe;
199   
200   if      ( ires == RESNULL ) {
201     return;
202   }
203   
204   else if (ires == RESSHAPE1) {
205     myBuildTool.MakeShell(newshe);
206     newshe = TopoDS::Shell(she1);
207   }
208   
209   else if (ires == RESSHAPE2) {
210     myBuildTool.MakeShell(newshe);
211     newshe = TopoDS::Shell(she2);
212   }
213   
214   else if ( ires == RESFACE1 ) {
215     if      (rankpfOU == 1) {
216       // resultat = face de rang 1 et face de rang 1 = face OUT
217       lmergesha1.Append(*pfOU);
218       ChangeMerged(fac2,myState2).Append(*pfOU);
219     }
220     else if (rankpfOU == 2) {
221       // resultat = face de rang 1 et face de rang 1 = faces IN
222       GCopyList(*plfIN,lmergesha1);
223       GCopyList(*plfIN,ChangeMerged(fac2,myState2));
224     }
225     return;
226   }
227   
228   else if ( ires == RESFACE2 ) {
229     if      (rankpfOU == 2) {
230       // resultat = face de rang 2 et face de rang 2 = face OUT
231       lmergesha1.Append(*pfOU);
232       ChangeMerged(fac1,myState1).Append(*pfOU);
233     }
234     else if (rankpfOU == 1) {
235       // resultat = face de rang 2 et face de rang 2 = faces IN
236       GCopyList(*plfIN,lmergesha1);
237       GCopyList(*plfIN,ChangeMerged(fac1,myState1));
238     }
239     return;
240   }
241   
242   else if (ires == RESNEWSHE) {
243     
244     itm1.Initialize(myKPMAPf1f2);
245     if (! itm1.More() ) return;
246     
247     TopTools_DataMapOfShapeShape addedfaces;
248     for (; itm1.More();itm1.Next()) {
249       
250       const TopoDS_Shape& f1 = itm1.Key();      
251       const TopTools_ListOfShape& lf2 = itm1.Value();
252       if (lf2.IsEmpty()) continue;
253       
254       TopTools_ListIteratorOfListOfShape it2;
255       it2.Initialize(lf2);
256       const TopoDS_Shape& f2 = it2.Value();
257       
258 /*#ifdef OCCT_DEBUG
259       Standard_Integer ii1 = myDataStructure->Shape(f1);
260       Standard_Integer ii2 = myDataStructure->Shape(f2);
261 #endif*/
262       Standard_Integer rankf1 = GShapeRank(f1);
263       Standard_Integer rankf2 = GShapeRank(f2);
264       if (rankf1 == 0) continue;
265       if (rankf2 == 0) continue;
266       
267       TopAbs_State stf1,stf2; KPclassFF(f1,f2,stf1,stf2);
268       if ( rankf1 == 1 ) KPiskoleanalyse(stf1,stf2,stsol1,stsol2,ires,ibid,ibid);
269       if ( rankf1 == 2 ) KPiskoleanalyse(stf2,stf1,stsol2,stsol1,ires,ibid,ibid);
270       if (ires == RESUNDEF) continue;
271       
272       Standard_Boolean r1 = (stsol1 == TopAbs_IN);
273       Standard_Boolean r2 = (stsol2 == TopAbs_IN);
274       TopoDS_Shape fac;
275       if ( rankf1 == 1 ) fac = KPmakeface(f1,lf2,stf1,stf2,r1,r2);
276       if ( rankf1 == 2 ) fac = KPmakeface(f1,lf2,stf1,stf2,r2,r1);
277       if ( fac.IsNull() ) continue;
278       if ( ! fac.IsNull() ) addedfaces.Bind(fac,fac);
279       
280       TopAbs_State statemergef1 = (rankf1 == 1) ? myState1 : myState2;
281       TopAbs_State statemergef2 = (rankf2 == 2) ? myState2 : myState1;
282       ChangeMerged(f1,statemergef1).Append(fac);
283       it2.Initialize(lf2);
284       for (;it2.More();it2.Next()) 
285         ChangeMerged(it2.Value(),statemergef2).Append(fac);
286       
287       // les faces de she1 sauf les tangentes et celles deja ajoutees
288       TopOpeBRepTool_ShapeExplorer fex1;
289       for (fex1.Init(she1,TopAbs_FACE); fex1.More(); fex1.Next()) {
290         const TopoDS_Shape& facur = fex1.Current();
291         
292         Standard_Boolean isfsd   = myKPMAPf1f2.IsBound(facur);
293         Standard_Boolean isadded = addedfaces.IsBound(facur);
294         Standard_Boolean toadd =  (!isfsd) && (!isadded) ;
295         
296         if ( toadd ) {
297           TopoDS_Shape fori = facur;
298           if (stsol1 == TopAbs_IN) fori.Complement();
299           addedfaces.Bind(fori,fori);
300         }
301       }
302       
303       // les faces de she2 sauf les tangentes et celles deja ajoutees
304       TopOpeBRepTool_ShapeExplorer fex2;
305       for (fex2.Init(she2,TopAbs_FACE); fex2.More(); fex2.Next()) {
306         const TopoDS_Shape& facur = fex2.Current();
307         
308         Standard_Boolean isfsd   = myKPMAPf1f2.IsBound(facur);
309         Standard_Boolean isadded = addedfaces.IsBound(facur);
310         Standard_Boolean toadd =  (!isfsd) && (!isadded) ;
311         
312         if ( toadd ) {
313           TopoDS_Shape fori = facur;
314           if (stsol2 == TopAbs_IN) fori.Complement();
315           addedfaces.Bind(fori,fori);
316         }
317       }
318     }  // === fin iteration fac1,fac2
319     
320     TopTools_DataMapIteratorOfDataMapOfShapeShape itadd(addedfaces);
321     Standard_Boolean yauadd = itadd.More();
322     if (yauadd) {
323       myBuildTool.MakeShell(newshe);
324       myBuildTool.Closed(newshe,Standard_True);  // NYI : check exact du caractere closed du shell
325     }
326     for (; itadd.More(); itadd.Next() ) {
327       const TopoDS_Shape& ftoadd = itadd.Key();
328       myBuildTool.AddShellFace(newshe,ftoadd);
329     }
330     
331   } // === fin RESNEWSHE 
332   
333   else {
334 #ifdef OCCT_DEBUG
335     cout<<"MergeKPartiskole : ires = "<<ires<<endl;
336 #endif
337   }
338   
339   TopoDS_Solid newsol;
340   if ( !newshe.IsNull() ) {
341     myBuildTool.MakeSolid(newsol);
342     myBuildTool.AddSolidShell(newsol,newshe);
343   }
344   
345   if ( icla1 == SHECLASAUTR || icla1 == SHEGARDAUTR ) {
346     // n.b. : ne pas prendre she1 accedant f1
347     TopTools_ListOfShape loshe1;
348     TopOpeBRepTool_ShapeExplorer ex1;
349     for (ex1.Init(sol1,TopAbs_SHELL); ex1.More(); ex1.Next()) {
350       const TopoDS_Shape& shecur = ex1.Current();
351       if (she1.IsEqual(shecur)) continue;
352       if (icla1 == SHECLASAUTR) {
353         TopAbs_State state1 = KPclasSS(shecur,fac1,sol2);
354         if (state1 == myState1) loshe1.Append(shecur);
355       }
356       else if (icla1 == SHEGARDAUTR) {
357         loshe1.Append(shecur);
358       }
359     }
360 #ifdef OCCT_DEBUG
361 //    Standard_Integer nshe1 = loshe1.Extent();
362 #endif
363     TopTools_ListIteratorOfListOfShape itloshe1;
364     for( itloshe1.Initialize(loshe1); itloshe1.More(); itloshe1.Next() ) {
365       const TopoDS_Shape& shecur = itloshe1.Value();
366       myBuildTool.AddSolidShell(newsol,shecur);
367     }
368   }
369   
370   if ( icla2 == SHECLASAUTR || icla2 == SHEGARDAUTR ) {
371     // n.b. : ne pas prendre she2 accedant f2
372     TopTools_ListOfShape loshe2;
373     TopOpeBRepTool_ShapeExplorer ex2;
374     for (ex2.Init(sol2,TopAbs_SHELL); ex2.More(); ex2.Next()) {
375       const TopoDS_Shape& shecur = ex2.Current();
376       if (she2.IsEqual(shecur)) continue;
377       if      (icla2 == SHECLASAUTR) {
378         TopAbs_State state2 = KPclasSS(shecur,fac2,sol1);
379         if (state2 == myState2) loshe2.Append(shecur);
380       }
381       else if (icla2 == SHEGARDAUTR) {
382         loshe2.Append(shecur);
383       }
384     }
385 #ifdef OCCT_DEBUG
386 //    Standard_Integer nshe2 = loshe2.Extent();
387 #endif
388     TopTools_ListIteratorOfListOfShape itloshe2;
389     for( itloshe2.Initialize(loshe2); itloshe2.More(); itloshe2.Next() ) {
390       const TopoDS_Shape& shecur = itloshe2.Value();
391       myBuildTool.AddSolidShell(newsol,shecur);
392     }
393   }
394   
395   // le solide final
396   if ( !newsol.IsNull() ) {
397     lmergesha1.Append(newsol);
398   }
399   
400 } // MergeKPartiskole
401
402
403 //=======================================================================
404 //function : KPiskole
405 //purpose  : detection faces collees
406 //=======================================================================
407
408 Standard_Integer TopOpeBRepBuild_Builder::KPiskole()
409 {  
410   
411   TopTools_ListOfShape lShsd1,lShsd2; // liste de solides HasSameDomain
412   TopTools_ListOfShape lfhsd1,lfhsd2; // liste de faces HasSameDomain
413   
414   Standard_Boolean iskp1 = KPiskolesh(myShape1,lShsd1,lfhsd1);
415   if ( !iskp1 ) return 0;
416   Standard_Integer nfhsd1 = lfhsd1.Extent();
417   if ( nfhsd1 == 0 ) return 0;
418   
419   Standard_Boolean iskp2 = KPiskolesh(myShape2,lShsd2,lfhsd2);
420   if ( !iskp2 ) return 0;
421   Standard_Integer nfhsd2 = lfhsd2.Extent();
422   if ( nfhsd2 == 0 ) return 0;
423   
424   // Si l'un des objets est constitue de plusieur solides on passe
425   // dans le cas general , sinon on obtient
426   //** Exception ** Standard_OutOfRange: TCollection_IndexedDataMap::FindFromKey at
427   // TopOpeBRepBuild_Builder::MergeKPartiskole(this = 0xf7988), 
428   // line 397 in "/adv_21/MDL/k1deb/ref/prod/TopOpeBRepBuild/src/TopOpeBRepBuild_KPart.cxx"
429   // DPF le 10/07/1997
430   Standard_Integer nshsd1 = lShsd1.Extent();
431   Standard_Integer nshsd2 = lShsd2.Extent();
432   if (nshsd1>1 || nshsd2>1) return 0;
433   
434   TopTools_ListOfShape lf1,lf2;
435   TopTools_ListOfShape les; //section
436   
437   for (TopTools_ListIteratorOfListOfShape itlf1(lfhsd1);
438        itlf1.More();itlf1.Next()) {
439     
440     const TopoDS_Shape& f1 = itlf1.Value();
441 #ifdef OCCT_DEBUG
442 //    Standard_Boolean isb1 = myKPMAPf1f2.IsBound(f1); // DEB
443 #endif
444     lf1.Clear(); lf1.Append(f1);
445     lf2.Clear(); KPSameDomain(lf1,lf2);
446 #ifdef OCCT_DEBUG
447 //    Standard_Integer n1 = lf1.Extent();
448 //    Standard_Integer n2 = lf2.Extent();
449 #endif
450     
451 #ifdef OCCT_DEBUG
452     Standard_Integer iF1; Standard_Boolean tSPS1 = GtraceSPS(f1,iF1);
453     if(tSPS1) { 
454       GdumpSHA(f1, (char *) "KPiskole ");cout<<endl;
455       GdumpSAMDOM(lf2, (char *) "lf2 : ");
456     }
457 #endif
458     
459     for (TopTools_ListIteratorOfListOfShape itlf2(lf2); 
460          itlf2.More(); itlf2.Next() ) {
461       
462       const TopoDS_Shape& f2 = itlf2.Value();
463 #ifdef OCCT_DEBUG
464 //      Standard_Boolean isb2 = myKPMAPf1f2.IsBound(f2); // DEB
465 #endif
466       TopAbs_State state1,state2;
467       Standard_Boolean classok = KPiskoleFF(f1,f2,state1,state2);
468       if ( ! classok ) return 0;
469       
470       // on va reconstuire la face OUT
471       if ( state1 == TopAbs_OUT && state2 == TopAbs_IN) {
472         Standard_Boolean isb1 = myKPMAPf1f2.IsBound(f1);
473         if ( ! isb1 ) { TopTools_ListOfShape los; myKPMAPf1f2.Bind(f1,los); }
474         TopTools_ListOfShape& los = myKPMAPf1f2.ChangeFind(f1);
475         los.Append(f2);
476         
477         Standard_Boolean isb2 = myKPMAPf1f2.IsBound(f2);
478         if ( ! isb2 ) { TopTools_ListOfShape los1; myKPMAPf1f2.Bind(f2,los1); }
479       }
480       else if ( state2 == TopAbs_OUT && state1 == TopAbs_IN) {
481         Standard_Boolean isb2 = myKPMAPf1f2.IsBound(f2);
482         if ( ! isb2 ) { TopTools_ListOfShape los; myKPMAPf1f2.Bind(f2,los); }
483         TopTools_ListOfShape& los = myKPMAPf1f2.ChangeFind(f2);
484         los.Append(f1);
485         
486         Standard_Boolean isb1 = myKPMAPf1f2.IsBound(f1);
487         if ( ! isb1 ) { TopTools_ListOfShape los1; myKPMAPf1f2.Bind(f1,los1); }
488       }
489       
490       // les aretes de la face IN sont des aretes de section
491       TopoDS_Shape fw;
492       if      (state1 == TopAbs_IN) fw = f1;
493       else if (state2 == TopAbs_IN) fw = f2;
494       if (fw.IsNull()) continue;
495       
496       TopOpeBRepTool_ShapeExplorer ex(fw,TopAbs_EDGE);
497       for (;ex.More();ex.Next()) les.Append(ex.Current());
498     } 
499   }
500   
501   // aretes de section iskole
502   TopOpeBRepDS_DataStructure& DS = myDataStructure->ChangeDS();
503   DS.InitSectionEdges(); TopTools_ListIteratorOfListOfShape it(les);
504   for (;it.More();it.Next()) DS.AddSectionEdge(TopoDS::Edge(it.Value()));
505   
506   return 1;
507 } // TopOpeBRepBuild_Builder::KPiskole
508
509 //=======================================================================
510 //function : KPiskoleanalyse
511 //purpose  : 
512 //=======================================================================
513
514 void TopOpeBRepBuild_Builder::KPiskoleanalyse(const TopAbs_State Stfac1, const TopAbs_State Stfac2,
515                            const TopAbs_State Stsol1, const TopAbs_State Stsol2,
516                            Standard_Integer& ires,Standard_Integer& icla1,Standard_Integer& icla2) const 
517 {
518   ires = RESUNDEF; icla1 = icla2 = SHEUNDEF;
519   
520   if      (Opefus())  {
521     if      (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_OUT) {
522       ires = RESNEWSHE; icla1 = SHEGARDAUTR; icla2 = SHEGARDAUTR;
523     }
524     else if (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_IN ) {
525       ires = RESSHAPE1; icla1 = SHECLASAUTR; icla2 = SHEAUCU;
526     }
527     else if (Stsol1 == TopAbs_IN  && Stsol2 == TopAbs_OUT) {
528       ires = RESSHAPE2; icla1 = SHEAUCU; icla2 = SHECLASAUTR;
529     }
530   }
531   else if (Opec12()) {
532     if      (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_OUT) {
533       ires = RESSHAPE1; icla1 = SHEGARDAUTR; icla2 = SHEAUCU;
534     }
535     else if (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_IN ) {
536       ires = RESNEWSHE; icla1 = SHECLASAUTR; icla2 = SHEAUCU;
537     }
538     else if (Stsol1 == TopAbs_IN  && Stsol2 == TopAbs_OUT) {
539       ires = RESNULL; icla1 = icla2 = SHEAUCU;
540     }
541   }
542   else if (Opec21()) {
543     if      (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_OUT) {
544       ires = RESSHAPE2; icla1 = SHEAUCU; icla2 = SHEGARDAUTR;
545     }
546     else if (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_IN ) {
547       ires = RESNULL; icla1 = icla2 = SHEAUCU;
548     }
549     else if (Stsol1 == TopAbs_IN  && Stsol2 == TopAbs_OUT) {
550       ires = RESNEWSHE; icla1 = SHEAUCU; icla2 = SHECLASAUTR;
551     }
552   }
553   else if (Opecom()) {
554     if (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_OUT) { 
555       if (Stfac1 == TopAbs_IN) {
556         ires = RESFACE1; icla1 = icla2 = SHEAUCU;
557       }
558       if (Stfac2 == TopAbs_IN) {
559         ires = RESFACE2; icla1 = icla2 = SHEAUCU;
560       }
561     }
562     else if (Stsol1 == TopAbs_OUT && Stsol2 == TopAbs_IN ) {
563       ires = RESSHAPE2; icla1 = SHECLASAUTR; icla2 = SHEGARDAUTR;
564     }
565     else if (Stsol1 == TopAbs_IN  && Stsol2 == TopAbs_OUT) {
566       ires = RESSHAPE1; icla1 = SHEGARDAUTR; icla2 = SHECLASAUTR;
567     }
568   }
569   
570 #ifdef OCCT_DEBUG
571   Standard_Boolean TKPB = TopOpeBRepBuild_GettraceKPB();
572   if (TKPB) cout<<"ires = "<<ires<<" icla1 "<<icla1<<" icla2 "<<icla2<<endl;
573 #endif
574 } // TopOpeBRepBuild_Builder::KPiskoleanalyse
575
576 Standard_EXPORT void FUNKP_KPmakefaces(const TopOpeBRepBuild_Builder& BU,
577                                        const TopoDS_Shape& Fac1,
578                                        const TopTools_ListOfShape& LF2,
579                                        const TopAbs_State Stfac1,
580                                        const TopAbs_State /*Stfac2*/,
581                                        const Standard_Boolean R1,
582                                        const Standard_Boolean R2,
583                                        TopTools_ListOfShape& Lres)
584 {
585   // reconstruisons la face qui contient les autres
586   BRep_Builder BB;
587   TopoDS_Face fac; BB.MakeFace(fac);
588   
589   Standard_Integer rankIN = 0;
590   TopTools_ListOfShape LFSO,LFDO;
591   
592 #ifdef OCCT_DEBUG
593   Standard_Integer iF1; Standard_Boolean tSPS = BU.GtraceSPS(Fac1,iF1);
594   if(tSPS) { BU.GdumpSHA(Fac1, (char *) "KPmakeFace ");cout<<endl; }
595 #endif
596   
597   if (Stfac1 == TopAbs_OUT) {
598     TopoDS_Shape aLocalShape = Fac1.EmptyCopied();
599     fac = TopoDS::Face(aLocalShape);
600 //    fac = TopoDS::Face(Fac1.EmptyCopied());
601     Standard_Integer rankF = BU.GShapeRank(Fac1);
602     rankIN = (rankF) ? ( (rankF==1) ? 2 : 1) : 0;
603     BU.GFindSamDomSODO(Fac1,LFSO,LFDO);
604   }
605   else {
606     Standard_ProgramError::Raise("KPmakeface Stfac1 != OUT");
607   }
608   
609   if (rankIN == 0) {
610     Standard_ProgramError::Raise("KPmakeface rankIN = 0");
611   }
612   
613   TopTools_ListOfShape LFIN;
614   BU.GFindSameRank(LFSO,rankIN,LFIN);
615   BU.GFindSameRank(LFDO,rankIN,LFIN);
616   
617 #ifdef OCCT_DEBUG
618   if(tSPS) { 
619     BU.GdumpSAMDOM(LFSO, (char *) "LESO : ");
620     BU.GdumpSAMDOM(LFDO, (char *) "LEDO : ");
621     BU.GdumpSAMDOM(LFIN, (char *) "LFIN : ");
622   }
623 #endif
624   
625   TopOpeBRepBuild_WireToFace wtof;
626   
627   TopOpeBRepTool_ShapeExplorer wex1;
628   for (wex1.Init(Fac1,TopAbs_WIRE); wex1.More(); wex1.Next()) {
629     const TopoDS_Shape& wicur = wex1.Current();
630     TopoDS_Wire wori = TopoDS::Wire(wicur);
631     if (R1) wori.Complement();
632     wtof.AddWire(wori);
633     //myBuildTool.AddFaceWire(fac,wori);
634   }
635   
636   TopOpeBRepTool_ShapeExplorer wex2;
637   for (TopTools_ListIteratorOfListOfShape it2(LF2);it2.More();it2.Next()) {
638     const TopoDS_Shape& Fac2 = it2.Value();
639     for (wex2.Init(Fac2,TopAbs_WIRE); wex2.More(); wex2.Next()) {
640       const TopoDS_Shape& wicur = wex2.Current();
641       TopoDS_Wire wori = TopoDS::Wire(wicur);
642       if (R2) wori.Complement();
643       wtof.AddWire(wori);
644       //myBuildTool.AddFaceWire(fac,wori);
645     }
646   }
647   
648   const TopoDS_Face& F1 = TopoDS::Face(Fac1);
649   wtof.MakeFaces(F1,Lres);
650 #ifdef OCCT_DEBUG
651 //  Standard_Integer nlres = Lres.Extent(); // DEB
652 #endif
653   
654   return;
655 } // FUNKP_KPmakefaces
656
657 //=======================================================================
658 //function : KPmakeface
659 //purpose  : 
660 //=======================================================================
661
662 TopoDS_Shape TopOpeBRepBuild_Builder::KPmakeface(const TopoDS_Shape& Fac1,
663                                                  const TopTools_ListOfShape& LF2,
664                                                  const TopAbs_State Stfac1,
665                                                  const TopAbs_State /*Stfac2*/,
666                                                  const Standard_Boolean R1,
667                                                  const Standard_Boolean R2)
668 {
669   // reconstruisons la face qui contient l'autre
670   BRep_Builder BB;
671   TopoDS_Face fac; BB.MakeFace(fac);
672   
673   Standard_Integer rankIN = 0;
674   TopTools_ListOfShape LFSO,LFDO;
675   
676 #ifdef OCCT_DEBUG
677   Standard_Integer iF1;
678   Standard_Boolean tSPS = GtraceSPS(Fac1,iF1);
679   if(tSPS) {
680     GdumpSHA(Fac1, (char *) "KPmakeFace ");
681     cout<<endl;
682   }
683 #endif
684   
685   if (Stfac1 == TopAbs_OUT) {
686     TopoDS_Shape aLocalShape = Fac1.EmptyCopied();
687     fac = TopoDS::Face(aLocalShape);
688 //    fac = TopoDS::Face(Fac1.EmptyCopied());
689     Standard_Integer rankF = GShapeRank(Fac1);
690     rankIN = (rankF) ? ( (rankF==1) ? 2 : 1) : 0;
691     GFindSamDomSODO(Fac1,LFSO,LFDO);
692   }
693   else {
694     Standard_ProgramError::Raise("KPmakeface Stfac1 != OUT");
695     return myFaceReference; //Dummy
696   }
697   
698   if (rankIN == 0) {
699     Standard_ProgramError::Raise("KPmakeface rankIN = 0");
700     return myFaceReference; //Dummy
701   }
702   
703   TopTools_ListOfShape LFIN;
704   GFindSameRank(LFSO,rankIN,LFIN);
705   GFindSameRank(LFDO,rankIN,LFIN);
706   
707 #ifdef OCCT_DEBUG
708   if(tSPS) { 
709     GdumpSAMDOM(LFSO, (char *) "LESO : ");
710     GdumpSAMDOM(LFDO, (char *) "LEDO : ");
711     GdumpSAMDOM(LFIN, (char *) "LFIN : ");
712   }
713 #endif
714   
715   
716   TopOpeBRepTool_ShapeExplorer wex1;
717   for (wex1.Init(Fac1,TopAbs_WIRE); wex1.More(); wex1.Next()) {
718     const TopoDS_Shape& wicur = wex1.Current();
719     TopoDS_Shape wori = wicur;
720     if (R1) wori.Complement();
721     myBuildTool.AddFaceWire(fac,wori);
722   }
723   
724   TopOpeBRepTool_ShapeExplorer wex2;
725   for (TopTools_ListIteratorOfListOfShape it2(LF2);it2.More();it2.Next()) {
726     const TopoDS_Shape& Fac2 = it2.Value();
727     for (wex2.Init(Fac2,TopAbs_WIRE); wex2.More(); wex2.Next()) {
728       const TopoDS_Shape& wicur = wex2.Current();
729       TopoDS_Shape wori = wicur;
730       if (R2) wori.Complement();
731       myBuildTool.AddFaceWire(fac,wori);
732     }
733   }
734   
735   return fac;
736 } // TopOpeBRepBuild_Builder::KPmakeface
737
738 Standard_EXPORT Standard_Boolean FUNKP_KPiskolesh(const TopOpeBRepBuild_Builder& BU,
739                                                   const TopOpeBRepDS_DataStructure& BDS,
740                                                   const TopoDS_Shape& Sarg,
741                                                   TopTools_ListOfShape& lShsd,
742                                                   TopTools_ListOfShape& /*lfhsd*/)
743      // <lShsd> : the list of solids same domain with <Sarg>
744      // sol is  <lShsd>'s first solid      
745      // <lfhsd> : the list of <sol>'s same domain faces, none of the list carries geometric interf
746 {
747   if ( Sarg.IsNull() ) return Standard_False;
748   
749   Standard_Integer nsol = BU.KPlhsd(Sarg,TopAbs_SOLID,lShsd);
750   if ( nsol == 0 ) return Standard_False;
751   const TopoDS_Shape& sol = lShsd.First();
752   
753   TopTools_ListOfShape lfhg; 
754   Standard_Integer nfhg = BU.KPlhg(sol,TopAbs_FACE,lfhg);
755   if ( nfhg != 0 ) {
756     TopTools_ListIteratorOfListOfShape its(lfhg);
757     for(; its.More(); its.Next()) {
758       TopOpeBRepDS_ListIteratorOfListOfInterference iti(BDS.ShapeInterferences(its.Value()));
759       for (;iti.More();iti.Next()) {
760         Handle(TopOpeBRepDS_ShapeShapeInterference) ssi;
761         ssi = Handle(TopOpeBRepDS_ShapeShapeInterference)::DownCast(iti.Value());
762         if (ssi.IsNull()) {
763           return Standard_False;
764         }
765       }
766     }  
767   }
768   return Standard_True;
769 } // FUNKP_KPiskolesh
770
771 //=======================================================================
772 //function : KPiskolesh
773 //purpose  : 
774 // KPiskolesh : 
775 // S est il un shape traite par le cas particulier du collage ?
776 // si oui : retourne un solide et une liste de faces de collage
777 //=======================================================================
778
779 Standard_Boolean TopOpeBRepBuild_Builder::KPiskolesh(const TopoDS_Shape& Sarg,
780                                                      TopTools_ListOfShape& lShsd,
781                                                      TopTools_ListOfShape& lfhsd) const 
782 {
783 #ifdef OCCT_DEBUG
784   Standard_Boolean TKPB = TopOpeBRepBuild_GettraceKPB();
785 #endif
786   const TopOpeBRepDS_DataStructure& BDS = myDataStructure->DS();
787   Standard_Boolean iskolesh = FUNKP_KPiskolesh(*this,BDS,Sarg,lShsd,lfhsd);
788   if (!iskolesh) return Standard_False;
789   
790 #ifdef OCCT_DEBUG
791   Standard_Integer nfhsd =
792 #endif
793               KPlhsd(Sarg,TopAbs_FACE,lfhsd);
794   TopTools_ListIteratorOfListOfShape it(lfhsd);
795   for (; it.More(); it.Next() ) {
796     const TopoDS_Shape& fac = it.Value();    
797     Standard_Boolean isplan = FUN_tool_plane(fac); //pro7993 BUG
798     if ( !isplan ) return Standard_False;
799
800     Standard_Integer nw = KPls(fac,TopAbs_WIRE);
801     if (nw > 1) return Standard_False; 
802
803     TopTools_ListOfShape lehg;
804     Standard_Integer nehg = KPlhg(fac,TopAbs_EDGE,lehg);
805     if ( nehg != 0 ) return Standard_False;
806     
807 #ifdef OCCT_DEBUG
808     Standard_Integer isol = myDataStructure->Shape(Sarg);
809     Standard_Integer ifac = myDataStructure->Shape(fac); 
810     if(TKPB){cout<<"isol "<<isol<<endl;}
811     if(TKPB){cout<<"nfhsd  "<<nfhsd<<endl;}
812     if(TKPB){cout<<"ifac "<<ifac<<endl;}
813     if(TKPB){cout<<"isplan "<<isplan<<endl;}
814     if(TKPB){cout<<"nehg "<<nehg<<endl;}
815     if(TKPB){cout<<endl;}
816 #endif
817   }
818   
819   return Standard_True;
820 } // TopOpeBRepBuild_Builder::KPiskolesh