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