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