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
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.
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.
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.
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>
34 #include <Standard_NoSuchObject.hxx>
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>
49 //=======================================================================
50 //function : LastModif
52 //=======================================================================
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)
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;
65 if (it.IsModification()) {
66 YaModif = Standard_True;
67 TNaming_NewShapeIterator it2(it);
69 const TopoDS_Shape& S1 = it.Shape();
74 MS.Add (S1); // Modified
78 LastModif(it2,it.Shape(),MS,Updated,Deleted);
85 //=======================================================================
86 //function : LastModif
88 //=======================================================================
90 static void LastModif(TNaming_NewShapeIterator& it,
91 TopTools_MapOfShape& MS,
92 const TopoDS_Shape& S,
93 TDF_LabelList& Deleted)
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);
102 const TopoDS_Shape& S1 = it.Shape();
107 MS.Add (S1); // Modified
111 LastModif(it2,MS,it.Shape(), Deleted);
118 //=======================================================================
119 //function : MakeShape
121 //=======================================================================
123 static TopoDS_Shape MakeShape (const TopTools_MapOfShape& MS)
125 if (!MS.IsEmpty ()) {
126 TopTools_MapIteratorOfMapOfShape it(MS);
127 if (MS.Extent() == 1) {
134 for (; it.More(); it.Next()){
140 return TopoDS_Shape();
143 //=======================================================================
144 //function : GetShape
146 //=======================================================================
147 TopoDS_Shape TNaming_Tool::GetShape(const Handle(TNaming_NamedShape)& NS)
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());
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());
179 MS.Add(itL.NewShape());
181 MS.Add(itL.NewShape());
184 MS.Add(itL.NewShape());
188 for (; itL.More(); itL.Next()) {
189 if (!itL.NewShape().IsNull()) MS.Add(itL.NewShape());
191 return MakeShape(MS);
194 //=======================================================================
195 //function : InitialShape
197 //=======================================================================
199 TopoDS_Shape TNaming_Tool::OriginalShape (const Handle(TNaming_NamedShape)& NS)
201 TNaming_Iterator itL (NS);
202 TopTools_MapOfShape MS;
203 for (; itL.More(); itL.Next()) {
204 MS.Add(itL.OldShape());
206 return MakeShape(MS);
209 //=======================================================================
210 static void ApplyOrientation (TopTools_MapOfShape& MS,
211 const TopAbs_Orientation OrientationToApply)
213 if (!MS.IsEmpty ()) {
214 TopTools_MapOfShape aMS;
216 TopTools_MapIteratorOfMapOfShape it(aMS);
217 for (; it.More(); it.Next()) {
218 if(it.Key().Orientation() != OrientationToApply) {
219 TopoDS_Shape aS = it.Key();
221 aS.Orientation(OrientationToApply);
228 //=======================================================================
229 //function : CurrentShape
231 //=======================================================================
233 TopoDS_Shape TNaming_Tool::CurrentShape(const Handle(TNaming_NamedShape)& Att)
235 TopTools_MapOfShape MS;
236 TDF_LabelList Deleted;
238 TNaming_Iterator itL (Att);
239 for (; itL.More(); itL.Next()) {
240 const TopoDS_Shape& S = itL.NewShape();
241 if (S.IsNull()) continue;
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();
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();
266 if(OrientationToApply == TopAbs_FORWARD || OrientationToApply == TopAbs_REVERSED)
267 YaOrientationToApply = Standard_True;
271 TNaming_NewShapeIterator it(itL);
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())
284 return MakeShape (MS);
287 //=======================================================================
288 //function : CurrentShape
290 //=======================================================================
291 TopoDS_Shape TNaming_Tool::CurrentShape(const Handle(TNaming_NamedShape)& Att,
292 const TDF_LabelMap& Updated)
294 TDF_Label Lab = Att->Label();
296 TopTools_MapOfShape MS;
297 TDF_LabelList Deleted;
299 if (!Updated.Contains(Lab)) {
300 return TopoDS_Shape();
303 TNaming_Iterator itL (Att);
304 for (; itL.More(); itL.Next()) {
305 const TopoDS_Shape& S = itL.NewShape();
306 if (S.IsNull()) continue;
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();
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();
331 if(OrientationToApply == TopAbs_FORWARD || OrientationToApply == TopAbs_REVERSED)
332 YaOrientationToApply = Standard_True;
336 TNaming_NewShapeIterator it(itL);
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())
349 return MakeShape (MS);
352 //=======================================================================
353 //function : CurrentNamedShape
355 //=======================================================================
356 Handle(TNaming_NamedShape) TNaming_Tool::CurrentNamedShape(const Handle(TNaming_NamedShape)& Att,
357 const TDF_LabelMap& Updated)
359 TopoDS_Shape CS = CurrentShape(Att,Updated);
361 Handle(TNaming_NamedShape) NS;
364 return NamedShape(CS,Att->Label());
367 //=======================================================================
368 //function : CurrentNamedShape
370 //=======================================================================
371 Handle(TNaming_NamedShape) TNaming_Tool::CurrentNamedShape(const Handle(TNaming_NamedShape)& Att)
374 TopoDS_Shape CS = CurrentShape(Att);
376 Handle(TNaming_NamedShape) NS;
379 return NamedShape(CS,Att->Label());
382 //=======================================================================
383 //function : FindModifUntil
385 //=======================================================================
387 static void FindModifUntil (TNaming_NewShapeIterator& it,
388 TopTools_MapOfShape& MS,
389 const Handle(TNaming_NamedShape)& Context)
391 for (; it.More(); it.Next()) {
392 if (!it.Shape().IsNull()) {
393 if (it.NamedShape() == Context) {
397 TNaming_NewShapeIterator it2(it);
398 FindModifUntil (it2,MS,Context);
404 //=======================================================================
405 //function : GeneratedShape
407 //=======================================================================
408 TopoDS_Shape TNaming_Tool::GeneratedShape(const TopoDS_Shape& S,
409 const Handle(TNaming_NamedShape)& Generation)
412 TopTools_MapOfShape MS;
413 Handle(TNaming_UsedShapes) US;
414 Generation->Label().Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
416 for (TNaming_NewShapeIterator it(S,US); it.More(); it.Next()) {
417 if (!it.Shape().IsNull() && it.NamedShape() == Generation) {
422 TNaming_NewShapeIterator it2(S,US);
423 FindModifUntil (it2,MS,Generation);
425 return MakeShape (MS);
428 //=======================================================================
429 //function : FirstOlds
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)
438 Standard_Integer TransDef;
439 Standard_Boolean YaModif = 0;
441 for (; it.More(); it.Next()) {
442 if (it.IsModification()) {
444 TNaming_OldShapeIterator it2(it);
446 Labels.Append(TNaming_Tool::Label(US,it.Shape(),TransDef));
450 FirstOlds(US,it.Shape(),it2,MS,Labels);
454 if (!YaModif) MS.Add(S);
457 //=======================================================================
458 //function : InitialShape
460 //=======================================================================
462 TopoDS_Shape TNaming_Tool::InitialShape(const TopoDS_Shape& S,
463 const TDF_Label& Acces,
464 TDF_LabelList& Labels)
466 Handle(TNaming_UsedShapes) US;
467 Acces.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
470 if (!TNaming_Tool::HasLabel(US,S)) return Res;
472 Standard_Integer Transdef;
473 Label(US,S,Transdef);
474 TopTools_MapOfShape MS;
475 TNaming_OldShapeIterator it(S,Transdef,US);
480 FirstOlds(US,S,it,MS,Labels);
482 return MakeShape(MS);
487 //=======================================================================
490 //=======================================================================
492 static void Back (const Handle(TNaming_NamedShape)& NS,
493 TNaming_MapOfNamedShape& MNS)
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();
500 Handle(TNaming_NamedShape) NOS = TNaming_Tool::NamedShape(OS,NS->Label());
501 // Continue de remonter
503 if (MNS.Add(NOS)) Back (NOS,MNS);
510 //=======================================================================
513 //=======================================================================
515 void TNaming_Tool::Collect(const Handle(TNaming_NamedShape)& NS,
516 TNaming_MapOfNamedShape& MNS,
517 const Standard_Boolean OnlyModif)
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);
537 //=======================================================================
538 //function : TNamingTool_Label
540 //=======================================================================
541 void TNamingTool_DumpLabel (const TopoDS_Shape& S,
542 const TDF_Label& Acces)
544 Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape(S,Acces);
545 NS->Label().EntryDump(cout);
549 #include <BRepTools.hxx>
550 //=======================================================================
553 //=======================================================================
554 void TNamingTool_Write (const TopoDS_Shape& S,
555 const Standard_CString File)
557 BRepTools::Write (S,File);
564 //=======================================================================
565 //function : FindShape
567 //=======================================================================
569 void TNaming_Tool::FindShape(const TDF_LabelMap& Valid,
570 const TDF_LabelMap& /*Forbiden*/,
571 const Handle(TNaming_NamedShape)& Arg,
574 if (!Valid.IsEmpty() && !Valid.Contains(Arg->Label())) return;
575 if (Arg.IsNull() || Arg->IsEmpty()) return;
577 // Which type of shape is being expected?
578 Handle(TNaming_Naming) aNaming;
579 if (!Arg->FindAttribute(TNaming_Naming::GetID(), aNaming)) {
581 // cout<<"TNaming_Tool::FindShape(): Naming attribute hasn't been found attached at the Argument label"<<endl;
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());
591 // cout<<"TNaming_Tool::FindShape(): Nb of sub shapes = "<<subShapes.Extent()<<endl;
594 // Looking for external arguments:
595 TNaming_ListOfNamedShape extArgs;
596 TDF_AttributeMap outRefs;
597 TDF_Tool::OutReferences(Arg->Label(), outRefs);
598 if (outRefs.IsEmpty()) {
600 // cout<<"TNaming_Tool::FindShape(): No out references have been found"<<endl;
604 for (TDF_MapIteratorOfAttributeMap itr(outRefs); itr.More(); itr.Next()) {
605 if (itr.Key()->DynamicType() == STANDARD_TYPE(TNaming_NamedShape)) {
607 // Standard_Integer nbExtArgs = extArgs.Extent();
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);
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;
622 // The iterator on external arguments:
623 TNaming_ListIteratorOfListOfNamedShape extArgsIterator (extArgs);
624 for(; extArgsIterator.More(); extArgsIterator.Next()) {
625 Handle(TNaming_NamedShape) anExtArg = extArgsIterator.Value();
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;
633 // cout<<"TNaming_Tool::FindShape(): A context shape hasn't been found at the father label of the external argument"<<endl;
639 // cout<<"TNaming_Tool::FindShape(): Searching in the external reference ";
640 // aContextNS->Label().EntryDump(cout); cout<<" ";
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());
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<<".";
662 if (DoesCoincide == subShapes.Extent()) {
664 // cout<<"TNaming_Tool::FindShape(): Found! ";
671 if (!S.IsNull()) break;
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;