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 <TopTools_MapOfOrientedShape.hxx>
50 #include <XCAFDoc.hxx>
51 #include <XCAFDoc_GraphNode.hxx>
52 #include <XCAFDoc_Location.hxx>
53 #include <XCAFDoc_ShapeMapTool.hxx>
54 #include <XCAFDoc_ShapeTool.hxx>
56 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_ShapeTool,TDF_Attribute)
58 static Standard_Boolean theAutoNaming = Standard_True;
60 // attribute methods //////////////////////////////////////////////////
62 //=======================================================================
65 //=======================================================================
67 const Standard_GUID& XCAFDoc_ShapeTool::GetID()
69 static Standard_GUID ShapeToolID ("efd212ee-6dfd-11d4-b9c8-0060b0ee281b");
74 //=======================================================================
77 //=======================================================================
79 Handle(XCAFDoc_ShapeTool) XCAFDoc_ShapeTool::Set(const TDF_Label& L)
81 Handle(XCAFDoc_ShapeTool) A;
82 if (!L.FindAttribute (XCAFDoc_ShapeTool::GetID(), A)) {
83 A = new XCAFDoc_ShapeTool ();
91 //=======================================================================
92 //function : Constructor
94 //=======================================================================
96 XCAFDoc_ShapeTool::XCAFDoc_ShapeTool()
98 hasSimpleShapes = Standard_False;
102 //=======================================================================
105 //=======================================================================
107 const Standard_GUID& XCAFDoc_ShapeTool::ID() const
112 //=======================================================================
115 //=======================================================================
117 void XCAFDoc_ShapeTool::Restore(const Handle(TDF_Attribute)& /*with*/)
121 //=======================================================================
122 //function : NewEmpty
124 //=======================================================================
126 Handle(TDF_Attribute) XCAFDoc_ShapeTool::NewEmpty() const
128 return new XCAFDoc_ShapeTool;
131 //=======================================================================
134 //=======================================================================
136 void XCAFDoc_ShapeTool::Paste (const Handle(TDF_Attribute)& /*into*/,
137 const Handle(TDF_RelocationTable)& /*RT*/) const
141 // Auxiliary methods //////////////////////////////////////////////////
143 //=======================================================================
144 //function : SetLabelNameByLink
146 //=======================================================================
147 static void SetLabelNameByLink(const TDF_Label L)
149 Handle(TDataStd_TreeNode) Node;
150 if (! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ||
151 ! Node->HasFather()) {
153 std::cout<<"Error: XCAFDoc_ShapeTool, SetLabelNameByLink(): NO NODE"<<std::endl;
157 TCollection_AsciiString Entry;
158 TDF_Tool::Entry ( Node->Father()->Label(), Entry );
159 Entry.Insert(1, "=>[");
162 TDataStd_Name::Set(L, TCollection_ExtendedString( Entry ));
166 //=======================================================================
167 //function : SetLabelNameByShape
169 //=======================================================================
170 static void SetLabelNameByShape(const TDF_Label L)
173 if (XCAFDoc_ShapeTool::GetShape(L, S) &&
174 ! L.IsAttribute(TDataStd_Name::GetID()) ) {
175 Standard_SStream Stream;
176 // TopAbs_ShapeEnum Type = S.ShapeType();
177 // if (Type == TopAbs_COMPOUND) Stream<<"ASSEMBLY";
179 TopAbs::Print(S.ShapeType(), Stream);
180 TCollection_AsciiString aName (Stream.str().c_str());
181 TDataStd_Name::Set(L, TCollection_ExtendedString(aName));
186 //=======================================================================
187 //function : SearchUsingMap
189 //=======================================================================
191 Standard_Boolean XCAFDoc_ShapeTool::SearchUsingMap(const TopoDS_Shape &S, TDF_Label &L,
192 const Standard_Boolean findWithoutLoc,
193 const Standard_Boolean findSubShape) const
196 if(myShapeLabels.IsBound(S)) {
197 L = myShapeLabels.Find(S);
198 return Standard_True;
203 if(myShapeLabels.IsBound(S0)) {
204 TDF_Label L1 = myShapeLabels.Find(S0);
205 TDF_LabelSequence Labels;
206 if(GetUsers(L1, Labels, Standard_True)) {
207 for(Standard_Integer i=1; i<=Labels.Length(); i++) {
208 TopoDS_Shape c = GetShape(Labels.Value(i));
211 return Standard_True;
217 return Standard_True;
221 if(hasSimpleShapes) {
222 if(mySimpleShapes.IsBound(S)) {
223 L = mySimpleShapes.Find(S);
224 return Standard_True;
226 if(mySimpleShapes.IsBound(S0)) {
227 L = mySimpleShapes.Find(S0);
228 return Standard_True;
232 if(!findSubShape) return Standard_False;
233 TDF_Label mainL = FindMainShapeUsingMap(S);
234 if(mainL.IsNull()) return Standard_False;
235 L = AddSubShape(mainL,S);
236 return !L.IsNull();//Standard_True;
240 //=======================================================================
243 //=======================================================================
245 Standard_Boolean XCAFDoc_ShapeTool::Search (const TopoDS_Shape &S,
247 const Standard_Boolean findInstance,
248 const Standard_Boolean findComponent,
249 const Standard_Boolean findSubShape) const
251 // search among shapes
252 Standard_Boolean isLocated = ! S.Location().IsIdentity();
255 // try to find top-level instance
256 if ( findInstance && FindShape ( S, L, Standard_True ) )
257 return Standard_True;
258 // try to find component of assembly
259 if ( findComponent ) {
260 TDF_LabelSequence labels;
261 GetShapes ( labels );
262 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
263 if ( ! IsAssembly ( labels.Value(i) ) ) continue;
264 TDF_LabelSequence comp;
265 GetComponents ( labels.Value(i), comp );
266 for ( Standard_Integer j=1; j <= comp.Length(); j++ ) {
267 TopoDS_Shape c = GetShape ( comp.Value(j) );
268 if ( c.IsSame ( S ) ) {
270 return Standard_True;
276 // try to find top-level simple shape
277 if ( FindShape ( S, L, Standard_False ) ) return Standard_True;
280 if ( ! findSubShape ) return Standard_False;
281 TDF_Label mainL = FindMainShape ( S );
282 if ( mainL.IsNull() ) return Standard_False;
283 L = AddSubShape ( mainL, S );
284 return !L.IsNull();//Standard_True;
287 //=======================================================================
288 //function : FindShape
290 //=======================================================================
292 Standard_Boolean XCAFDoc_ShapeTool::FindShape (const TopoDS_Shape& S,
294 const Standard_Boolean findInstance) const
296 // search for null-located shape
298 if ( ! findInstance ) {
303 // this code is used instead of the following for performance reasons
304 if (TNaming_Tool::HasLabel(Label(), S0)) {
306 L = TNaming_Tool::Label(Label(), S0, TransDef);
309 return Standard_False;
312 return Standard_True;
314 // Try to find shape manually
315 TDF_ChildIDIterator it(Label(), TNaming_NamedShape::GetID());
316 for (; it.More(); it.Next()) {
317 TDF_Label aLabel = it.Value()->Label();
318 Handle(TNaming_NamedShape) NS;
319 if ( aLabel.FindAttribute(TNaming_NamedShape::GetID(), NS) &&
320 S0.IsSame ( TNaming_Tool::GetShape(NS) ) ) {
322 return Standard_True;
327 return Standard_False;
330 //=======================================================================
331 //function : FindShape
333 //=======================================================================
335 TDF_Label XCAFDoc_ShapeTool::FindShape (const TopoDS_Shape& S,
336 const Standard_Boolean findInstance) const
339 if (FindShape(S, L, findInstance))
344 //=======================================================================
345 //function : GetShape
347 //=======================================================================
349 Standard_Boolean XCAFDoc_ShapeTool::GetShape (const TDF_Label& L, TopoDS_Shape& S)
351 Handle(XCAFDoc_Location) LocationAttribute;
354 TopoDS_Compound EmptyComp;
356 B.MakeCompound(EmptyComp);
360 // for instance, get referred shape
361 Handle(TDataStd_TreeNode) Node;
362 if ( L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) && Node->HasFather() &&
363 L.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute)) {
364 if ( ! GetShape(Node->Father()->Label(), S) ) return Standard_False;
365 S.Move ( LocationAttribute->Get() );
366 return Standard_True;
369 // else just return shape on this label
370 Handle(TNaming_NamedShape) NS;
371 if ( ! L.FindAttribute(TNaming_NamedShape::GetID(), NS) ) return Standard_False;
372 S = TNaming_Tool::GetShape(NS);
373 return Standard_True;
376 //=======================================================================
377 //function : GetShape
379 //=======================================================================
381 TopoDS_Shape XCAFDoc_ShapeTool::GetShape(const TDF_Label& L)
388 //=======================================================================
389 //function : NewShape
391 //=======================================================================
393 TDF_Label XCAFDoc_ShapeTool::NewShape() const
395 TopoDS_Compound aShape;
397 tdsB.MakeCompound ( aShape );
400 TDF_Label aLabel = aTag.NewChild(Label());
402 TNaming_Builder tnBuild(aLabel);
403 tnBuild.Generated(aShape);
408 //=======================================================================
409 //function : SetShape
411 //=======================================================================
413 void XCAFDoc_ShapeTool::SetShape (const TDF_Label& L, const TopoDS_Shape& S)
415 TNaming_Builder tnBuild(L);
416 tnBuild.Generated(S);
417 Handle(XCAFDoc_ShapeMapTool) A = XCAFDoc_ShapeMapTool::Set(L);
418 // if ( ! L.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A) ) {
419 // A = XCAFDoc_ShapeMapTool::Set(L);
420 // L.AddAttribute(A);
424 if(!myShapeLabels.IsBound(S)) {
425 myShapeLabels.Bind(S,L);
429 //=======================================================================
430 //function : MakeReference
432 //=======================================================================
434 void XCAFDoc_ShapeTool::MakeReference (const TDF_Label &L,
435 const TDF_Label &refL,
436 const TopLoc_Location &loc)
439 XCAFDoc_Location::Set(L, loc);
442 Handle(TDataStd_TreeNode) refNode, mainNode;
443 mainNode = TDataStd_TreeNode::Set ( refL, XCAFDoc::ShapeRefGUID() );
444 refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::ShapeRefGUID() );
445 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
446 mainNode->Append(refNode);
449 SetLabelNameByLink(L);
452 //=======================================================================
453 //function : addShape
455 //=======================================================================
457 TDF_Label XCAFDoc_ShapeTool::addShape (const TopoDS_Shape& S, const Standard_Boolean makeAssembly)
459 TDF_Label ShapeLabel;
462 // search if the shape already exists (with the same location)
463 if ( S.IsNull() || FindShape ( S, ShapeLabel, Standard_True ) ) return ShapeLabel;
465 // else add a new label
466 ShapeLabel = aTag.NewChild(Label());
468 // if shape has location, make a reference to the same shape without location
469 if ( ! S.Location().IsIdentity() /*&& FindShape ( S, L )*/ ) {
473 TDF_Label L = addShape ( S0, makeAssembly );
474 MakeReference ( ShapeLabel, L, S.Location() );
478 // else add a shape to a label
479 TNaming_Builder tnBuild(ShapeLabel);
480 tnBuild.Generated(S);
482 Handle(XCAFDoc_ShapeMapTool) A = XCAFDoc_ShapeMapTool::Set(ShapeLabel);
483 // if ( ! ShapeLabel.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A) ) {
484 // A = XCAFDoc_ShapeMapTool::Set(ShapeLabel);
485 // ShapeLabel.AddAttribute(A);
490 SetLabelNameByShape(ShapeLabel);
492 // if shape is Compound and flag is set, create assembly
493 if ( makeAssembly && S.ShapeType() == TopAbs_COMPOUND ) {
494 // mark assembly by assigning UAttribute
495 Handle(TDataStd_UAttribute) Uattr;
496 Uattr = TDataStd_UAttribute::Set ( ShapeLabel, XCAFDoc::AssemblyGUID() );
498 TDataStd_Name::Set(ShapeLabel, TCollection_ExtendedString("ASSEMBLY"));
500 // iterate on components
501 TopoDS_Iterator Iterator(S);
502 for (; Iterator.More(); Iterator.Next()) {
503 // get label for component`s shape
504 TopoDS_Shape Scomp = Iterator.Value(), S0 = Scomp;
507 TDF_Label compL = addShape ( S0, makeAssembly );
509 // add a component as reference
510 TDF_Label RefLabel = aTag.NewChild(ShapeLabel);
511 MakeReference ( RefLabel, compL, Scomp.Location() );
515 if(!IsAssembly(ShapeLabel)) {
516 //const TopTools_IndexedMapOfShape tmpMap = A->GetMap();
517 //for(Standard_Integer i=1; i<=tmpMap.Extent(); i++)
518 //mySubShapes.Bind(tmpMap.FindKey(i),ShapeLabel);
519 for(Standard_Integer i=1; i<=A->GetMap().Extent(); i++)
521 TopoDS_Shape aSh = A->GetMap().FindKey(i);
522 mySubShapes.Bind(aSh,ShapeLabel);
524 //mySubShapes.Bind(ShapeLabel,A->GetMap());
531 //=======================================================================
532 //function : prepareAssembly
534 //=======================================================================
535 static Standard_Boolean prepareAssembly (const TopoDS_Shape& theShape,
536 TopoDS_Shape& theOUTShape)
538 // iterate on components
539 theOUTShape = theShape;
540 if (theShape.ShapeType() == TopAbs_COMPOUND) {
542 // check if shape if frosen
543 if (!theOUTShape.Free())
544 theOUTShape.Free(Standard_True);
546 TopTools_SequenceOfShape aSubShapeSeq;
547 TopoDS_Iterator Iterator(theShape);
548 for (; Iterator.More(); Iterator.Next())
549 aSubShapeSeq.Append(Iterator.Value());
550 for (Standard_Integer i = 1; i <= aSubShapeSeq.Length(); i++) {
551 TopoDS_Shape Scomp = aSubShapeSeq.Value(i);
552 TopoDS_Shape aNewScomp;
553 B.Remove(theOUTShape, Scomp);
554 prepareAssembly( Scomp, aNewScomp );
555 TopLoc_Location aLoc;
556 aLoc = aNewScomp.Location();
557 if ( aLoc.IsIdentity() ) {
558 // create an "empty" location
560 aTrsf.SetScale(gp_Pnt(0,0,0), 1);
561 aLoc = TopLoc_Location( aTrsf );
562 aNewScomp.Location( aLoc );
564 B.Add(theOUTShape, aNewScomp);
567 return Standard_True;
571 //=======================================================================
572 //function : AddShape
574 //=======================================================================
576 TDF_Label XCAFDoc_ShapeTool::AddShape (const TopoDS_Shape& theShape,
577 const Standard_Boolean makeAssembly,
578 const Standard_Boolean makePrepare)
580 // PTV 17.02.2003 to avoid components without location.
581 TopoDS_Shape S = theShape;
582 if ( makePrepare && makeAssembly && S.ShapeType() == TopAbs_COMPOUND )
583 prepareAssembly( theShape, S ); // OCC1669
585 TDF_Label L = addShape(S,makeAssembly);
587 if(!myShapeLabels.IsBound(S)) {
588 myShapeLabels.Bind(S,L);
593 //return addShape( S, makeAssembly );
596 //=======================================================================
597 //function : RemoveShape
599 //=======================================================================
601 Standard_Boolean XCAFDoc_ShapeTool::RemoveShape (const TDF_Label& L,
602 const Standard_Boolean removeCompletely) const
604 if ( ! IsTopLevel ( L ) || ! IsFree ( L ) ) return Standard_False;
606 Handle(TDataStd_TreeNode) aNode;
608 if (removeCompletely &&
609 L.FindAttribute (XCAFDoc::ShapeRefGUID(), aNode) &&
610 aNode->HasFather() &&
611 L.IsAttribute (XCAFDoc_Location::GetID()))
613 aLabel = aNode->Father()->Label();
616 L.ForgetAllAttributes (Standard_True);
618 if (removeCompletely && !aLabel.IsNull())
620 return RemoveShape(aLabel);
622 return Standard_True;
626 //=======================================================================
629 //=======================================================================
631 void XCAFDoc_ShapeTool::Init()
633 hasSimpleShapes = Standard_False;
637 //=======================================================================
638 //function : SetAutoNaming
640 //=======================================================================
642 void XCAFDoc_ShapeTool::SetAutoNaming (const Standard_Boolean V)
648 //=======================================================================
649 //function : AutoNaming
651 //=======================================================================
653 Standard_Boolean XCAFDoc_ShapeTool::AutoNaming()
655 return theAutoNaming;
659 //=======================================================================
660 //function : ComputeShapes
662 //=======================================================================
664 void XCAFDoc_ShapeTool::ComputeShapes(const TDF_Label& L)
666 TDF_ChildIterator it(L);
667 for(; it.More(); it.Next()) {
668 TDF_Label L1 = it.Value();
671 if(!myShapeLabels.IsBound(S)) {
672 mySimpleShapes.Bind(S,L1);
680 //=======================================================================
681 //function : ComputeSimpleShapes
683 //=======================================================================
685 void XCAFDoc_ShapeTool::ComputeSimpleShapes()
687 ComputeShapes(Label());
688 hasSimpleShapes = Standard_True;
692 //=======================================================================
693 //function : GetShapes
695 //=======================================================================
697 void XCAFDoc_ShapeTool::GetShapes(TDF_LabelSequence& Labels) const
701 TDF_ChildIterator it(Label());
702 for (; it.More(); it.Next()) {
703 TDF_Label L = it.Value();
705 if ( GetShape ( L, S ) ) Labels.Append ( L );
710 //=======================================================================
711 //function : GetFreeShapes
713 //=======================================================================
715 void XCAFDoc_ShapeTool::GetFreeShapes (TDF_LabelSequence& FreeLabels) const
719 TDF_ChildIterator it(Label());
720 for (; it.More(); it.Next()) {
721 TDF_Label L = it.Value();
723 if ( GetShape ( L, S ) && IsFree ( L ) ) FreeLabels.Append ( L );
727 //=======================================================================
728 //function : IsTopLevel
730 //=======================================================================
732 Standard_Boolean XCAFDoc_ShapeTool::IsTopLevel (const TDF_Label& L) const
734 return L.Father() == Label();
737 //=======================================================================
740 //=======================================================================
742 Standard_Boolean XCAFDoc_ShapeTool::IsShape (const TDF_Label& L)
744 return IsSimpleShape ( L ) || IsAssembly ( L ) || IsReference ( L );
747 //=======================================================================
748 //function : IsSimpleShape
750 //=======================================================================
752 Standard_Boolean XCAFDoc_ShapeTool::IsSimpleShape (const TDF_Label& L)
754 Handle(TNaming_NamedShape) NS;
755 return L.FindAttribute ( TNaming_NamedShape::GetID(), NS ) &&
756 ! IsAssembly ( L ) && ! IsReference ( L );
759 //=======================================================================
760 //function : IsReference
762 //=======================================================================
764 Standard_Boolean XCAFDoc_ShapeTool::IsReference (const TDF_Label& L)
766 Handle(TDataStd_TreeNode) Node;
767 return L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) && Node->HasFather();
770 //=======================================================================
771 //function : IsAssembly
773 //=======================================================================
775 Standard_Boolean XCAFDoc_ShapeTool::IsAssembly (const TDF_Label& L)
777 Handle(TDataStd_UAttribute) Uattr;
778 return L.FindAttribute(XCAFDoc::AssemblyGUID(), Uattr);
781 //=======================================================================
782 //function : IsComponent
784 //=======================================================================
786 Standard_Boolean XCAFDoc_ShapeTool::IsComponent (const TDF_Label& L)
788 return IsReference ( L ) && IsAssembly ( L.Father() );
791 //=======================================================================
792 //function : IsCompound
794 //=======================================================================
796 Standard_Boolean XCAFDoc_ShapeTool::IsCompound (const TDF_Label& L)
798 Handle(TDataStd_Name) Name;
799 if (L.FindAttribute(TDataStd_Name::GetID(),Name)) {
800 TCollection_ExtendedString estr1 = Name->Get();
801 TCollection_ExtendedString estr2("COMPOUND");
803 return Standard_True;
806 return Standard_False;
809 //=======================================================================
810 //function : IsSubShape
812 //=======================================================================
814 Standard_Boolean XCAFDoc_ShapeTool::IsSubShape (const TDF_Label& L)
816 return IsSimpleShape ( L ) && IsShape ( L.Father() );
819 //=======================================================================
822 //=======================================================================
824 Standard_Boolean XCAFDoc_ShapeTool::IsFree (const TDF_Label& L)
826 Handle(TDataStd_TreeNode) Node;
827 if ( ! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ||
828 ! Node->HasFirst() ) return Standard_True;
830 return Standard_False;
833 //=======================================================================
834 //function : GetUsers
835 //purpose : Returns number of users (0 if shape is free)
836 //=======================================================================
838 Standard_Integer XCAFDoc_ShapeTool::GetUsers (const TDF_Label& L,
839 TDF_LabelSequence& Labels,
840 const Standard_Boolean getsubchilds)
842 Standard_Integer NbUsers=0;
843 Handle(TDataStd_TreeNode) Node ;
845 if ( ! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ) return NbUsers;
847 Node = Node->First();
848 while ( ! Node.IsNull() ) {
850 if ( getsubchilds ) {
851 TDF_Label underL = Node->Label().Father();
852 NbUsers += GetUsers ( underL, Labels, getsubchilds );
855 Labels.Append(Node->Label());
862 //=======================================================================
863 //function : NbComponents
865 //=======================================================================
867 Standard_Integer XCAFDoc_ShapeTool::NbComponents (const TDF_Label& L,
868 const Standard_Boolean getsubchilds)
870 TDF_LabelSequence subLabels;
871 GetComponents (L, subLabels, getsubchilds);
872 return subLabels.Length();
875 //=======================================================================
876 //function : GetComponents
878 //=======================================================================
880 Standard_Boolean XCAFDoc_ShapeTool::GetComponents (const TDF_Label& L, TDF_LabelSequence& Labels,
881 const Standard_Boolean getsubchilds)
883 if ( ! IsAssembly(L) ) return Standard_False;
885 TDF_ChildIterator It(L);
886 for (; It.More(); It.Next() ) {
887 TDF_Label comp = It.Value();
888 if ( IsComponent ( comp ) ) {
889 if ( getsubchilds ) {
891 if ( GetReferredShape ( comp, underL ) )
892 GetComponents ( underL, Labels, getsubchilds);
894 Labels.Append ( comp );
897 return Standard_True;
900 //=======================================================================
901 //function : GetLocation
903 //=======================================================================
905 TopLoc_Location XCAFDoc_ShapeTool::GetLocation (const TDF_Label& L)
907 Handle(XCAFDoc_Location) LocationAttribute;
908 if (L.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute))
909 return LocationAttribute->Get();
911 Handle(TNaming_NamedShape) NS;
913 if ( L.FindAttribute ( TNaming_NamedShape::GetID(), NS ) ) {
914 S = TNaming_Tool::GetShape(NS);
919 //=======================================================================
920 //function : GetReferredShape
922 //=======================================================================
924 Standard_Boolean XCAFDoc_ShapeTool::GetReferredShape (const TDF_Label& L,
927 if ( ! IsReference(L) ) return Standard_False;
929 Handle (TDataStd_TreeNode) Node;
930 L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node);
931 Label = Node->Father()->Label();
932 return Standard_True;
935 //=======================================================================
936 //function : AddComponent
938 //=======================================================================
940 TDF_Label XCAFDoc_ShapeTool::AddComponent (const TDF_Label& assembly,
941 const TDF_Label& compL,
942 const TopLoc_Location &Loc)
946 // check that shape is assembly
947 if ( ! IsAssembly(assembly) ) {
948 // if it is simple shape, make it assembly
949 if ( IsSimpleShape(assembly) )
950 TDataStd_UAttribute::Set ( assembly, XCAFDoc::AssemblyGUID() );
954 // add a component as reference
956 L = aTag.NewChild(assembly);
957 MakeReference ( L, compL, Loc );
959 // map shape to label
961 if (GetShape(L, aShape))
963 if (!myShapeLabels.IsBound(aShape))
964 myShapeLabels.Bind(aShape, L);
970 //=======================================================================
971 //function : AddComponent
973 //=======================================================================
975 TDF_Label XCAFDoc_ShapeTool::AddComponent (const TDF_Label& assembly,
976 const TopoDS_Shape& comp,
977 const Standard_Boolean expand)
979 // get label for component`s shape
980 TopoDS_Shape S0 = comp;
984 compL = AddShape ( S0, expand );
986 // add component by its label
987 return AddComponent ( assembly, compL, comp.Location() );
990 //=======================================================================
991 //function : RemoveComponent
993 //=======================================================================
995 void XCAFDoc_ShapeTool::RemoveComponent (const TDF_Label& comp) const
997 if ( IsComponent(comp) )
999 comp.ForgetAllAttributes();
1003 //=======================================================================
1004 //function : UpdateAssemblies
1006 //=======================================================================
1008 void XCAFDoc_ShapeTool::UpdateAssemblies()
1010 // We start from the free shapes (roots in the assembly structure)
1011 TDF_LabelSequence aRootLabels;
1012 GetFreeShapes(aRootLabels);
1014 // Iterate over the free shapes
1015 for ( TDF_LabelSequence::Iterator anIt(aRootLabels); anIt.More(); anIt.Next() )
1017 TDF_Label aRefLabel = anIt.Value();
1018 if (IsReference(aRefLabel))
1020 GetReferredShape(aRefLabel, aRefLabel);
1022 const TDF_Label& aRootLab = aRefLabel;
1023 TopoDS_Shape anAssemblyShape;
1024 updateComponent(aRootLab, anAssemblyShape);
1028 //=======================================================================
1029 //function : IsSubShape
1031 //=======================================================================
1033 //static Standard_Boolean CheckSubShape (const TopoDS_Shape &S, const TopoDS_Shape &sub)
1035 // if ( S.IsSame ( sub ) ) return Standard_True;
1037 // if ( S.ShapeType() >= sub.ShapeType() ) return Standard_False;
1039 // for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
1040 // if ( CheckSubShape ( it.Value(), sub ) ) return Standard_True;
1042 // return Standard_False;
1045 //=======================================================================
1046 //function : IsSubShape
1048 //=======================================================================
1050 Standard_Boolean XCAFDoc_ShapeTool::IsSubShape (const TDF_Label &shapeL,
1051 const TopoDS_Shape &sub) const
1053 Handle(XCAFDoc_ShapeMapTool) A;
1054 if (!shapeL.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A))
1056 TopoDS_Shape aShape = GetShape(shapeL);
1057 if (aShape.IsNull())
1058 return Standard_False;
1059 A = XCAFDoc_ShapeMapTool::Set(shapeL);
1060 A->SetShape(aShape);
1063 return A->IsSubShape(sub);
1066 //=======================================================================
1067 //function : FindSubShape
1069 //=======================================================================
1071 Standard_Boolean XCAFDoc_ShapeTool::FindSubShape (const TDF_Label &shapeL,
1072 const TopoDS_Shape &sub,
1076 return Standard_False;
1078 if (TNaming_Tool::HasLabel(Label(), sub)) {
1080 L = TNaming_Tool::Label(Label(), sub, TransDef);
1082 return Standard_False;
1083 if (L.Father() == shapeL)
1084 return Standard_True;
1088 return Standard_False;
1091 // if subshape was found wrong, try to do it manually
1092 // it can be possible if several part shapes has the same subshapes
1094 TDF_ChildIterator aChldLabIt(shapeL);
1095 for (; aChldLabIt.More(); aChldLabIt.Next() ) {
1096 TDF_Label aSubLabel = aChldLabIt.Value();
1097 Handle(TNaming_NamedShape) NS;
1098 if (!aSubLabel.FindAttribute(TNaming_NamedShape::GetID(), NS))
1100 TopoDS_Shape aSubShape = TNaming_Tool::GetShape(NS);
1101 if (!aSubShape.IsNull() && aSubShape.IsSame(sub)) {
1103 return Standard_True;
1107 return Standard_False;
1110 //=======================================================================
1111 //function : AddSubShape
1113 //=======================================================================
1115 TDF_Label XCAFDoc_ShapeTool::AddSubShape (const TDF_Label &shapeL,
1116 const TopoDS_Shape &sub) const
1119 if (!IsSimpleShape(shapeL) || !IsTopLevel(shapeL))
1122 if ( FindSubShape ( shapeL, sub, L ) ) return L;
1124 if (!IsSubShape(shapeL, sub))
1128 L = aTag.NewChild(shapeL);
1130 TNaming_Builder tnBuild(L);
1131 tnBuild.Generated(sub);
1136 //=======================================================================
1137 //function : AddSubShape
1139 //=======================================================================
1141 Standard_Boolean XCAFDoc_ShapeTool::AddSubShape(const TDF_Label &shapeL,
1142 const TopoDS_Shape &sub,
1143 TDF_Label &addedSubShapeL) const
1145 addedSubShapeL = TDF_Label();
1146 // Check if adding subshape is possible
1147 if (!IsSimpleShape(shapeL) || !IsTopLevel(shapeL))
1148 return Standard_False;
1150 // Try to find already existed subshape
1151 if (FindSubShape(shapeL, sub, addedSubShapeL))
1152 return Standard_False;
1154 if (!IsSubShape(shapeL, sub))
1155 return Standard_False;
1158 addedSubShapeL = aTag.NewChild(shapeL);
1159 TNaming_Builder tnBuild(addedSubShapeL);
1160 tnBuild.Generated(sub);
1162 return Standard_True;
1166 //=======================================================================
1167 //function : FindMainShapeUsingMap
1169 //=======================================================================
1171 TDF_Label XCAFDoc_ShapeTool::FindMainShapeUsingMap(const TopoDS_Shape &sub) const
1173 //for(Standard_Integer i=1; i<=myNotAssemblies.Length(); i++) {
1174 // TDF_Label L = myNotAssemblies.Value(i);
1175 // if(IsSubShape(L,sub)) return L;
1177 if(mySubShapes.IsBound(sub))
1178 return mySubShapes.Find(sub);
1184 //=======================================================================
1185 //function : FindMainShape
1187 //=======================================================================
1189 TDF_Label XCAFDoc_ShapeTool::FindMainShape (const TopoDS_Shape &sub) const
1191 TDF_ChildIterator it(Label());
1192 for (; it.More(); it.Next()) {
1193 TDF_Label L = it.Value();
1195 if ( IsSimpleShape( L ) && IsSubShape ( L, sub ) ) return L;
1202 //=======================================================================
1203 //function : GetSubShapes
1205 //=======================================================================
1207 Standard_Boolean XCAFDoc_ShapeTool::GetSubShapes (const TDF_Label &L,
1208 TDF_LabelSequence& Labels)
1210 TDF_ChildIterator It(L);
1211 for (; It.More(); It.Next() ) {
1212 TDF_Label sub = It.Value();
1213 if ( IsSubShape ( sub ) ) Labels.Append ( sub );
1215 return Labels.Length() >0;
1218 //=======================================================================
1219 //function : BaseLabel
1221 //=======================================================================
1223 TDF_Label XCAFDoc_ShapeTool::BaseLabel () const
1228 //=======================================================================
1229 //function : DumpAssembly
1230 //purpose : recursive part of Dump()
1231 //=======================================================================
1233 static void DumpAssembly(Standard_OStream& theDumpLog,
1235 const Standard_Integer level,
1236 const Standard_Boolean deep)
1239 XCAFDoc_ShapeTool::GetShape(L, S);
1242 for (Standard_Integer i=0; i<level; i++)
1245 TCollection_AsciiString Entry;
1246 TDF_Tool::Entry(L, Entry);
1248 if(XCAFDoc_ShapeTool::IsAssembly(L))
1250 theDumpLog<<"ASSEMBLY ";
1252 else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
1254 if(L.Father().Father().Father().IsRoot())
1255 theDumpLog<<"PART ";
1259 theDumpLog<<"INSTANCE ";
1261 TopAbs::Print(S.ShapeType(), theDumpLog);
1263 theDumpLog<<" "<<Entry;
1264 if(XCAFDoc_ShapeTool::IsReference(L))
1266 Handle(TDataStd_TreeNode) aRef;
1267 L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
1268 TDF_Tool::Entry(aRef->Father()->Label(), Entry);
1269 theDumpLog<<" (refers to "<<Entry<<")";
1271 Handle(TDataStd_Name) Name;
1272 if (L.FindAttribute(TDataStd_Name::GetID(), Name))
1273 theDumpLog<<" \""<<Name->Get()<<"\" ";
1276 theDumpLog<<"("<<*(void**)&S.TShape();
1277 if (! S.Location().IsIdentity())
1278 theDumpLog<<", "<< *(void**)&S.Location();
1281 theDumpLog<<std::endl;
1283 Handle(TDataStd_TreeNode) Node;
1284 TDF_ChildIterator NodeIterator(L);
1285 for (; NodeIterator.More(); NodeIterator.Next()) {
1286 DumpAssembly(theDumpLog, NodeIterator.Value(), level+1, deep);
1289 theDumpLog<<std::endl;
1292 //=======================================================================
1295 //=======================================================================
1297 Standard_OStream& XCAFDoc_ShapeTool::Dump(Standard_OStream& theDumpLog, const Standard_Boolean deep) const
1299 Standard_Integer level = 0;
1300 // TopTools_SequenceOfShape SeqShapes;
1301 TDF_LabelSequence SeqLabels;
1302 GetShapes( SeqLabels);
1304 if (SeqLabels.Length()>0) theDumpLog<<std::endl;
1306 for (i=1; i<=SeqLabels.Length(); i++) {
1307 DumpAssembly(theDumpLog, SeqLabels.Value(i), level, deep);
1311 GetFreeShapes(SeqLabels);
1312 theDumpLog<<std::endl<<"Free Shapes: "<<SeqLabels.Length()<<std::endl;
1313 for (i = 1; i<=SeqLabels.Length(); i++) {
1314 DumpShape(theDumpLog, SeqLabels.Value(i), level, deep);
1315 theDumpLog<<std::endl;
1320 //=======================================================================
1322 //purpose : override
1323 //=======================================================================
1325 Standard_OStream& XCAFDoc_ShapeTool::Dump(Standard_OStream& theDumpLog) const
1327 TDF_Attribute::Dump (theDumpLog);
1328 Dump (theDumpLog, Standard_False);
1332 //=======================================================================
1333 //function : DumpShape
1335 //=======================================================================
1337 void XCAFDoc_ShapeTool::DumpShape(Standard_OStream& theDumpLog, const TDF_Label& L,const Standard_Integer level,const Standard_Boolean deep)
1340 if(! XCAFDoc_ShapeTool::GetShape(L, S) ) return;
1341 for (Standard_Integer i=0; i<level; i++)
1344 if(XCAFDoc_ShapeTool::IsAssembly(L))
1346 theDumpLog<<"ASSEMBLY ";
1348 else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
1350 if(L.Father().Father().Father().IsRoot())
1351 theDumpLog<<"PART ";
1355 theDumpLog<<"INSTANCE ";
1357 TopAbs::Print(S.ShapeType(), theDumpLog);
1359 TCollection_AsciiString Entry;
1360 TDF_Tool::Entry(L, Entry);
1361 theDumpLog<<" "<<Entry;
1362 if(XCAFDoc_ShapeTool::IsReference(L))
1364 Handle(TDataStd_TreeNode) aRef;
1365 L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
1366 TDF_Tool::Entry(aRef->Father()->Label(), Entry);
1367 theDumpLog<<" (refers to "<<Entry<<")";
1369 //std::cout<<std::endl;
1370 Handle(TDataStd_Name) Name;
1371 if (L.FindAttribute(TDataStd_Name::GetID(),Name))
1372 theDumpLog<<" \""<<Name->Get()<<"\" ";
1375 theDumpLog<<"("<<*(void**)&S.TShape();
1376 if (! S.Location().IsIdentity())
1377 theDumpLog<<", "<< *(void**)&S.Location();
1382 //=======================================================================
1383 //function : IsExternRef
1385 //=======================================================================
1387 Standard_Boolean XCAFDoc_ShapeTool::IsExternRef(const TDF_Label& L)
1389 Handle(TDataStd_UAttribute) Uattr;
1390 return L.FindAttribute(XCAFDoc::ExternRefGUID(), Uattr);
1393 //=======================================================================
1394 //function : SetExternRefs
1396 //=======================================================================
1398 void XCAFDoc_ShapeTool::SetExternRefs(const TDF_Label& L,
1399 const TColStd_SequenceOfHAsciiString& SHAS) const
1401 TDF_Label ShapeLabel = L.NewChild();
1402 TDataStd_UAttribute::Set(ShapeLabel,XCAFDoc::ExternRefGUID());
1403 for(Standard_Integer i=1; i<=SHAS.Length(); i++) {
1404 TDF_Label tmplbl = ShapeLabel.FindChild(i,Standard_True);
1405 Handle(TCollection_HAsciiString) str = SHAS(i);
1406 TCollection_ExtendedString extstr(str->String());
1407 TDataStd_Name::Set(tmplbl,extstr);
1411 //=======================================================================
1412 //function : SetExternRefs
1414 //=======================================================================
1416 TDF_Label XCAFDoc_ShapeTool::SetExternRefs(const TColStd_SequenceOfHAsciiString& SHAS) const
1418 TDF_Label ShapeLabel;
1421 ShapeLabel = aTag.NewChild(Label());
1422 TDataStd_UAttribute::Set(ShapeLabel,XCAFDoc::ExternRefGUID());
1423 for(Standard_Integer i=1; i<=SHAS.Length(); i++) {
1424 TDF_Label tmplbl = ShapeLabel.FindChild(i,Standard_True);
1425 Handle(TCollection_HAsciiString) str = SHAS(i);
1426 TCollection_ExtendedString extstr(str->String());
1427 TDataStd_Name::Set(tmplbl,extstr);
1432 //=======================================================================
1433 //function : GetExternRefs
1435 //=======================================================================
1437 void XCAFDoc_ShapeTool::GetExternRefs(const TDF_Label& L,
1438 TColStd_SequenceOfHAsciiString& SHAS)
1440 Handle(TDataStd_Name) TDN;
1442 for(Standard_Integer i=1; i<=L.NbChildren(); i++) {
1443 tmplbl = L.FindChild(i);
1444 if(tmplbl.FindAttribute(TDataStd_Name::GetID(),TDN)) {
1445 TCollection_ExtendedString extstr = TDN->Get();
1446 Handle(TCollection_HAsciiString) str =
1447 new TCollection_HAsciiString(TCollection_AsciiString(extstr, '?'));
1453 // API: API work with SHUO (Specified Higher Usage Occurrance) structure
1455 //=======================================================================
1456 //function : GetSHUO
1458 //=======================================================================
1460 Standard_Boolean XCAFDoc_ShapeTool::GetSHUO (const TDF_Label& SHUOLabel,
1461 Handle(XCAFDoc_GraphNode)& aSHUOAttr)
1463 if ( !SHUOLabel.FindAttribute( XCAFDoc::SHUORefGUID(), aSHUOAttr ) )
1464 return Standard_False;
1465 return Standard_True;
1468 //=======================================================================
1469 //function : GetAllComponentSHUO
1471 //=======================================================================
1473 Standard_Boolean XCAFDoc_ShapeTool::GetAllComponentSHUO (const TDF_Label& theCompLabel,
1474 TDF_AttributeSequence& theSHUOAttrs)
1476 TDF_ChildIterator it(theCompLabel);
1477 for (; it.More(); it.Next()) {
1478 TDF_Label L = it.Value();
1479 Handle(XCAFDoc_GraphNode) aSHUOAttr;
1480 if ( GetSHUO( L, aSHUOAttr ) )
1481 theSHUOAttrs.Append( aSHUOAttr );
1483 return (theSHUOAttrs.Length() > 0);
1486 //=======================================================================
1487 //function : SetSHUO
1489 //=======================================================================
1491 Standard_Boolean XCAFDoc_ShapeTool::SetSHUO (const TDF_LabelSequence& labels,
1492 Handle(XCAFDoc_GraphNode)& MainSHUOAttr) const
1494 MainSHUOAttr.Nullify();
1495 // check number of labels
1496 if (labels.Length() < 2)
1497 return Standard_False;
1498 // check is all labels contains components of any assemblyies
1500 for (i = 1; i <= labels.Length(); i++)
1501 if ( !IsComponent(labels.Value(i)) )
1502 return Standard_False;
1505 TDF_Label UpperSubL = aTag.NewChild( labels( 1 ) );
1506 if (theAutoNaming) {
1507 TCollection_ExtendedString Entry("SHUO");
1508 TDataStd_Name::Set(UpperSubL, TCollection_ExtendedString( Entry ));
1510 Handle(XCAFDoc_GraphNode) aUpperSHUO;
1511 aUpperSHUO = XCAFDoc_GraphNode::Set( UpperSubL, XCAFDoc::SHUORefGUID() );
1512 // init out argument by main upper usage SHUO
1513 MainSHUOAttr = aUpperSHUO;
1514 // add other next_usage occurrences.
1515 for (i = 2; i <= labels.Length(); i++) {
1516 TDF_Label NextSubL = aTag.NewChild( labels( i ) );
1517 if (theAutoNaming) {
1518 TCollection_ExtendedString EntrySub("SHUO-");
1520 TDataStd_Name::Set(NextSubL, TCollection_ExtendedString( EntrySub ));
1522 Handle(XCAFDoc_GraphNode) aNextSHUO;
1523 aNextSHUO = XCAFDoc_GraphNode::Set( NextSubL, XCAFDoc::SHUORefGUID() );
1525 aUpperSHUO->SetChild( aNextSHUO );
1526 aNextSHUO->SetFather( aUpperSHUO );
1527 // now lets next_usage become upper_usage for next level of SHUO
1528 aUpperSHUO = aNextSHUO;
1529 UpperSubL = NextSubL;
1532 return Standard_True;
1535 //=======================================================================
1536 //function : GetSHUOUpperUsage
1538 //=======================================================================
1540 Standard_Boolean XCAFDoc_ShapeTool::GetSHUOUpperUsage (const TDF_Label& NextUsageL,
1541 TDF_LabelSequence& aLabels)
1543 Handle(XCAFDoc_GraphNode) aNextSHUO;
1544 if( !GetSHUO( NextUsageL, aNextSHUO ) || aNextSHUO->NbFathers()<1 )
1545 return Standard_False;
1547 // get upper_usage SHAO
1548 for (Standard_Integer i = 1; i <= aNextSHUO->NbFathers(); i++)
1549 aLabels.Append( aNextSHUO->GetFather(i)->Label() );
1550 return Standard_True;
1553 //=======================================================================
1554 //function : GetSHUONextUsage
1556 //=======================================================================
1558 Standard_Boolean XCAFDoc_ShapeTool::GetSHUONextUsage (const TDF_Label& UpperUsageL,
1559 TDF_LabelSequence& aLabels)
1561 Handle(XCAFDoc_GraphNode) aUpperSHUO;
1562 if ( !GetSHUO( UpperUsageL, aUpperSHUO ) || aUpperSHUO->NbChildren()<1 )
1563 return Standard_False;
1564 // get upper_usage SHAO
1565 for (Standard_Integer i = 1; i <= aUpperSHUO->NbChildren(); i++)
1566 aLabels.Append( aUpperSHUO->GetChild(i)->Label() );
1567 return Standard_True;
1570 //=======================================================================
1571 //function : RemoveSHUO
1573 //=======================================================================
1575 Standard_Boolean XCAFDoc_ShapeTool::RemoveSHUO (const TDF_Label& L) const
1577 L.ForgetAllAttributes (Standard_True);
1578 return Standard_True;
1581 //=======================================================================
1582 //function : checkForShape
1583 //purpose : auxilary
1584 //=======================================================================
1586 static Standard_Boolean checkForShape (const TopoDS_Shape& theShape,
1587 const TopoDS_Shape& theCurSh,
1588 const TDF_Label& theUserL,
1589 TDF_LabelSequence& theLabels)
1591 // the label of an assembly which contains this component
1592 TDF_Label aSuperUserL = theUserL.Father();
1593 TopLoc_Location aSupLoc, aCompLoc;
1594 aSupLoc = ::XCAFDoc_ShapeTool::GetLocation ( aSuperUserL );
1595 aCompLoc = ::XCAFDoc_ShapeTool::GetLocation ( theUserL );
1596 TopoDS_Shape aCopySh = theCurSh;
1597 aCompLoc = aCompLoc.Multiplied( theCurSh.Location() );
1598 aSupLoc = aSupLoc.Multiplied( aCompLoc );
1599 aCopySh.Location( aSupLoc );
1600 if ( aCopySh.IsSame( theShape ) ) {
1601 theLabels.Prepend( theUserL );
1602 return Standard_True;
1604 // try to search deeply (upper by assmebly structure)
1605 TDF_LabelSequence aNewLabels;
1606 for (Standard_Integer j = 1; j <= theLabels.Length(); j++)
1607 aNewLabels.Append( theLabels.Value( j ) );
1608 aNewLabels.Prepend( theUserL );
1609 TDF_LabelSequence aUsers;
1610 ::XCAFDoc_ShapeTool::GetUsers( aSuperUserL, aUsers );
1611 for (Standard_Integer i = 1; i <= aUsers.Length(); i++)
1612 if ( checkForShape( theShape, aCopySh, aUsers.Value( i ), aNewLabels ) ) {
1614 theLabels = aNewLabels;
1615 return Standard_True;
1617 return Standard_False;
1620 //=======================================================================
1621 //function : FindComponent
1623 //=======================================================================
1625 Standard_Boolean XCAFDoc_ShapeTool::FindComponent (const TopoDS_Shape& theShape,
1626 TDF_LabelSequence& theLabels) const
1629 // search for a top-level shape that corresponds to this component
1630 TopoDS_Shape S0 = theShape;
1631 TopLoc_Location loc;
1632 S0.Location ( loc );
1633 TDF_Label aRefL = FindShape( S0 );
1635 return Standard_False; // cannot find top-level shape.
1637 TDF_LabelSequence aUsers;
1638 ::XCAFDoc_ShapeTool::GetUsers( aRefL, aUsers );
1639 for (Standard_Integer i = 1; i <= aUsers.Length(); i++)
1640 if ( checkForShape( theShape, S0, aUsers.Value( i ), theLabels ) )
1643 return (theLabels.Length() > 0);
1646 //=======================================================================
1647 //function : getShapesOfSHUO
1648 //purpose : auxilary
1649 //=======================================================================
1651 static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
1652 const Handle(XCAFDoc_ShapeTool)& theSTool,
1653 const TDF_Label& theSHUOlab,
1654 TopoDS_Shape& theShape)
1656 Handle(XCAFDoc_GraphNode) SHUO;
1657 TDF_LabelSequence aLabSeq;
1658 theSTool->GetSHUONextUsage( theSHUOlab, aLabSeq );
1659 if (aLabSeq.Length() >= 1)
1660 for (Standard_Integer i = 1; i <= aLabSeq.Length(); i++) {
1661 TDF_Label aSubCompL = aLabSeq.Value( i );
1662 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSubCompL.Father() );
1663 // create new map of laocation (to not merge locations from different shapes)
1664 TopLoc_IndexedMapOfLocation aNewPrevLocMap;
1665 for (Standard_Integer m = 1; m <= theaPrevLocMap.Extent(); m++)
1666 aNewPrevLocMap.Add( theaPrevLocMap.FindKey( m ) );
1667 aNewPrevLocMap.Add( compLoc );
1668 // got for the new sublocations and corresponding shape
1669 getShapesOfSHUO( aNewPrevLocMap, theSTool, aSubCompL, theShape );
1672 TopoDS_Shape aSHUO_NUSh = theSTool->GetShape ( theSHUOlab.Father() );
1673 if ( aSHUO_NUSh.IsNull() ) return Standard_False;
1674 // cause got shape with location already.
1675 TopLoc_Location nullLoc;
1676 aSHUO_NUSh.Location ( nullLoc );
1677 // multiply the locations
1678 Standard_Integer intMapLenght = theaPrevLocMap.Extent();
1679 if ( intMapLenght < 1 )
1680 return Standard_False; // should not be, but to avoid exception...?
1681 TopLoc_Location SupcompLoc;
1682 SupcompLoc = theaPrevLocMap.FindKey( intMapLenght );
1683 if (intMapLenght > 1) {
1684 Standard_Integer l = intMapLenght - 1;
1686 SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
1690 aSHUO_NUSh.Location( SupcompLoc );
1691 theShape = aSHUO_NUSh;
1693 return (!theShape.IsNull());
1696 //=======================================================================
1697 //function : GetSHUOInstance
1699 //=======================================================================
1701 TopoDS_Shape XCAFDoc_ShapeTool::GetSHUOInstance (const Handle(XCAFDoc_GraphNode)& theSHUO) const
1703 TopoDS_Shape aShape;
1704 if (theSHUO.IsNull())
1707 TDF_Label aSHUOlab = theSHUO->Label();
1708 // get location of the assembly
1709 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father().Father() );
1710 // get location of the component
1711 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father() );
1712 TopLoc_IndexedMapOfLocation aPrevLocMap;
1713 // get previous setted location
1714 if ( !loc.IsIdentity() )
1715 aPrevLocMap.Add( loc );
1716 aPrevLocMap.Add( compLoc );
1717 // get shape by recurse method
1718 const Handle(XCAFDoc_ShapeTool)& STool = this;
1719 getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aShape );
1724 //=======================================================================
1725 //function : getUsersShapesOfSHUO
1726 //purpose : auxilary
1727 //=======================================================================
1729 static Standard_Boolean getUsersShapesOfSHUO (TopLoc_IndexedMapOfLocation& aPrevLocMap,
1730 const Handle(XCAFDoc_ShapeTool)& STool,
1731 const TDF_Label& aSHUOlab,
1732 const TDF_Label& theUserL,
1733 TopTools_SequenceOfShape& theSHUOShapeSeq)
1735 TopLoc_IndexedMapOfLocation aNewPrevLocMap;
1736 // get location of the assembly
1737 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( theUserL.Father() );
1738 // get location of the component
1739 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( theUserL );
1740 // get previous setted location
1741 aNewPrevLocMap.Add( loc );
1742 aNewPrevLocMap.Add( compLoc );
1744 for (i = 1; i <= aPrevLocMap.Extent(); i++)
1745 aNewPrevLocMap.Add( aPrevLocMap.FindKey(i) );
1746 TDF_Label L = theUserL.Father();
1747 TDF_LabelSequence usersLab;
1748 ::XCAFDoc_ShapeTool::GetUsers( L, usersLab );
1749 if (usersLab.Length() == 0) {
1750 TopoDS_Shape aShape;
1751 getShapesOfSHUO( aNewPrevLocMap, STool, aSHUOlab, aShape );
1752 if (!aShape.IsNull()) {
1753 theSHUOShapeSeq.Append(aShape);
1754 return Standard_True;
1757 // now iterates on users of this assembly as component
1758 for ( i = 1; i <= usersLab.Length(); i++ ) {
1759 TDF_Label aNewUserL = usersLab.Value(i);
1760 getUsersShapesOfSHUO( aNewPrevLocMap, STool, aSHUOlab, aNewUserL, theSHUOShapeSeq );
1763 return (theSHUOShapeSeq.Length() > 1);
1766 //=======================================================================
1767 //function : GetAllSHUOInstances
1768 //purpose : auxilary
1769 //=======================================================================
1771 Standard_Boolean XCAFDoc_ShapeTool::GetAllSHUOInstances (const Handle(XCAFDoc_GraphNode)& theSHUO,
1772 TopTools_SequenceOfShape& theSHUOShapeSeq) const
1774 if (theSHUO.IsNull())
1775 return Standard_False;
1777 TDF_Label aSHUOlab = theSHUO->Label();
1778 TopLoc_IndexedMapOfLocation aPrevLocMap;
1779 // get location of the assembly
1780 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father().Father() );
1781 // get location of the component
1782 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father() );
1783 // get previous setted location
1784 if ( !loc.IsIdentity() )
1785 aPrevLocMap.Add( loc );
1786 aPrevLocMap.Add( compLoc );
1787 // get label of assembly
1788 TDF_Label L = aSHUOlab.Father().Father();
1789 TDF_LabelSequence usersLab;
1790 ::XCAFDoc_ShapeTool::GetUsers( L, usersLab );
1791 TopoDS_Shape aShape;
1792 const Handle(XCAFDoc_ShapeTool)& STool = this;
1793 if (usersLab.Length() == 0) {
1794 getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aShape );
1795 if (!aShape.IsNull()) {
1796 theSHUOShapeSeq.Append(aShape);
1797 return Standard_True;
1800 // now iterates on users of this assembly as component
1801 for (Standard_Integer i = 1; i <= usersLab.Length(); i++) {
1802 TDF_Label aUserL = usersLab.Value(i);
1803 getUsersShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aUserL, theSHUOShapeSeq );
1806 return (theSHUOShapeSeq.Length() > 1);
1809 //=======================================================================
1810 //function : SetInstanceSHUO
1812 //=======================================================================
1814 Handle(XCAFDoc_GraphNode) XCAFDoc_ShapeTool::SetInstanceSHUO (const TopoDS_Shape& theShape) const
1816 Handle(XCAFDoc_GraphNode) SHUO;
1817 TDF_LabelSequence aLabels;
1818 if ( FindComponent( theShape, aLabels ) )
1819 // set shuo structure on labels of component-assembly structure
1820 SetSHUO( aLabels, SHUO );
1824 //=======================================================================
1825 //function : FindSHUO
1827 //=======================================================================
1829 Standard_Boolean XCAFDoc_ShapeTool::FindSHUO (const TDF_LabelSequence& theLabels,
1830 Handle(XCAFDoc_GraphNode)& theSHUOAttr)
1832 TDF_AttributeSequence SHUOAttrs;
1833 TDF_Label aCompLabel = theLabels.Value(1);
1834 if (! ::XCAFDoc_ShapeTool::GetAllComponentSHUO( aCompLabel, SHUOAttrs ) )
1835 return Standard_False;
1836 // WARNING: manage that each SHUO upper_usage have only one SHUO next_usage
1837 for (Standard_Integer i = 1; i <= SHUOAttrs.Length(); i++) {
1838 TDF_LabelSequence aCondidate;
1839 Handle(XCAFDoc_GraphNode) anSHUO = Handle(XCAFDoc_GraphNode)::DownCast(SHUOAttrs.Value(i));
1840 aCondidate.Append( anSHUO->Label().Father() );
1841 while (anSHUO->NbChildren()) {
1842 anSHUO = anSHUO->GetChild( 1 );
1843 aCondidate.Append( anSHUO->Label().Father() );
1845 // check the label sequences
1846 Standard_Boolean isEqual = Standard_True;
1847 if (theLabels.Length() != aCondidate.Length())
1848 isEqual = Standard_False;
1850 for (Standard_Integer li = 1; li <= theLabels.Length(); li++)
1851 if ( theLabels.Value(li) != aCondidate.Value(li) ) {
1852 isEqual = Standard_False;
1858 theSHUOAttr = Handle(XCAFDoc_GraphNode)::DownCast(SHUOAttrs.Value(i));
1861 return ( !theSHUOAttr.IsNull() );
1864 //=======================================================================
1867 //=======================================================================
1868 Standard_Boolean XCAFDoc_ShapeTool::Expand (const TDF_Label& theShapeL)
1870 if (theShapeL.IsNull() || IsAssembly(theShapeL))
1871 return Standard_False;
1873 TopoDS_Shape aShape = GetShape(theShapeL);
1874 if (aShape.IsNull())
1875 return Standard_False;
1877 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1878 Standard_Boolean isExpandedType = aShapeType == TopAbs_COMPOUND || aShapeType == TopAbs_COMPSOLID ||
1879 aShapeType == TopAbs_SHELL || aShapeType == TopAbs_WIRE;
1882 TopoDS_Iterator anIter(aShape);
1883 for(; anIter.More(); anIter.Next())
1885 const TopoDS_Shape& aChildShape = anIter.Value();
1886 TDF_Label aChild, aPart;
1888 // Find child shape as subshape of expanded shape
1889 FindSubShape(theShapeL, aChildShape, aChild);
1890 Handle(TDataStd_Name) anAttr;
1891 //make child (if color isn't set or if it is compound)
1892 if (aChild.IsNull()) {
1893 aChild = AddSubShape(theShapeL, aChildShape);
1897 aChild.FindAttribute(TDataStd_Name::GetID(), anAttr);
1900 // Try to find child shape as already existed part
1901 aPart = FindShape(aChildShape.Located(TopLoc_Location()));
1902 if (aPart.IsNull()) {
1903 // Create new part to link child shape
1904 aPart = AddShape(aChildShape.Located(TopLoc_Location()), Standard_False, Standard_False);
1906 // Add shape manually, if already existed subshape found instead of creation of new part
1907 if (!aPart.IsNull() && !IsTopLevel(aPart)) {
1908 if (!GetReferredShape(aPart, aPart)) {
1910 aPart = aTag.NewChild(Label());
1911 SetShape(aPart, aChildShape.Located(TopLoc_Location()));
1916 if (!anAttr.IsNull()) {
1917 TDataStd_Name::Set(aPart, anAttr->Get());
1920 Standard_SStream Stream;
1921 TopAbs::Print(aChildShape.ShapeType(), Stream);
1922 TCollection_AsciiString aName(Stream.str().c_str());
1923 TDataStd_Name::Set(aPart, TCollection_ExtendedString(aName));
1925 MakeReference(aChild, aPart, aChildShape.Location());
1926 makeSubShape(theShapeL, aPart, aChildShape, aChildShape.Location());
1928 //set assembly attribute
1929 TDataStd_UAttribute::Set(theShapeL, XCAFDoc::AssemblyGUID());
1930 return Standard_True;
1932 return Standard_False;
1935 //=======================================================================
1936 //function : makeSubShape
1938 //=======================================================================
1940 void XCAFDoc_ShapeTool::makeSubShape (const TDF_Label& theMainShapeL,
1941 const TDF_Label& thePart,
1942 const TopoDS_Shape& theShape,
1943 const TopLoc_Location& theLoc)
1945 TopoDS_Iterator anIter(theShape);
1946 Standard_Boolean isCompoundPart = (GetShape(thePart).ShapeType() == TopAbs_COMPOUND);
1947 Standard_Boolean isAssembly = IsAssembly(thePart);
1949 for(; anIter.More(); anIter.Next()) {
1950 const TopoDS_Shape& aChildShape = anIter.Value();
1951 TDF_Label aChildLabel;
1952 FindSubShape(theMainShapeL, aChildShape, aChildLabel);
1953 if(!aChildLabel.IsNull()) {
1955 aChildLabel.ForgetAllAttributes();
1956 makeSubShape(theMainShapeL, thePart, aChildShape, theLoc);
1960 Handle(TDataStd_Name) anAttr;
1961 aChildLabel.FindAttribute(TDataStd_Name::GetID(), anAttr);
1962 TopLoc_Location aSubLoc;
1963 // Calculate location for subshapes of compound parts
1964 aSubLoc = aChildShape.Location();
1966 aSubLoc = theLoc.Inverted() * aSubLoc;
1968 TDF_Label aSubLabel;
1969 // Identical location and empty location are not the same for ShapeTool, so try to process both
1970 // in case of aSubLoc is not identical, the second Add try will not affect algorithm.
1971 Standard_Boolean isNewSubL;
1972 isNewSubL = AddSubShape(thePart, aChildShape.Located(aSubLoc), aSubLabel);
1973 if (aSubLabel.IsNull())
1975 isNewSubL = AddSubShape(thePart, aChildShape.Located(TopLoc_Location()), aSubLabel);
1978 //set name to sub shape
1979 if (!anAttr.IsNull()) {
1980 TDataStd_Name::Set(aSubLabel, anAttr->Get());
1983 Standard_SStream Stream;
1984 TopAbs::Print(aChildShape.ShapeType(), Stream);
1985 TCollection_AsciiString aName(Stream.str().c_str());
1986 TDataStd_Name::Set(aSubLabel, TCollection_ExtendedString(aName));
1988 // Create auxiliary link, it will be removed during moving attributes
1989 MakeReference(aSubLabel, aChildLabel, aChildShape.Location());
1992 aChildLabel.ForgetAllAttributes();
1996 makeSubShape(theMainShapeL, thePart, aChildShape, theLoc);
2000 //=======================================================================
2001 //function : updateComponent
2003 //=======================================================================
2005 Standard_Boolean XCAFDoc_ShapeTool::updateComponent(const TDF_Label& theItemLabel,
2006 TopoDS_Shape& theUpdatedShape) const
2008 if ( !IsAssembly(theItemLabel) )
2009 return Standard_False; // Do nothing for non-assemblies
2011 // Get the currently stored compound for the assembly
2012 TopoDS_Shape aCurrentRootShape;
2013 GetShape(theItemLabel, aCurrentRootShape);
2014 TopTools_MapOfOrientedShape aCurrentRootShapeMap (aCurrentRootShape.NbChildren());
2016 // Get components of the assembly
2017 TDF_LabelSequence aComponentLabs;
2018 GetComponents(theItemLabel, aComponentLabs);
2020 // This flag indicates whether to update the compound of the assembly
2021 Standard_Boolean isModified = Standard_False;
2023 // Compare the number of components in XDE structure with the number of
2024 // components in topological structure. A component may happen to be removed,
2025 // so we have to update the assembly compound
2026 const Standard_Integer aNumTopoComponents = aCurrentRootShape.NbChildren();
2028 if ( aNumTopoComponents != aComponentLabs.Length() )
2029 isModified = Standard_True;
2031 // Iterate over the assembly components. If at least one component is
2032 // modified (this is the recursive check), then the actually stored
2033 // compound has to be updated
2034 TopTools_ListOfShape aComponentShapes;
2036 for ( TDF_LabelSequence::Iterator aCompIt(aComponentLabs); aCompIt.More(); aCompIt.Next() )
2038 const TDF_Label& aComponentLab = aCompIt.Value();
2040 // Take the referred assembly item (ultimately, a part for an instance)
2041 TDF_Label aComponentRefLab;
2042 GetReferredShape(aComponentLab, aComponentRefLab);
2044 // Shape comes with some placement transformation here
2045 TopoDS_Shape aComponentShape;
2046 GetShape(aComponentLab, aComponentShape);
2047 TopLoc_Location aComponentLoc = aComponentShape.Location();
2049 // If the component is a sub-assembly, then its associated compound
2050 // has to be processed in the same manner
2051 if ( IsAssembly(aComponentRefLab) )
2054 if ( updateComponent(aComponentRefLab, aComponentShape) )
2057 isModified = Standard_True;
2059 aComponentShape.Location(aComponentLoc); // Apply placement
2064 // Search for a part in the actual compound of the ultimate assembly.
2065 // If the part is there, then the compound is up-to-date, so it does not require rebuilding
2068 if (aCurrentRootShapeMap.IsEmpty())
2070 // optimize search for next labels in aComponentLabs
2071 for (TopoDS_Iterator aTopoIt(aCurrentRootShape); aTopoIt.More(); aTopoIt.Next())
2073 aCurrentRootShapeMap.Add (aTopoIt.Value());
2076 if (!aCurrentRootShapeMap.Contains (aComponentShape))
2078 // Part has been modified somewhere, so the compound has to be rebuilt
2079 isModified = Standard_True;
2084 // Fill the list of shapes composing a new compound for the assembly
2085 aComponentShapes.Append(aComponentShape);
2088 // If any component is modified, we update the currently stored shape
2091 TopoDS_Compound anUpdatedCompound;
2093 aBB.MakeCompound(anUpdatedCompound);
2095 // Compose new compound
2096 for ( TopTools_ListIteratorOfListOfShape aShapeIt(aComponentShapes); aShapeIt.More(); aShapeIt.Next() )
2098 aBB.Add( anUpdatedCompound, aShapeIt.Value() );
2101 // Store the updated shape as an output
2102 theUpdatedShape = anUpdatedCompound;
2104 // Use topological naming services to store the updated shape in XDE
2105 TNaming_Builder NB(theItemLabel);
2106 NB.Generated(theUpdatedShape);
2112 //=======================================================================
2113 //function : GetNamedProperties
2115 //=======================================================================
2117 Handle(TDataStd_NamedData) XCAFDoc_ShapeTool::GetNamedProperties (const TDF_Label& theLabel,
2118 const Standard_Boolean theToCreate) const
2120 Handle(TDataStd_NamedData) aNamedProperty;
2121 if (!theLabel.FindAttribute(TDataStd_NamedData::GetID(), aNamedProperty) && theToCreate)
2123 aNamedProperty = TDataStd_NamedData::Set(theLabel);
2126 return aNamedProperty;
2129 //=======================================================================
2130 //function : GetNamedProperties
2132 //=======================================================================
2134 Handle(TDataStd_NamedData) XCAFDoc_ShapeTool::GetNamedProperties (const TopoDS_Shape& theShape,
2135 const Standard_Boolean theToCreate) const
2137 Handle(TDataStd_NamedData) aNamedProperty;
2139 if (!Search (theShape, aLabel))
2140 return aNamedProperty;
2142 aNamedProperty = GetNamedProperties (aLabel, theToCreate);
2144 return aNamedProperty;