0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / TNaming / TNaming_Localizer.cxx
1 // Created on: 1997-06-11
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1997-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 <TNaming_Localizer.ixx>
18 #include <TNaming_Identifier.hxx>
19 #include <TNaming_ShapesSet.hxx>
20 #include <TNaming_ListIteratorOfListOfMapOfShape.hxx>
21 #include <TNaming_ListIteratorOfListOfIndexedDataMapOfShapeListOfShape.hxx>
22 #include <TNaming_DataMapOfShapeShapesSet.hxx>
23 #include <TNaming_IteratorOnShapesSet.hxx>
24 #include <TNaming_Evolution.hxx>
25 #include <TNaming_Iterator.hxx>
26 #include <TNaming_NewShapeIterator.hxx>
27 #include <TNaming_OldShapeIterator.hxx>
28 #include <TNaming_Tool.hxx>
29 #include <TNaming_ListOfNamedShape.hxx>
30 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
31 #include <TDF_Label.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <TopTools_ListOfShape.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
35 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
36 #include <TopTools_MapIteratorOfMapOfShape.hxx>
37 #include <TopExp.hxx> 
38 #include <TopExp_Explorer.hxx>
39
40 #define OCC351
41 #ifdef DEB
42 //#define MDTV_DEB_SC
43 #ifdef MDTV_DEB_SC
44 #include <TDF_Tool.hxx>
45 #include <TDF_MapIteratorOfLabelMap.hxx>
46
47 #include <TCollection_AsciiString.hxx>
48 #include <BRepTools.hxx>
49 void LPrintEntry(const TDF_Label&       label)
50 {
51   TCollection_AsciiString entry;
52   TDF_Tool::Entry(label, entry);
53   cout << "LabelEntry = "<< entry << endl;
54 }
55 static void LWrite(const TopoDS_Shape& shape,
56                       const Standard_CString filename) 
57 {
58   char buf[256];
59   if(strlen(filename) > 256) return;
60 #if defined WNT 
61   strcpy_s (buf, filename);
62 #else
63   strcpy (buf, filename);
64 #endif
65   char* p = buf;
66   while (*p) {
67     if(*p == ':')
68       *p = '-';
69     p++;
70   }
71   ofstream save (buf);
72   if(!save) 
73     cout << "File " << buf << " was not created: rdstate = " << save.rdstate() << endl;
74   save << "DBRep_DrawableShape" << endl << endl;
75   if(!shape.IsNull()) BRepTools::Write(shape, save);
76   save.close();
77 }
78
79 //=======================================================================
80 static void LWriteNSOnLabel (const Handle(TNaming_NamedShape)& NS,  
81                                      const Standard_CString filename) 
82 {
83   if(!NS.IsNull() && !NS->IsEmpty() ) {
84     TCollection_AsciiString aNam (filename);
85     TCollection_AsciiString oldS ("_Old");
86     TCollection_AsciiString newS ("_New_");
87     Standard_Integer i(0);
88     TNaming_Iterator it(NS);
89     for(;it.More(); it.Next(),i++) {
90       TCollection_AsciiString aName1 = aNam + oldS + i + ".brep";
91       TCollection_AsciiString aName2 = aNam + newS + i + ".brep";
92       const TopoDS_Shape& oldShape = it.OldShape();
93       const TopoDS_Shape& newShape = it.NewShape();
94       if(!oldShape.IsNull())
95         LWrite ( oldShape, aName1.ToCString());
96       if(!newShape.IsNull())
97         LWrite ( newShape, aName2.ToCString());      
98     }
99   }
100 }
101 #endif
102 #endif
103 //=======================================================================
104  //function : FindFeatureInAncestors
105 //purpose  : Cherche les ancetres de S qui sont sous des labels
106 //=======================================================================
107
108 void TNaming_Localizer::FindFeaturesInAncestors 
109 (const TopoDS_Shape&                        S,
110  const TopoDS_Shape&                        Context,
111  TopTools_MapOfShape&                       AncInFeature)
112 {
113 #ifdef MDTV_DEB_SC
114   LWrite(S, "Localizer_S.brep");
115   LWrite(Context, "Localizer_Context.brep");
116 #endif
117
118   const TopTools_IndexedDataMapOfShapeListOfShape& Anc = Ancestors(Context,S.ShapeType());
119   
120   if (Anc.Contains(S)) {
121 #ifdef MDTV_DEB_SC
122     cout <<"Localizer: S in ancestor" <<endl;
123 #endif
124     const TopTools_ListOfShape& L = Anc.FindFromKey(S);
125     TopTools_ListIteratorOfListOfShape itL(L);
126     for (; itL.More(); itL.Next()) {
127       const TopoDS_Shape& AS = itL.Value();
128 #ifdef MDTV_DEB_SC
129       LWrite(AS, "Localizer_AS.brep");
130 #endif      
131       Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape(AS,myUS->Label());
132       if (!NS.IsNull()) {
133 //      if (TNaming_Tool::HasLabel(myUS,AS)) {
134         AncInFeature.Add(AS);
135       }
136       else if (AS.ShapeType() > TopAbs_FACE) {
137         FindFeaturesInAncestors (AS, Context, AncInFeature);
138       }
139       else {
140 #ifdef DEB
141         cout <<" TNaming_Localization : Failure in the research of ancetres in TDF"<<endl;
142 #endif
143       }
144     }
145     
146   }
147   else {
148 #ifdef DEB
149     cout <<" TNaming_Localization : S n est pas dans le solide"<<endl;//S is not in the solid
150 #endif
151   }
152 }
153
154 //=======================================================================
155 //function : SubShapes
156 //purpose  : 
157 //=======================================================================
158
159 const TopTools_MapOfShape& TNaming_Localizer::SubShapes (const TopoDS_Shape&    In,
160                                                          const TopAbs_ShapeEnum TS)
161 {
162
163   TopTools_ListIteratorOfListOfShape     itS(myShapeWithSubShapes) ;
164   TNaming_ListIteratorOfListOfMapOfShape itSS(mySubShapes);
165 //  Standard_Boolean Found = Standard_False;
166   for (; itS.More(); itS.Next(),itSS.Next()) {
167     if (In.IsSame(itS.Value())) {
168       TopTools_MapOfShape& SubShapes = itSS.Value();
169       for (TopExp_Explorer exp(In,TS); exp.More(); exp.Next()) {
170         const TopoDS_Shape& SS = exp.Current();
171         if (SubShapes.Contains(SS)) {
172           break;
173         }
174         SubShapes.Add(SS);
175       }
176       return SubShapes;
177     }
178   }
179
180   TopTools_MapOfShape emptyMap;
181   mySubShapes.Prepend(emptyMap);
182   myShapeWithSubShapes.Prepend(In);
183   
184   TopTools_MapOfShape& SubShapes = mySubShapes.First();
185   for (TopExp_Explorer exp(In,TS); exp.More(); exp.Next()) {
186     const TopoDS_Shape& SS = exp.Current();
187     if (SubShapes.Contains(SS)) {
188       break;
189     }
190     SubShapes.Add(SS);
191   }
192   return SubShapes;
193 }
194
195
196 //=======================================================================
197 //function : Ancestors
198 //purpose  : 
199 //=======================================================================
200
201 const TopTools_IndexedDataMapOfShapeListOfShape& TNaming_Localizer::Ancestors
202 (const TopoDS_Shape&     In,
203  const TopAbs_ShapeEnum  TS)
204 {
205   TopTools_ListIteratorOfListOfShape                           itS(myShapeWithAncestors) ;
206   TNaming_ListIteratorOfListOfIndexedDataMapOfShapeListOfShape itA(myAncestors);
207 //  Standard_Boolean Found = Standard_False;
208   for (; itS.More(); itS.Next(),itA.Next()) {
209     if (In.IsSame(itS.Value())) {
210       //-----------------------
211       // Ancetres existent.
212       //-----------------------
213       TopTools_IndexedDataMapOfShapeListOfShape& Anc = itA.Value();
214
215       TopExp_Explorer exp(In,TS);
216 #ifdef DEB
217       if (!exp.More())   cout <<" TNaming_Localization : Construction ancetres impossible"<<endl;
218 #endif
219       const TopoDS_Shape& SS = exp.Current();
220       
221       if (Anc.Contains(SS)) {
222         return Anc;
223       }
224       else {
225         //----------------------------
226         // Completion des ancetres.
227         //----------------------------
228         TopAbs_ShapeEnum TA = TopAbs_FACE;
229         if (TS == TopAbs_EDGE)   TA = TopAbs_FACE;
230         if (TS == TopAbs_VERTEX) TA = TopAbs_EDGE;
231         if (TA >= In.ShapeType()) {
232           TopExp::MapShapesAndAncestors(In, TS, TA, Anc);
233         }
234         else {
235 #ifdef DEB
236           cout <<" TNaming_Localization : Construction ancetres impossible"<<endl;
237 #endif
238         }
239       }
240       return Anc;
241     }
242   }
243   //-----------------------------------
244   // Construction des ancetres
245   //-----------------------------------
246   TopTools_IndexedDataMapOfShapeListOfShape emptyAnc;
247   myShapeWithAncestors.Prepend(In);
248   myAncestors         .Prepend(emptyAnc);
249
250   TopAbs_ShapeEnum TA=TopAbs_COMPOUND;
251
252   if (TS == TopAbs_VERTEX)      TA = TopAbs_EDGE;
253   else if (TS == TopAbs_EDGE)   TA = TopAbs_FACE;
254   else if (TS == TopAbs_FACE)   TA = TopAbs_SOLID;
255   if ((TS == TopAbs_EDGE || TS == TopAbs_VERTEX || TS == TopAbs_FACE) && TA >= In.ShapeType()) {
256     TopExp::MapShapesAndAncestors(In, TS, TA, myAncestors.First());
257   }
258   else {
259 #ifdef DEB
260     cout <<" TNaming_Localization : Construction ancetres impossible"<<endl;
261 #endif
262   }
263   return myAncestors.First();
264 }
265
266 //=======================================================================
267 //function : IsNew
268 //purpose  : 
269 //=======================================================================
270
271 Standard_Boolean TNaming_Localizer::IsNew (const TopoDS_Shape&    S,
272                                            const Handle(TNaming_NamedShape)& NS)
273 {
274   TNaming_Iterator itLab(NS);
275   for (; itLab.More(); itLab.Next()) {
276     if (itLab.OldShape().IsSame(S)) {
277       return Standard_False;
278     }
279     if (itLab.NewShape().IsSame(S)) {
280       return Standard_True;
281     }
282   }
283 #ifdef DEB
284   cout <<"TNaming_Localizer:IsNewInLab : Shape n est pas dans le Label."<<endl;
285 #endif
286   return Standard_False;
287 }
288
289
290 //=======================================================================
291 //function : Back
292 //purpose  : 
293 //=======================================================================
294
295 void TNaming_Localizer::GoBack (const TopoDS_Shape&         S,
296                                 const TDF_Label&            Lab,
297                                 const TNaming_Evolution     Evol,
298                                 TopTools_ListOfShape&       LBS,
299                                 TNaming_ListOfNamedShape&   LBNS)
300 {
301 //  Standard_Integer TrDef;
302
303   TNaming_OldShapeIterator it(S,myCurTrans,myUS);
304   TopoDS_Shape             Sol; 
305   if (!it.More()) {
306     //-----------------------------------------------------------
307     // Pas d'ascendants => Recherche et exploration  du contenant
308     //----------------------------------------------------------
309     const TDF_Label& Father  = Lab.Father();
310     TNaming_Iterator itLab(Father);
311     if(itLab.More()) 
312       Sol = itLab.OldShape();      
313     //-------------------------------------------
314     // Recherche des ancetres dans des features.
315     //-------------------------------------------
316     if (!Sol.IsNull()) {
317       TopTools_MapOfShape AncInFeature;
318       FindFeaturesInAncestors (S, Sol, AncInFeature); 
319       TopTools_MapIteratorOfMapOfShape itF(AncInFeature);
320       for ( ; itF.More(); itF.Next()) {
321         const TopoDS_Shape& AncOfS = itF.Key();
322         LBS  .Append(AncOfS);
323         LBNS.Append(TNaming_Tool::NamedShape(AncOfS,Lab));
324       }
325     }
326   } 
327   else {
328     for ( ; it.More(); it.Next()) {
329   //      if (it.NamedShape()->Evolution() != TNaming_SELECTED) {
330       if (it.NamedShape()->Evolution() == Evol) {
331         Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape(it.Shape(),Lab);
332         if (!NS.IsNull()) {
333           LBS.Append  (it.Shape());
334           LBNS.Append (TNaming_Tool::NamedShape(it.Shape(),Lab));
335         }
336         else {
337 #ifdef DEB
338           cout <<"TNaming_Localizer: Shape modifie sans avoir ete cree"<<endl;
339 #endif
340         }
341       }
342     }
343   }
344 }
345 /*
346 //=======================================================================
347 //function : Backward
348 //purpose  :
349 //=======================================================================
350
351 void TNaming_Localizer::Backward (const TopoDS_Shape&  S, 
352                                   TDF_LabelMap&        Primitives, 
353                                   TopTools_MapOfShape& ValidShapes)
354
355   Standard_Integer  PrevTrans = myCurTrans - 1;
356   Standard_Integer  TrDef;
357   TDF_Label         Lab      = TNaming_Tool::Label (myUS, S, TrDef);
358   TNaming_Evolution Evol     = Evolution(Lab);
359
360
361   TopTools_ListOfShape  LBS;
362   TDF_LabelList         LBLab;
363
364   GoBack(S,Lab,LBS,LBLab);
365   
366
367   TopTools_ListIteratorOfListOfShape itLBS  (LBS);
368   TDF_ListIteratorOfLabelList        itLBLab(LBLab);
369
370   if (LBS.IsEmpty()) {
371     Primitives.Add(Lab);
372   }
373   for ( ; itLBS.More(); itLBS.Next(), itLBLab.Next()) {
374     const TopoDS_Shape& OS    = itLBS.Value();
375     const TDF_Label&    LabOS = itLBLab.Value();
376
377     Evol = Evolution(LabOS);
378
379     if (TNaming_Tool::ValidUntil(OS,myUS) >= myCurTrans) {
380       //---------------------------------------------------------
381       // Le Shape est valid dans la transaction myCurTrans => STOP
382       //---------------------------------------------------------
383       ValidShapes.Add(OS);
384     }
385     else if (Evol == TNaming_PRIMITIVE) {    
386       Primitives.Add(LabOS);
387     }
388     else if ((Evol == TNaming_GENERATED) && IsNewInLab (OS,LabOS,PrevTrans)) {
389       //--------------------------------------------------------------
390       // Passage  par une generation
391       // le shape dans myCurTrans descendra d un element de cet attribut.
392       // Localisation de OS dans la transaction courrante.
393       // les shapes obtenus seront des antecedants du shape cherche.
394       //--------------------------------------------------------------
395       
396       // A faire seulememt si OS est un newShape dans LabOS.
397       TNaming_ShapesSet ResGen;    
398       TopoDS_Shape      PrevIn;
399       TDF_Label         Father  = LabOS.Father();
400       TNaming_Iterator  itLab(Father,PrevTrans);
401       for (; itLab.More(); itLab.Next()) {
402         PrevIn= itLab.OldShape();
403         break;
404       }
405       Localize(PrevIn,LabOS,OS,ResGen);
406       for (TNaming_IteratorOnShapesSet itLoc(ResGen); itLoc.More(); itLoc.Next()) {
407         ValidShapes.Add(itLoc.Value());
408       } 
409     }
410     else if (Evol == TNaming_SELECTED) {
411       //PAS FINI.
412       TNaming_ShapesSet ResSel;
413       TopoDS_Shape      PrevIn,CurIn;
414 //      FindIn  (LabOS,PrevIn,CurIn);  
415       Localize(PrevIn,CurIn,OS,ResSel);
416       for (TNaming_IteratorOnShapesSet itLoc(ResSel); itLoc.More(); itLoc.Next()) {
417         ValidShapes.Add(itLoc.Value());
418       }
419     }
420     else {
421       Backward(itLBS.Value(),Primitives,ValidShapes);
422     }
423   }
424 }
425 */
426
427 //=======================================================================
428 //function : NamedShape
429 //purpose  : 
430 //=======================================================================
431
432 Handle(TNaming_NamedShape) NamedShape(const TDF_Label& Lab)
433 {
434   Handle(TNaming_NamedShape) NS;
435   Lab.FindAttribute(TNaming_NamedShape::GetID(),NS);
436   return NS;
437 }
438
439 //=======================================================================
440 //function : Backward
441 //purpose  :
442 //=======================================================================
443
444 void TNaming_Localizer::Backward (const Handle(TNaming_NamedShape)& NS,
445                                   const TopoDS_Shape&               S, 
446                                   TNaming_MapOfNamedShape&          Primitives, 
447                                   TopTools_MapOfShape&              Shapes)
448
449   TNaming_Evolution Evol     = NS->Evolution();
450   TDF_Label         LabNS    = NS->Label();
451
452   TopTools_ListOfShape      LBS;
453   TNaming_ListOfNamedShape  LBNS;
454
455   GoBack(S,LabNS,Evol,LBS,LBNS);
456   
457
458   TopTools_ListIteratorOfListOfShape     itLBS  (LBS);
459   TNaming_ListIteratorOfListOfNamedShape itLBNS (LBNS);
460
461   if (LBS.IsEmpty()) {
462     Primitives.Add(NS);
463   }
464   for ( ; itLBS.More(); itLBS.Next(), itLBNS.Next()) {
465     const TopoDS_Shape&        OS  = itLBS.Value();
466     Handle(TNaming_NamedShape) NOS = itLBNS.Value();
467     Evol = NOS->Evolution();
468     if (Evol == TNaming_PRIMITIVE) {    
469       Primitives.Add(NOS);
470     }
471     else if (Evol == TNaming_GENERATED) {
472       Shapes.Add(OS);
473     }
474     else {
475       Backward(NOS, itLBS.Value(),Primitives,Shapes);
476     }
477   }
478 }
479
480 //=======================================================================
481 //function : ValidCandidat
482 //purpose  : 
483 //=======================================================================
484
485 #ifdef DEB
486 /*static Standard_Boolean StoreValid (const TopoDS_Shape&        S,
487                                     const TopTools_MapOfShape& ShapeOfSol,
488                                     TopAbs_ShapeEnum           TargetType,
489                                     TNaming_ShapesSet&         Res) 
490 {
491   Standard_Boolean Valid = Standard_False;
492
493   if (ShapeOfSol.Contains(S)) {
494     if (S.ShapeType() == TargetType) {
495       Res.Add(S);
496       return Standard_True;
497     }
498     else  if (S.ShapeType() < TargetType) {
499       for (TopExp_Explorer exp(S,TargetType); exp.More(); exp.Next()) {
500         const TopoDS_Shape& SS = exp.Current();
501         Res.Add(SS);
502         Valid = Standard_True;
503       }
504     }
505   }
506   return Valid;
507 }*/
508 #endif
509
510 /*
511 //=======================================================================
512 //function : Forward
513 //purpose  : 
514 //=======================================================================
515
516 void TNaming_Localizer::GoForward(const TopoDS_Shape&               S,
517                                   const TopTools_MapOfShape&        Target,
518                                   const TopAbs_ShapeEnum            TargetType,
519                                   TNaming_ShapesSet&                Res)
520                                          
521 {
522   Standard_Integer TrDef;
523   TDF_Label Lab = TNaming_Tool::Label (myUS, S, TrDef);
524   if (StoreValid (S, Target, TargetType, Res)) {
525     return;
526   }
527   TNaming_Evolution        Evol = Evolution(Lab);
528   TNaming_NewShapeIterator NewIt(S,myCurTrans,myUS);
529   
530   for ( ; NewIt.More(); NewIt.Next()) {
531     const TopoDS_Shape& NS = NewIt.Shape();
532     GoForward ( NS, Target, TargetType, Res);
533   }
534 }
535
536 */
537
538 //=======================================================================
539 //function : FindNeighbourg
540 //purpose  : 
541 //=======================================================================
542
543 void TNaming_Localizer::FindNeighbourg (const TopoDS_Shape&      Sol,
544                                         const TopoDS_Shape&      S,
545                                         TopTools_MapOfShape&     Neighbourg)
546 {  
547   TopAbs_ShapeEnum       TA = S.ShapeType();
548   TopAbs_ShapeEnum       TS=TopAbs_COMPOUND;
549
550   if (TA == TopAbs_FACE) TS = TopAbs_EDGE;
551   if (TA == TopAbs_EDGE) TS = TopAbs_VERTEX;
552   if (TA == TopAbs_VERTEX) TS = TopAbs_VERTEX; // szy 30.03.10
553   const TopTools_IndexedDataMapOfShapeListOfShape& Anc = Ancestors(Sol,TS); 
554 // szy 30.03.10 to process case when Candidate is of type Vertex
555 //  if (TA == TopAbs_VERTEX) {
556 //#ifdef DEB
557 //    cout <<"construction voisins des vertex impossible"<<endl;
558 //#endif
559 //    return;
560 //  }
561   for (TopExp_Explorer  Exp(S,TS); Exp.More(); Exp.Next()) {
562     const TopoDS_Shape& SS = Exp.Current();
563     if (!Anc.Contains(SS)) {
564       //----------------------------------------------------
565       // Construction des ancetres
566       //----------------------------------------------------
567       break;
568     }
569     else {
570       TopTools_ListIteratorOfListOfShape itL(Anc.FindFromKey(SS));
571       for ( ; itL.More(); itL.Next()) {
572         if (!itL.Value().IsSame(S)) {
573           Neighbourg.Add(itL.Value());
574         }
575       }
576     }
577   }
578 }
579
580 //=======================================================================
581 //function : TNaming_Localizer
582 //purpose  : 
583 //=======================================================================
584
585 TNaming_Localizer::TNaming_Localizer() 
586 {
587 }
588
589
590 //=======================================================================
591 //function : Init
592 //purpose  : 
593 //=======================================================================
594
595 void TNaming_Localizer::Init(const Handle(TNaming_UsedShapes)& US,
596                              const Standard_Integer            CurTrans) 
597 {
598   myUS       = US;
599   myCurTrans = CurTrans;
600 }
601
602
603 //=======================================================================
604 //function : Explode
605 //purpose  : 
606 //=======================================================================
607
608 #ifdef DEB
609 /*static void Explode (TNaming_ShapesSet& Res,
610                      TopAbs_ShapeEnum   TS,
611                      TNaming_ShapesSet& ResGoodType) 
612 {
613   TNaming_IteratorOnShapesSet it(Res);
614   for ( ; it.More(); it.Next()) {
615     const TopoDS_Shape& S = it.Value();
616     TopExp_Explorer exp(S,TS);
617     for (; exp.More(); exp.Next()) {
618       ResGoodType.Add(exp.Current());
619     }
620   }
621 } */ 
622 #endif
623
624 /*
625 //=======================================================================
626 //function : Localize
627 //purpose  : 
628 //=======================================================================
629
630 void TNaming_Localizer::Localize(const TopoDS_Shape& PrevIn,
631                                  const TopoDS_Shape& CurIn ,
632                                  const TopoDS_Shape& S,
633                                  TNaming_ShapesSet& Res) 
634 {  
635   Res.Clear();
636   TDF_LabelMap                 Primitives;
637   TopTools_MapOfShape          ValidShapes;
638   Standard_Integer             PrevTrans = myCurTrans-1;
639
640   const TopTools_MapOfShape& CurSubShapes = SubShapes (CurIn,S.ShapeType());
641
642   Standard_Boolean HasLabel = TNaming_Tool::HasLabel(myUS,S);
643
644   if (HasLabel) {
645     Standard_Integer  TrDef;
646     TDF_Label         Lab      = TNaming_Tool::Label (myUS, S, TrDef);
647     TNaming_Evolution Evol     = Evolution(Lab);
648     if (Evol == TNaming_PRIMITIVE) {
649       //------------------------
650       // Label est une primitive
651       //------------------------
652       Primitives.Add(Lab);
653       Forward  (CurSubShapes,S.ShapeType(),Primitives,ValidShapes,Res);
654       return;
655     }
656     if ((Evol == TNaming_GENERATED) && IsNewInLab (S,Lab,PrevTrans)) {
657       Localize(PrevIn,Lab,S,Res);
658       return;
659     }
660   }
661   
662   if (HasLabel && HasAscendant(myUS,S,PrevTrans)) {
663     //-------------------
664     // Remontee Descente.
665     //-------------------
666     Backward (S, Primitives, ValidShapes);
667     Forward  (CurSubShapes,S.ShapeType(),Primitives,ValidShapes,Res);
668   }
669   else {
670     //----------------------------------------------
671     // Localisation des ancetres.
672     //----------------------------------------------
673     TopTools_MapOfShape AncInFeature;
674     FindFeaturesInAncestors (S, PrevIn, AncInFeature); 
675     TopTools_MapIteratorOfMapOfShape itF(AncInFeature);
676     Standard_Boolean First = Standard_True;
677
678     for ( ; itF.More(); itF.Next()) {
679       const TopoDS_Shape& AncOfS = itF.Key();
680       TNaming_ShapesSet   ResAnc;
681
682       Localize (PrevIn, CurIn, AncOfS, ResAnc);
683
684       //---------------------------
685       // Res = Commun des resultats
686       //---------------------------
687       if (First) {
688         Explode (ResAnc,S.ShapeType(),Res);
689         First = 0;
690       }
691       else {
692         TNaming_ShapesSet ResGoodType;
693         Explode (ResAnc,S.ShapeType(),ResGoodType);
694         Res.Filter(ResGoodType);
695       }
696     }
697   }
698 }
699 */
700
701 //=======================================================================
702 //function : FindGenerator
703 //purpose  : Finds all generators of the <S> kept in <NS>
704 //=======================================================================
705
706 void TNaming_Localizer::FindGenerator (const Handle(TNaming_NamedShape)& NS,
707                                        const TopoDS_Shape&               S,
708                                        TopTools_ListOfShape& theListOfGenerators)
709      
710
711   Handle(TNaming_UsedShapes) US;
712   TDF_Label LabNS = NS->Label();
713   (LabNS.Root()).FindAttribute(TNaming_UsedShapes::GetID(),US);
714   
715   for (TNaming_OldShapeIterator it (S,US); it.More(); it.Next()) {
716     if (it.Label() == LabNS) {
717       theListOfGenerators.Append(it.Shape());
718 //      break; //szy 16.10.03
719
720     }
721   }  
722 }
723
724 //=======================================================================
725 //function : FindShapeContext
726 //purpose  : Finds context of the shape <S>.
727 //         : Looks for all oldshapes kept at father label of <NS>.
728 //         : If <S> validated as subshape of one of the old shapes -
729 //         : this oldshape is Context.
730 //=======================================================================
731
732 void TNaming_Localizer::FindShapeContext (const Handle(TNaming_NamedShape)& NS,
733                                           const TopoDS_Shape&               S,
734                                                 TopoDS_Shape&               SC)
735      
736
737 #ifdef MDTV_DEB_SC
738     LWrite(S, "FSC_Sel.brep"); LPrintEntry( NS->Label());
739 #endif
740   TopTools_ListOfShape aList;
741   TDF_Label Father = NS->Label().Father();
742   TNaming_Iterator  itLab(Father);
743   for (; itLab.More(); itLab.Next()) {
744     aList.Append(itLab.OldShape()); //szy
745   }
746 // szy 
747   TopTools_ListIteratorOfListOfShape it(aList);
748   Standard_Boolean found = 0;
749   for(;it.More();it.Next()) {
750     SC = it.Value();
751 #ifdef MDTV_DEB_SC
752     LWrite(SC, "FSC_OldShape.brep");
753 #endif
754     if (SC.IsNull()) continue;
755     else {
756       if (SC.ShapeType() < S.ShapeType()) {
757         for (TopExp_Explorer exp(SC,S.ShapeType()); exp.More(); exp.Next()) {
758           if (exp.Current().IsSame(S)) {
759             found = 1;
760 #ifdef MDTV_DEB_SC
761             cout << "Find Context shape = " << SC.TShape() << "ShapeType = " << SC.ShapeType() <<endl;
762 #endif      
763             break;
764           }
765         }
766         if(found) break;
767       }
768     }
769   }
770 // end szy
771 #ifdef OCC351
772   if (!SC.IsNull()) {
773     Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(SC,Father);
774     if (!aNS.IsNull()) {
775 #ifdef MDTV_DEB_SC
776       cout << "FindShapeContext: ";LPrintEntry(aNS->Label());
777 #endif
778       if (aNS->Label().Father().FindAttribute(TNaming_NamedShape::GetID(),aNS)) {
779         TopoDS_Shape aShape;
780 #ifdef MDTV_DEB_SC
781         LWriteNSOnLabel(aNS, "FSC");
782 #endif
783         TNaming_Iterator anIter(aNS->Label());
784         for(;anIter.More();anIter.Next()) {
785           aShape = anIter.NewShape();
786           if (!aShape.IsNull()) break;
787         }
788         if (!aShape.IsNull()) SC=aShape;
789       }
790     }
791   }
792 #endif
793
794 }
795
796 /*
797 //=======================================================================
798 //function : Localize
799 //purpose  : 
800 //=======================================================================
801
802 void TNaming_Localizer::Localize(const TopoDS_Shape& PrevIn,
803                                  const TDF_Label&    InLab ,
804                                  const TopoDS_Shape& S,
805                                  TNaming_ShapesSet& Res) 
806 {  
807   Res.Clear();
808   TDF_LabelMap                 Primitives;
809   TopTools_MapOfShape          ValidShapes;
810   Standard_Integer             PrevTrans = myCurTrans - 1;
811
812   //---------------------------------------------
813   // Recuperation du nouveau contenant generateur.
814   //---------------------------------------------
815   TopoDS_Shape      CurIn;
816   TDF_Label         Father  = InLab.Father();
817   TNaming_Iterator  itLab(Father,myCurTrans);
818   for (; itLab.More(); itLab.Next()) {
819     CurIn= itLab.OldShape();
820     break;
821   }
822   
823   Standard_Boolean First = 1;
824   TNaming_OldShapeIterator OldIt(S, PrevTrans,myUS);
825   
826   for (; OldIt.More(); OldIt.Next()) {
827     if (OldIt.Label().IsEqual(InLab)) {
828       TNaming_ShapesSet RO;
829       TNaming_ShapesSet RInLab;      
830       const TopoDS_Shape& OS = OldIt.Shape();
831       //---------------------------------
832       // Localisation des generateurs.
833       //---------------------------------
834       Localize(PrevIn,CurIn ,OS, RO);
835       
836       //--------------------------------------------------------------------
837       // Resultat = intersection des descendants(dans InLab) des generateurs
838       //--------------------------------------------------------------------
839       TNaming_IteratorOnShapesSet itRO(RO);
840       for (; itRO.More(); itRO.Next()) {
841         const TopoDS_Shape& CO = itRO.Value();
842         TNaming_NewShapeIterator NewIt(CO,myCurTrans,myUS);
843         for (; NewIt.More(); NewIt.Next()) {
844           if (NewIt.Label().IsEqual(InLab)) {
845             if (First) Res.Add(NewIt.Shape());
846             else {
847               RInLab.Add(NewIt.Shape());
848             }
849           }
850         }
851       }
852       if (!First) Res.Filter(RInLab);
853       First = Standard_False;
854     }
855   }
856 }
857
858 //=======================================================================
859 //function : Forward
860 //purpose  : 
861 //=======================================================================
862
863 void  TNaming_Localizer::Forward(const TopTools_MapOfShape&  CurSubShapes,
864                                  const TopAbs_ShapeEnum      TS,
865                                  const TDF_LabelMap&         Primitives,
866                                  const TopTools_MapOfShape&  ValidShapes,
867                                  TNaming_ShapesSet&          Res)
868 {
869   //-------------------------------------------------------
870   // Descente dans la transaction courrante = myCurTrans
871   //----------------------------------------------------------    
872   TopTools_MapIteratorOfMapOfShape itV(ValidShapes);
873   Standard_Boolean  First = 1;
874   Standard_Boolean  YaFromValid      = 0;
875   Standard_Boolean  YaFromPrimitives = 0;
876   
877   for (; itV.More(); itV.Next()) {
878     YaFromValid = 1;
879     const TopoDS_Shape& NS = itV.Key();
880     if (First) {
881       GoForward  (NS, CurSubShapes, TS, Res);    
882       First = 0;
883     }
884     else {
885       TNaming_ShapesSet   ResNS;
886       GoForward  (NS, CurSubShapes, TS, ResNS);
887       Res.Filter(ResNS);
888     }
889   } 
890
891   TDF_MapIteratorOfLabelMap itP(Primitives);
892   TNaming_ShapesSet ResPrim;
893
894   for ( ; itP.More(); itP.Next()) {
895     YaFromPrimitives = 1;
896     const TDF_Label&  Lab = itP.Key();
897     TNaming_Iterator  itL(Lab,myCurTrans);
898     TNaming_ShapesSet ResLab;
899
900     for (; itL.More(); itL.Next()) {
901       const TopoDS_Shape& NS = itL.NewShape();
902       GoForward  (NS, CurSubShapes, TS, ResLab);    
903     }
904     if (First) {
905       ResPrim = ResLab;
906       First   = 0;
907     }
908     else 
909       ResPrim.Filter(ResLab);
910   }
911
912   if (YaFromValid) {
913     if (YaFromPrimitives) {
914       Res.Filter(ResPrim);
915     }
916   }
917   else {
918     Res = ResPrim;
919   }
920 }
921
922
923 //=======================================================================
924 //function : FilterbyNeighbourgs
925 //purpose  : 
926 //=======================================================================
927
928 void TNaming_Localizer::FilterByNeighbourgs(const TopoDS_Shape& PrevIn,
929                                             const TopoDS_Shape& CurIn ,
930                                             const TopoDS_Shape& S,
931                                             TNaming_ShapesSet& Res) 
932 {
933   TopTools_MapOfShape         Neighbourg;
934   TopAbs_ShapeEnum            TA = S.ShapeType();
935   TopAbs_ShapeEnum            TS = TopAbs_ShapeEnum (S.ShapeType()+1);
936   TNaming_DataMapOfShapeShapesSet MapShapeRes;
937   const TopTools_IndexedDataMapOfShapeListOfShape& PreAnc = 
938     Ancestors(PrevIn,TS);
939
940   //--------------------------------
941   // Construction des Voisins.
942   //--------------------------------
943   FindNeighbourg (PrevIn,PreAnc,S,Neighbourg);
944   TopTools_MapIteratorOfMapOfShape itNeig(Neighbourg);
945   
946   TNaming_ShapesSet NewNeig;
947   for (; itNeig.More(); itNeig.Next()) {
948     const TopoDS_Shape& Neig = itNeig.Key();
949     //--------------------------------------------
950     // Localisation des voisins.
951     //--------------------------------------------
952     if (!MapShapeRes.IsBound(Neig)) { 
953       TNaming_ShapesSet ResNeig;
954       Localize(PrevIn,CurIn,Neig,ResNeig);
955       MapShapeRes.Bind(Neig,ResNeig);
956       NewNeig.Add(ResNeig);
957     }
958     else {
959       NewNeig.Add(MapShapeRes(Neig));
960     }
961   }
962
963   //---------------------------------------------
964   // Filtre la solution par le resultat du voisin.
965   // ie : F est solution si ses voisins dans CurSol
966   //      sont dans les descendants des voisins 
967   //---------------------------------------------
968   TNaming_ShapesSet Reject;
969   TNaming_IteratorOnShapesSet itRes(Res);
970
971   const TopTools_IndexedDataMapOfShapeListOfShape& CurAnc = 
972     Ancestors(CurIn,TS);
973   
974   for (; itRes.More(); itRes.Next()) {
975     const TopoDS_Shape& Cand = itRes.Value();
976     TopTools_MapOfShape Neighbourg;
977     FindNeighbourg (CurIn,CurAnc,Cand,Neighbourg);
978     TopTools_MapIteratorOfMapOfShape itNeig(Neighbourg);
979     for (; itNeig.More(); itNeig.Next()) {
980       const TopoDS_Shape& Neig = itNeig.Key();
981       if (!NewNeig.Contains(Neig))  {
982         Reject.Add(Cand);
983         break;
984       }
985     }
986   }
987   Res.Remove(Reject);
988
989 */