1 // Created on: 2000-08-03
2 // Created by: data exchange team
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BRep_Builder.hxx>
19 #include <gp_Trsf.hxx>
20 #include <Standard_GUID.hxx>
21 #include <Standard_Type.hxx>
22 #include <TCollection_AsciiString.hxx>
23 #include <TCollection_ExtendedString.hxx>
24 #include <TCollection_HAsciiString.hxx>
25 #include <TColStd_SequenceOfHAsciiString.hxx>
26 #include <TDataStd_ChildNodeIterator.hxx>
27 #include <TDataStd_Name.hxx>
28 #include <TDataStd_TreeNode.hxx>
29 #include <TDataStd_UAttribute.hxx>
30 #include <TDF_Attribute.hxx>
31 #include <TDF_ChildIDIterator.hxx>
32 #include <TDF_ChildIterator.hxx>
33 #include <TDF_Label.hxx>
34 #include <TDF_LabelMap.hxx>
35 #include <TDF_LabelSequence.hxx>
36 #include <TDF_MapIteratorOfLabelMap.hxx>
37 #include <TDF_RelocationTable.hxx>
38 #include <TDF_Tool.hxx>
39 #include <TDocStd_Document.hxx>
40 #include <TNaming_Builder.hxx>
41 #include <TNaming_NamedShape.hxx>
42 #include <TNaming_Tool.hxx>
43 #include <TopLoc_IndexedMapOfLocation.hxx>
44 #include <TopLoc_Location.hxx>
45 #include <TopoDS_Compound.hxx>
46 #include <TopoDS_Iterator.hxx>
47 #include <TopoDS_Shape.hxx>
48 #include <TopTools_ListOfShape.hxx>
49 #include <XCAFDoc.hxx>
50 #include <XCAFDoc_GraphNode.hxx>
51 #include <XCAFDoc_Location.hxx>
52 #include <XCAFDoc_ShapeMapTool.hxx>
53 #include <XCAFDoc_ShapeTool.hxx>
55 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_ShapeTool,TDF_Attribute)
57 static Standard_Boolean theAutoNaming = Standard_True;
59 // attribute methods //////////////////////////////////////////////////
61 //=======================================================================
64 //=======================================================================
66 const Standard_GUID& XCAFDoc_ShapeTool::GetID()
68 static Standard_GUID ShapeToolID ("efd212ee-6dfd-11d4-b9c8-0060b0ee281b");
73 //=======================================================================
76 //=======================================================================
78 Handle(XCAFDoc_ShapeTool) XCAFDoc_ShapeTool::Set(const TDF_Label& L)
80 Handle(XCAFDoc_ShapeTool) A;
81 if (!L.FindAttribute (XCAFDoc_ShapeTool::GetID(), A)) {
82 A = new XCAFDoc_ShapeTool ();
90 //=======================================================================
91 //function : Constructor
93 //=======================================================================
95 XCAFDoc_ShapeTool::XCAFDoc_ShapeTool()
97 hasSimpleShapes = Standard_False;
101 //=======================================================================
104 //=======================================================================
106 const Standard_GUID& XCAFDoc_ShapeTool::ID() const
111 //=======================================================================
114 //=======================================================================
116 void XCAFDoc_ShapeTool::Restore(const Handle(TDF_Attribute)& /*with*/)
120 //=======================================================================
121 //function : NewEmpty
123 //=======================================================================
125 Handle(TDF_Attribute) XCAFDoc_ShapeTool::NewEmpty() const
127 return new XCAFDoc_ShapeTool;
130 //=======================================================================
133 //=======================================================================
135 void XCAFDoc_ShapeTool::Paste (const Handle(TDF_Attribute)& /*into*/,
136 const Handle(TDF_RelocationTable)& /*RT*/) const
140 // Auxiliary methods //////////////////////////////////////////////////
142 //=======================================================================
143 //function : SetLabelNameByLink
145 //=======================================================================
146 static void SetLabelNameByLink(const TDF_Label L)
148 Handle(TDataStd_TreeNode) Node;
149 if (! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ||
150 ! Node->HasFather()) {
152 cout<<"Error: XCAFDoc_ShapeTool, SetLabelNameByLink(): NO NODE"<<endl;
156 TCollection_AsciiString Entry;
157 TDF_Tool::Entry ( Node->Father()->Label(), Entry );
158 Entry.Insert(1, "=>[");
161 TDataStd_Name::Set(L, TCollection_ExtendedString( Entry ));
165 //=======================================================================
166 //function : SetLabelNameByShape
168 //=======================================================================
169 static void SetLabelNameByShape(const TDF_Label L)
172 if (XCAFDoc_ShapeTool::GetShape(L, S) &&
173 ! L.IsAttribute(TDataStd_Name::GetID()) ) {
174 Standard_SStream Stream;
175 // TopAbs_ShapeEnum Type = S.ShapeType();
176 // if (Type == TopAbs_COMPOUND) Stream<<"ASSEMBLY";
178 TopAbs::Print(S.ShapeType(), Stream);
179 TCollection_AsciiString aName (Stream.str().c_str());
180 TDataStd_Name::Set(L, TCollection_ExtendedString(aName));
185 //=======================================================================
186 //function : SearchUsingMap
188 //=======================================================================
190 Standard_Boolean XCAFDoc_ShapeTool::SearchUsingMap(const TopoDS_Shape &S, TDF_Label &L,
191 const Standard_Boolean findWithoutLoc,
192 const Standard_Boolean findSubShape) const
195 if(myShapeLabels.IsBound(S)) {
196 L = myShapeLabels.Find(S);
197 return Standard_True;
202 if(myShapeLabels.IsBound(S0)) {
203 TDF_Label L1 = myShapeLabels.Find(S0);
204 TDF_LabelSequence Labels;
205 if(GetUsers(L1, Labels, Standard_True)) {
206 for(Standard_Integer i=1; i<=Labels.Length(); i++) {
207 TopoDS_Shape c = GetShape(Labels.Value(i));
210 return Standard_True;
216 return Standard_True;
220 if(hasSimpleShapes) {
221 if(mySimpleShapes.IsBound(S)) {
222 L = mySimpleShapes.Find(S);
223 return Standard_True;
225 if(mySimpleShapes.IsBound(S0)) {
226 L = mySimpleShapes.Find(S0);
227 return Standard_True;
231 if(!findSubShape) return Standard_False;
232 TDF_Label mainL = FindMainShapeUsingMap(S);
233 if(mainL.IsNull()) return Standard_False;
234 L = AddSubShape(mainL,S);
235 return !L.IsNull();//Standard_True;
239 //=======================================================================
242 //=======================================================================
244 Standard_Boolean XCAFDoc_ShapeTool::Search (const TopoDS_Shape &S,
246 const Standard_Boolean findInstance,
247 const Standard_Boolean findComponent,
248 const Standard_Boolean findSubShape) const
250 // search among shapes
251 Standard_Boolean isLocated = ! S.Location().IsIdentity();
254 // try to find top-level instance
255 if ( findInstance && FindShape ( S, L, Standard_True ) )
256 return Standard_True;
257 // try to find component of assembly
258 if ( findComponent ) {
259 TDF_LabelSequence labels;
260 GetShapes ( labels );
261 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
262 if ( ! IsAssembly ( labels.Value(i) ) ) continue;
263 TDF_LabelSequence comp;
264 GetComponents ( labels.Value(i), comp );
265 for ( Standard_Integer j=1; j <= comp.Length(); j++ ) {
266 TopoDS_Shape c = GetShape ( comp.Value(j) );
267 if ( c.IsSame ( S ) ) {
269 return Standard_True;
275 // try to find top-level simple shape
276 if ( FindShape ( S, L, Standard_False ) ) return Standard_True;
279 if ( ! findSubShape ) return Standard_False;
280 TDF_Label mainL = FindMainShape ( S );
281 if ( mainL.IsNull() ) return Standard_False;
282 L = AddSubShape ( mainL, S );
283 return !L.IsNull();//Standard_True;
286 //=======================================================================
287 //function : FindShape
289 //=======================================================================
291 Standard_Boolean XCAFDoc_ShapeTool::FindShape (const TopoDS_Shape& S,
293 const Standard_Boolean findInstance) const
295 // search for null-located shape
297 if ( ! findInstance ) {
302 // this code is used instead of the following for performance reasons
303 if (TNaming_Tool::HasLabel(Label(), S0)) {
305 L = TNaming_Tool::Label(Label(), S0, TransDef);
306 return Standard_True;
309 TDF_ChildIDIterator it(myLabel,TNaming_NamedShape::GetID());
310 for (; it.More(); it.Next()) {
311 TDF_Label aLabel = it.Value()->Label();
312 Handle(TNaming_NamedShape) NS;
313 if ( aLabel.FindAttribute(TNaming_NamedShape::GetID(), NS) &&
314 S0.IsSame ( TNaming_Tool::GetShape(NS) ) ) {
316 return Standard_True;
320 return Standard_False;
323 //=======================================================================
324 //function : FindShape
326 //=======================================================================
328 TDF_Label XCAFDoc_ShapeTool::FindShape (const TopoDS_Shape& S,
329 const Standard_Boolean findInstance) const
332 FindShape ( S, L, findInstance );
336 //=======================================================================
337 //function : GetShape
339 //=======================================================================
341 Standard_Boolean XCAFDoc_ShapeTool::GetShape (const TDF_Label& L, TopoDS_Shape& S)
343 Handle(XCAFDoc_Location) LocationAttribute;
346 TopoDS_Compound EmptyComp;
348 B.MakeCompound(EmptyComp);
352 // for instance, get referred shape
353 Handle(TDataStd_TreeNode) Node;
354 if ( L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) && Node->HasFather() &&
355 L.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute)) {
356 if ( ! GetShape(Node->Father()->Label(), S) ) return Standard_False;
357 S.Move ( LocationAttribute->Get() );
358 return Standard_True;
361 // else just return shape on this label
362 Handle(TNaming_NamedShape) NS;
363 if ( ! L.FindAttribute(TNaming_NamedShape::GetID(), NS) ) return Standard_False;
364 S = TNaming_Tool::GetShape(NS);
365 return Standard_True;
368 //=======================================================================
369 //function : GetShape
371 //=======================================================================
373 TopoDS_Shape XCAFDoc_ShapeTool::GetShape(const TDF_Label& L)
380 //=======================================================================
381 //function : NewShape
383 //=======================================================================
385 TDF_Label XCAFDoc_ShapeTool::NewShape() const
387 TopoDS_Compound aShape;
389 tdsB.MakeCompound ( aShape );
392 TDF_Label aLabel = aTag.NewChild(Label());
394 TNaming_Builder tnBuild(aLabel);
395 tnBuild.Generated(aShape);
400 //=======================================================================
401 //function : SetShape
403 //=======================================================================
405 void XCAFDoc_ShapeTool::SetShape (const TDF_Label& L, const TopoDS_Shape& S)
407 TNaming_Builder tnBuild(L);
408 tnBuild.Generated(S);
409 Handle(XCAFDoc_ShapeMapTool) A = XCAFDoc_ShapeMapTool::Set(L);
410 // if ( ! L.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A) ) {
411 // A = XCAFDoc_ShapeMapTool::Set(L);
412 // L.AddAttribute(A);
416 if(!myShapeLabels.IsBound(S)) {
417 myShapeLabels.Bind(S,L);
421 //=======================================================================
422 //function : MakeReference
424 //=======================================================================
426 void XCAFDoc_ShapeTool::MakeReference (const TDF_Label &L,
427 const TDF_Label &refL,
428 const TopLoc_Location &loc)
431 XCAFDoc_Location::Set(L, loc);
434 Handle(TDataStd_TreeNode) refNode, mainNode;
435 mainNode = TDataStd_TreeNode::Set ( refL, XCAFDoc::ShapeRefGUID() );
436 refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::ShapeRefGUID() );
437 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
438 mainNode->Append(refNode);
441 SetLabelNameByLink(L);
444 //=======================================================================
445 //function : addShape
447 //=======================================================================
449 TDF_Label XCAFDoc_ShapeTool::addShape (const TopoDS_Shape& S, const Standard_Boolean makeAssembly)
451 TDF_Label ShapeLabel;
454 // search if the shape already exists (with the same location)
455 if ( S.IsNull() || FindShape ( S, ShapeLabel, Standard_True ) ) return ShapeLabel;
457 // else add a new label
458 ShapeLabel = aTag.NewChild(Label());
460 // if shape has location, make a reference to the same shape without location
461 if ( ! S.Location().IsIdentity() /*&& FindShape ( S, L )*/ ) {
465 TDF_Label L = addShape ( S0, makeAssembly );
466 MakeReference ( ShapeLabel, L, S.Location() );
470 // else add a shape to a label
471 TNaming_Builder tnBuild(ShapeLabel);
472 tnBuild.Generated(S);
474 Handle(XCAFDoc_ShapeMapTool) A = XCAFDoc_ShapeMapTool::Set(ShapeLabel);
475 // if ( ! ShapeLabel.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A) ) {
476 // A = XCAFDoc_ShapeMapTool::Set(ShapeLabel);
477 // ShapeLabel.AddAttribute(A);
482 SetLabelNameByShape(ShapeLabel);
484 // if shape is Compound and flag is set, create assembly
485 if ( makeAssembly && S.ShapeType() == TopAbs_COMPOUND ) {
486 // mark assembly by assigning UAttribute
487 Handle(TDataStd_UAttribute) Uattr;
488 Uattr = TDataStd_UAttribute::Set ( ShapeLabel, XCAFDoc::AssemblyGUID() );
490 TDataStd_Name::Set(ShapeLabel, TCollection_ExtendedString("ASSEMBLY"));
492 // iterate on components
493 TopoDS_Iterator Iterator(S);
494 for (; Iterator.More(); Iterator.Next()) {
495 // get label for component`s shape
496 TopoDS_Shape Scomp = Iterator.Value(), S0 = Scomp;
499 TDF_Label compL = addShape ( S0, makeAssembly );
501 // add a component as reference
502 TDF_Label RefLabel = aTag.NewChild(ShapeLabel);
503 MakeReference ( RefLabel, compL, Scomp.Location() );
507 if(!IsAssembly(ShapeLabel)) {
508 //const TopTools_IndexedMapOfShape tmpMap = A->GetMap();
509 //for(Standard_Integer i=1; i<=tmpMap.Extent(); i++)
510 //mySubShapes.Bind(tmpMap.FindKey(i),ShapeLabel);
511 for(Standard_Integer i=1; i<=A->GetMap().Extent(); i++)
513 TopoDS_Shape aSh = A->GetMap().FindKey(i);
514 mySubShapes.Bind(aSh,ShapeLabel);
516 //mySubShapes.Bind(ShapeLabel,A->GetMap());
523 //=======================================================================
524 //function : prepareAssembly
526 //=======================================================================
527 static Standard_Boolean prepareAssembly (const TopoDS_Shape& theShape,
528 TopoDS_Shape& theOUTShape)
530 // iterate on components
531 theOUTShape = theShape;
532 if (theShape.ShapeType() == TopAbs_COMPOUND) {
534 // check if shape if frosen
535 if (!theOUTShape.Free())
536 theOUTShape.Free(Standard_True);
538 TopTools_SequenceOfShape aSubShapeSeq;
539 TopoDS_Iterator Iterator(theShape);
540 for (; Iterator.More(); Iterator.Next())
541 aSubShapeSeq.Append(Iterator.Value());
542 for (Standard_Integer i = 1; i <= aSubShapeSeq.Length(); i++) {
543 TopoDS_Shape Scomp = aSubShapeSeq.Value(i);
544 TopoDS_Shape aNewScomp;
545 B.Remove(theOUTShape, Scomp);
546 prepareAssembly( Scomp, aNewScomp );
547 TopLoc_Location aLoc;
548 aLoc = aNewScomp.Location();
549 if ( aLoc.IsIdentity() ) {
550 // create an "empty" location
552 aTrsf.SetScale(gp_Pnt(0,0,0), 1);
553 aLoc = TopLoc_Location( aTrsf );
554 aNewScomp.Location( aLoc );
556 B.Add(theOUTShape, aNewScomp);
559 return Standard_True;
563 //=======================================================================
564 //function : AddShape
566 //=======================================================================
568 TDF_Label XCAFDoc_ShapeTool::AddShape (const TopoDS_Shape& theShape,
569 const Standard_Boolean makeAssembly,
570 const Standard_Boolean makePrepare)
572 // PTV 17.02.2003 to avoid components without location.
573 TopoDS_Shape S = theShape;
574 if ( makePrepare && makeAssembly && S.ShapeType() == TopAbs_COMPOUND )
575 prepareAssembly( theShape, S ); // OCC1669
577 TDF_Label L = addShape(S,makeAssembly);
579 if(!myShapeLabels.IsBound(S)) {
580 myShapeLabels.Bind(S,L);
585 //return addShape( S, makeAssembly );
588 //=======================================================================
589 //function : RemoveShape
591 //=======================================================================
593 Standard_Boolean XCAFDoc_ShapeTool::RemoveShape (const TDF_Label& L,
594 const Standard_Boolean removeCompletely) const
596 if ( ! IsTopLevel ( L ) || ! IsFree ( L ) ) return Standard_False;
598 Handle(TDataStd_TreeNode) aNode;
600 if (removeCompletely &&
601 L.FindAttribute (XCAFDoc::ShapeRefGUID(), aNode) &&
602 aNode->HasFather() &&
603 L.IsAttribute (XCAFDoc_Location::GetID()))
605 aLabel = aNode->Father()->Label();
608 L.ForgetAllAttributes (Standard_True);
610 if (removeCompletely && !aLabel.IsNull())
612 return RemoveShape(aLabel);
614 return Standard_True;
618 //=======================================================================
621 //=======================================================================
623 void XCAFDoc_ShapeTool::Init()
625 hasSimpleShapes = Standard_False;
629 //=======================================================================
630 //function : SetAutoNaming
632 //=======================================================================
634 void XCAFDoc_ShapeTool::SetAutoNaming (const Standard_Boolean V)
640 //=======================================================================
641 //function : AutoNaming
643 //=======================================================================
645 Standard_Boolean XCAFDoc_ShapeTool::AutoNaming()
647 return theAutoNaming;
651 //=======================================================================
652 //function : ComputeShapes
654 //=======================================================================
656 void XCAFDoc_ShapeTool::ComputeShapes(const TDF_Label& L)
658 TDF_ChildIterator it(L);
659 for(; it.More(); it.Next()) {
660 TDF_Label L1 = it.Value();
663 if(!myShapeLabels.IsBound(S)) {
664 mySimpleShapes.Bind(S,L1);
672 //=======================================================================
673 //function : ComputeSimpleShapes
675 //=======================================================================
677 void XCAFDoc_ShapeTool::ComputeSimpleShapes()
679 ComputeShapes(Label());
680 hasSimpleShapes = Standard_True;
684 //=======================================================================
685 //function : GetShapes
687 //=======================================================================
689 void XCAFDoc_ShapeTool::GetShapes(TDF_LabelSequence& Labels) const
693 TDF_ChildIterator it(Label());
694 for (; it.More(); it.Next()) {
695 TDF_Label L = it.Value();
697 if ( GetShape ( L, S ) ) Labels.Append ( L );
702 //=======================================================================
703 //function : GetFreeShapes
705 //=======================================================================
707 void XCAFDoc_ShapeTool::GetFreeShapes (TDF_LabelSequence& FreeLabels) const
711 TDF_ChildIterator it(Label());
712 for (; it.More(); it.Next()) {
713 TDF_Label L = it.Value();
715 if ( GetShape ( L, S ) && IsFree ( L ) ) FreeLabels.Append ( L );
719 //=======================================================================
720 //function : IsTopLevel
722 //=======================================================================
724 Standard_Boolean XCAFDoc_ShapeTool::IsTopLevel (const TDF_Label& L) const
726 return L.Father() == Label();
729 //=======================================================================
732 //=======================================================================
734 Standard_Boolean XCAFDoc_ShapeTool::IsShape (const TDF_Label& L)
736 return IsSimpleShape ( L ) || IsAssembly ( L ) || IsReference ( L );
739 //=======================================================================
740 //function : IsSimpleShape
742 //=======================================================================
744 Standard_Boolean XCAFDoc_ShapeTool::IsSimpleShape (const TDF_Label& L)
746 Handle(TNaming_NamedShape) NS;
747 return L.FindAttribute ( TNaming_NamedShape::GetID(), NS ) &&
748 ! IsAssembly ( L ) && ! IsReference ( L );
751 //=======================================================================
752 //function : IsReference
754 //=======================================================================
756 Standard_Boolean XCAFDoc_ShapeTool::IsReference (const TDF_Label& L)
758 Handle(TDataStd_TreeNode) Node;
759 return L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) && Node->HasFather();
762 //=======================================================================
763 //function : IsAssembly
765 //=======================================================================
767 Standard_Boolean XCAFDoc_ShapeTool::IsAssembly (const TDF_Label& L)
769 Handle(TDataStd_UAttribute) Uattr;
770 return L.FindAttribute(XCAFDoc::AssemblyGUID(), Uattr);
773 //=======================================================================
774 //function : IsComponent
776 //=======================================================================
778 Standard_Boolean XCAFDoc_ShapeTool::IsComponent (const TDF_Label& L)
780 return IsReference ( L ) && IsAssembly ( L.Father() );
783 //=======================================================================
784 //function : IsCompound
786 //=======================================================================
788 Standard_Boolean XCAFDoc_ShapeTool::IsCompound (const TDF_Label& L)
790 Handle(TDataStd_Name) Name;
791 if (L.FindAttribute(TDataStd_Name::GetID(),Name)) {
792 TCollection_ExtendedString estr1 = Name->Get();
793 TCollection_ExtendedString estr2("COMPOUND");
795 return Standard_True;
798 return Standard_False;
801 //=======================================================================
802 //function : IsSubShape
804 //=======================================================================
806 Standard_Boolean XCAFDoc_ShapeTool::IsSubShape (const TDF_Label& L)
808 return IsSimpleShape ( L ) && IsShape ( L.Father() );
811 //=======================================================================
814 //=======================================================================
816 Standard_Boolean XCAFDoc_ShapeTool::IsFree (const TDF_Label& L)
818 Handle(TDataStd_TreeNode) Node;
819 if ( ! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ||
820 ! Node->HasFirst() ) return Standard_True;
822 return Standard_False;
825 //=======================================================================
826 //function : GetUsers
827 //purpose : Returns number of users (0 if shape is free)
828 //=======================================================================
830 Standard_Integer XCAFDoc_ShapeTool::GetUsers (const TDF_Label& L,
831 TDF_LabelSequence& Labels,
832 const Standard_Boolean getsubchilds)
834 Standard_Integer NbUsers=0;
835 Handle(TDataStd_TreeNode) Node ;
837 if ( ! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ) return NbUsers;
839 Node = Node->First();
840 while ( ! Node.IsNull() ) {
842 if ( getsubchilds ) {
843 TDF_Label underL = Node->Label().Father();
844 NbUsers += GetUsers ( underL, Labels, getsubchilds );
847 Labels.Append(Node->Label());
854 //=======================================================================
855 //function : NbComponents
857 //=======================================================================
859 Standard_Integer XCAFDoc_ShapeTool::NbComponents (const TDF_Label& L,
860 const Standard_Boolean getsubchilds)
862 TDF_LabelSequence subLabels;
863 GetComponents (L, subLabels, getsubchilds);
864 return subLabels.Length();
867 //=======================================================================
868 //function : GetComponents
870 //=======================================================================
872 Standard_Boolean XCAFDoc_ShapeTool::GetComponents (const TDF_Label& L, TDF_LabelSequence& Labels,
873 const Standard_Boolean getsubchilds)
875 if ( ! IsAssembly(L) ) return Standard_False;
877 TDF_ChildIterator It(L);
878 for (; It.More(); It.Next() ) {
879 TDF_Label comp = It.Value();
880 if ( IsComponent ( comp ) ) {
881 if ( getsubchilds ) {
883 if ( GetReferredShape ( comp, underL ) )
884 GetComponents ( underL, Labels, getsubchilds);
886 Labels.Append ( comp );
889 return Standard_True;
892 //=======================================================================
893 //function : GetLocation
895 //=======================================================================
897 TopLoc_Location XCAFDoc_ShapeTool::GetLocation (const TDF_Label& L)
899 Handle(XCAFDoc_Location) LocationAttribute;
900 if (L.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute))
901 return LocationAttribute->Get();
903 Handle(TNaming_NamedShape) NS;
905 if ( L.FindAttribute ( TNaming_NamedShape::GetID(), NS ) ) {
906 S = TNaming_Tool::GetShape(NS);
911 //=======================================================================
912 //function : GetReferredShape
914 //=======================================================================
916 Standard_Boolean XCAFDoc_ShapeTool::GetReferredShape (const TDF_Label& L,
919 if ( ! IsReference(L) ) return Standard_False;
921 Handle (TDataStd_TreeNode) Node;
922 L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node);
923 Label = Node->Father()->Label();
924 return Standard_True;
927 //=======================================================================
928 //function : AddComponent
930 //=======================================================================
932 TDF_Label XCAFDoc_ShapeTool::AddComponent (const TDF_Label& assembly,
933 const TDF_Label& compL,
934 const TopLoc_Location &Loc) const
938 // check that shape is assembly
939 if ( ! IsAssembly(assembly) ) {
940 // if it is simple shape, make it assembly
941 if ( IsSimpleShape(assembly) )
942 TDataStd_UAttribute::Set ( assembly, XCAFDoc::AssemblyGUID() );
946 // add a component as reference
948 L = aTag.NewChild(assembly);
949 MakeReference ( L, compL, Loc );
954 //=======================================================================
955 //function : AddComponent
957 //=======================================================================
959 TDF_Label XCAFDoc_ShapeTool::AddComponent (const TDF_Label& assembly,
960 const TopoDS_Shape& comp,
961 const Standard_Boolean expand)
963 // get label for component`s shape
964 TopoDS_Shape S0 = comp;
968 compL = AddShape ( S0, expand );
970 // add component by its label
971 return AddComponent ( assembly, compL, comp.Location() );
974 //=======================================================================
975 //function : RemoveComponent
977 //=======================================================================
979 void XCAFDoc_ShapeTool::RemoveComponent (const TDF_Label& comp) const
981 if ( IsComponent(comp) )
983 comp.ForgetAllAttributes();
987 //=======================================================================
988 //function : UpdateAssemblies
990 //=======================================================================
992 void XCAFDoc_ShapeTool::UpdateAssemblies()
994 // We start from the free shapes (roots in the assembly structure)
995 TDF_LabelSequence aRootLabels;
996 GetFreeShapes(aRootLabels);
998 // Iterate over the free shapes
999 for ( TDF_LabelSequence::Iterator anIt(aRootLabels); anIt.More(); anIt.Next() )
1001 const TDF_Label& aRootLab = anIt.Value();
1003 TopoDS_Shape anAssemblyShape;
1004 updateComponent(aRootLab, anAssemblyShape);
1008 //=======================================================================
1009 //function : IsSubShape
1011 //=======================================================================
1013 //static Standard_Boolean CheckSubShape (const TopoDS_Shape &S, const TopoDS_Shape &sub)
1015 // if ( S.IsSame ( sub ) ) return Standard_True;
1017 // if ( S.ShapeType() >= sub.ShapeType() ) return Standard_False;
1019 // for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
1020 // if ( CheckSubShape ( it.Value(), sub ) ) return Standard_True;
1022 // return Standard_False;
1025 //=======================================================================
1026 //function : IsSubShape
1028 //=======================================================================
1030 Standard_Boolean XCAFDoc_ShapeTool::IsSubShape (const TDF_Label &shapeL,
1031 const TopoDS_Shape &sub) const
1033 Handle(XCAFDoc_ShapeMapTool) A;
1034 if ( ! shapeL.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A) )
1035 return Standard_False;
1037 //TopoDS_Shape S = GetShape ( shapeL );
1038 //return ! S.IsNull() && CheckSubShape ( S, sub );
1040 return A->IsSubShape(sub);
1043 //=======================================================================
1044 //function : FindSubShape
1046 //=======================================================================
1048 Standard_Boolean XCAFDoc_ShapeTool::FindSubShape (const TDF_Label &shapeL,
1049 const TopoDS_Shape &sub,
1052 // this code is used instead of the following for performance reasons
1053 if ( TNaming_Tool::HasLabel(Label(), sub) ) {
1055 L = TNaming_Tool::Label(Label(), sub, TransDef);
1056 return ( ! L.IsNull() && L.Father() == shapeL );
1060 TDF_ChildIterator chldLabIt(shapeL);
1061 for (; chldLabIt.More(); chldLabIt.Next() ) {
1062 TDF_Label subLabel = chldLabIt.Value();
1064 if ( GetShape ( subLabel, S ) && S.IsSame ( sub ) ) {
1066 return Standard_True;
1070 return Standard_False;
1073 //=======================================================================
1074 //function : AddSubShape
1076 //=======================================================================
1078 TDF_Label XCAFDoc_ShapeTool::AddSubShape (const TDF_Label &shapeL,
1079 const TopoDS_Shape &sub) const
1082 if ( FindSubShape ( shapeL, sub, L ) ) return L;
1084 if ( ! IsSubShape ( shapeL, sub ) ) return L;
1087 L = aTag.NewChild(shapeL);
1089 TNaming_Builder tnBuild(L);
1090 tnBuild.Generated(sub);
1092 // if(!mySubShapes.IsBound(sub))
1093 // mySubShapes.Bind(sub,L);
1099 //=======================================================================
1100 //function : FindMainShapeUsingMap
1102 //=======================================================================
1104 TDF_Label XCAFDoc_ShapeTool::FindMainShapeUsingMap(const TopoDS_Shape &sub) const
1106 //for(Standard_Integer i=1; i<=myNotAssemblies.Length(); i++) {
1107 // TDF_Label L = myNotAssemblies.Value(i);
1108 // if(IsSubShape(L,sub)) return L;
1110 if(mySubShapes.IsBound(sub))
1111 return mySubShapes.Find(sub);
1117 //=======================================================================
1118 //function : FindMainShape
1120 //=======================================================================
1122 TDF_Label XCAFDoc_ShapeTool::FindMainShape (const TopoDS_Shape &sub) const
1124 TDF_ChildIterator it(Label());
1125 for (; it.More(); it.Next()) {
1126 TDF_Label L = it.Value();
1127 if ( ! IsAssembly ( L ) && IsSubShape ( L, sub ) ) return L;
1134 //=======================================================================
1135 //function : GetSubShapes
1137 //=======================================================================
1139 Standard_Boolean XCAFDoc_ShapeTool::GetSubShapes (const TDF_Label &L,
1140 TDF_LabelSequence& Labels)
1142 TDF_ChildIterator It(L);
1143 for (; It.More(); It.Next() ) {
1144 TDF_Label sub = It.Value();
1145 if ( IsSubShape ( sub ) ) Labels.Append ( sub );
1147 return Labels.Length() >0;
1150 //=======================================================================
1151 //function : BaseLabel
1153 //=======================================================================
1155 TDF_Label XCAFDoc_ShapeTool::BaseLabel () const
1160 //=======================================================================
1161 //function : DumpAssembly
1162 //purpose : recursive part of Dump()
1163 //=======================================================================
1165 static void DumpAssembly(Standard_OStream& theDumpLog,
1167 const Standard_Integer level,
1168 const Standard_Boolean deep)
1171 XCAFDoc_ShapeTool::GetShape(L, S);
1174 for (Standard_Integer i=0; i<level; i++)
1177 TCollection_AsciiString Entry;
1178 TDF_Tool::Entry(L, Entry);
1180 if(XCAFDoc_ShapeTool::IsAssembly(L))
1182 theDumpLog<<"ASSEMBLY ";
1184 else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
1186 if(L.Father().Father().Father().IsRoot())
1187 theDumpLog<<"PART ";
1191 theDumpLog<<"INSTANCE ";
1193 TopAbs::Print(S.ShapeType(), theDumpLog);
1195 theDumpLog<<" "<<Entry;
1196 if(XCAFDoc_ShapeTool::IsReference(L))
1198 Handle(TDataStd_TreeNode) aRef;
1199 L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
1200 TDF_Tool::Entry(aRef->Father()->Label(), Entry);
1201 theDumpLog<<" (refers to "<<Entry<<")";
1203 Handle(TDataStd_Name) Name;
1204 if (L.FindAttribute(TDataStd_Name::GetID(), Name))
1205 theDumpLog<<" \""<<Name->Get()<<"\" ";
1208 theDumpLog<<"("<<*(void**)&S.TShape();
1209 if (! S.Location().IsIdentity())
1210 theDumpLog<<", "<< *(void**)&S.Location();
1215 Handle(TDataStd_TreeNode) Node;
1216 TDF_ChildIterator NodeIterator(L);
1217 for (; NodeIterator.More(); NodeIterator.Next()) {
1218 DumpAssembly(theDumpLog, NodeIterator.Value(), level+1, deep);
1224 //=======================================================================
1227 //=======================================================================
1229 Standard_OStream& XCAFDoc_ShapeTool::Dump(Standard_OStream& theDumpLog, const Standard_Boolean deep) const
1231 Standard_Integer level = 0;
1232 // TopTools_SequenceOfShape SeqShapes;
1233 TDF_LabelSequence SeqLabels;
1234 GetShapes( SeqLabels);
1236 if (SeqLabels.Length()>0) theDumpLog<<endl;
1238 for (i=1; i<=SeqLabels.Length(); i++) {
1239 DumpAssembly(theDumpLog, SeqLabels.Value(i), level, deep);
1243 GetFreeShapes(SeqLabels);
1244 theDumpLog<<endl<<"Free Shapes: "<<SeqLabels.Length()<<endl;
1245 for (i = 1; i<=SeqLabels.Length(); i++) {
1246 DumpShape(theDumpLog, SeqLabels.Value(i), level, deep);
1252 //=======================================================================
1254 //purpose : override
1255 //=======================================================================
1257 Standard_OStream& XCAFDoc_ShapeTool::Dump(Standard_OStream& theDumpLog) const
1259 TDF_Attribute::Dump (theDumpLog);
1260 Dump (theDumpLog, Standard_False);
1264 //=======================================================================
1265 //function : DumpShape
1267 //=======================================================================
1269 void XCAFDoc_ShapeTool::DumpShape(Standard_OStream& theDumpLog, const TDF_Label& L,const Standard_Integer level,const Standard_Boolean deep)
1272 if(! XCAFDoc_ShapeTool::GetShape(L, S) ) return;
1273 for (Standard_Integer i=0; i<level; i++)
1276 if(XCAFDoc_ShapeTool::IsAssembly(L))
1278 theDumpLog<<"ASSEMBLY ";
1280 else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
1282 if(L.Father().Father().Father().IsRoot())
1283 theDumpLog<<"PART ";
1287 theDumpLog<<"INSTANCE ";
1289 TopAbs::Print(S.ShapeType(), theDumpLog);
1291 TCollection_AsciiString Entry;
1292 TDF_Tool::Entry(L, Entry);
1293 theDumpLog<<" "<<Entry;
1294 if(XCAFDoc_ShapeTool::IsReference(L))
1296 Handle(TDataStd_TreeNode) aRef;
1297 L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
1298 TDF_Tool::Entry(aRef->Father()->Label(), Entry);
1299 theDumpLog<<" (refers to "<<Entry<<")";
1302 Handle(TDataStd_Name) Name;
1303 if (L.FindAttribute(TDataStd_Name::GetID(),Name))
1304 theDumpLog<<" \""<<Name->Get()<<"\" ";
1307 theDumpLog<<"("<<*(void**)&S.TShape();
1308 if (! S.Location().IsIdentity())
1309 theDumpLog<<", "<< *(void**)&S.Location();
1314 //=======================================================================
1315 //function : IsExternRef
1317 //=======================================================================
1319 Standard_Boolean XCAFDoc_ShapeTool::IsExternRef(const TDF_Label& L)
1321 Handle(TDataStd_UAttribute) Uattr;
1322 return L.FindAttribute(XCAFDoc::ExternRefGUID(), Uattr);
1325 //=======================================================================
1326 //function : SetExternRefs
1328 //=======================================================================
1330 void XCAFDoc_ShapeTool::SetExternRefs(const TDF_Label& L,
1331 const TColStd_SequenceOfHAsciiString& SHAS) const
1333 TDF_Label ShapeLabel = L.NewChild();
1334 TDataStd_UAttribute::Set(ShapeLabel,XCAFDoc::ExternRefGUID());
1335 for(Standard_Integer i=1; i<=SHAS.Length(); i++) {
1336 TDF_Label tmplbl = ShapeLabel.FindChild(i,Standard_True);
1337 Handle(TCollection_HAsciiString) str = SHAS(i);
1338 TCollection_ExtendedString extstr(str->String());
1339 TDataStd_Name::Set(tmplbl,extstr);
1343 //=======================================================================
1344 //function : SetExternRefs
1346 //=======================================================================
1348 TDF_Label XCAFDoc_ShapeTool::SetExternRefs(const TColStd_SequenceOfHAsciiString& SHAS) const
1350 TDF_Label ShapeLabel;
1353 ShapeLabel = aTag.NewChild(Label());
1354 TDataStd_UAttribute::Set(ShapeLabel,XCAFDoc::ExternRefGUID());
1355 for(Standard_Integer i=1; i<=SHAS.Length(); i++) {
1356 TDF_Label tmplbl = ShapeLabel.FindChild(i,Standard_True);
1357 Handle(TCollection_HAsciiString) str = SHAS(i);
1358 TCollection_ExtendedString extstr(str->String());
1359 TDataStd_Name::Set(tmplbl,extstr);
1364 //=======================================================================
1365 //function : GetExternRefs
1367 //=======================================================================
1369 void XCAFDoc_ShapeTool::GetExternRefs(const TDF_Label& L,
1370 TColStd_SequenceOfHAsciiString& SHAS)
1372 Handle(TDataStd_Name) TDN;
1374 for(Standard_Integer i=1; i<=L.NbChildren(); i++) {
1375 tmplbl = L.FindChild(i);
1376 if(tmplbl.FindAttribute(TDataStd_Name::GetID(),TDN)) {
1377 TCollection_ExtendedString extstr = TDN->Get();
1378 Handle(TCollection_HAsciiString) str =
1379 new TCollection_HAsciiString(TCollection_AsciiString(extstr, '?'));
1385 // API: API work with SHUO (Specified Higher Usage Occurrance) structure
1387 //=======================================================================
1388 //function : GetSHUO
1390 //=======================================================================
1392 Standard_Boolean XCAFDoc_ShapeTool::GetSHUO (const TDF_Label& SHUOLabel,
1393 Handle(XCAFDoc_GraphNode)& aSHUOAttr)
1395 if ( !SHUOLabel.FindAttribute( XCAFDoc::SHUORefGUID(), aSHUOAttr ) )
1396 return Standard_False;
1397 return Standard_True;
1400 //=======================================================================
1401 //function : GetAllComponentSHUO
1403 //=======================================================================
1405 Standard_Boolean XCAFDoc_ShapeTool::GetAllComponentSHUO (const TDF_Label& theCompLabel,
1406 TDF_AttributeSequence& theSHUOAttrs)
1408 TDF_ChildIterator it(theCompLabel);
1409 for (; it.More(); it.Next()) {
1410 TDF_Label L = it.Value();
1411 Handle(XCAFDoc_GraphNode) aSHUOAttr;
1412 if ( GetSHUO( L, aSHUOAttr ) )
1413 theSHUOAttrs.Append( aSHUOAttr );
1415 return (theSHUOAttrs.Length() > 0);
1418 //=======================================================================
1419 //function : SetSHUO
1421 //=======================================================================
1423 Standard_Boolean XCAFDoc_ShapeTool::SetSHUO (const TDF_LabelSequence& labels,
1424 Handle(XCAFDoc_GraphNode)& MainSHUOAttr) const
1426 MainSHUOAttr.Nullify();
1427 // check number of labels
1428 if (labels.Length() < 2)
1429 return Standard_False;
1430 // check is all labels contains components of any assemblyies
1432 for (i = 1; i <= labels.Length(); i++)
1433 if ( !IsComponent(labels.Value(i)) )
1434 return Standard_False;
1437 TDF_Label UpperSubL = aTag.NewChild( labels( 1 ) );
1438 if (theAutoNaming) {
1439 TCollection_ExtendedString Entry("SHUO");
1440 TDataStd_Name::Set(UpperSubL, TCollection_ExtendedString( Entry ));
1442 Handle(XCAFDoc_GraphNode) aUpperSHUO;
1443 aUpperSHUO = XCAFDoc_GraphNode::Set( UpperSubL, XCAFDoc::SHUORefGUID() );
1444 // init out argument by main upper usage SHUO
1445 MainSHUOAttr = aUpperSHUO;
1446 // add other next_usage occurrences.
1447 for (i = 2; i <= labels.Length(); i++) {
1448 TDF_Label NextSubL = aTag.NewChild( labels( i ) );
1449 if (theAutoNaming) {
1450 TCollection_ExtendedString EntrySub("SHUO-");
1452 TDataStd_Name::Set(NextSubL, TCollection_ExtendedString( EntrySub ));
1454 Handle(XCAFDoc_GraphNode) aNextSHUO;
1455 aNextSHUO = XCAFDoc_GraphNode::Set( NextSubL, XCAFDoc::SHUORefGUID() );
1457 aUpperSHUO->SetChild( aNextSHUO );
1458 aNextSHUO->SetFather( aUpperSHUO );
1459 // now lets next_usage become upper_usage for next level of SHUO
1460 aUpperSHUO = aNextSHUO;
1461 UpperSubL = NextSubL;
1464 return Standard_True;
1467 //=======================================================================
1468 //function : GetSHUOUpperUsage
1470 //=======================================================================
1472 Standard_Boolean XCAFDoc_ShapeTool::GetSHUOUpperUsage (const TDF_Label& NextUsageL,
1473 TDF_LabelSequence& aLabels)
1475 Handle(XCAFDoc_GraphNode) aNextSHUO;
1476 if( !GetSHUO( NextUsageL, aNextSHUO ) || aNextSHUO->NbFathers()<1 )
1477 return Standard_False;
1479 // get upper_usage SHAO
1480 for (Standard_Integer i = 1; i <= aNextSHUO->NbFathers(); i++)
1481 aLabels.Append( aNextSHUO->GetFather(i)->Label() );
1482 return Standard_True;
1485 //=======================================================================
1486 //function : GetSHUONextUsage
1488 //=======================================================================
1490 Standard_Boolean XCAFDoc_ShapeTool::GetSHUONextUsage (const TDF_Label& UpperUsageL,
1491 TDF_LabelSequence& aLabels)
1493 Handle(XCAFDoc_GraphNode) aUpperSHUO;
1494 if ( !GetSHUO( UpperUsageL, aUpperSHUO ) || aUpperSHUO->NbChildren()<1 )
1495 return Standard_False;
1496 // get upper_usage SHAO
1497 for (Standard_Integer i = 1; i <= aUpperSHUO->NbChildren(); i++)
1498 aLabels.Append( aUpperSHUO->GetChild(i)->Label() );
1499 return Standard_True;
1502 //=======================================================================
1503 //function : RemoveSHUO
1505 //=======================================================================
1507 Standard_Boolean XCAFDoc_ShapeTool::RemoveSHUO (const TDF_Label& L) const
1509 L.ForgetAllAttributes (Standard_True);
1510 return Standard_True;
1513 //=======================================================================
1514 //function : checkForShape
1515 //purpose : auxilary
1516 //=======================================================================
1518 static Standard_Boolean checkForShape (const TopoDS_Shape& theShape,
1519 const TopoDS_Shape& theCurSh,
1520 const TDF_Label& theUserL,
1521 TDF_LabelSequence& theLabels)
1523 // the label of an assembly which contains this component
1524 TDF_Label aSuperUserL = theUserL.Father();
1525 TopLoc_Location aSupLoc, aCompLoc;
1526 aSupLoc = ::XCAFDoc_ShapeTool::GetLocation ( aSuperUserL );
1527 aCompLoc = ::XCAFDoc_ShapeTool::GetLocation ( theUserL );
1528 TopoDS_Shape aCopySh = theCurSh;
1529 aCompLoc = aCompLoc.Multiplied( theCurSh.Location() );
1530 aSupLoc = aSupLoc.Multiplied( aCompLoc );
1531 aCopySh.Location( aSupLoc );
1532 if ( aCopySh.IsSame( theShape ) ) {
1533 theLabels.Prepend( theUserL );
1534 return Standard_True;
1536 // try to search deeply (upper by assmebly structure)
1537 TDF_LabelSequence aNewLabels;
1538 for (Standard_Integer j = 1; j <= theLabels.Length(); j++)
1539 aNewLabels.Append( theLabels.Value( j ) );
1540 aNewLabels.Prepend( theUserL );
1541 TDF_LabelSequence aUsers;
1542 ::XCAFDoc_ShapeTool::GetUsers( aSuperUserL, aUsers );
1543 for (Standard_Integer i = 1; i <= aUsers.Length(); i++)
1544 if ( checkForShape( theShape, aCopySh, aUsers.Value( i ), aNewLabels ) ) {
1546 theLabels = aNewLabels;
1547 return Standard_True;
1549 return Standard_False;
1552 //=======================================================================
1553 //function : FindComponent
1555 //=======================================================================
1557 Standard_Boolean XCAFDoc_ShapeTool::FindComponent (const TopoDS_Shape& theShape,
1558 TDF_LabelSequence& theLabels) const
1561 // search for a top-level shape that corresponds to this component
1562 TopoDS_Shape S0 = theShape;
1563 TopLoc_Location loc;
1564 S0.Location ( loc );
1565 TDF_Label aRefL = FindShape( S0 );
1567 return Standard_False; // cannot find top-level shape.
1569 TDF_LabelSequence aUsers;
1570 ::XCAFDoc_ShapeTool::GetUsers( aRefL, aUsers );
1571 for (Standard_Integer i = 1; i <= aUsers.Length(); i++)
1572 if ( checkForShape( theShape, S0, aUsers.Value( i ), theLabels ) )
1575 return (theLabels.Length() > 0);
1578 //=======================================================================
1579 //function : getShapesOfSHUO
1580 //purpose : auxilary
1581 //=======================================================================
1583 static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
1584 const Handle(XCAFDoc_ShapeTool)& theSTool,
1585 const TDF_Label& theSHUOlab,
1586 TopoDS_Shape& theShape)
1588 Handle(XCAFDoc_GraphNode) SHUO;
1589 TDF_LabelSequence aLabSeq;
1590 theSTool->GetSHUONextUsage( theSHUOlab, aLabSeq );
1591 if (aLabSeq.Length() >= 1)
1592 for (Standard_Integer i = 1; i <= aLabSeq.Length(); i++) {
1593 TDF_Label aSubCompL = aLabSeq.Value( i );
1594 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSubCompL.Father() );
1595 // create new map of laocation (to not merge locations from different shapes)
1596 TopLoc_IndexedMapOfLocation aNewPrevLocMap;
1597 for (Standard_Integer m = 1; m <= theaPrevLocMap.Extent(); m++)
1598 aNewPrevLocMap.Add( theaPrevLocMap.FindKey( m ) );
1599 aNewPrevLocMap.Add( compLoc );
1600 // got for the new sublocations and corresponding shape
1601 getShapesOfSHUO( aNewPrevLocMap, theSTool, aSubCompL, theShape );
1604 TopoDS_Shape aSHUO_NUSh = theSTool->GetShape ( theSHUOlab.Father() );
1605 if ( aSHUO_NUSh.IsNull() ) return Standard_False;
1606 // cause got shape with location already.
1607 TopLoc_Location nullLoc;
1608 aSHUO_NUSh.Location ( nullLoc );
1609 // multiply the locations
1610 Standard_Integer intMapLenght = theaPrevLocMap.Extent();
1611 if ( intMapLenght < 1 )
1612 return Standard_False; // should not be, but to avoid exception...?
1613 TopLoc_Location SupcompLoc;
1614 SupcompLoc = theaPrevLocMap.FindKey( intMapLenght );
1615 if (intMapLenght > 1) {
1616 Standard_Integer l = intMapLenght - 1;
1618 SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
1622 aSHUO_NUSh.Location( SupcompLoc );
1623 theShape = aSHUO_NUSh;
1625 return (!theShape.IsNull());
1628 //=======================================================================
1629 //function : GetSHUOInstance
1631 //=======================================================================
1633 TopoDS_Shape XCAFDoc_ShapeTool::GetSHUOInstance (const Handle(XCAFDoc_GraphNode)& theSHUO) const
1635 TopoDS_Shape aShape;
1636 if (theSHUO.IsNull())
1639 TDF_Label aSHUOlab = theSHUO->Label();
1640 // get location of the assembly
1641 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father().Father() );
1642 // get location of the component
1643 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father() );
1644 TopLoc_IndexedMapOfLocation aPrevLocMap;
1645 // get previous setted location
1646 if ( !loc.IsIdentity() )
1647 aPrevLocMap.Add( loc );
1648 aPrevLocMap.Add( compLoc );
1649 // get shape by recurse method
1650 const Handle(XCAFDoc_ShapeTool)& STool = this;
1651 getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aShape );
1656 //=======================================================================
1657 //function : getUsersShapesOfSHUO
1658 //purpose : auxilary
1659 //=======================================================================
1661 static Standard_Boolean getUsersShapesOfSHUO (TopLoc_IndexedMapOfLocation& aPrevLocMap,
1662 const Handle(XCAFDoc_ShapeTool)& STool,
1663 const TDF_Label& aSHUOlab,
1664 const TDF_Label& theUserL,
1665 TopTools_SequenceOfShape& theSHUOShapeSeq)
1667 TopLoc_IndexedMapOfLocation aNewPrevLocMap;
1668 // get location of the assembly
1669 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( theUserL.Father() );
1670 // get location of the component
1671 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( theUserL );
1672 // get previous setted location
1673 aNewPrevLocMap.Add( loc );
1674 aNewPrevLocMap.Add( compLoc );
1676 for (i = 1; i <= aPrevLocMap.Extent(); i++)
1677 aNewPrevLocMap.Add( aPrevLocMap.FindKey(i) );
1678 TDF_Label L = theUserL.Father();
1679 TDF_LabelSequence usersLab;
1680 ::XCAFDoc_ShapeTool::GetUsers( L, usersLab );
1681 if (usersLab.Length() == 0) {
1682 TopoDS_Shape aShape;
1683 getShapesOfSHUO( aNewPrevLocMap, STool, aSHUOlab, aShape );
1684 if (!aShape.IsNull()) {
1685 theSHUOShapeSeq.Append(aShape);
1686 return Standard_True;
1689 // now iterates on users of this assembly as component
1690 for ( i = 1; i <= usersLab.Length(); i++ ) {
1691 TDF_Label aNewUserL = usersLab.Value(i);
1692 getUsersShapesOfSHUO( aNewPrevLocMap, STool, aSHUOlab, aNewUserL, theSHUOShapeSeq );
1695 return (theSHUOShapeSeq.Length() > 1);
1698 //=======================================================================
1699 //function : GetAllSHUOInstances
1700 //purpose : auxilary
1701 //=======================================================================
1703 Standard_Boolean XCAFDoc_ShapeTool::GetAllSHUOInstances (const Handle(XCAFDoc_GraphNode)& theSHUO,
1704 TopTools_SequenceOfShape& theSHUOShapeSeq) const
1706 if (theSHUO.IsNull())
1707 return Standard_False;
1709 TDF_Label aSHUOlab = theSHUO->Label();
1710 TopLoc_IndexedMapOfLocation aPrevLocMap;
1711 // get location of the assembly
1712 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father().Father() );
1713 // get location of the component
1714 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father() );
1715 // get previous setted location
1716 if ( !loc.IsIdentity() )
1717 aPrevLocMap.Add( loc );
1718 aPrevLocMap.Add( compLoc );
1719 // get label of assembly
1720 TDF_Label L = aSHUOlab.Father().Father();
1721 TDF_LabelSequence usersLab;
1722 ::XCAFDoc_ShapeTool::GetUsers( L, usersLab );
1723 TopoDS_Shape aShape;
1724 const Handle(XCAFDoc_ShapeTool)& STool = this;
1725 if (usersLab.Length() == 0) {
1726 getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aShape );
1727 if (!aShape.IsNull()) {
1728 theSHUOShapeSeq.Append(aShape);
1729 return Standard_True;
1732 // now iterates on users of this assembly as component
1733 for (Standard_Integer i = 1; i <= usersLab.Length(); i++) {
1734 TDF_Label aUserL = usersLab.Value(i);
1735 getUsersShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aUserL, theSHUOShapeSeq );
1738 return (theSHUOShapeSeq.Length() > 1);
1741 //=======================================================================
1742 //function : SetInstanceSHUO
1744 //=======================================================================
1746 Handle(XCAFDoc_GraphNode) XCAFDoc_ShapeTool::SetInstanceSHUO (const TopoDS_Shape& theShape) const
1748 Handle(XCAFDoc_GraphNode) SHUO;
1749 TDF_LabelSequence aLabels;
1750 if ( FindComponent( theShape, aLabels ) )
1751 // set shuo structure on labels of component-assembly structure
1752 SetSHUO( aLabels, SHUO );
1756 //=======================================================================
1757 //function : FindSHUO
1759 //=======================================================================
1761 Standard_Boolean XCAFDoc_ShapeTool::FindSHUO (const TDF_LabelSequence& theLabels,
1762 Handle(XCAFDoc_GraphNode)& theSHUOAttr)
1764 TDF_AttributeSequence SHUOAttrs;
1765 TDF_Label aCompLabel = theLabels.Value(1);
1766 if (! ::XCAFDoc_ShapeTool::GetAllComponentSHUO( aCompLabel, SHUOAttrs ) )
1767 return Standard_False;
1768 // WARNING: manage that each SHUO upper_usage have only one SHUO next_usage
1769 for (Standard_Integer i = 1; i <= SHUOAttrs.Length(); i++) {
1770 TDF_LabelSequence aCondidate;
1771 Handle(XCAFDoc_GraphNode) anSHUO = Handle(XCAFDoc_GraphNode)::DownCast(SHUOAttrs.Value(i));
1772 aCondidate.Append( anSHUO->Label().Father() );
1773 while (anSHUO->NbChildren()) {
1774 anSHUO = anSHUO->GetChild( 1 );
1775 aCondidate.Append( anSHUO->Label().Father() );
1777 // check the label sequences
1778 Standard_Boolean isEqual = Standard_True;
1779 if (theLabels.Length() != aCondidate.Length())
1780 isEqual = Standard_False;
1782 for (Standard_Integer li = 1; li <= theLabels.Length(); li++)
1783 if ( theLabels.Value(li) != aCondidate.Value(li) ) {
1784 isEqual = Standard_False;
1790 theSHUOAttr = Handle(XCAFDoc_GraphNode)::DownCast(SHUOAttrs.Value(i));
1793 return ( !theSHUOAttr.IsNull() );
1796 //=======================================================================
1799 //=======================================================================
1801 Standard_Boolean XCAFDoc_ShapeTool::Expand (const TDF_Label& Shape)
1804 return Standard_False;
1806 TopoDS_Shape aShape = GetShape(Shape);
1807 if(!aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND && !IsAssembly(Shape))
1809 //set assembly attribute
1810 TDataStd_UAttribute::Set ( Shape, XCAFDoc::AssemblyGUID() );
1812 TopoDS_Iterator anIter(aShape);
1813 for(; anIter.More(); anIter.Next())
1815 TopoDS_Shape aChildShape = anIter.Value();
1816 TDF_Label aChild = FindShape(aChildShape, Standard_True);
1819 Handle(TDataStd_Name) anAttr;
1820 //make part for child
1821 TDF_Label aPart = aTag.NewChild(Label());
1822 //make child (if color isn't set or if it is compound)
1825 TopLoc_Location nulloc;
1826 aChild = aTag.NewChild(Shape);
1827 SetShape(aChild, aChildShape);
1828 SetShape(aPart, aChildShape.Located(nulloc));
1833 aChild.FindAttribute(TDataStd_Name::GetID(), anAttr);
1834 TopLoc_Location nulloc;
1835 SetShape(aPart, aChildShape.Located(nulloc));
1839 if(!anAttr.IsNull())
1841 TDataStd_Name::Set(aPart, anAttr->Get());
1845 Standard_SStream Stream;
1846 TopAbs::Print(aChildShape.ShapeType(), Stream);
1847 TCollection_AsciiString aName (Stream.str().c_str());
1848 TDataStd_Name::Set(aPart, TCollection_ExtendedString(aName));
1851 MakeReference(aChild, aPart, aChildShape.Location());
1853 makeSubShape(aPart, aChildShape);
1855 return Standard_True;
1857 return Standard_False;
1860 //=======================================================================
1861 //function : makeSubShape
1863 //=======================================================================
1865 void XCAFDoc_ShapeTool::makeSubShape (const TDF_Label& Part, const TopoDS_Shape& Shape)
1868 TopoDS_Iterator anIter(Shape);
1869 for(; anIter.More(); anIter.Next())
1871 TopoDS_Shape aChildShape = anIter.Value();
1872 TDF_Label aChildLabel = FindShape(aChildShape,Standard_True);
1873 if(!aChildLabel.IsNull())
1876 Handle(TDataStd_Name) anAttr;
1877 aChildLabel.FindAttribute(TDataStd_Name::GetID(), anAttr);
1878 TopLoc_Location nulloc;
1880 TDF_Label aSubLabel = aTag.NewChild(Part);
1881 SetShape(aSubLabel, aChildShape.Located(nulloc));
1882 //set name to sub shape
1883 if(!anAttr.IsNull())
1885 TDataStd_Name::Set(aSubLabel, anAttr->Get());
1889 Standard_SStream Stream;
1890 TopAbs::Print(aChildShape.ShapeType(), Stream);
1891 TCollection_AsciiString aName (Stream.str().c_str());
1892 TDataStd_Name::Set(aSubLabel, TCollection_ExtendedString(aName));
1894 MakeReference(aChildLabel, aSubLabel, aChildShape.Location());
1896 makeSubShape(Part, aChildShape);
1900 //=======================================================================
1901 //function : updateComponent
1903 //=======================================================================
1905 Standard_Boolean XCAFDoc_ShapeTool::updateComponent(const TDF_Label& theItemLabel,
1906 TopoDS_Shape& theUpdatedShape) const
1908 if ( !IsAssembly(theItemLabel) )
1909 return Standard_False; // Do nothing for non-assemblies
1911 // Get the currently stored compound for the assembly
1912 TopoDS_Shape aCurrentRootShape;
1913 GetShape(theItemLabel, aCurrentRootShape);
1915 // Get components of the assembly
1916 TDF_LabelSequence aComponentLabs;
1917 GetComponents(theItemLabel, aComponentLabs);
1919 // This flag indicates whether to update the compound of the assembly
1920 Standard_Boolean isModified = Standard_False;
1922 // Compare the number of components in XDE structure with the number of
1923 // components in topological structure. A component may happen to be removed,
1924 // so we have to update the assembly compound
1925 Standard_Integer aNumTopoComponents = 0;
1926 for ( TopoDS_Iterator aTopIt(aCurrentRootShape); aTopIt.More(); aTopIt.Next() )
1927 aNumTopoComponents++;
1929 if ( aNumTopoComponents != aComponentLabs.Length() )
1930 isModified = Standard_True;
1932 // Iterate over the assembly components. If at least one component is
1933 // modified (this is the recursive check), then the actually stored
1934 // compound has to be updated
1935 TopTools_ListOfShape aComponentShapes;
1937 for ( TDF_LabelSequence::Iterator aCompIt(aComponentLabs); aCompIt.More(); aCompIt.Next() )
1939 const TDF_Label& aComponentLab = aCompIt.Value();
1941 // Take the referred assembly item (ultimately, a part for an instance)
1942 TDF_Label aComponentRefLab;
1943 GetReferredShape(aComponentLab, aComponentRefLab);
1945 // Shape comes with some placement transformation here
1946 TopoDS_Shape aComponentShape;
1947 GetShape(aComponentLab, aComponentShape);
1948 TopLoc_Location aComponentLoc = aComponentShape.Location();
1950 // If the component is a sub-assembly, then its associated compound
1951 // has to be processed in the same manner
1952 if ( IsAssembly(aComponentRefLab) )
1955 if ( updateComponent(aComponentRefLab, aComponentShape) )
1958 isModified = Standard_True;
1960 aComponentShape.Location(aComponentLoc); // Apply placement
1965 // Search for a part in the actual compound of the ultimate assembly.
1966 // If the part is there, then the compound is up-to-date, so it does
1967 // not require rebuilding
1968 Standard_Boolean isPartFound = Standard_False;
1969 for ( TopoDS_Iterator aTopoIt(aCurrentRootShape); aTopoIt.More(); aTopoIt.Next() )
1971 if ( aTopoIt.Value() == aComponentShape )
1973 isPartFound = Standard_True;
1978 if ( !isPartFound && !isModified )
1979 isModified = Standard_True; // Part has been modified somewhere, so the compound
1980 // has to be rebuilt
1983 // Fill the list of shapes composing a new compound for the assembly
1984 aComponentShapes.Append(aComponentShape);
1987 // If any component is modified, we update the currently stored shape
1990 TopoDS_Compound anUpdatedCompound;
1992 aBB.MakeCompound(anUpdatedCompound);
1994 // Compose new compound
1995 for ( TopTools_ListIteratorOfListOfShape aShapeIt(aComponentShapes); aShapeIt.More(); aShapeIt.Next() )
1997 aBB.Add( anUpdatedCompound, aShapeIt.Value() );
2000 // Store the updated shape as an output
2001 theUpdatedShape = anUpdatedCompound;
2003 // Use topological naming services to store the updated shape in XDE
2004 TNaming_Builder NB(theItemLabel);
2005 NB.Generated(theUpdatedShape);