0023766: Shape orientation processing mechanism redesign
[occt.git] / src / TNaming / TNaming_Tool.cxx
1 // Created on: 1997-01-06
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 #include <TDF_LabelList.hxx>
23 #include <TNaming_Tool.ixx>
24 #include <TNaming_NamedShape.hxx>
25 #include <TNaming_NewShapeIterator.hxx>
26 #include <TNaming_OldShapeIterator.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TNaming_RefShape.hxx>
29 #include <TopoDS_Compound.hxx>
30 #include <BRep_Builder.hxx>
31 #include <TopTools_MapOfShape.hxx>
32 #include <TopTools_MapIteratorOfMapOfShape.hxx>
33
34 #include <Standard_NoSuchObject.hxx>
35
36 #define BUC60862
37
38 #ifdef BUC60862
39 #include <TNaming_Naming.hxx>
40 #include <TNaming_ListOfNamedShape.hxx>
41 #include <TDF_AttributeMap.hxx>
42 #include <TDF_Tool.hxx>
43 #include <TDF_MapIteratorOfAttributeMap.hxx>
44 #include <TDF_ChildIterator.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
47 #endif
48
49 //=======================================================================
50 //function : LastModif 
51 //purpose  : 
52 //=======================================================================
53
54 static void LastModif(      TNaming_NewShapeIterator& it,
55                       const TopoDS_Shape&             S,
56                             TopTools_MapOfShape&      MS,
57                       const TDF_LabelMap&             Updated,
58                             TDF_LabelList&            Deleted)
59
60   Standard_Boolean YaModif = Standard_False;
61   for (; it.More(); it.Next()) {
62     const TDF_Label&    Lab = it.Label();
63     if (!Updated.Contains(Lab)) continue;
64     
65     if (it.IsModification()) {
66       YaModif = Standard_True;
67       TNaming_NewShapeIterator it2(it);
68       if (!it2.More()) {
69         const TopoDS_Shape& S1   = it.Shape();
70         if (S1.IsNull()) {
71           Deleted.Append(Lab);
72         }
73         else {
74           MS.Add (S1);  // Modified
75         }
76       }
77       else
78         LastModif(it2,it.Shape(),MS,Updated,Deleted);
79     }
80   }
81   if (!YaModif) 
82     MS.Add(S);  
83 }
84
85 //=======================================================================
86 //function : LastModif 
87 //purpose  : 
88 //=======================================================================
89
90 static void LastModif(TNaming_NewShapeIterator& it,
91                       TopTools_MapOfShape&      MS,
92                       const TopoDS_Shape&       S,
93                       TDF_LabelList&            Deleted)
94 {
95   Standard_Boolean YaModif = Standard_False;
96   for (; it.More(); it.Next()) {
97     const TDF_Label&    Lab = it.Label();
98     if (it.IsModification()) {
99       YaModif = Standard_True;
100       TNaming_NewShapeIterator it2(it);
101       if (!it2.More()) {
102         const TopoDS_Shape& S1   = it.Shape();
103         if (S1.IsNull()) {
104           Deleted.Append(Lab);
105         }
106         else {
107           MS.Add (S1);  // Modified
108         }
109       }
110       else 
111         LastModif(it2,MS,it.Shape(), Deleted);
112     }
113   }
114   if (!YaModif) 
115     MS.Add(S);
116 }
117
118 //=======================================================================
119 //function : MakeShape
120 //purpose  : 
121 //=======================================================================
122
123 static TopoDS_Shape MakeShape (const TopTools_MapOfShape& MS) 
124 {  
125   if (!MS.IsEmpty ()) {
126     TopTools_MapIteratorOfMapOfShape it(MS);
127     if (MS.Extent() == 1) {
128       return it.Key();
129     }
130     else {
131       TopoDS_Compound C;
132       BRep_Builder B;
133       B.MakeCompound(C);
134       for (; it.More(); it.Next()){ 
135         B.Add(C,it.Key());
136       }
137       return C;
138     }
139   }
140   return TopoDS_Shape();  
141 }
142
143 //=======================================================================
144 //function : GetShape
145 //purpose  : 
146 //=======================================================================
147 TopoDS_Shape TNaming_Tool::GetShape(const Handle(TNaming_NamedShape)& NS)
148 {  
149   TNaming_Iterator itL (NS);
150   TopTools_MapOfShape  MS;
151   if(NS->Evolution() == TNaming_SELECTED) {
152     for (; itL.More(); itL.Next()) {
153       if(!itL.NewShape().IsNull()) {
154                 if(itL.NewShape().ShapeType() != TopAbs_VERTEX ) { //OR-N
155               Handle (TNaming_Naming)  aNaming;
156               NS->Label().FindAttribute(TNaming_Naming::GetID(), aNaming);
157               if(!aNaming.IsNull()) {
158                     if(aNaming->GetName().Orientation() == TopAbs_FORWARD ||
159                                 aNaming->GetName().Orientation() == TopAbs_REVERSED) {
160                       TopoDS_Shape aS = itL.NewShape();
161                           if(aNaming->GetName().Type() == TNaming_ORIENTATION) {
162                             aS.Orientation(aNaming->GetName().Orientation());
163                           } else {
164                                 Handle (TNaming_Naming)  aNaming2;
165                                 TDF_ChildIterator it(aNaming->Label());
166                                 for(;it.More();it.Next()) {
167                                   const TDF_Label& aLabel = it.Value();
168                                   aLabel.FindAttribute(TNaming_Naming::GetID(), aNaming2);
169                       if(!aNaming2.IsNull()) {
170                                     if(aNaming2->GetName().Type() == TNaming_ORIENTATION) {
171                                           aS.Orientation(aNaming2->GetName().Orientation());
172                                           break;
173                                         }
174                                   }
175                                 }
176                           }
177                       MS.Add(aS);
178                         } else  
179                             MS.Add(itL.NewShape());
180                   } else
181                   MS.Add(itL.NewShape());               
182                 } //
183                 else
184                 MS.Add(itL.NewShape());
185           }
186         }  
187   } else 
188     for (; itL.More(); itL.Next()) {
189       if (!itL.NewShape().IsNull()) MS.Add(itL.NewShape());      
190     }
191   return MakeShape(MS);
192 }
193
194 //=======================================================================
195 //function : InitialShape
196 //purpose  : 
197 //=======================================================================
198
199 TopoDS_Shape TNaming_Tool::OriginalShape (const Handle(TNaming_NamedShape)& NS)
200 {
201   TNaming_Iterator itL (NS);
202   TopTools_MapOfShape  MS;
203   for (; itL.More(); itL.Next()) {
204     MS.Add(itL.OldShape());
205   }
206   return MakeShape(MS);
207 }  
208
209 //=======================================================================
210 static void ApplyOrientation (TopTools_MapOfShape& MS, 
211                               const TopAbs_Orientation OrientationToApply)
212 {
213  if (!MS.IsEmpty ()) {
214    TopTools_MapOfShape aMS;
215    aMS.Assign(MS);
216    TopTools_MapIteratorOfMapOfShape it(aMS);
217    for (; it.More(); it.Next()) {
218      if(it.Key().Orientation() != OrientationToApply) {
219        TopoDS_Shape aS = it.Key();
220        MS.Remove(aS);
221        aS.Orientation(OrientationToApply);
222        MS.Add(aS);
223      }  
224    }
225  }
226 }
227
228 //=======================================================================
229 //function : CurrentShape
230 //purpose  : 
231 //=======================================================================
232
233 TopoDS_Shape TNaming_Tool::CurrentShape(const Handle(TNaming_NamedShape)& Att)
234
235   TopTools_MapOfShape MS;
236   TDF_LabelList Deleted;
237
238   TNaming_Iterator itL (Att);
239   for (; itL.More(); itL.Next()) {
240     const TopoDS_Shape& S = itL.NewShape();
241     if (S.IsNull()) continue;
242 //OR-N
243     Standard_Boolean YaOrientationToApply(Standard_False);
244     TopAbs_Orientation OrientationToApply(TopAbs_FORWARD);
245     if(Att->Evolution() == TNaming_SELECTED) {
246       if (itL.More() && itL.NewShape().ShapeType() != TopAbs_VERTEX) {                  
247                 Handle (TNaming_Naming)  aNaming;
248                 Att->Label().FindAttribute(TNaming_Naming::GetID(), aNaming);
249                 if(!aNaming.IsNull()) {
250                   if(aNaming->GetName().Type() == TNaming_ORIENTATION) {
251                          OrientationToApply = aNaming->GetName().Orientation();
252                   } else {
253                 Handle (TNaming_Naming)  aNaming2;
254                         TDF_ChildIterator it(aNaming->Label());
255                         for(;it.More();it.Next()) {
256                           const TDF_Label& aLabel = it.Value();
257                           aLabel.FindAttribute(TNaming_Naming::GetID(), aNaming2);
258                   if(!aNaming2.IsNull()) {
259                             if(aNaming2->GetName().Type() == TNaming_ORIENTATION) {
260                                   OrientationToApply = aNaming2->GetName().Orientation();
261                                   break;
262                                 }
263                           }
264                         }
265                   }                     
266                   if(OrientationToApply == TopAbs_FORWARD || OrientationToApply == TopAbs_REVERSED)
267                         YaOrientationToApply = Standard_True;
268                 }
269           }
270         } //
271     TNaming_NewShapeIterator it(itL);
272     if (!it.More()) {
273         MS.Add(S);
274     }
275     else {
276 //      LastModif(it, MS, S, Deleted);
277       TopTools_MapOfShape MS2;  // to be optimized later
278       LastModif(it, MS2, S, Deleted);
279       if (YaOrientationToApply) ApplyOrientation (MS2, OrientationToApply);
280       for (TopTools_MapIteratorOfMapOfShape itMS2(MS2); itMS2.More();itMS2.Next()) 
281         MS.Add(itMS2.Key());
282     }
283   }
284   return MakeShape (MS);
285 }
286
287 //=======================================================================
288 //function : CurrentShape
289 //purpose  : 
290 //=======================================================================
291 TopoDS_Shape TNaming_Tool::CurrentShape(const Handle(TNaming_NamedShape)& Att,
292                                         const TDF_LabelMap&           Updated)
293 {  
294   TDF_Label Lab = Att->Label();
295
296   TopTools_MapOfShape MS;
297   TDF_LabelList Deleted;
298   
299   if (!Updated.Contains(Lab)) {
300     return TopoDS_Shape();
301   }
302
303   TNaming_Iterator itL (Att);
304   for (; itL.More(); itL.Next()) {
305     const TopoDS_Shape& S = itL.NewShape();
306     if (S.IsNull()) continue;
307 //OR-N
308     Standard_Boolean YaOrientationToApply(Standard_False);
309     TopAbs_Orientation OrientationToApply(TopAbs_FORWARD);
310     if(Att->Evolution() == TNaming_SELECTED) {
311       if (itL.More() && itL.NewShape().ShapeType() != TopAbs_VERTEX) {
312                 Handle (TNaming_Naming)  aNaming;
313                 Att->Label().FindAttribute(TNaming_Naming::GetID(), aNaming);
314                 if(!aNaming.IsNull()) {
315                   if(aNaming->GetName().Type() == TNaming_ORIENTATION) {
316                          OrientationToApply = aNaming->GetName().Orientation();
317                   } else {
318                 Handle (TNaming_Naming)  aNaming2;
319                         TDF_ChildIterator it(aNaming->Label());
320                         for(;it.More();it.Next()) {
321                           const TDF_Label& aLabel = it.Value();
322                           aLabel.FindAttribute(TNaming_Naming::GetID(), aNaming2);
323                   if(!aNaming2.IsNull()) {
324                             if(aNaming2->GetName().Type() == TNaming_ORIENTATION) {
325                                   OrientationToApply = aNaming2->GetName().Orientation();
326                                   break;
327                                 }
328                           }
329                         }
330                   }
331                   if(OrientationToApply == TopAbs_FORWARD || OrientationToApply == TopAbs_REVERSED)
332                         YaOrientationToApply = Standard_True;           
333                 }
334       }
335     } //
336     TNaming_NewShapeIterator it(itL);
337     if (!it.More()) {
338       MS.Add(S);
339     }
340     else {
341 //      LastModif(it, S, MS, Updated, Deleted);
342       TopTools_MapOfShape MS2; // to be optimized later
343       LastModif(it, S, MS2, Updated, Deleted);
344       if (YaOrientationToApply) ApplyOrientation (MS2, OrientationToApply);
345       for (TopTools_MapIteratorOfMapOfShape itMS2(MS2); itMS2.More();itMS2.Next()) 
346         MS.Add(itMS2.Key());
347     }
348   }
349   return MakeShape (MS);
350 }
351
352 //=======================================================================
353 //function : CurrentNamedShape
354 //purpose  : 
355 //=======================================================================
356 Handle(TNaming_NamedShape) TNaming_Tool::CurrentNamedShape(const Handle(TNaming_NamedShape)& Att,
357                                                            const TDF_LabelMap&           Updated)
358 {
359   TopoDS_Shape CS = CurrentShape(Att,Updated);
360   if (CS.IsNull()) {
361     Handle(TNaming_NamedShape) NS;
362     return NS;
363   }
364   return NamedShape(CS,Att->Label());
365 }
366
367 //=======================================================================
368 //function : CurrentNamedShape
369 //purpose  : 
370 //=======================================================================
371 Handle(TNaming_NamedShape) TNaming_Tool::CurrentNamedShape(const Handle(TNaming_NamedShape)& Att)
372
373 {
374   TopoDS_Shape CS = CurrentShape(Att);
375   if (CS.IsNull()) {
376     Handle(TNaming_NamedShape) NS;
377     return NS;
378   }
379   return NamedShape(CS,Att->Label());
380 }
381
382 //=======================================================================
383 //function : FindModifUntil
384 //purpose  : 
385 //=======================================================================
386
387 static void FindModifUntil (TNaming_NewShapeIterator&         it,
388                             TopTools_MapOfShape&              MS,
389                             const Handle(TNaming_NamedShape)& Context)
390
391   for (; it.More(); it.Next()) {
392     if (!it.Shape().IsNull()) {
393       if (it.NamedShape() == Context) {
394         MS.Add(it.Shape());
395       }
396       else {
397         TNaming_NewShapeIterator it2(it);
398         FindModifUntil (it2,MS,Context);
399       }
400     }
401   }
402 }
403
404 //=======================================================================
405 //function : GeneratedShape
406 //purpose  : 
407 //=======================================================================
408 TopoDS_Shape TNaming_Tool::GeneratedShape(const TopoDS_Shape&               S,
409                                           const Handle(TNaming_NamedShape)& Generation)
410
411 {  
412   TopTools_MapOfShape MS;
413   Handle(TNaming_UsedShapes) US;
414   Generation->Label().Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
415   
416   for (TNaming_NewShapeIterator it(S,US);  it.More(); it.Next()) {
417     if (!it.Shape().IsNull() && it.NamedShape() == Generation) {
418       MS.Add(it.Shape());
419     }
420   }
421   if (MS.IsEmpty()) {
422     TNaming_NewShapeIterator it2(S,US);
423     FindModifUntil (it2,MS,Generation);
424   }
425   return MakeShape (MS);
426 }
427
428 //=======================================================================
429 //function : FirstOlds
430 //purpose  : 
431 //=======================================================================
432 void TNaming_Tool::FirstOlds (const Handle(TNaming_UsedShapes)& US,
433                               const TopoDS_Shape&         S,
434                               TNaming_OldShapeIterator&  it,
435                               TopTools_MapOfShape&       MS,
436                               TDF_LabelList&          Labels) 
437 {  
438   Standard_Integer TransDef;
439   Standard_Boolean YaModif = 0;
440
441   for (; it.More(); it.Next()) {
442     if (it.IsModification()) {
443       YaModif = 1;
444       TNaming_OldShapeIterator it2(it);
445       if (!it2.More()) { 
446         Labels.Append(TNaming_Tool::Label(US,it.Shape(),TransDef));
447         MS.Add (it.Shape()); 
448       }
449       else {
450         FirstOlds(US,it.Shape(),it2,MS,Labels);
451       }
452     }
453   }
454   if (!YaModif) MS.Add(S);
455 }
456
457 //=======================================================================
458 //function : InitialShape
459 //purpose  : 
460 //=======================================================================
461
462 TopoDS_Shape TNaming_Tool::InitialShape(const TopoDS_Shape&  S,
463                                         const TDF_Label&     Acces,
464                                         TDF_LabelList&       Labels) 
465 {
466   Handle(TNaming_UsedShapes) US;
467   Acces.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
468   TopoDS_Shape Res;
469
470   if (!TNaming_Tool::HasLabel(US,S)) return Res;
471
472   Standard_Integer Transdef;
473   Label(US,S,Transdef); 
474   TopTools_MapOfShape MS;
475   TNaming_OldShapeIterator it(S,Transdef,US);
476   if (!it.More()) {
477     return S;
478   }
479   else {
480     FirstOlds(US,S,it,MS,Labels);
481   }
482   return MakeShape(MS);
483 }
484
485
486
487 //=======================================================================
488 //function : Back
489 //purpose  : 
490 //=======================================================================
491
492 static void Back (const Handle(TNaming_NamedShape)& NS,
493                         TNaming_MapOfNamedShape&    MNS)
494 {  
495   for (TNaming_Iterator it(NS); it.More(); it.Next()) {
496     if (it.NewShape().IsNull()) continue;
497     for (TNaming_OldShapeIterator Oldit(it); Oldit.More(); Oldit.Next()) {
498       const TopoDS_Shape& OS = Oldit.Shape();
499       if (!OS.IsNull()) {
500         Handle(TNaming_NamedShape) NOS = TNaming_Tool::NamedShape(OS,NS->Label());
501         // Continue de remonter
502         if (!NOS.IsNull()) { 
503           if (MNS.Add(NOS)) Back (NOS,MNS);
504         }
505       }
506     }
507   }
508 }
509
510 //=======================================================================
511 //function : Collect
512 //purpose  : 
513 //=======================================================================
514
515 void TNaming_Tool::Collect(const Handle(TNaming_NamedShape)& NS,
516                                  TNaming_MapOfNamedShape&    MNS,
517                            const Standard_Boolean            OnlyModif)
518 {
519   MNS.Add(NS);
520   Back   (NS,MNS);
521
522   for (TNaming_Iterator it(NS); it.More(); it.Next()) {
523     if  (it.NewShape().IsNull()) continue;
524     for (TNaming_NewShapeIterator NewIt(it); NewIt.More(); NewIt.Next()) {
525       if (!OnlyModif || NewIt.IsModification()) {
526         // Continue la descente
527         Collect(NewIt.NamedShape(),MNS,OnlyModif);
528       }
529     }
530   }
531 }
532
533
534 // Pour DEBUGGER 
535 #ifdef DEB
536
537 //=======================================================================
538 //function : TNamingTool_Label
539 //purpose  : 
540 //=======================================================================
541 void TNamingTool_DumpLabel (const TopoDS_Shape& S,
542                             const TDF_Label&    Acces)
543 {
544   Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape(S,Acces);
545   NS->Label().EntryDump(cout);
546   cout<<endl;
547 }
548
549 #include <BRepTools.hxx>
550 //=======================================================================
551 //function : 
552 //purpose  : 
553 //=======================================================================
554 void TNamingTool_Write (const TopoDS_Shape& S,
555                         const Standard_CString File)
556 {
557   BRepTools::Write (S,File);  
558 }
559
560 #endif
561
562
563 #ifdef BUC60862
564 //=======================================================================
565 //function : FindShape
566 //purpose  : ANaming
567 //=======================================================================
568
569 void TNaming_Tool::FindShape(const TDF_LabelMap&               Valid,
570                              const TDF_LabelMap&               /*Forbiden*/,
571                              const Handle(TNaming_NamedShape)& Arg,
572                              TopoDS_Shape&                     S)
573 {
574   if (!Valid.IsEmpty() && !Valid.Contains(Arg->Label())) return;
575   if (Arg.IsNull() || Arg->IsEmpty()) return;
576   
577   // Which type of shape is being expected?
578   Handle(TNaming_Naming) aNaming;
579   if (!Arg->FindAttribute(TNaming_Naming::GetID(), aNaming)) {
580 #ifdef DEB
581 //    cout<<"TNaming_Tool::FindShape(): Naming attribute hasn't been found attached at the Argument label"<<endl;
582 #endif
583     return;
584   }
585
586   // Looking for sub shapes of the result shape
587   TopTools_MapOfShape subShapes;
588   TopExp_Explorer anExpl(Arg->Get(), (TopAbs_ShapeEnum)((int)(aNaming->GetName().ShapeType()) + 1));
589   for (; anExpl.More(); anExpl.Next()) subShapes.Add(anExpl.Current());
590 #ifdef DEB
591 //  cout<<"TNaming_Tool::FindShape(): Nb of sub shapes = "<<subShapes.Extent()<<endl;
592 #endif
593
594   // Looking for external arguments:
595   TNaming_ListOfNamedShape extArgs;
596   TDF_AttributeMap outRefs;
597   TDF_Tool::OutReferences(Arg->Label(), outRefs);
598   if (outRefs.IsEmpty()) {
599 #ifdef DEB
600 //    cout<<"TNaming_Tool::FindShape(): No out references have been found"<<endl;
601 #endif
602     return;
603   }
604   for (TDF_MapIteratorOfAttributeMap itr(outRefs); itr.More(); itr.Next()) {
605     if (itr.Key()->DynamicType() == STANDARD_TYPE(TNaming_NamedShape)) {
606 #ifdef DEB
607 //      Standard_Integer nbExtArgs = extArgs.Extent();
608 #endif
609       const Handle(TNaming_NamedShape)& anExtArg = Handle(TNaming_NamedShape)::DownCast(itr.Key());
610       const Handle(TNaming_NamedShape)& aCurrentExtArg = TNaming_Tool::CurrentNamedShape(anExtArg);
611       if (!aCurrentExtArg.IsNull() && !aCurrentExtArg->IsEmpty())
612         extArgs.Append(aCurrentExtArg);
613 #ifdef DEB
614 //      if (extArgs.Extent() - 1 == nbExtArgs) {
615 //      cout<<"TNaming_Tool::FindShape(): An external reference has been found at ";
616 //      itr.Key()->Label().EntryDump(cout); cout<<endl;
617 //      }
618 #endif
619     }
620   }
621
622   // The iterator on external arguments:
623   TNaming_ListIteratorOfListOfNamedShape extArgsIterator (extArgs);
624   for(; extArgsIterator.More(); extArgsIterator.Next()) {
625     Handle(TNaming_NamedShape) anExtArg = extArgsIterator.Value();
626
627     // Looking for context:
628     Handle(TNaming_NamedShape) aContextNS;
629     if (anExtArg->Label().Father().IsNull() ||
630         !anExtArg->Label().Father().FindAttribute(TNaming_NamedShape::GetID(), aContextNS)) {
631       aContextNS = anExtArg;
632 // #ifdef DEB
633 //       cout<<"TNaming_Tool::FindShape(): A context shape hasn't been found at the father label of the external argument"<<endl;
634 // #endif
635 //       continue;
636     }
637
638 #ifdef DEB
639 //    cout<<"TNaming_Tool::FindShape(): Searching in the external reference ";
640 //    aContextNS->Label().EntryDump(cout); cout<<"  ";
641 #endif
642     
643     // Lets find the sub shape of the context which coincides with our sub shapes (subShapes map):
644     TopExp_Explorer explC(aContextNS->Get(), aNaming->GetName().ShapeType()), explSubC;
645     for (; explC.More(); explC.Next()) {
646       Standard_Integer  DoesCoincide = 0;
647       const TopoDS_Shape& possibleResult = explC.Current();
648       TopTools_MapOfShape subShapesOfResult;
649       for (explSubC.Init(possibleResult, (TopAbs_ShapeEnum)((int)(aNaming->GetName().ShapeType()) + 1)); 
650            explSubC.More(); explSubC.Next()) {
651         subShapesOfResult.Add(explSubC.Current());
652       }
653       if (subShapesOfResult.Extent() != subShapes.Extent()) continue;
654       for (TopTools_MapIteratorOfMapOfShape itrR(subShapesOfResult); itrR.More(); itrR.Next()) {
655         for (TopTools_MapIteratorOfMapOfShape itr1(subShapes); itr1.More(); itr1.Next()) {
656           if (itrR.Key().IsSame(itr1.Key())) {
657             DoesCoincide++; //cout<<".";
658             break;
659           }
660         }
661       }
662       if (DoesCoincide == subShapes.Extent()) {
663 #ifdef DEB
664 //      cout<<"TNaming_Tool::FindShape(): Found! ";
665 #endif
666         S = possibleResult;
667         break;
668       }
669     }
670
671     if (!S.IsNull()) break;
672 #ifdef DEB
673 //    cout<<endl;
674 #endif
675
676   }
677
678 #ifdef DEB
679   if (S.IsNull()) {
680     cout<<"TNaming_Tool::FindShape(): There hasn't been found a sub shape of the context shape coinciding with the sub shapes of naming"<<endl;
681   }
682 #endif
683       
684 }
685 #endif