4b268233c65c4e8890a052b020576ab38b639fca
[occt.git] / src / TNaming / TNaming_Naming.cxx
1 // Created on: 1997-09-03
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_Naming.ixx>
24
25 #include <TNaming_Builder.hxx>
26 #include <TNaming_Tool.hxx>
27 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
28 #include <TNaming_Iterator.hxx>
29 #include <TNaming_Scope.hxx>
30 #include <TNaming_NamingTool.hxx>
31 #include <TNaming_Name.hxx>
32 #include <TNaming_Identifier.hxx>
33 #include <TNaming_Localizer.hxx>
34 #include <TNaming_NewShapeIterator.hxx>
35 #include <TNaming_OldShapeIterator.hxx>
36 #include <TNaming_Selector.hxx>
37 #include <TNaming.hxx>
38 #include <TDF_ChildIterator.hxx>
39 #include <TDF_LabelList.hxx>
40 #include <TDF_Data.hxx>
41 #include <TDF_LabelMap.hxx>
42 #include <TDF_TagSource.hxx>
43 #include <TopoDS_Iterator.hxx>
44 #include <TopTools_MapOfShape.hxx>
45 #include <TopExp.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <TopTools_MapIteratorOfMapOfShape.hxx>
48 #include <Standard_DomainError.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
52 #include <TColStd_MapOfInteger.hxx>
53 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
54 #include <TopTools_MapIteratorOfMapOfOrientedShape.hxx>
55 #include <TopTools_HArray1OfShape.hxx>
56 #include <BRep_Tool.hxx>
57 #include <TopoDS.hxx>
58 // #include <TNaming_NCollections.hxx>
59 #include <NCollection_Map.hxx>   
60 #include <NCollection_DataMap.hxx>
61 typedef NCollection_Map<TopoDS_Shape> TNaming_MapOfShape; 
62 typedef TNaming_MapOfShape::Iterator TNaming_MapIteratorOfMapOfShape;
63 typedef NCollection_DataMap<TopoDS_Shape, TNaming_MapOfShape> TNaming_DataMapOfShapeMapOfShape; 
64 typedef TNaming_DataMapOfShapeMapOfShape::Iterator TNaming_DataMapIteratorOfDataMapOfShapeMapOfShape; 
65 // The bug concerns access to a null object in the method Filter():
66 #define BUC60847
67
68 #define OCC273
69 #define OCC350
70 #define OCC352
71 #define ALLOW_CHILD_NBS
72 //#define MDTV_DEB_CC
73 //#define MDTV_DEB_OR
74 //#define MDTV_DEB_MOD
75 //#define MDTV_OR
76 //#define MDTV_DEB_INNS
77 //#define MDTV_DEB_NBS
78 //#define MDTV_DEB_71
79 //#define MDTV_DEB_WIN
80 #ifdef MDTV_DEB
81 #include <TDF_MapIteratorOfLabelMap.hxx> 
82 #include <TCollection_AsciiString.hxx>
83 #include <TDF_Tool.hxx>
84 #include <BRepTools.hxx>
85 #include <TNaming_Tool.hxx>
86 #include <DbgTools.hxx>
87 #endif
88 #ifdef DEB
89 #include <TDF_Tool.hxx>
90 #include <TDF_MapIteratorOfLabelMap.hxx>
91
92 #include <TCollection_AsciiString.hxx>
93 #include <BRepTools.hxx>
94 void Print_Entry(const TDF_Label&       label)
95 {
96   TCollection_AsciiString entry;
97   TDF_Tool::Entry(label, entry);
98   cout << "LabelEntry = "<< entry << endl;
99 }
100 static void Write(const TopoDS_Shape& shape,
101                       const Standard_CString filename) 
102 {
103   char buf[256];
104   if(strlen(filename) > 256) return;
105   strcpy (buf, filename);
106   char* p = buf;
107   while (*p) {
108     if(*p == ':')
109       *p = '-';
110     p++;
111   }
112   ofstream save (buf);
113   if(!save) 
114     cout << "File " << buf << " was not created: rdstate = " << save.rdstate() << endl;
115   save << "DBRep_DrawableShape" << endl << endl;
116   if(!shape.IsNull()) BRepTools::Write(shape, save);
117   save.close();
118 }
119 void WriteNSOnLabel(const Handle(TNaming_NamedShape) & NS, const TCollection_AsciiString& Nam)
120 {
121   if (!NS.IsNull()) {
122     TCollection_AsciiString entry;
123     TDF_Tool::Entry(NS->Label(), entry);
124     TopoDS_Shape Sh = TNaming_Tool::GetShape (NS);
125     if(!Sh.IsNull()) {
126       TCollection_AsciiString Entry = Nam + entry + ".brep";
127       Write(Sh, Entry.ToCString());
128     }
129     else
130       cout << "WriteNSOnLabel>>> TopoDS_Shape IS NULL on Entry = "<< entry << endl;
131   }
132   else
133     cout << "WriteNSOnLabel >>>  NamedShape IS NULL" << endl;
134 }
135 //=======================================================================
136 static void Write(const TopTools_MapOfShape& MS,  const Standard_CString filename) 
137 {
138   if (!MS.IsEmpty ()) {
139     TCollection_AsciiString aNam (filename);
140     Standard_Integer i(0);
141     TopTools_MapIteratorOfMapOfShape it(MS);
142     for(;it.More();it.Next(),i++) {
143       TCollection_AsciiString aName = aNam + "_" + i + ".brep";
144       Write ( it.Key(), aName.ToCString());
145     }
146   }
147 }
148 #endif
149
150 //==========================================================================================
151 static Handle(TNaming_NamedShape) BuildName (const  TDF_Label&   F,
152                                              TNaming_Scope&                    MDF,
153                                              const TopoDS_Shape&               S,
154                                              const TopoDS_Shape&               Context, 
155                                              const Handle(TNaming_NamedShape)& Stop,
156                                              const Standard_Boolean            Geometry);
157
158 //=======================================================================
159 static Standard_Integer RepeatabilityInContext(const TopoDS_Shape& Selection, 
160                                                const TopoDS_Shape& Context);
161 //=======================================================================
162 extern Standard_Boolean IsEqual (const TopoDS_Shape& S1, const TopoDS_Shape& S2);
163
164 //=======================================================================
165 //function : Solve
166 //purpose  : voir avec YFR comment retrouver le bon resulat et le mettre 
167 //         : dans le NamedShape de L
168 //=======================================================================
169 Standard_Boolean  TNaming_Naming::Solve (TDF_LabelMap& Valid) 
170
171   Handle(TNaming_Naming) subname;
172   for (TDF_ChildIterator it (Label(),Standard_False); it.More(); it.Next()) {
173 #ifdef MDTV_DEB_NBS
174     TCollection_AsciiString anEntry;
175     TDF_Tool::Entry(it.Value(), anEntry);
176     cout << "TNaming_Naming::Solve: Label to be solved = " << anEntry << endl;
177 #endif
178     if (it.Value().FindAttribute(TNaming_Naming::GetID(),subname)) {
179       if (!subname->Solve (Valid)) {
180         return Standard_False; // not necessary to continue
181       }
182     }
183   }
184 #ifdef MDTV_DEB_CC
185   TDF_MapIteratorOfLabelMap anItr(Valid);
186   cout << "TNaming_Naming::Solve:: Valid label Map" << endl;
187   for (; anItr.More(); anItr.Next()) {
188     const TDF_Label& aLabel = anItr.Key();
189     TCollection_AsciiString anEntry;
190     TDF_Tool::Entry(aLabel, anEntry);
191     cout << "Label = " << anEntry << endl;
192   }
193 #endif
194   if (Regenerate(Valid)) {
195     if (!Valid.IsEmpty()) Valid.Add(Label());
196     return Standard_True;
197   }
198   return Standard_False; 
199 }
200  
201  
202 //=======================================================================
203 //function : GetID
204 //purpose  : 
205 //=======================================================================
206
207 const Standard_GUID& TNaming_Naming::GetID () 
208 {
209   static Standard_GUID TNaming_NamingID("c0a19201-5b78-11d1-8940-080009dc3333");
210   return TNaming_NamingID; 
211 }
212
213 //=======================================================================
214 //function : Insert
215 //purpose  : 
216 //=======================================================================
217
218 Handle(TNaming_Naming) TNaming_Naming::Insert (const TDF_Label& under) 
219 {
220   Handle(TNaming_Naming) N;  
221   TDF_Label child = TDF_TagSource::NewChild(under);
222   N = new TNaming_Naming ();  
223   child.AddAttribute (N);
224   return N;
225 }  
226   
227 //=======================================================================
228 //function : BuildNS
229 //purpose  : returns a new NamedShape, which is built as selection: 
230 //         : TNaming_Builder::Select("S","S") at the new child label of the label <F>. 
231 //=======================================================================
232
233 static Handle(TNaming_NamedShape)  BuildNS (const TDF_Label&        F,
234                                             const TopoDS_Shape&     S,
235                                             const TNaming_NameType& Name)
236 {
237   Handle (TNaming_Naming) Naming = TNaming_Naming::Insert (F);
238   
239   TNaming_Name& theName = Naming->ChangeName();
240   theName.ShapeType(S.ShapeType());
241   theName.Shape(S); 
242   theName.Type(Name);
243   TNaming_Builder B(Naming->Label());
244   B.Select(S,S);
245   return B.NamedShape();
246 }
247
248 //=======================================================================
249 //function : FindIndex
250 //purpose  : 
251 //=======================================================================
252
253 static Standard_Integer FindIndex(const Handle(TNaming_NamedShape)& NS,
254                                   const TopoDS_Shape&               S)
255 {
256   TDF_LabelList Labels;
257   TopoDS_Shape IS = TNaming_Tool::InitialShape(S,NS->Label(),Labels);
258   Standard_Integer Index = 1;
259   for (TNaming_Iterator itNS(NS); itNS.More(); itNS.Next(),Index++) {
260     if (IS.IsSame(itNS.NewShape())) break;
261   }
262   return Index;
263 }
264
265
266 //=======================================================================
267 //function : CompareInGeneration
268 //purpose  : returns true, only if the specified NS contains only <S> in 
269 //         : the "new shapes" set.
270 //=======================================================================
271
272 static Standard_Boolean CompareInGeneration (const Handle(TNaming_NamedShape)& NS,
273                                              const TopoDS_Shape&               S)
274 {
275   for (TNaming_Iterator it(NS); it.More(); it.Next()) {
276     if (!it.NewShape().IsSame(S)) return 0;
277   }
278   return 1;
279 }
280
281 #ifdef OCC350
282
283 //=======================================================================
284 //function : GetShapeEvolutions
285 //purpose  : returns Standard_True, if target has parent in source; list contains inheritance chain
286 //=======================================================================
287 static Standard_Boolean GetShapeEvolutions(const TopoDS_Shape&               theTarget, // this is changed in recursion
288                                            const Handle(TNaming_NamedShape)& theSource,
289                                            TopTools_ListOfShape&             aList)     // returns list in the backward order
290 {
291   Handle(TNaming_NamedShape) aTarget = TNaming_Tool::NamedShape(theTarget,theSource->Label());
292   if (!aTarget.IsNull()) {
293 #ifdef MDTV_DEB_71
294     cout <<"GetShapeEvolutions: target NS = ";
295     Print_Entry(aTarget->Label());
296     cout <<"GetShapeEvolutions: Source NS = ";
297     Print_Entry(theSource->Label());
298     TCollection_AsciiString aNam("GetShapeEvolutions");
299     WriteNSOnLabel(aTarget,aNam);
300 #endif
301     if (aTarget->Label() == theSource->Label()) return Standard_True; // check if target is in the source
302   } else return Standard_False;
303
304   TNaming_Iterator anIter(aTarget);
305   for(;anIter.More();anIter.Next()) { // check all appropriate old shapes of target
306 #ifdef MDTV_DEB_71
307     if(!anIter.OldShape().IsNull()) {
308       Write(anIter.OldShape(), "Target_OldS.brep");
309       cout <<"Target OldS TS =" <<anIter.OldShape().TShape()->This() <<endl;
310     }
311     if(!anIter.NewShape().IsNull()) {
312       Write(anIter.NewShape(), "Target_NewS.brep");
313       cout <<"Target NewS TS =" <<anIter.NewShape().TShape()->This() <<endl;
314     }  
315 #endif
316     if (anIter.OldShape().IsNull() || anIter.NewShape().IsNull()) continue;
317     if (!anIter.NewShape().IsSame(theTarget)) continue;
318     if (GetShapeEvolutions(anIter.OldShape(),theSource,aList)) { // recursion: now target is old shape
319       aList.Append(theTarget); // if oldshape has the source as parent (or belongs to it) , fill the list
320       return Standard_True;
321     }
322   }
323   return Standard_False;
324 }
325
326 //=======================================================================
327 //function : CompareInModification
328 //purpose  : returns empty named shape if naming is already done
329 //=======================================================================
330
331 static Handle(TNaming_NamedShape) CompareInModification (const Handle(TNaming_NamedShape)& NS, // parent
332                                                          const TopoDS_Shape&               S)  // target
333 {
334   Handle(TNaming_NamedShape) aResult;
335   if (S.IsNull() || NS.IsNull()) return aResult;
336 #ifdef MDTV_DEB_71
337   cout <<"CompareInModification: parent NS = ";
338   Print_Entry(NS->Label());
339   Write(S, "CompareInM_S.brep");
340   TCollection_AsciiString aNam("CompareInM");
341   WriteNSOnLabel(NS,aNam);
342 #endif
343   Handle(TNaming_NamedShape) aSource; // parent NamedShape, which can be found by TopoDS shape
344   TNaming_Iterator anIt(NS);
345   for(;anIt.More() && aSource.IsNull();anIt.Next()) {
346     if (!anIt.NewShape().IsNull()) {
347       aSource = TNaming_Tool::NamedShape(anIt.NewShape(),NS->Label());
348 #ifdef MDTV_DEB_71
349       TCollection_AsciiString aNam("CompareInM_Source");
350       WriteNSOnLabel(aSource,aNam);
351 #endif
352     }
353   }
354   // searching for 1:n to the same label modifications (in this case current naming is insufficient)
355   TopTools_ListOfShape aList;
356   if (GetShapeEvolutions(S,aSource,aList) && aList.Extent() > 0) {
357     TopTools_ListIteratorOfListOfShape anIter(aList);
358     for(;anIter.More();anIter.Next()) {
359       aResult = TNaming_Tool::NamedShape(anIter.Value(),NS->Label());
360       if (aResult->Evolution()!=TNaming_MODIFY) { // evolution must be modify, otherwise everything is OK
361         aResult.Nullify();
362         return aResult;
363       }
364       TopTools_MapOfShape aMap; // collection of the old shapes of the shape from list
365       TNaming_Iterator aNIter1(aResult);
366
367       for(;aNIter1.More();aNIter1.Next()) {
368         if (aNIter1.NewShape().IsSame(anIter.Value()))  {// if isSame 
369           aMap.Add(aNIter1.OldShape());
370         }
371       }
372       TNaming_Iterator aNIter2(aResult); // if some another shapes has oldshape from map, return namedshape with this oldshape
373
374       for(;aNIter2.More();aNIter2.Next()) {
375         if (aNIter2.NewShape().IsSame(anIter.Value())) continue;
376         if (aMap.Contains(aNIter2.OldShape())) { // if one shape was modified to the two at the shared label, return this one
377           aResult = TNaming_Tool::NamedShape(aNIter2.OldShape(),NS->Label());
378           if (!aResult.IsNull()) return aResult;
379         }
380       }
381     }
382     aResult.Nullify();
383   }
384   return aResult;
385 }
386 #endif
387
388 //=======================================================================
389 static Standard_Boolean FillSMap(const TopoDS_Shape& S, TopTools_MapOfShape& MS)
390 {   
391   if(S.IsNull()) return Standard_False; 
392   Standard_Boolean isHomogen(Standard_True);
393   TopAbs_ShapeEnum aPrevType(TopAbs_SHAPE);
394   TopoDS_Iterator it(S);
395   for (; it.More(); it.Next()) {
396     const TopAbs_ShapeEnum aType = it.Value().ShapeType();      
397 #ifdef MDTV_DEB_CC
398     cout <<"TestSolution_FillMap: S_Type = :" << it.Value().ShapeType() <<" TShape = " << it.Value().TShape()->This() <<endl;
399 #endif
400     if(aType > TopAbs_COMPSOLID) {
401       MS.Add(it.Value());
402       if(aPrevType == TopAbs_SHAPE)
403         aPrevType = aType;
404       else if(aPrevType != aType) 
405         isHomogen = Standard_False;       
406     }
407     else 
408       if(!FillSMap(it.Value(), MS))
409         isHomogen = Standard_False;  
410   }
411   return isHomogen;
412 }
413 //=======================================================================
414 //function : Compare
415 //purpose  : checks naming of the shape <S> in the NamedShape <NS>. 
416 //         : Returns true, if it's correct. Details ==>
417 //         : The method takes all modifications of the "NS" (see CurrentShape method), 
418 //         : which are in the "MDF" (if it's not empty) before <Stop> shape and check them.
419 //         : whether these modifications contain only "S". If yes then the method 
420 //         : returns true, otherwise it returns false.
421 //=======================================================================
422
423 static Standard_Boolean Compare (const Handle(TNaming_NamedShape)& NS,
424                                  const TNaming_Scope&              MDF,
425                                  const Handle(TNaming_NamedShape)& Stop,
426                                  const TopoDS_Shape&               S)
427 {
428   TDF_LabelMap Forbiden;
429   TopTools_MapOfShape MS;
430   if (!Stop.IsNull()) TNaming_NamingTool::BuildDescendants(Stop,Forbiden);
431   TNaming_NamingTool::CurrentShape(MDF.GetValid(),Forbiden,NS,MS);
432 #ifdef MDTV_DEB_NBS
433   Write(S, "Compare_S.brep");
434   cout <<  "S: TShape = " <<S.TShape()->This() <<endl;
435   Standard_Integer i =1;
436   TopTools_MapIteratorOfMapOfShape it(MS);
437   TCollection_AsciiString aNam("Compare_MS_");
438   TCollection_AsciiString ext(".brep");
439   for(;it.More();it.Next(), i++) {
440     TCollection_AsciiString aName = aNam + i + ext;
441     Write(it.Key(), aName.ToCString()) ;
442     cout << aName.ToCString()<< ": TShape = " <<it.Key().TShape()->This() <<endl;
443   }
444 #endif
445   return (MS.Contains(S) && MS.Extent() == 1);
446 }
447 //=======================================================================
448 //function : TestSolution
449 //purpose  : returns true, if last modification of shape from "NS" is equal to "S":
450 //         : If shape "S" has atomic type (TopAbs_FACE, TopAbs_EDGE, TopAbs_VERTEX), 
451 //         : then returns S.IsSame(shape from "NS"). 
452 //         : Otherwise the result of exploration of these shapes must be same.
453 //=======================================================================
454
455 static Standard_Boolean TestSolution(const TNaming_Scope&      MDF,
456                                      const Handle(TNaming_NamedShape)& NS,
457                                      const TopoDS_Shape&               S)
458
459
460   if (NS.IsNull()) return Standard_False;
461   TopoDS_Shape Res = MDF.CurrentShape(NS);// last modification of NS taken into account Valid map
462   if(S.IsNull() || Res.IsNull()) return Standard_False;
463 #ifdef MDTV_DEB_CC
464   Write(S, "TSol_S.brep");
465   Write(Res, "TSol_Res.brep");
466 #endif  
467 #ifdef OCC352
468
469   if ((S.ShapeType() == TopAbs_FACE  ||
470       S.ShapeType() == TopAbs_EDGE  ||
471       S.ShapeType() == TopAbs_VERTEX ) && 
472       Res.ShapeType() != TopAbs_COMPOUND) { 
473     return (Res.IsSame(S));
474   } else if (S.ShapeType() == TopAbs_SOLID ||
475         S.ShapeType() == TopAbs_COMPSOLID) {
476     TopTools_MapOfShape aMS;
477     TopExp_Explorer exp;
478     for (exp.Init(S,TopAbs_FACE) ; exp.More(); exp.Next()) {
479       aMS.Add(exp.Current());
480     }
481     for (exp.Init(Res,TopAbs_FACE) ; exp.More(); exp.Next()) { //content of MS and Res should be the same 
482       if (aMS.Contains(exp.Current())) {
483         aMS.Remove(exp.Current());
484       }
485       else return 0;
486     }
487     return aMS.IsEmpty();
488   } else {
489
490 #else
491
492   if (S.ShapeType() == TopAbs_SOLID ||
493       S.ShapeType() == TopAbs_FACE  ||
494       S.ShapeType() == TopAbs_EDGE  ||
495       S.ShapeType() == TopAbs_VERTEX ) { 
496     return (Res.IsSame(S));
497   }
498   else {
499
500 #endif
501
502     TopTools_MapOfShape MS;
503     Standard_Boolean isHom = FillSMap(S, MS);
504     TopAbs_ShapeEnum aType(TopAbs_SHAPE); 
505     TColStd_MapOfInteger aView;
506     TopTools_MapIteratorOfMapOfShape itm(MS);
507     for(;itm.More();itm.Next()) {
508       aType = itm.Key().ShapeType();
509       if(isHom) 
510         break;
511        else 
512         aView.Add(itm.Key().ShapeType());      
513     }
514     
515     if (MS.Contains(Res)) {
516       MS.Remove(Res);
517       if (MS.IsEmpty()) return 1;
518     }
519     if (Res.ShapeType() == TopAbs_SOLID ||
520         Res.ShapeType() == TopAbs_COMPSOLID || 
521         Res.ShapeType() == TopAbs_COMPOUND) 
522       {
523         TopExp_Explorer exp;
524         if(isHom)
525           for (exp.Init(Res,aType) ; exp.More(); exp.Next()) {
526             if (MS.Contains(exp.Current())) {
527               MS.Remove(exp.Current());
528             }
529           } else {
530             TColStd_MapIteratorOfMapOfInteger itm(aView);
531             for(;itm.More();itm.Next()) {
532               TopAbs_ShapeEnum aType = (TopAbs_ShapeEnum)itm.Key();
533               for (exp.Init(Res,aType) ; exp.More(); exp.Next()) {
534                 if (MS.Contains(exp.Current())) {
535                   MS.Remove(exp.Current());
536                 }
537               }
538             }
539           }
540       } else {
541         if(S.IsSame(Res))
542           return Standard_True;
543         TopoDS_Iterator it(Res);
544         for (; it.More(); it.Next()) { //content of MS and Res should be the same 
545           if (MS.Contains(it.Value())) {
546             MS.Remove(it.Value());
547           }
548           else return 0; 
549         }
550       }
551     return MS.IsEmpty();
552   }
553 }
554
555 //=======================================================================
556 //function : FindNewShapeInFather
557 //purpose  :
558 //=======================================================================
559
560 static void FindNewShapeInFather (const Handle(TNaming_NamedShape)& NS,
561                                   TopoDS_Shape&                     SC)
562 {  
563   TDF_Label         Father  = NS->Label().Father(); 
564   TNaming_Iterator  itLab(Father);
565   for (; itLab.More(); itLab.Next()) {
566     SC= itLab.NewShape();
567     break;
568   }
569 }
570
571 //=======================================================================
572 //function : NextModif
573 //purpose  : 
574 //=======================================================================
575
576 static Handle(TNaming_NamedShape) NextModif(const Handle(TNaming_NamedShape)& NS)
577 {
578   Handle (TNaming_NamedShape) Next;
579   if (!NS.IsNull()) {
580     TNaming_NewShapeIterator it(NS); 
581     if (it.More()&& it.IsModification()) Next = it.NamedShape();
582   }
583   return Next;
584 }
585 //=======================================================================
586 // C1 - cand shape of the father, C2 - shape of rebuild child Naming attr.
587 //      (to be Compound of elementary shapes)
588 //=======================================================================
589 static Standard_Boolean IsContSame(const TopoDS_Shape& C1, const TopoDS_Shape& C2)
590 {
591   Standard_Boolean aRes(Standard_False);
592   if(!C1.IsNull() && !C2.IsNull()) {
593     TopTools_MapOfShape aMap;
594     if(FillSMap(C1, aMap)) {
595       aRes = Standard_True;
596       TopoDS_Iterator it(C2);
597       for(;it.More();it.Next()) {
598         if(!aMap.Contains(it.Value())) {
599           aRes = Standard_False;
600           break;
601         }
602       }
603     }
604   }
605   return aRes;
606 }
607
608 //=======================================================================
609 // Identifies the case when Filter haven't sense because of multiplicity
610 //=======================================================================
611 static Standard_Boolean IsMultipleCase(const TopoDS_Shape&        S,
612                                  const TopoDS_Shape&        Context,
613                                  const TopTools_MapOfShape& Neighbourgs) {
614   
615   TopTools_IndexedDataMapOfShapeListOfShape aDM;
616   TNaming_MapOfShape aM;
617   TopTools_MapOfShape aNbs;
618   aNbs.Assign(Neighbourgs);
619   aNbs.Add(S);
620   TNaming_DataMapOfShapeMapOfShape aDMM;
621   TopExp::MapShapesAndAncestors (Context, TopAbs_EDGE, TopAbs_FACE, aDM);
622   TopTools_MapIteratorOfMapOfShape it(aNbs);
623   for(;it.More();it.Next()) {
624     if(aDM.Contains(it.Key())) {
625       TNaming_MapOfShape aMS;
626       const TopTools_ListOfShape& aL = aDM.FindFromKey(it.Key());      
627       TopTools_ListIteratorOfListOfShape lit(aL);
628       for(;lit.More();lit.Next()) {
629         aM.Add(lit.Value());
630         aMS.Add(lit.Value());
631       }
632       if(aMS.Extent())
633         aDMM.Bind(it.Key(), aMS);
634     } else {
635 #ifdef DEB
636       cout << "Key is not BOUND!" <<endl;
637 #endif
638       return Standard_False;
639     }
640   }
641   
642   Standard_Boolean isCommon(Standard_True);
643   TNaming_MapIteratorOfMapOfShape itm(aM);
644   for(;itm.More();itm.Next()) {
645     isCommon = Standard_True; // statement: this shape (itm.Key()) is common (to be checked below)
646     TNaming_DataMapIteratorOfDataMapOfShapeMapOfShape itdm(aDMM);
647     for (;itdm.More();itdm.Next()) {
648       const TNaming_MapOfShape& aMap = itdm.Value();
649       if(!aMap.Contains(itm.Key())) {
650         isCommon = Standard_False;
651         break;
652       }
653     }
654     if(isCommon) break; // common single face found
655   }
656   if(isCommon && aM.Extent() < aNbs.Extent()) {// number of unique faces (to have single solution) 
657                                                //should be at least no less than (Nb of Neighbourgs) +1
658     return Standard_True;
659   }
660   return Standard_False;
661 }
662 //=======================================================================
663 //function : Filter
664 //purpose  : sets the name with type "FILTERBYNEIGHBOURGS" and returns true, 
665 //         : if it was correctly done.
666 //         : 
667 //         :
668 //=======================================================================
669
670 static Standard_Boolean Filter (const TDF_Label&                  F,
671                                 TNaming_Scope&                    MDF,
672                                 const TopoDS_Shape&               S,
673                                 const TopoDS_Shape&               Context,
674                                 TNaming_Localizer&                Localizer,
675                                 Handle(TNaming_NamedShape)&       NS,
676                                 const Standard_Integer            Lev)
677
678   // 1. Localizer.FindNeighbourg("Context", "S", "Neighbourg") - looks for neighbourgs of  "S" with the same shape type in the "Context" 
679   //    shape and adds them to the "Neighbourg" map;
680   // 2. If "Neighbourg" map is empty, tries to do the same with the new context shape: shape from the father label of the "S" named shape;
681   // 3. If "Neighbourg" map is still empty, returns false;
682   // 4. Adds to the new child of label "L" new TNaming_Naming attribute with the next properties name: shape type is shape type of the "S",
683   //     type is TNaming_FILTERBYNEIGHBOURGS, stop shape ( "Until" ) is "Context" of corresponding NamedShape; 
684   //     first argument of name is "NS" NamedShape.
685   // 5. Adds to the Name all shapes from "Neighbourg" map: build it with the BuildName( new generated TNaming_Naming attribute, MDF, 
686   //    argument shape from "Neighbourg", "Context", NextModif( "Until" ), true ) method.
687   // 6. Creates resulting NamedShape with the "Regenerate" ( it just calls TName::Solve method ) method from TNaming_Naming class.
688   // 7. If Compare( result NamedShape, MDF, stop, "S" ) returns true, "Filter" method returns true, else returns false.
689
690   //------------------------------
691   // Construction des voisins ==> of neighbors.
692   //------------------------------
693   Standard_Integer aLev(Lev);
694   TopTools_MapOfShape Neighbourg;
695   Localizer.FindNeighbourg (Context,S,Neighbourg);
696 #ifdef MDTV_DEB_NBS
697   //DbgTools::DisplayShape(Context, F, Quantity_NOC_GREEN);
698   //DbgTools::DisplayShape(S, F, Quantity_NOC_BLUE1);  
699   Write(Context, "FNBS_Context.brep");
700   Write(S, "FNBS_S.brep");
701 #endif
702 #ifdef OCC273
703   // mpv : NS and shape must be the same
704   Standard_Boolean isIn = Standard_False;
705   TNaming_Iterator anIter(NS);
706   for(;anIter.More();anIter.Next()) {
707 #ifdef MDTV_DEB
708           //DbgTools::DisplayShape(anIter.NewShape(), F, Quantity_NOC_RED);
709 #endif
710     if (anIter.NewShape().IsSame(S)) {
711       isIn = Standard_True;
712       break;
713     }
714   }
715   if (!isIn) if (!TNaming_Tool::NamedShape(S,F).IsNull()) NS = TNaming_Tool::NamedShape(S,F);
716 //  if (!TNaming_Tool::NamedShape(S,F).IsNull()) NS = TNaming_Tool::NamedShape(S,F);
717 #endif
718   
719   if (Neighbourg.IsEmpty()) {
720     // Recherche du vrai context. (Research of context truth)
721     Handle(TNaming_NamedShape) GenS = TNaming_Tool::NamedShape(S,NS->Label());
722 #ifdef BUC60847
723     if (GenS.IsNull()) return Standard_False;
724 #endif
725     TDF_Label Father = (GenS->Label()).Father();
726     Father.FindAttribute(TNaming_NamedShape::GetID(),GenS);
727     TopoDS_Shape GoodContext = TNaming_Tool::GetShape(GenS);
728     Localizer.FindNeighbourg (GoodContext,S,Neighbourg);
729   }
730   
731
732   if (Neighbourg.IsEmpty()) {
733 #ifdef DEB
734     cout <<"FindNeighbourg: impossible"<<endl;
735 #endif
736     return 0;  
737   } else {
738 #ifdef MDTV_DEB_NBS
739     Write(Neighbourg, "Neighbourgs");
740 #endif
741     aLev++;
742     //cout <<"Filter: Lev = " << aLev << endl;
743   }
744   if(aLev > 3) return 0; 
745 #ifdef ALLOW_CHILD_NBS   
746   Handle(TNaming_Naming) aFNaming;
747   TopoDS_Shape aFatherCandSh;
748   F.FindAttribute(TNaming_Naming::GetID(), aFNaming);
749   if(!aFNaming.IsNull()) {
750     const TNaming_Name& aName = aFNaming->GetName();
751     if (aName.Type() == TNaming_FILTERBYNEIGHBOURGS) {
752       aFatherCandSh = aName.Arguments().First()->Get();
753     }
754   } 
755   if(S.ShapeType() == TopAbs_EDGE && aFatherCandSh.IsNull()) {
756     //check the applicability
757     if(!NS.IsNull() && !NS->Get().IsNull() && NS->Get().ShapeType() == TopAbs_COMPOUND)
758       if(IsMultipleCase(S, Context, Neighbourg)) {
759         //cout << "Filter: ==> MultipleCase!" << endl;
760         NS->Label().FindAttribute(TNaming_Naming::GetID(), aFNaming);
761         if(!aFNaming.IsNull()) {
762           TNaming_Name& aName = aFNaming->ChangeName();
763           if (aName.Type() == TNaming_INTERSECTION) {
764             Standard_Integer ij(1);
765             TNaming_ListIteratorOfListOfNamedShape itA(aName.Arguments()); 
766             for (; itA.More(); itA.Next(), ij++) {
767               const TopoDS_Shape& aFace = TNaming_Tool::CurrentShape(itA.Value());
768 #ifdef MDTV_DEB_MOD
769               Write(aFace, "First_Face.brep");
770               cout <<"Selection TS = " << S.TShape()->This() <<endl;
771 #endif
772               Standard_Integer i(1), indxW(0),indxE(0),nbW(0),nbE(0), indxF(0);
773               Standard_Boolean isFound(Standard_False);
774               TopoDS_Iterator it(aFace);
775               for (;it.More();it.Next(),i++) {
776                 nbW++;
777 #ifdef MDTV_DEB_MOD
778                 Write(it.Value(), "First_Wire.brep");
779 #endif
780                 if(!isFound) {
781                   Standard_Integer j(1);
782                   TopoDS_Iterator it2(it.Value());
783                   for (;it2.More();it2.Next(),j++) {
784                     nbE++;
785 #ifdef MDTV_DEB_MOD
786                     Write(it2.Value(), "First_Wire.brep");
787                     cout <<"Edge TS = " << it2.Value().TShape()->This() <<endl;
788 #endif
789                     if(S.IsEqual(it2.Value())) {
790                       indxE = j;
791                       indxW = i;
792                       indxF = ij;
793                       isFound = Standard_True;              
794                     }
795                   }
796                 }             
797               }
798               if(isFound) {
799                 Standard_Integer Index = indxE & 0x000000FF;
800                 Index = Index | (nbE   << 8);
801                 Index = Index | (indxW << 16);
802                 Index = Index | (nbW   << 20);
803                 Index = Index | (indxF << 24);
804                 aName.Index(Index);           
805                 //------------------------------
806                 // Compute the TNaming_NamedShape
807                 //------------------------------
808                 aFNaming->Regenerate(MDF.ChangeValid());
809                 aFNaming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
810                 Handle (TNaming_NamedShape) Until = TNaming_Tool::NamedShape(Context,NS->Label());
811                 Handle (TNaming_NamedShape) Stop  = NextModif(Until); 
812                 if (Compare (NS,MDF,Stop,S)) return 1;
813                 break; 
814               }    
815             }
816           } 
817         }       
818         return 0;
819       }
820   }
821 #endif
822
823   //-----------------------------------------------------
824   // Construction function de naming. et insertion sous F
825   //-----------------------------------------------------
826   Handle (TNaming_Naming) NF = TNaming_Naming::Insert (F);
827   
828   Handle (TNaming_NamedShape) Until = TNaming_Tool::NamedShape(Context,NS->Label());
829   Handle (TNaming_NamedShape) Stop  = NextModif(Until); 
830   TNaming_Name& theName = NF->ChangeName();
831   theName.ShapeType(S.ShapeType());
832   theName.Shape(S);
833   theName.Type(TNaming_FILTERBYNEIGHBOURGS);
834   theName.Append(NS);
835   theName.StopNamedShape (Until);
836 #ifdef MDTV_DEB_NBS
837   cout << "FilterByNBS: ";
838   Print_Entry(NF->Label());
839 #endif
840   //---------------------
841   // Naming des voisins.
842   //---------------------
843
844   TopTools_MapIteratorOfMapOfShape itN(Neighbourg);
845   for (; itN.More(); itN.Next()) {
846 #ifdef ALLOW_CHILD_NBS    
847     const TopoDS_Shape& aS = itN.Key();   
848     Handle (TNaming_NamedShape) aNS = 
849       BuildName(NF->Label(), MDF, aS, Context, Stop, 1);          
850 #ifdef MDTV_DEB_NBS
851     const TopoDS_Shape& aS2 = aNS->Get(); 
852     if(!aS.IsNull())
853       cout << "Shape arg type = " << aS.ShapeType() <<" TSH = " << aS.TShape()->This()<<endl;
854     if(!aS2.IsNull())
855       cout << "Build shape type = " << aS2.ShapeType() <<" TSH = " << aS2.TShape()->This()<<endl;
856 #endif
857                   
858     const TopoDS_Shape aSNS = aNS->Get();  //allow child level
859     Standard_Boolean allowChild(Standard_True);
860     if(!aSNS.IsNull() && aSNS.ShapeType() == TopAbs_COMPOUND && !aFatherCandSh.IsNull()) 
861       allowChild = !IsContSame(aFatherCandSh, aSNS);
862     if(allowChild && !aSNS.IsNull() && aS.ShapeType() != aSNS.ShapeType() && 
863        aSNS.ShapeType() == TopAbs_COMPOUND)
864       { // aLev < 3 
865 #ifdef MDTV_DEB_NBS
866         cout <<"Father label = ";
867         Print_Entry(aNS->Label().Father());
868         Write(aS,"SelectionS.brep");
869         Write(aSNS,"SelectionSNS.brep");
870 #endif
871         Handle(TNaming_Naming) aNaming;
872         Standard_Boolean StandardFilter(Standard_True);
873         aNS->FindAttribute(TNaming_Naming::GetID(), aNaming);
874         if(!aNaming.IsNull()) {
875           const TNaming_Name& aName = aNaming->GetName();
876           if (aName.Type() == TNaming_GENERATION) 
877             StandardFilter = Standard_False;
878           if(StandardFilter)
879             if (!Compare (aNS,MDF,Stop,aS)) {
880               TNaming_Localizer aLocalizer;
881               Filter (NF->Label(), MDF,aS,Context, aLocalizer,aNS, aLev);
882             }
883         }
884       }    
885     theName.Append(aNS);
886 #else 
887     theName.Append(BuildName(NF->Label(), MDF, itN.Key(), Context, Stop, 1));
888 #endif
889   }
890   //------------------------------
891   // Compute the TNaming_NamedShape
892   //------------------------------
893   NF->Regenerate(MDF.ChangeValid());
894   NF->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
895
896   //-----------------
897   // Check du filtre.
898   //-----------------
899   if (Compare (NS,MDF,Stop,S)) return 1;
900 #ifdef MDTV_DEB
901   cout <<"TNaming_Naming::Name Filter insufficient"<<endl;
902 #endif
903   return 0;
904 }
905
906
907 //=======================================================================
908 //function : BuildNameInNS
909 //purpose  : Calls BuildName method, but with new context and new stop shape.  
910 //         : Context is searched at the father label of the "Context" label : 
911 //         : old shapes from the NamedShape at the defined father label.
912 //         : If it's impossible, then "S" set as context. 
913 //         : If "S" is in new context, then stop shape is named shape, 
914 //         : which belongs to the father label of the "Context" named shape.
915 //         : For example, face (F2) of prism (P) is generated from the edge (E) 
916 //         : of primitive face (F1) of box (B). F2 is named as GENERATION from E.
917 //         : Naming of E is done with help BuildNameInNS function:   
918 //         : with context B and stop shape P. 
919 //=======================================================================
920 static Handle(TNaming_NamedShape) BuildNameInNS (const TDF_Label&                  F,
921                                                  TNaming_Scope&                    MDF,
922                                                  const TopoDS_Shape&               S,
923                                                  const Handle(TNaming_NamedShape)& Context,
924                                                  const Handle(TNaming_NamedShape)& Stop,
925                                                  const Standard_Boolean            Geometry)
926
927 {
928   // il faut determiner un nouveau context et un nouveau Stop.
929   // it is necessary to determine new a context and a new Stop
930   TopoDS_Shape SC;
931   Handle(TNaming_NamedShape) NewStop = Stop;
932   
933   TNaming_Localizer::FindShapeContext (Context,S,SC);
934
935   if(!SC.IsNull()){
936 // <Context> is Ident.NamedShapeOfGeneration() ==
937     TDF_Label Father = Context->Label().Father();
938     Father.FindAttribute(TNaming_NamedShape::GetID(),NewStop);
939 #ifdef MDTV_DEB_INNS
940     if(!Stop.IsNull())
941       {cout <<" Stop NS : "; Print_Entry( Stop->Label());}
942     if(!NewStop.IsNull())
943       {cout <<" NewStop : ";  Print_Entry( NewStop->Label());}
944     cout <<"ContextLabel: "; Print_Entry( Context->Label());
945     cout <<"Father      : "; Print_Entry( Father);
946 #endif
947   }
948 #ifdef MDTV_DEB_INNS
949   if(NewStop.IsNull())
950     cout <<"BuildNameInNS:: NewStop shape is  NULL" << endl;  
951 #endif 
952   return BuildName (F,MDF,S,SC,NewStop,Geometry);
953 }
954
955 //=======================================================================
956 //function : 
957 //purpose  : 
958 //=======================================================================
959
960 static Handle(TNaming_NamedShape) BuildName (const TDF_Label&                  F,
961                                              TNaming_Scope&               MDF,
962                                              const TopoDS_Shape&               Selection,
963                                              const TopoDS_Shape&               Context,
964                                              const Handle(TNaming_NamedShape)& Stop,
965                                              const Standard_Boolean            Geom)
966 {
967
968  
969   
970   // Create an identifier
971   Standard_Boolean OnlyOne      = !Geom;
972   Standard_Boolean IsGeneration = Standard_False;
973 #ifdef MDTV_DEB_MOD
974   cout <<"BuildName: F =>  ";
975   Print_Entry(F);
976   cout <<" Selection type = " << Selection.ShapeType() << " TS = " << Selection.TShape()->This() << endl;
977   Write(Selection, "BName_Selection.brep");
978   Write(Context, "BName_Context.brep");
979 #endif
980   TNaming_Identifier Ident(F, Selection, Context,OnlyOne);
981
982   Handle (TNaming_Naming)  Naming;
983   Handle (TNaming_NamedShape) NS;
984
985   if (!Ident.IsDone()) {
986     return BuildNS (F,Selection, TNaming_UNKNOWN);
987   }
988   if (Ident.IsFeature() && Stop.IsNull()) {
989     //-------------
990     // Deja Nomme
991     //-------------
992     if   (!OnlyOne) return Ident.FeatureArg();
993     else            NS =   Ident.FeatureArg();
994   }
995   else {  
996     //---------------------------------------------
997     // Construction de la fonction d identification.
998     //---------------------------------------------
999     //Standard_Boolean NotOnlyOne = 0;
1000
1001     Naming = TNaming_Naming::Insert(F);
1002    
1003     TNaming_Name& theName = Naming->ChangeName();
1004     theName.ShapeType(Selection.ShapeType());
1005     theName.Shape(Selection);
1006     theName.Type(Ident.Type());
1007 #ifdef MDTV_DEB_MOD
1008     cout <<"BuildName: Inserted Naming Att at ";
1009     Print_Entry(Naming->Label());
1010     cout <<" NameType = " << theName.Type() <<endl;             
1011 #endif
1012     if (Ident.IsFeature()) {
1013       theName.Append(Ident.FeatureArg());
1014     }
1015     if (theName.Type() == TNaming_GENERATION) { 
1016       theName.Append(Ident.NamedShapeOfGeneration());
1017       IsGeneration = Standard_True;
1018     } 
1019     if (theName.Type() == TNaming_CONSTSHAPE) {
1020       theName.Index(FindIndex(Ident.FeatureArg(),Selection));
1021     } 
1022     //------------------------------------
1023     // Renseignement du NamedShape d arret.
1024     //------------------------------------
1025     theName.StopNamedShape (Stop);
1026 #ifdef MDTV_DEB_MOD
1027     if(!Stop.IsNull()) {
1028         TCollection_AsciiString Es;
1029       TDF_Tool::Entry(Stop->Label(), Es);
1030       cout <<"StopNS at Label = "<< Es << endl;
1031     }
1032 #endif 
1033     //---------------------------------
1034     // Identification des arguments. 
1035     //---------------------------------
1036
1037     for (Ident.InitArgs(); Ident.MoreArgs(); Ident.NextArg()) {
1038       if (Ident.ArgIsFeature()) {
1039         theName.Append(Ident.FeatureArg());
1040 #ifdef MDTV_DEB_MOD
1041         if(!Ident.FeatureArg().IsNull()) {
1042           TCollection_AsciiString E;
1043           TDF_Tool::Entry(Ident.FeatureArg()->Label(), E);
1044           cout <<"Added argument NS from Label = "<< E << endl;
1045         }
1046 #endif 
1047       }
1048       else {
1049 #ifdef MDTV_DEB_MOD
1050           cout <<"BuildName: NameType = " <<theName.Type() << " NS ";
1051           Print_Entry(Naming->Label());
1052           cout <<"Ident.ShapeArg() type = " << Ident.ShapeArg().ShapeType() << " TS = " << Ident.ShapeArg().TShape()->This() << endl;   
1053            Write(Ident.ShapeArg(), "BName_ShapeArg.brep");
1054 #endif  
1055           if (theName.Type() == TNaming_GENERATION)
1056           theName.Append(BuildNameInNS(Naming->Label(),MDF,Ident.ShapeArg(),Ident.NamedShapeOfGeneration(),Stop,Geom));
1057         else 
1058           theName.Append(BuildName(Naming->Label(),MDF,Ident.ShapeArg(),Context,Stop,Geom));
1059       }
1060     }
1061
1062     //------------------------
1063     // Reconstruction of Name
1064     //------------------------
1065     Naming->Regenerate(MDF.ChangeValid()); 
1066 #ifdef MDTV_DEB_MOD
1067     TCollection_AsciiString E2;
1068     TDF_Tool::Entry(Naming->Label(), E2);
1069     cout <<"Regenerated Naming Att at Label = "<< E2 << endl;
1070 #endif 
1071     Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1072     if(NS.IsNull()) return NS; 
1073     if (MDF.WithValid()) MDF.Valid(NS->Label());
1074 #ifdef MDTV_DEB_MOD
1075     if(!NS.IsNull()) {
1076       TCollection_AsciiString E;
1077       TDF_Tool::Entry(NS->Label(), E);
1078       cout <<"Regenerated NS at Label = "<< E << endl;
1079     }
1080 #endif 
1081   }
1082
1083   if (OnlyOne) {
1084     //-------------------------------------------------
1085     // Filtre par les voisins
1086     // pour construire le nom correspondant a S.
1087     //------------------------------------------------- 
1088
1089 #ifdef OCC273
1090    if(NS.IsNull()) return NS; 
1091 #endif
1092
1093     TNaming_Localizer Localizer;
1094     TNaming_Iterator itNS(NS); 
1095     if (itNS.More()) {
1096       //----------------
1097       // Check + Filtre
1098       //----------------
1099
1100 #ifdef OCC350
1101
1102       Standard_Boolean StandardFilter = !IsGeneration;
1103       if (IsGeneration) {
1104         if (!CompareInGeneration (NS,Selection)) {
1105
1106           TopoDS_Shape               NewContext;
1107           Handle(TNaming_NamedShape) NewStop;
1108           FindNewShapeInFather (Ident.NamedShapeOfGeneration(),NewContext);
1109           Filter (F,MDF,Selection,NewContext,Localizer,NS,0);
1110         }
1111       } else if (Ident.Type() == TNaming_MODIFUNTIL ||
1112                  (Ident.Type() == TNaming_INTERSECTION && Naming->ChangeName().Arguments().Extent() == 1)) {
1113 #ifdef MDTV_DEB_MOD
1114         cout <<"BuildName(CompareInModification): NameType = " <<Ident.Type() << " NS ";
1115         Print_Entry(Ident.Type() == TNaming_MODIFUNTIL ? NS->Label() : Naming->ChangeName().Arguments().First()->Label());
1116         cout <<"Selection type = " << Selection.ShapeType() << " TS = " << Selection.TShape()->This() << endl;  
1117 #endif
1118         Handle(TNaming_NamedShape) NewNS =
1119           CompareInModification(Ident.Type() == TNaming_MODIFUNTIL ? NS : Naming->ChangeName().Arguments().First(), Selection);
1120         if (!NewNS.IsNull()) { // there is need to describe name in detail: modification with type 1:n in the same label
1121           StandardFilter = Standard_False;
1122           if (Ident.IsFeature()) { // for MODIFUNTIL: change it to the GENERATION
1123             Naming = TNaming_Naming::Insert(F);
1124             TNaming_Name& theName = Naming->ChangeName();
1125             theName.ShapeType(Selection.ShapeType());
1126             theName.Shape(Selection); //szy 21.10.03
1127             theName.Type(TNaming_GENERATION);
1128             theName.Append(TNaming_Tool::NamedShape(Selection,F));
1129             theName.Append(NewNS);
1130             Naming->Regenerate(MDF.ChangeValid());
1131             Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1132
1133           }
1134           Filter (F,MDF,Selection,Context,Localizer,NS,0);
1135         }
1136       }
1137       if (StandardFilter) if (!Compare (NS,MDF,Stop,Selection)) {
1138         Filter (F,MDF,Selection,Context,Localizer,NS,0);
1139       }
1140
1141 #else
1142
1143       if (IsGeneration) {
1144         if (!CompareInGeneration (NS,Selection)) {
1145           TopoDS_Shape               NewContext;
1146           Handle(TNaming_NamedShape) NewStop;
1147           FindNewShapeInFather (Ident.NamedShapeOfGeneration(),NewContext);
1148           Filter (F,MDF,Selection,NewContext,Localizer,NS,0);
1149         }
1150       }
1151       else {
1152         if (!Compare (NS,MDF,Stop,Selection))
1153           Filter (F,MDF,Selection,Context,Localizer,NS,0);
1154       }
1155
1156 #endif
1157
1158     }
1159   }
1160   if (MDF.WithValid()) MDF.Valid(NS->Label());
1161 #ifdef MDTV_DEB_MOD
1162     if(!NS.IsNull()) {
1163       TCollection_AsciiString E;
1164       TDF_Tool::Entry(NS->Label(), E);
1165       cout <<"Returned NS from Label = "<< E << endl;
1166     }
1167 #endif 
1168   return NS;
1169 }
1170
1171 //=======================================================================
1172 //function : Validate
1173 //purpose  : 
1174 //=======================================================================
1175
1176 static void Validate(TNaming_Scope&    MDF,
1177                      TNaming_OldShapeIterator& it)
1178 {
1179   MDF.Valid(it.Label()); 
1180   MDF.ValidChildren(it.Label());
1181
1182   TNaming_OldShapeIterator it2(it);
1183   for (; it2.More(); it2.Next()) {
1184     Validate (MDF,it2);
1185   }
1186 }
1187
1188 //=======================================================================
1189 //function : UnValidate
1190 //purpose  : 
1191 //=======================================================================
1192
1193 static void UnValidate(TNaming_Scope&    MDF,
1194                        TNaming_NewShapeIterator& it)
1195 {
1196   MDF.Unvalid(it.Label()); 
1197   MDF.UnvalidChildren(it.Label());
1198
1199   TNaming_NewShapeIterator it2(it);
1200   for (; it2.More(); it2.Next()) {
1201     UnValidate (MDF,it2);
1202   }
1203 }
1204
1205 //=======================================================================
1206 //function : BuildScope
1207 //purpose  : adds to the MDF the label of <Context> NamedShape, 
1208 //         : its children, all its oldShapes and its children.
1209 //         : unvalidates all newShapes and it's children.
1210 //         : If <Context> is null or next modification has an empty newShape 
1211 //         : ( this shape was deleted ), then MDF.WithValid(Standard_False) 
1212 //         : and nothing is added to the scope.
1213 //=======================================================================
1214
1215 static void BuildScope (TNaming_Scope&    MDF,
1216                              const TopoDS_Shape&    Context,
1217                              const TDF_Label&       Acces) 
1218 {
1219   if (Context.IsNull()) {
1220     MDF.WithValid(Standard_False);
1221     return;
1222   }
1223
1224   //----------------------------------------------------
1225   // Is context the current state
1226   //----------------------------------------------------
1227   Handle(TNaming_NamedShape) NS   = TNaming_Tool::NamedShape(Context,Acces);
1228   Handle(TNaming_NamedShape) Next = NextModif(NS); // if NS has subsequent evolution = MODIFY, return it
1229   if (Next.IsNull()) {  
1230     MDF.WithValid(Standard_False);
1231     return;
1232   }
1233   //----------------------------- 
1234   // a posteriori naming
1235   //-----------------------------  
1236   MDF.WithValid(Standard_True);
1237   MDF.Valid(NS->Label());
1238   MDF.ValidChildren(NS->Label());
1239   TNaming_OldShapeIterator it(Context,Acces);
1240
1241   for (; it.More(); it.Next()) {
1242     Validate(MDF,it);
1243   }
1244
1245   TNaming_NewShapeIterator it2(Context,Acces);
1246   for (;it2.More(); it2.Next()) {
1247     UnValidate (MDF,it2);
1248   }
1249 }
1250
1251 //=======================================================================
1252 static Standard_Boolean HasAncFace(const TopoDS_Shape& Context, 
1253                                    const TopoDS_Shape& W, TopoDS_Shape& Face)
1254
1255   Standard_Boolean hasFace(Standard_False);
1256   if(W.ShapeType() != TopAbs_WIRE)
1257     return hasFace;
1258   TopExp_Explorer exp(Context, TopAbs_FACE);
1259   for(;exp.More(); exp.Next()) {
1260     for (TopoDS_Iterator it(exp.Current()) ; it.More(); it.Next()) {
1261       if(it.Value().IsEqual(W)) {// is the Wire ?
1262         Face = exp.Current();
1263         if(!Face.IsNull()) {
1264           hasFace = Standard_True;
1265           //      cout << "HasAncFace: TS = " <<theFace.TShape()->This() <<endl;
1266           break;
1267         }       
1268       }
1269     }
1270     if(hasFace) break;
1271   }
1272   return hasFace;
1273 }
1274
1275 //=======================================================================
1276 //function : BuildNameWire
1277 //purpose  : Names Wire
1278 //=======================================================================
1279
1280 static Handle(TNaming_NamedShape) BuildNameWire (const TDF_Label&                  F,
1281                                                  TNaming_Scope&                    MDF,
1282                                                  const TopoDS_Shape&               Selection,
1283                                                  const TopoDS_Shape&               Context,
1284                                                  const Handle(TNaming_NamedShape)& Stop,
1285                                                  const Standard_Boolean            Geom)
1286 {  
1287   Handle (TNaming_NamedShape) NS;
1288   Standard_Boolean found(Standard_False);
1289   Handle (TNaming_Naming)  Naming;
1290   if(!F.FindAttribute(TNaming_Naming::GetID(), Naming)) {
1291     Naming = new TNaming_Naming ();
1292     F.AddAttribute (Naming);
1293     TNaming_Name& theName = Naming->ChangeName();
1294     theName.ShapeType(Selection.ShapeType());
1295     theName.Shape(Selection); 
1296   } 
1297
1298   TNaming_Name& theName = Naming->ChangeName();  
1299   TopoDS_Shape aFace;
1300   Standard_Boolean hasFace = HasAncFace(Context, Selection, aFace);
1301   if(Selection.ShapeType() == TopAbs_WIRE && Context.ShapeType() < Selection.ShapeType() && hasFace) {  
1302     theName.Type(TNaming_WIREIN);
1303     if(Context.ShapeType() == TopAbs_FACE) {
1304       for (TopoDS_Iterator it(Context) ; it.More(); it.Next()) {
1305         if(it.Value().IsEqual(Selection)) {
1306           if (TNaming_Selector::IsIdentified (F, Context, NS, Geom)) {          
1307             theName.Append(NS);
1308             found = Standard_True;
1309             break;
1310           }
1311         }
1312       }
1313       if(!found)
1314         return BuildNS (F,Selection, TNaming_UNKNOWN);
1315     } else {
1316         Standard_Integer indx(0), i(0);
1317         for (TopoDS_Iterator it(aFace); it.More(); it.Next()) {
1318           i++;
1319           if(it.Value().IsEqual(Selection)) {
1320             found = Standard_True;
1321             theName.Append(BuildName (Naming->Label(),MDF,aFace,Context,Stop,Geom));
1322             indx = i;
1323           }
1324         }
1325         if(found) {
1326           const Standard_Integer num(i);
1327           indx = indx | num << 8;
1328           //cout << " final Index = " << indx <<endl;
1329           theName.Index(indx);
1330         } else
1331           return BuildNS (F,Selection, TNaming_UNKNOWN);
1332       }
1333   }
1334   else {
1335     theName.Type(TNaming_UNION);
1336     for (TopExp_Explorer exp(Selection,TopAbs_EDGE) ; exp.More(); exp.Next()) {
1337       if(exp.Current().IsNull()) continue;
1338       if (BRep_Tool::Degenerated(TopoDS::Edge(exp.Current()))) continue;//03.03.2010
1339       theName.Append(BuildName (Naming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1340     }
1341   }
1342   //Naming->GetName().Solve(Naming->Label(),MDF.GetValid());
1343   Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);  
1344   return NS;
1345 }
1346
1347 //=======================================================================
1348 static Standard_Boolean IsOneIn (const TopoDS_Shape& S, const TopoDS_Shape& Context)
1349 {
1350   Standard_Boolean found(Standard_False);
1351   if(S.IsNull() || Context.IsNull()) return found;
1352   for (TopExp_Explorer exp(Context,S.ShapeType()); exp.More(); exp.Next()) {
1353     if (exp.Current().IsEqual(S)) {
1354       found = Standard_True;
1355       break;
1356     }
1357   }
1358   return found;
1359 }
1360
1361 //=======================================================================
1362 static Standard_Boolean IsAllIn (const TopoDS_Shape& S, const TopoDS_Shape& Context)
1363 {
1364 #ifdef MDTV_DEB_CC
1365   Write(S, "IsAllIn_Sel.brep");
1366 #endif
1367   Standard_Boolean found(Standard_False);
1368   if(S.IsNull() || Context.IsNull()) return found;
1369   Standard_Integer num1(0), num2(0);
1370   for(TopoDS_Iterator it(S);it.More();it.Next(),num1++) {
1371 #ifdef MDTV_DEB_CC
1372   cout <<"S sub-shape type = " << it.Value().ShapeType() <<endl;
1373   Write (it.Value(), "Sel_ItValue.brep");
1374 #endif
1375   if(it.Value().ShapeType() != TopAbs_COMPOUND)
1376     for (TopExp_Explorer exp(Context,it.Value().ShapeType()); exp.More(); exp.Next()) {
1377 #ifdef MDTV_DEB_CC
1378   cout <<"Context sub-shape type = " << exp.Current().ShapeType() <<endl;
1379   Write(exp.Current(), "Contex_Curnt.brep");
1380 #endif
1381       if (exp.Current().IsEqual(it.Value())) { 
1382         num2++;
1383         break;
1384       }
1385     } else {
1386       Standard_Boolean isAll = IsAllIn(it.Value(), Context);
1387       if(isAll) 
1388         num2++;
1389     }
1390   }
1391   if(num1 == num2)
1392     found = Standard_True;
1393 #ifdef MDTV_DEB_CC
1394   else
1395     cout <<"Compound case : selected num1 = " << num1 << " context contains num2 = " << num2 << endl;
1396 #endif
1397   return found;
1398 }
1399 //=======================================================================
1400 //function : RepeatabilityInContext
1401 //purpose  : 
1402 //=======================================================================
1403 static Standard_Integer RepeatabilityInContext(const TopoDS_Shape& Selection, 
1404                                                const TopoDS_Shape& Context)
1405 {
1406   Standard_Integer aNum(0);
1407   if (!Context.IsNull() && !Selection.IsNull()) {
1408 //    Write(Selection, "Repeat_Selection.brep");
1409 //    Write(Context, "Repeat_Context.brep");
1410     if (Context.ShapeType() < Selection.ShapeType()) {
1411       for (TopExp_Explorer exp(Context,Selection.ShapeType()); exp.More(); exp.Next()) {
1412         if (exp.Current().IsSame(Selection)) 
1413           aNum++;
1414       }
1415     } 
1416     else if(Selection.ShapeType() == TopAbs_COMPOUND) {
1417       TopoDS_Iterator it(Selection);
1418       for(;it.More();it.Next()) {
1419         Standard_Integer n(0);
1420         for (TopExp_Explorer exp(Context,it.Value().ShapeType()); exp.More(); exp.Next()) {
1421           if (exp.Current().IsSame(it.Value())) {
1422             n++;
1423           }
1424         }
1425         if(n > aNum) aNum = n;
1426       }
1427     } 
1428   }
1429 #ifdef MDTV_DEB_OR
1430       cout <<"RepeatabilityInContext: = " <<aNum <<endl;
1431 #endif 
1432   return aNum;
1433 }
1434 //=======================================================================
1435 //function : BuildAggregationNam
1436 //purpose  : 
1437 //=======================================================================
1438 static void BuildAggregationName (const TDF_Label&                  F,
1439                                   TNaming_Scope&                    MDF,
1440                                   const TopoDS_Shape&               S,
1441                                   const TopoDS_Shape&               Context,
1442                                   const Handle(TNaming_NamedShape)& Stop,
1443                                   const Standard_Boolean            Geom)
1444 {
1445   const Standard_Boolean found2 = IsAllIn(S, Context); 
1446   Handle (TNaming_Naming)  Naming;
1447   if(!F.FindAttribute(TNaming_Naming::GetID(), Naming)) {
1448     Naming = new TNaming_Naming ();
1449     F.AddAttribute (Naming);
1450     TNaming_Name& theName = Naming->ChangeName();
1451     theName.ShapeType(S.ShapeType());
1452     theName.Shape(S); 
1453   } 
1454 #ifdef MDTV_DEB_CC
1455   cout <<"BuildAggregationName ==> ";
1456   Print_Entry(Naming->Label());
1457 #endif
1458   TNaming_Name& theName = Naming->ChangeName();  
1459   for (TopoDS_Iterator itc(S) ; itc.More(); itc.Next()) {
1460     const TopoDS_Shape& aS = itc.Value();
1461     if ((aS.ShapeType() == TopAbs_SOLID && !TNaming_Tool::NamedShape(aS,Naming->Label()).IsNull()) ||
1462         aS.ShapeType() == TopAbs_FACE  ||
1463         aS.ShapeType() == TopAbs_EDGE  ||
1464         aS.ShapeType() == TopAbs_VERTEX ) {
1465       theName.Append(BuildName (F, MDF, aS,Context,Stop,Geom));
1466     } else { // ==> union of union || union of wires
1467       TopAbs_ShapeEnum atomTyp; 
1468       switch (aS.ShapeType()) 
1469         {
1470         case TopAbs_SOLID:
1471         case TopAbs_SHELL:
1472           atomTyp = TopAbs_FACE;
1473           break;
1474         case TopAbs_WIRE:
1475           atomTyp = TopAbs_EDGE;
1476           break;
1477         default: 
1478           atomTyp = TopAbs_SHAPE;
1479         }
1480       
1481       Handle(TNaming_NamedShape) aNS;
1482       Handle (TNaming_Naming)  aNaming = TNaming_Naming::Insert(F); 
1483       TNaming_Name&               aName = aNaming->ChangeName();          
1484       aName.ShapeType(aS.ShapeType());
1485       aName.Shape(aS); 
1486       aName.Type(TNaming_UNION);
1487       
1488       if (atomTyp != TopAbs_SHAPE) {
1489         if(aS.ShapeType() == TopAbs_WIRE) {
1490           aNS = BuildNameWire (aNaming->Label(), MDF, aS, Context,Stop,Geom);
1491         }
1492         else {
1493           for (TopExp_Explorer exp(aS,atomTyp) ; exp.More(); exp.Next()) {
1494             aName.Append(BuildName (aNaming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1495           }
1496         }
1497       } else {
1498 #ifdef MDTV_DEB_CC
1499         cout << "atomic type is NOT defined ... ==> Aggregation" <<endl;
1500 #endif
1501         BuildAggregationName(aNaming->Label(),MDF, aS, Context,Stop,Geom);
1502       }
1503       if(found2) {
1504         aNS = TNaming_Tool::NamedShape(Context, F);
1505         if(!aNS.IsNull()) 
1506           aNaming->ChangeName().ContextLabel(aNS->Label());
1507       }
1508       
1509       aNaming->GetName().Solve(aNaming->Label(),MDF.GetValid());
1510       if(aNaming->Label().FindAttribute(TNaming_NamedShape::GetID(),aNS))
1511         if (!Geom && TestSolution(MDF,aNS,aS)) {
1512           theName.Append(aNS);
1513         }
1514     }
1515   }
1516 }
1517
1518
1519 //=======================================================================
1520 //function : Name
1521 //purpose  : 
1522 //=======================================================================
1523
1524 Handle(TNaming_NamedShape) TNaming_Naming::Name (const TDF_Label&       F,
1525                                                  const TopoDS_Shape&    S,
1526                                                  const TopoDS_Shape&    Context,
1527                                                  const Standard_Boolean Geom,
1528                                                  const Standard_Boolean KeepOrientation,
1529                                                  const Standard_Boolean BNProblem)
1530
1531 {
1532   Handle(TNaming_NamedShape) NS;
1533   if (KeepOrientation) {
1534 #ifdef MDTV_DEB_INNS
1535     cout <<"KeepOR = 1: "; Print_Entry(F);
1536 #endif
1537     Standard_Integer aNum = RepeatabilityInContext(S, Context);
1538
1539     Standard_Boolean aBNproblem = (BNProblem) ? (aNum /*== 1*/ && S != Context) : Standard_False;
1540
1541     if (aNum > 1 || aBNproblem) {
1542       TopoDS_Shape UC = TNaming::FindUniqueContext(S, Context);
1543       Handle(TopTools_HArray1OfShape) Arr;
1544       if (UC.IsNull() && S.ShapeType() == TopAbs_COMPOUND) {
1545         UC = TNaming::FindUniqueContextSet(S, Context, Arr);
1546 #ifdef MDTV_DEB_CC
1547         Write(UC, "UniqueContextSet.brep");
1548         Write(S,  "InitialSelection.brep");
1549         if(S.ShapeType()==TopAbs_COMPOUND) {
1550           TCollection_AsciiString aNam("S_");
1551           TopoDS_Iterator it(S);
1552           for(int i=1;it.More();it.Next(),i++) {           
1553             TCollection_AsciiString aName = aNam + i + ".brep";     
1554             Write(it.Value(), aName.ToCString());
1555           }
1556         }
1557 #endif  
1558       }
1559       if(!UC.IsNull()) {
1560         Handle (TNaming_Naming)  Naming = TNaming_Naming::Insert(F);
1561         TNaming_Name&           theName = Naming->ChangeName();
1562         theName.ShapeType(S.ShapeType());
1563         theName.Shape(S); 
1564         theName.Type(TNaming_ORIENTATION);
1565
1566         if (!TNaming_Selector::IsIdentified (F, S, NS, Geom)) 
1567           NS = TNaming_Naming::Name(Naming->Label(),S,Context,Geom,0);
1568         theName.Append (NS);
1569 #ifdef MDTV_OR
1570         cout << " Sel Label ==> "; Print_Entry(NS->Label());
1571 #endif
1572 //szy 21.10.2009        
1573         if(S.ShapeType() == TopAbs_EDGE && UC.ShapeType() == TopAbs_FACE) {
1574           if(RepeatabilityInContext(S, UC) == 2) { //sim. edge
1575             TopoDS_Iterator itw(UC);
1576             for(;itw.More();itw.Next()) {
1577               Standard_Boolean found(Standard_False);
1578               TopoDS_Iterator it(itw.Value());
1579               for(int i=1;it.More();it.Next(),i++) {
1580                 if(it.Value().IsEqual(S)) {
1581                   theName.Index(i);
1582                   found = Standard_True;
1583 #ifdef MDTV_OR
1584                   cout << "ORDER = " << i <<endl;
1585 #endif
1586                   break;
1587                 }
1588               }
1589               if(found) break;
1590             }
1591           }
1592         }
1593 //      
1594         if(S.ShapeType() == TopAbs_COMPOUND && Arr->Length() > 1) {
1595           // N arguments: to be optimized to avoid duplication of the same Context shape
1596           for(Standard_Integer i = Arr->Lower(); i <= Arr->Upper(); i++) {
1597             NS = TNaming_Naming::Name(Naming->Label(), Arr->Value(i), Context, Geom, 1, aBNproblem);
1598             theName.Append (NS);
1599           }
1600         } else {          
1601           NS = TNaming_Naming::Name(Naming->Label(),UC,Context, Geom, 1, aBNproblem);
1602           theName.Append (NS);
1603 #ifdef MDTV_OR
1604         cout << " Cont Label ==> "; Print_Entry(NS->Label());
1605 #endif
1606         }
1607       //Naming->Update();
1608         TNaming_Scope MDF;
1609         BuildScope (MDF,Context,F);
1610         Naming->GetName().Solve(Naming->Label(),MDF.GetValid());
1611         Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1612         theName.ContextLabel(NS->Label());
1613         if (Geom) return NS;
1614         if(NS.IsNull()) {
1615         cout <<" %%% WARNING: TNaming_Naming::Name:  FAILED"<<endl;
1616         return BuildNS (F,S, TNaming_UNKNOWN);
1617       }
1618
1619         if (!Geom && TestSolution(MDF,NS,S)) return NS;      
1620         cout <<" %%% WARNING: TNaming_Naming::Name:  FAILED"<<endl;
1621
1622         // Naming n is  unsatisfactory
1623         return BuildNS (F,S, TNaming_UNKNOWN);
1624       }
1625     } else
1626       if (TNaming_Selector::IsIdentified (F, S, NS, Geom))
1627         return NS;
1628   }
1629
1630   //------------------------------------------------------------
1631   // Construction du MDF tel que <Context> soit le dernier etat
1632   // valide,
1633   // Ceci pour les localisation a posteriori par exemple. 
1634   //------------------------------------------------------------  
1635
1636   TNaming_Scope MDF;
1637   BuildScope (MDF,Context,F);
1638   Handle(TNaming_NamedShape) Stop;
1639
1640 #ifdef OCC352
1641
1642   if ((S.ShapeType() == TopAbs_SOLID && !TNaming_Tool::NamedShape(S,F).IsNull()) ||
1643
1644 #else
1645
1646   if (S.ShapeType() == TopAbs_SOLID ||
1647
1648 #endif
1649
1650       S.ShapeType() == TopAbs_FACE  ||
1651       S.ShapeType() == TopAbs_EDGE  ||
1652       S.ShapeType() == TopAbs_VERTEX ) {
1653     //---------------------------------------
1654     // Localisation de S comme element simple.
1655     //---------------------------------------
1656     Handle(TNaming_NamedShape) NS = BuildName (F,MDF,S,Context,Stop,Geom);
1657     if (Geom) return NS; 
1658     if (!Geom && TestSolution(MDF,NS,S)) return NS; 
1659   }
1660   else {
1661     //----------------------------------------------------
1662     // Localisation de S comme ensemble d elements simples.
1663     //-----------------------------------------------------
1664     Handle(TNaming_NamedShape) NS;
1665     Handle (TNaming_Naming)  Naming = TNaming_Naming::Insert(F); 
1666     TNaming_Name&           theName = Naming->ChangeName();
1667
1668     theName.ShapeType(S.ShapeType());// modified by vro 05.09.00
1669     theName.Shape(S); 
1670     if(S.ShapeType() != TopAbs_WIRE) 
1671       theName.Type(TNaming_UNION);
1672
1673 #ifdef OCC352
1674     TopAbs_ShapeEnum atomType;
1675     switch (S.ShapeType()) {
1676     case TopAbs_COMPSOLID:
1677     case TopAbs_SOLID:
1678     case TopAbs_SHELL:
1679       atomType = TopAbs_FACE;
1680       break;
1681     case TopAbs_WIRE:
1682       atomType = TopAbs_EDGE;
1683       break;
1684     default:
1685       atomType = TopAbs_SHAPE;
1686     }
1687     Standard_Boolean found(Standard_False);
1688     if (!Context.IsNull()) {
1689       if (Context.ShapeType() < S.ShapeType()) 
1690         found = IsOneIn(S, Context);
1691       if(found) {
1692         NS = TNaming_Tool::NamedShape(Context, F);
1693         if(!NS.IsNull()) 
1694           theName.ContextLabel(NS->Label());
1695       }
1696     }
1697     if (atomType == TopAbs_SHAPE) {
1698       if(S.ShapeType() == TopAbs_COMPOUND) {
1699         BuildAggregationName(Naming->Label(),MDF, S, Context,Stop,Geom);
1700       } else { 
1701         for (TopoDS_Iterator it(S) ; it.More(); it.Next()) {
1702           theName.Append(BuildName (Naming->Label(),MDF,it.Value(),Context,Stop,Geom));
1703         }
1704       }
1705     } else {
1706       if(S.ShapeType() == TopAbs_WIRE)
1707         NS = BuildNameWire (Naming->Label(), MDF, S, Context,Stop,Geom);
1708       else {
1709         theName.Type(TNaming_UNION);
1710         for (TopExp_Explorer exp(S,atomType) ; exp.More(); exp.Next()) {
1711           theName.Append(BuildName (Naming->Label(),MDF,exp.Current(),Context,Stop,Geom));
1712         }
1713       }
1714     }
1715 #else    
1716     for (TopoDS_Iterator it(S) ; it.More(); it.Next()) {
1717       theName.Append(BuildName (Naming->Label(),MDF,it.Value(),Context,Stop,Geom));
1718     }
1719 #endif
1720
1721     //Naming->Update(); 
1722     Naming->GetName().Solve(Naming->Label(),MDF.GetValid());
1723     Naming->Label().FindAttribute(TNaming_NamedShape::GetID(),NS);
1724     if (Geom) return NS; 
1725
1726 #ifdef OCC273
1727     if(NS.IsNull()) return BuildNS (F,S, TNaming_UNKNOWN); 
1728 #endif
1729
1730     if (!Geom && TestSolution(MDF,NS,S)) return NS; 
1731   }
1732   
1733       cout <<" %%% WARNING: TNaming_Naming::Name:  FAILED"<<endl;
1734
1735   // Naming n is not satisfactory
1736   return BuildNS (F,S, TNaming_UNKNOWN); 
1737 }
1738
1739
1740 //=======================================================================
1741 //function : TNaming_Naming
1742 //purpose  : 
1743 //=======================================================================
1744
1745 TNaming_Naming::TNaming_Naming() {}
1746
1747 //=======================================================================
1748 //function : ID
1749 //purpose  : 
1750 //=======================================================================
1751
1752 const Standard_GUID& TNaming_Naming::ID () const
1753 {
1754   return GetID(); 
1755 }
1756
1757
1758 //=======================================================================
1759 //function : IsDefined
1760 //purpose  : 
1761 //=======================================================================
1762
1763 Standard_Boolean TNaming_Naming::IsDefined() const
1764 {
1765   return (myName.Type() != TNaming_UNKNOWN);
1766 }
1767
1768 //=======================================================================
1769 //function : GetName
1770 //purpose  : 
1771 //=======================================================================
1772
1773 const TNaming_Name& TNaming_Naming::GetName() const
1774 {
1775   return myName;
1776 }
1777
1778 //=======================================================================
1779 //function : ChangeName
1780 //purpose  : 
1781 //=======================================================================
1782
1783 TNaming_Name& TNaming_Naming::ChangeName()
1784 {
1785   return myName;
1786 }
1787
1788 //=======================================================================
1789 //function : Regenerate
1790 //purpose  : idem designer
1791 //=======================================================================
1792
1793 Standard_Boolean TNaming_Naming::Regenerate (TDF_LabelMap& MDF)  
1794
1795 {
1796   return myName.Solve(Label(),MDF);
1797 }
1798
1799
1800 //=======================================================================
1801 //function : NewEmpty
1802 //purpose  : 
1803 //=======================================================================
1804
1805 Handle(TDF_Attribute) TNaming_Naming::NewEmpty () const
1806 {  
1807   return new TNaming_Naming (); 
1808 }
1809
1810
1811 //=======================================================================
1812 //function : Restore
1813 //purpose  : 
1814 //=======================================================================
1815
1816 void TNaming_Naming::Restore(const Handle(TDF_Attribute)& other) 
1817 {
1818   Handle(TNaming_Naming) OtherNaming = Handle(TNaming_Naming)::DownCast(other);
1819   myName = OtherNaming->ChangeName();
1820 }
1821
1822 //=======================================================================
1823 //function : Paste
1824 //purpose  : 
1825 //=======================================================================
1826
1827 void TNaming_Naming::Paste (const Handle(TDF_Attribute)& into,
1828                                const Handle(TDF_RelocationTable)& RT) const
1829 {
1830   Handle(TNaming_Naming) NewNaming = Handle(TNaming_Naming)::DownCast(into);
1831   myName.Paste(NewNaming->ChangeName(),RT);
1832 }
1833   
1834 //=======================================================================
1835 //function : References
1836 //purpose  : Redefined from TDF_Attribute
1837 //=======================================================================
1838
1839 void TNaming_Naming::References(const Handle(TDF_DataSet)& DataSet) const
1840 {
1841   // Iteration on NamedShape of the name
1842   TNaming_ListIteratorOfListOfNamedShape it(myName.Arguments());
1843   for (;it.More();it.Next()) DataSet->AddAttribute(it.Value());
1844   if (!myName.StopNamedShape().IsNull()) DataSet->AddAttribute(myName.StopNamedShape());
1845 }
1846 //=======================================================================
1847 //function : Dump
1848 //purpose  : 
1849 //=======================================================================
1850
1851 Standard_OStream& TNaming_Naming::Dump (Standard_OStream& anOS) const
1852 {  
1853   anOS << "TNaming_Naming";
1854   return anOS;
1855 }
1856
1857
1858 //=======================================================================
1859 //function :ExtendedDump
1860 //purpose  : 
1861 //=======================================================================
1862
1863 void TNaming_Naming::ExtendedDump(Standard_OStream& anOS,
1864                                   const TDF_IDFilter& /*aFilter*/,
1865                                   TDF_AttributeIndexedMap& /*aMap*/) const
1866 {
1867   anOS << "TNaming_Naming ExtendedDump  ";
1868   //anOS<<"myContext: #" <<aMap.Add(myContext)<<endl; 
1869 }
1870