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.
16 #include <XCAFDoc_ShapeTool.hxx>
18 #include <BRep_Builder.hxx>
20 #include <gp_Trsf.hxx>
21 #include <Standard_GUID.hxx>
22 #include <Standard_Type.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TCollection_ExtendedString.hxx>
25 #include <TCollection_HAsciiString.hxx>
26 #include <TColStd_SequenceOfHAsciiString.hxx>
27 #include <TDataStd_ChildNodeIterator.hxx>
28 #include <TDataStd_Name.hxx>
29 #include <TDataStd_TreeNode.hxx>
30 #include <TDataStd_UAttribute.hxx>
31 #include <TDF_Attribute.hxx>
32 #include <TDF_ChildIDIterator.hxx>
33 #include <TDF_ChildIterator.hxx>
34 #include <TDF_Label.hxx>
35 #include <TDF_LabelMap.hxx>
36 #include <TDF_LabelSequence.hxx>
37 #include <TDF_MapIteratorOfLabelMap.hxx>
38 #include <TDF_RelocationTable.hxx>
39 #include <TDF_Tool.hxx>
40 #include <TDocStd_Document.hxx>
41 #include <TNaming_Builder.hxx>
42 #include <TNaming_NamedShape.hxx>
43 #include <TNaming_Tool.hxx>
44 #include <TopLoc_IndexedMapOfLocation.hxx>
45 #include <TopLoc_Location.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_Iterator.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_MapOfOrientedShape.hxx>
51 #include <XCAFDoc.hxx>
52 #include <XCAFDoc_GraphNode.hxx>
53 #include <XCAFDoc_Location.hxx>
54 #include <XCAFDoc_ShapeMapTool.hxx>
56 IMPLEMENT_DERIVED_ATTRIBUTE_WITH_TYPE(XCAFDoc_ShapeTool,TDataStd_GenericEmpty,"xcaf","ShapeTool")
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 // Auxiliary methods //////////////////////////////////////////////////
114 //=======================================================================
115 //function : SetLabelNameByLink
117 //=======================================================================
118 static void SetLabelNameByLink(const TDF_Label L)
120 Handle(TDataStd_TreeNode) Node;
121 if (! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ||
122 ! Node->HasFather()) {
124 std::cout<<"Error: XCAFDoc_ShapeTool, SetLabelNameByLink(): NO NODE"<<std::endl;
128 TCollection_AsciiString Entry;
129 TDF_Tool::Entry ( Node->Father()->Label(), Entry );
130 Entry.Insert(1, "=>[");
133 TDataStd_Name::Set(L, TCollection_ExtendedString( Entry ));
137 //=======================================================================
138 //function : SetLabelNameByShape
140 //=======================================================================
141 static void SetLabelNameByShape(const TDF_Label L)
144 if (XCAFDoc_ShapeTool::GetShape(L, S) &&
145 ! L.IsAttribute(TDataStd_Name::GetID()) ) {
146 Standard_SStream Stream;
147 // TopAbs_ShapeEnum Type = S.ShapeType();
148 // if (Type == TopAbs_COMPOUND) Stream<<"ASSEMBLY";
150 TopAbs::Print(S.ShapeType(), Stream);
151 TCollection_AsciiString aName (Stream.str().c_str());
152 TDataStd_Name::Set(L, TCollection_ExtendedString(aName));
157 //=======================================================================
158 //function : SearchUsingMap
160 //=======================================================================
162 Standard_Boolean XCAFDoc_ShapeTool::SearchUsingMap(const TopoDS_Shape &S, TDF_Label &L,
163 const Standard_Boolean findWithoutLoc,
164 const Standard_Boolean findSubShape) const
167 if(myShapeLabels.IsBound(S)) {
168 L = myShapeLabels.Find(S);
169 return Standard_True;
174 if(myShapeLabels.IsBound(S0)) {
175 TDF_Label L1 = myShapeLabels.Find(S0);
176 TDF_LabelSequence Labels;
177 if(GetUsers(L1, Labels, Standard_True)) {
178 for(Standard_Integer i=1; i<=Labels.Length(); i++) {
179 TopoDS_Shape c = GetShape(Labels.Value(i));
182 return Standard_True;
188 return Standard_True;
192 if(hasSimpleShapes) {
193 if(mySimpleShapes.IsBound(S)) {
194 L = mySimpleShapes.Find(S);
195 return Standard_True;
197 if(mySimpleShapes.IsBound(S0)) {
198 L = mySimpleShapes.Find(S0);
199 return Standard_True;
203 if(!findSubShape) return Standard_False;
204 TDF_Label mainL = FindMainShapeUsingMap(S);
205 if(mainL.IsNull()) return Standard_False;
206 L = AddSubShape(mainL,S);
207 return !L.IsNull();//Standard_True;
211 //=======================================================================
214 //=======================================================================
216 Standard_Boolean XCAFDoc_ShapeTool::Search (const TopoDS_Shape &S,
218 const Standard_Boolean findInstance,
219 const Standard_Boolean findComponent,
220 const Standard_Boolean findSubShape) const
222 // search among shapes
223 Standard_Boolean isLocated = ! S.Location().IsIdentity();
226 // try to find top-level instance
227 if ( findInstance && FindShape ( S, L, Standard_True ) )
228 return Standard_True;
229 // try to find component of assembly
230 if ( findComponent ) {
231 TDF_LabelSequence labels;
232 GetShapes ( labels );
233 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
234 if ( ! IsAssembly ( labels.Value(i) ) ) continue;
235 TDF_LabelSequence comp;
236 GetComponents ( labels.Value(i), comp );
237 for ( Standard_Integer j=1; j <= comp.Length(); j++ ) {
238 TopoDS_Shape c = GetShape ( comp.Value(j) );
239 if ( c.IsSame ( S ) ) {
241 return Standard_True;
247 // try to find top-level simple shape
248 if ( FindShape ( S, L, Standard_False ) ) return Standard_True;
251 if ( ! findSubShape ) return Standard_False;
252 TDF_Label mainL = FindMainShape ( S );
253 if ( mainL.IsNull() ) return Standard_False;
254 L = AddSubShape ( mainL, S );
255 return !L.IsNull();//Standard_True;
258 //=======================================================================
259 //function : FindShape
261 //=======================================================================
263 Standard_Boolean XCAFDoc_ShapeTool::FindShape (const TopoDS_Shape& S,
265 const Standard_Boolean findInstance) const
267 // search for null-located shape
269 if ( ! findInstance ) {
274 // this code is used instead of the following for performance reasons
275 if (TNaming_Tool::HasLabel(Label(), S0)) {
277 L = TNaming_Tool::Label(Label(), S0, TransDef);
280 return Standard_False;
283 return Standard_True;
285 // Try to find shape manually
286 TDF_ChildIDIterator it(Label(), TNaming_NamedShape::GetID());
287 for (; it.More(); it.Next()) {
288 TDF_Label aLabel = it.Value()->Label();
289 Handle(TNaming_NamedShape) NS;
290 if ( aLabel.FindAttribute(TNaming_NamedShape::GetID(), NS) &&
291 S0.IsSame ( TNaming_Tool::GetShape(NS) ) ) {
293 return Standard_True;
298 return Standard_False;
301 //=======================================================================
302 //function : FindShape
304 //=======================================================================
306 TDF_Label XCAFDoc_ShapeTool::FindShape (const TopoDS_Shape& S,
307 const Standard_Boolean findInstance) const
310 if (FindShape(S, L, findInstance))
315 //=======================================================================
316 //function : GetShape
318 //=======================================================================
320 Standard_Boolean XCAFDoc_ShapeTool::GetShape (const TDF_Label& L, TopoDS_Shape& S)
322 Handle(XCAFDoc_Location) LocationAttribute;
325 TopoDS_Compound EmptyComp;
327 B.MakeCompound(EmptyComp);
331 // for instance, get referred shape
332 Handle(TDataStd_TreeNode) Node;
333 if ( L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) && Node->HasFather() &&
334 L.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute)) {
335 if ( ! GetShape(Node->Father()->Label(), S) ) return Standard_False;
336 S.Move ( LocationAttribute->Get() );
337 return Standard_True;
340 // else just return shape on this label
341 Handle(TNaming_NamedShape) NS;
342 if ( ! L.FindAttribute(TNaming_NamedShape::GetID(), NS) ) return Standard_False;
343 S = TNaming_Tool::GetShape(NS);
344 return Standard_True;
347 //=======================================================================
348 //function : GetShape
350 //=======================================================================
352 TopoDS_Shape XCAFDoc_ShapeTool::GetShape(const TDF_Label& L)
359 //=======================================================================
360 //function : NewShape
362 //=======================================================================
364 TDF_Label XCAFDoc_ShapeTool::NewShape() const
366 TopoDS_Compound aShape;
368 tdsB.MakeCompound ( aShape );
371 TDF_Label aLabel = aTag.NewChild(Label());
373 TNaming_Builder tnBuild(aLabel);
374 tnBuild.Generated(aShape);
379 //=======================================================================
380 //function : SetShape
382 //=======================================================================
384 void XCAFDoc_ShapeTool::SetShape (const TDF_Label& L, const TopoDS_Shape& S)
386 TNaming_Builder tnBuild(L);
387 tnBuild.Generated(S);
388 Handle(XCAFDoc_ShapeMapTool) A = XCAFDoc_ShapeMapTool::Set(L);
389 // if ( ! L.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A) ) {
390 // A = XCAFDoc_ShapeMapTool::Set(L);
391 // L.AddAttribute(A);
395 if(!myShapeLabels.IsBound(S)) {
396 myShapeLabels.Bind(S,L);
400 //=======================================================================
401 //function : MakeReference
403 //=======================================================================
405 void XCAFDoc_ShapeTool::MakeReference (const TDF_Label &L,
406 const TDF_Label &refL,
407 const TopLoc_Location &loc)
410 XCAFDoc_Location::Set(L, loc);
413 Handle(TDataStd_TreeNode) refNode, mainNode;
414 mainNode = TDataStd_TreeNode::Set ( refL, XCAFDoc::ShapeRefGUID() );
415 refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::ShapeRefGUID() );
416 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
417 mainNode->Append(refNode);
420 SetLabelNameByLink(L);
423 //=======================================================================
424 //function : addShape
426 //=======================================================================
428 TDF_Label XCAFDoc_ShapeTool::addShape (const TopoDS_Shape& S, const Standard_Boolean makeAssembly)
430 TDF_Label ShapeLabel;
433 // search if the shape already exists (with the same location)
434 if ( S.IsNull() || FindShape ( S, ShapeLabel, Standard_True ) ) return ShapeLabel;
436 // else add a new label
437 ShapeLabel = aTag.NewChild(Label());
439 // if shape has location, make a reference to the same shape without location
440 if ( ! S.Location().IsIdentity() /*&& FindShape ( S, L )*/ ) {
444 TDF_Label L = addShape ( S0, makeAssembly );
445 MakeReference ( ShapeLabel, L, S.Location() );
449 // else add a shape to a label
450 TNaming_Builder tnBuild(ShapeLabel);
451 tnBuild.Generated(S);
453 Handle(XCAFDoc_ShapeMapTool) A = XCAFDoc_ShapeMapTool::Set(ShapeLabel);
454 // if ( ! ShapeLabel.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A) ) {
455 // A = XCAFDoc_ShapeMapTool::Set(ShapeLabel);
456 // ShapeLabel.AddAttribute(A);
461 SetLabelNameByShape(ShapeLabel);
463 // if shape is Compound and flag is set, create assembly
464 if ( makeAssembly && S.ShapeType() == TopAbs_COMPOUND ) {
465 // mark assembly by assigning UAttribute
466 Handle(TDataStd_UAttribute) Uattr;
467 Uattr = TDataStd_UAttribute::Set ( ShapeLabel, XCAFDoc::AssemblyGUID() );
469 TDataStd_Name::Set(ShapeLabel, TCollection_ExtendedString("ASSEMBLY"));
471 // iterate on components
472 TopoDS_Iterator Iterator(S);
473 for (; Iterator.More(); Iterator.Next()) {
474 // get label for component`s shape
475 TopoDS_Shape Scomp = Iterator.Value(), S0 = Scomp;
478 TDF_Label compL = addShape ( S0, makeAssembly );
480 // add a component as reference
481 TDF_Label RefLabel = aTag.NewChild(ShapeLabel);
482 MakeReference ( RefLabel, compL, Scomp.Location() );
486 if(!IsAssembly(ShapeLabel)) {
487 //const TopTools_IndexedMapOfShape tmpMap = A->GetMap();
488 //for(Standard_Integer i=1; i<=tmpMap.Extent(); i++)
489 //mySubShapes.Bind(tmpMap.FindKey(i),ShapeLabel);
490 for(Standard_Integer i=1; i<=A->GetMap().Extent(); i++)
492 TopoDS_Shape aSh = A->GetMap().FindKey(i);
493 mySubShapes.Bind(aSh,ShapeLabel);
494 //if shape has location, make a reference to the same shape without location
495 if (!aSh.Location().IsIdentity()) {
496 TopoDS_Shape S0 = aSh;
499 mySubShapes.Bind(S0, ShapeLabel);
502 //mySubShapes.Bind(ShapeLabel,A->GetMap());
509 //=======================================================================
510 //function : prepareAssembly
512 //=======================================================================
513 static Standard_Boolean prepareAssembly (const TopoDS_Shape& theShape,
514 TopoDS_Shape& theOUTShape)
516 // iterate on components
517 theOUTShape = theShape;
518 if (theShape.ShapeType() == TopAbs_COMPOUND) {
520 // check if shape if frosen
521 if (!theOUTShape.Free())
522 theOUTShape.Free(Standard_True);
524 TopTools_SequenceOfShape aSubShapeSeq;
525 TopoDS_Iterator Iterator(theShape);
526 for (; Iterator.More(); Iterator.Next())
527 aSubShapeSeq.Append(Iterator.Value());
528 for (Standard_Integer i = 1; i <= aSubShapeSeq.Length(); i++) {
529 TopoDS_Shape Scomp = aSubShapeSeq.Value(i);
530 TopoDS_Shape aNewScomp;
531 B.Remove(theOUTShape, Scomp);
532 prepareAssembly( Scomp, aNewScomp );
533 TopLoc_Location aLoc;
534 aLoc = aNewScomp.Location();
535 if ( aLoc.IsIdentity() ) {
536 // create an "empty" location
538 aTrsf.SetScale(gp_Pnt(0,0,0), 1);
539 aLoc = TopLoc_Location( aTrsf );
540 aNewScomp.Location( aLoc );
542 B.Add(theOUTShape, aNewScomp);
545 return Standard_True;
549 //=======================================================================
550 //function : AddShape
552 //=======================================================================
554 TDF_Label XCAFDoc_ShapeTool::AddShape (const TopoDS_Shape& theShape,
555 const Standard_Boolean makeAssembly,
556 const Standard_Boolean makePrepare)
558 // PTV 17.02.2003 to avoid components without location.
559 TopoDS_Shape S = theShape;
560 if ( makePrepare && makeAssembly && S.ShapeType() == TopAbs_COMPOUND )
561 prepareAssembly( theShape, S ); // OCC1669
563 TDF_Label L = addShape(S,makeAssembly);
565 if(!myShapeLabels.IsBound(S)) {
566 myShapeLabels.Bind(S,L);
571 //return addShape( S, makeAssembly );
574 //=======================================================================
575 //function : RemoveShape
577 //=======================================================================
579 Standard_Boolean XCAFDoc_ShapeTool::RemoveShape (const TDF_Label& L,
580 const Standard_Boolean removeCompletely) const
582 if ( ! IsTopLevel ( L ) || ! IsFree ( L ) ) return Standard_False;
584 Handle(TDataStd_TreeNode) aNode;
586 if (removeCompletely &&
587 L.FindAttribute (XCAFDoc::ShapeRefGUID(), aNode) &&
588 aNode->HasFather() &&
589 L.IsAttribute (XCAFDoc_Location::GetID()))
591 aLabel = aNode->Father()->Label();
594 L.ForgetAllAttributes (Standard_True);
596 if (removeCompletely && !aLabel.IsNull())
598 return RemoveShape(aLabel);
600 return Standard_True;
604 //=======================================================================
607 //=======================================================================
609 void XCAFDoc_ShapeTool::Init()
611 hasSimpleShapes = Standard_False;
615 //=======================================================================
616 //function : SetAutoNaming
618 //=======================================================================
620 void XCAFDoc_ShapeTool::SetAutoNaming (const Standard_Boolean V)
626 //=======================================================================
627 //function : AutoNaming
629 //=======================================================================
631 Standard_Boolean XCAFDoc_ShapeTool::AutoNaming()
633 return theAutoNaming;
637 //=======================================================================
638 //function : ComputeShapes
640 //=======================================================================
642 void XCAFDoc_ShapeTool::ComputeShapes(const TDF_Label& L)
644 TDF_ChildIterator it(L);
645 for(; it.More(); it.Next()) {
646 TDF_Label L1 = it.Value();
649 if(!myShapeLabels.IsBound(S)) {
650 mySimpleShapes.Bind(S,L1);
658 //=======================================================================
659 //function : ComputeSimpleShapes
661 //=======================================================================
663 void XCAFDoc_ShapeTool::ComputeSimpleShapes()
665 ComputeShapes(Label());
666 hasSimpleShapes = Standard_True;
670 //=======================================================================
671 //function : GetShapes
673 //=======================================================================
675 void XCAFDoc_ShapeTool::GetShapes(TDF_LabelSequence& Labels) const
679 TDF_ChildIterator it(Label());
680 for (; it.More(); it.Next()) {
681 TDF_Label L = it.Value();
683 if ( GetShape ( L, S ) ) Labels.Append ( L );
688 //=======================================================================
689 //function : GetFreeShapes
691 //=======================================================================
693 void XCAFDoc_ShapeTool::GetFreeShapes (TDF_LabelSequence& FreeLabels) const
697 TDF_ChildIterator it(Label());
698 for (; it.More(); it.Next()) {
699 TDF_Label L = it.Value();
701 if ( GetShape ( L, S ) && IsFree ( L ) ) FreeLabels.Append ( L );
705 //=======================================================================
706 //function : IsTopLevel
708 //=======================================================================
710 Standard_Boolean XCAFDoc_ShapeTool::IsTopLevel (const TDF_Label& L) const
712 return L.Father() == Label();
715 //=======================================================================
718 //=======================================================================
720 Standard_Boolean XCAFDoc_ShapeTool::IsShape (const TDF_Label& L)
722 return IsSimpleShape ( L ) || IsAssembly ( L ) || IsReference ( L );
725 //=======================================================================
726 //function : IsSimpleShape
728 //=======================================================================
730 Standard_Boolean XCAFDoc_ShapeTool::IsSimpleShape (const TDF_Label& L)
732 Handle(TNaming_NamedShape) NS;
733 return L.FindAttribute ( TNaming_NamedShape::GetID(), NS ) &&
734 ! IsAssembly ( L ) && ! IsReference ( L );
737 //=======================================================================
738 //function : IsReference
740 //=======================================================================
742 Standard_Boolean XCAFDoc_ShapeTool::IsReference (const TDF_Label& L)
744 Handle(TDataStd_TreeNode) Node;
745 return L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) && Node->HasFather();
748 //=======================================================================
749 //function : IsAssembly
751 //=======================================================================
753 Standard_Boolean XCAFDoc_ShapeTool::IsAssembly (const TDF_Label& L)
755 Handle(TDataStd_UAttribute) Uattr;
756 return L.FindAttribute(XCAFDoc::AssemblyGUID(), Uattr);
759 //=======================================================================
760 //function : IsComponent
762 //=======================================================================
764 Standard_Boolean XCAFDoc_ShapeTool::IsComponent (const TDF_Label& L)
766 return IsReference ( L ) && IsAssembly ( L.Father() );
769 //=======================================================================
770 //function : IsCompound
772 //=======================================================================
774 Standard_Boolean XCAFDoc_ShapeTool::IsCompound (const TDF_Label& L)
776 Handle(TDataStd_Name) Name;
777 if (L.FindAttribute(TDataStd_Name::GetID(),Name)) {
778 TCollection_ExtendedString estr1 = Name->Get();
779 TCollection_ExtendedString estr2("COMPOUND");
781 return Standard_True;
784 return Standard_False;
787 //=======================================================================
788 //function : IsSubShape
790 //=======================================================================
792 Standard_Boolean XCAFDoc_ShapeTool::IsSubShape (const TDF_Label& L)
794 return IsSimpleShape ( L ) && IsShape ( L.Father() );
797 //=======================================================================
800 //=======================================================================
802 Standard_Boolean XCAFDoc_ShapeTool::IsFree (const TDF_Label& L)
804 Handle(TDataStd_TreeNode) Node;
805 if ( ! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ||
806 ! Node->HasFirst() ) return Standard_True;
808 return Standard_False;
811 //=======================================================================
812 //function : GetUsers
813 //purpose : Returns number of users (0 if shape is free)
814 //=======================================================================
816 Standard_Integer XCAFDoc_ShapeTool::GetUsers (const TDF_Label& L,
817 TDF_LabelSequence& Labels,
818 const Standard_Boolean getsubchilds)
820 Standard_Integer NbUsers=0;
821 Handle(TDataStd_TreeNode) Node ;
823 if ( ! L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node) ) return NbUsers;
825 Node = Node->First();
826 while ( ! Node.IsNull() ) {
828 if ( getsubchilds ) {
829 TDF_Label underL = Node->Label().Father();
830 NbUsers += GetUsers ( underL, Labels, getsubchilds );
833 Labels.Append(Node->Label());
840 //=======================================================================
841 //function : NbComponents
843 //=======================================================================
845 Standard_Integer XCAFDoc_ShapeTool::NbComponents (const TDF_Label& L,
846 const Standard_Boolean getsubchilds)
848 TDF_LabelSequence subLabels;
849 GetComponents (L, subLabels, getsubchilds);
850 return subLabels.Length();
853 //=======================================================================
854 //function : GetComponents
856 //=======================================================================
858 Standard_Boolean XCAFDoc_ShapeTool::GetComponents (const TDF_Label& L, TDF_LabelSequence& Labels,
859 const Standard_Boolean getsubchilds)
861 if ( ! IsAssembly(L) ) return Standard_False;
863 TDF_ChildIterator It(L);
864 for (; It.More(); It.Next() ) {
865 TDF_Label comp = It.Value();
866 if ( IsComponent ( comp ) ) {
867 if ( getsubchilds ) {
869 if ( GetReferredShape ( comp, underL ) )
870 GetComponents ( underL, Labels, getsubchilds);
872 Labels.Append ( comp );
875 return Standard_True;
878 //=======================================================================
879 //function : GetLocation
881 //=======================================================================
883 TopLoc_Location XCAFDoc_ShapeTool::GetLocation (const TDF_Label& L)
885 Handle(XCAFDoc_Location) LocationAttribute;
886 if (L.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute))
887 return LocationAttribute->Get();
889 Handle(TNaming_NamedShape) NS;
891 if ( L.FindAttribute ( TNaming_NamedShape::GetID(), NS ) ) {
892 S = TNaming_Tool::GetShape(NS);
897 //=======================================================================
898 //function : GetReferredShape
900 //=======================================================================
902 Standard_Boolean XCAFDoc_ShapeTool::GetReferredShape (const TDF_Label& L,
905 if ( ! IsReference(L) ) return Standard_False;
907 Handle (TDataStd_TreeNode) Node;
908 L.FindAttribute(XCAFDoc::ShapeRefGUID(), Node);
909 Label = Node->Father()->Label();
910 return Standard_True;
913 //=======================================================================
914 //function : AddComponent
916 //=======================================================================
918 TDF_Label XCAFDoc_ShapeTool::AddComponent (const TDF_Label& assembly,
919 const TDF_Label& compL,
920 const TopLoc_Location &Loc)
924 // check that shape is assembly
925 if ( ! IsAssembly(assembly) ) {
926 // if it is simple shape, make it assembly
927 if ( IsSimpleShape(assembly) )
928 TDataStd_UAttribute::Set ( assembly, XCAFDoc::AssemblyGUID() );
932 // add a component as reference
934 L = aTag.NewChild(assembly);
935 MakeReference ( L, compL, Loc );
937 // map shape to label
939 if (GetShape(L, aShape))
941 if (!myShapeLabels.IsBound(aShape))
942 myShapeLabels.Bind(aShape, L);
948 //=======================================================================
949 //function : AddComponent
951 //=======================================================================
953 TDF_Label XCAFDoc_ShapeTool::AddComponent (const TDF_Label& assembly,
954 const TopoDS_Shape& comp,
955 const Standard_Boolean expand)
957 // get label for component`s shape
958 TopoDS_Shape S0 = comp;
962 compL = AddShape ( S0, expand );
964 // add component by its label
965 return AddComponent ( assembly, compL, comp.Location() );
968 //=======================================================================
969 //function : RemoveComponent
971 //=======================================================================
973 void XCAFDoc_ShapeTool::RemoveComponent (const TDF_Label& comp) const
975 if ( IsComponent(comp) )
977 comp.ForgetAllAttributes();
981 //=======================================================================
982 //function : UpdateAssemblies
984 //=======================================================================
986 void XCAFDoc_ShapeTool::UpdateAssemblies()
988 // We start from the free shapes (roots in the assembly structure)
989 TDF_LabelSequence aRootLabels;
990 GetFreeShapes(aRootLabels);
992 // Iterate over the free shapes
993 TDF_LabelMap anUpdated;
994 for ( TDF_LabelSequence::Iterator anIt(aRootLabels); anIt.More(); anIt.Next() )
996 TDF_Label aRefLabel = anIt.Value();
997 if (IsReference(aRefLabel))
999 GetReferredShape(aRefLabel, aRefLabel);
1001 const TDF_Label& aRootLab = aRefLabel;
1002 TopoDS_Shape anAssemblyShape;
1003 updateComponent(aRootLab, anAssemblyShape, anUpdated);
1007 //=======================================================================
1008 //function : IsSubShape
1010 //=======================================================================
1012 //static Standard_Boolean CheckSubShape (const TopoDS_Shape &S, const TopoDS_Shape &sub)
1014 // if ( S.IsSame ( sub ) ) return Standard_True;
1016 // if ( S.ShapeType() >= sub.ShapeType() ) return Standard_False;
1018 // for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
1019 // if ( CheckSubShape ( it.Value(), sub ) ) return Standard_True;
1021 // return Standard_False;
1024 //=======================================================================
1025 //function : IsSubShape
1027 //=======================================================================
1029 Standard_Boolean XCAFDoc_ShapeTool::IsSubShape (const TDF_Label &shapeL,
1030 const TopoDS_Shape &sub) const
1032 Handle(XCAFDoc_ShapeMapTool) A;
1033 if (!shapeL.FindAttribute(XCAFDoc_ShapeMapTool::GetID(), A))
1035 TopoDS_Shape aShape = GetShape(shapeL);
1036 if (aShape.IsNull())
1037 return Standard_False;
1038 A = XCAFDoc_ShapeMapTool::Set(shapeL);
1039 A->SetShape(aShape);
1042 return A->IsSubShape(sub);
1045 //=======================================================================
1046 //function : FindSubShape
1048 //=======================================================================
1050 Standard_Boolean XCAFDoc_ShapeTool::FindSubShape (const TDF_Label &shapeL,
1051 const TopoDS_Shape &sub,
1055 return Standard_False;
1057 if (TNaming_Tool::HasLabel(Label(), sub)) {
1059 L = TNaming_Tool::Label(Label(), sub, TransDef);
1061 return Standard_False;
1062 if (L.Father() == shapeL)
1063 return Standard_True;
1067 return Standard_False;
1070 // if subshape was found wrong, try to do it manually
1071 // it can be possible if several part shapes has the same subshapes
1073 TDF_ChildIterator aChldLabIt(shapeL);
1074 for (; aChldLabIt.More(); aChldLabIt.Next() ) {
1075 TDF_Label aSubLabel = aChldLabIt.Value();
1076 Handle(TNaming_NamedShape) NS;
1077 if (!aSubLabel.FindAttribute(TNaming_NamedShape::GetID(), NS))
1079 TopoDS_Shape aSubShape = TNaming_Tool::GetShape(NS);
1080 if (!aSubShape.IsNull() && aSubShape.IsSame(sub)) {
1082 return Standard_True;
1086 return Standard_False;
1089 //=======================================================================
1090 //function : AddSubShape
1092 //=======================================================================
1094 TDF_Label XCAFDoc_ShapeTool::AddSubShape (const TDF_Label &shapeL,
1095 const TopoDS_Shape &sub) const
1098 AddSubShape(shapeL, sub, L);
1103 //=======================================================================
1104 //function : AddSubShape
1106 //=======================================================================
1108 Standard_Boolean XCAFDoc_ShapeTool::AddSubShape(const TDF_Label &shapeL,
1109 const TopoDS_Shape &sub,
1110 TDF_Label &addedSubShapeL) const
1112 addedSubShapeL = TDF_Label();
1113 // Check if adding subshape is possible
1114 if (!IsSimpleShape(shapeL) || !IsTopLevel(shapeL))
1115 return Standard_False;
1117 TopoDS_Shape aSubShape = sub;
1118 Standard_Boolean isDefined = Standard_True;
1119 if (!IsSubShape(shapeL, sub))
1121 isDefined = Standard_False;
1122 // Try to find a subshape as a part of the main shape.
1123 // If location of subshape has been removed,
1124 // take the shape with the location from the main shape
1125 if (sub.Location().IsIdentity())
1127 TDF_LabelSequence aShapeLSeq;
1128 for (TopoDS_Iterator it(GetShape(shapeL)); it.More() && !isDefined; it.Next())
1130 TopoDS_Shape aShape = it.Value();
1131 if (sub.IsSame(aShape.Located(TopLoc_Location())))
1133 isDefined = Standard_True;
1140 return Standard_False;
1142 // Try to find already existed subshape
1143 if (FindSubShape(shapeL, aSubShape, addedSubShapeL))
1144 return Standard_False;
1147 addedSubShapeL = aTag.NewChild(shapeL);
1148 TNaming_Builder tnBuild(addedSubShapeL);
1149 tnBuild.Generated(aSubShape);
1151 return Standard_True;
1155 //=======================================================================
1156 //function : FindMainShapeUsingMap
1158 //=======================================================================
1160 TDF_Label XCAFDoc_ShapeTool::FindMainShapeUsingMap(const TopoDS_Shape &sub) const
1162 //for(Standard_Integer i=1; i<=myNotAssemblies.Length(); i++) {
1163 // TDF_Label L = myNotAssemblies.Value(i);
1164 // if(IsSubShape(L,sub)) return L;
1166 if(mySubShapes.IsBound(sub))
1167 return mySubShapes.Find(sub);
1173 //=======================================================================
1174 //function : FindMainShape
1176 //=======================================================================
1178 TDF_Label XCAFDoc_ShapeTool::FindMainShape (const TopoDS_Shape &sub) const
1180 TDF_ChildIterator it(Label());
1181 for (; it.More(); it.Next()) {
1182 TDF_Label L = it.Value();
1184 if ( IsSimpleShape( L ) && IsSubShape ( L, sub ) ) return L;
1191 //=======================================================================
1192 //function : GetSubShapes
1194 //=======================================================================
1196 Standard_Boolean XCAFDoc_ShapeTool::GetSubShapes (const TDF_Label &L,
1197 TDF_LabelSequence& Labels)
1199 TDF_ChildIterator It(L);
1200 for (; It.More(); It.Next() ) {
1201 TDF_Label sub = It.Value();
1202 if ( IsSubShape ( sub ) ) Labels.Append ( sub );
1204 return Labels.Length() >0;
1207 //=======================================================================
1208 //function : BaseLabel
1210 //=======================================================================
1212 TDF_Label XCAFDoc_ShapeTool::BaseLabel () const
1217 //=======================================================================
1218 //function : DumpAssembly
1219 //purpose : recursive part of Dump()
1220 //=======================================================================
1222 static void DumpAssembly(Standard_OStream& theDumpLog,
1224 const Standard_Integer level,
1225 const Standard_Boolean deep)
1228 XCAFDoc_ShapeTool::GetShape(L, S);
1231 for (Standard_Integer i=0; i<level; i++)
1234 TCollection_AsciiString Entry;
1235 TDF_Tool::Entry(L, Entry);
1237 if(XCAFDoc_ShapeTool::IsAssembly(L))
1239 theDumpLog<<"ASSEMBLY ";
1241 else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
1243 if(L.Father().Father().Father().IsRoot())
1244 theDumpLog<<"PART ";
1248 theDumpLog<<"INSTANCE ";
1250 TopAbs::Print(S.ShapeType(), theDumpLog);
1252 theDumpLog<<" "<<Entry;
1253 if(XCAFDoc_ShapeTool::IsReference(L))
1255 Handle(TDataStd_TreeNode) aRef;
1256 L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
1257 TDF_Tool::Entry(aRef->Father()->Label(), Entry);
1258 theDumpLog<<" (refers to "<<Entry<<")";
1260 Handle(TDataStd_Name) Name;
1261 if (L.FindAttribute(TDataStd_Name::GetID(), Name))
1262 theDumpLog<<" \""<<Name->Get()<<"\" ";
1265 theDumpLog<<"("<<*(void**)&S.TShape();
1266 if (! S.Location().IsIdentity())
1267 theDumpLog<<", "<< *(void**)&S.Location();
1270 theDumpLog<<std::endl;
1272 Handle(TDataStd_TreeNode) Node;
1273 TDF_ChildIterator NodeIterator(L);
1274 for (; NodeIterator.More(); NodeIterator.Next()) {
1275 DumpAssembly(theDumpLog, NodeIterator.Value(), level+1, deep);
1278 theDumpLog<<std::endl;
1281 //=======================================================================
1284 //=======================================================================
1286 Standard_OStream& XCAFDoc_ShapeTool::Dump(Standard_OStream& theDumpLog, const Standard_Boolean deep) const
1288 Standard_Integer level = 0;
1289 // TopTools_SequenceOfShape SeqShapes;
1290 TDF_LabelSequence SeqLabels;
1291 GetShapes( SeqLabels);
1293 if (SeqLabels.Length()>0) theDumpLog<<std::endl;
1295 for (i=1; i<=SeqLabels.Length(); i++) {
1296 DumpAssembly(theDumpLog, SeqLabels.Value(i), level, deep);
1300 GetFreeShapes(SeqLabels);
1301 theDumpLog<<std::endl<<"Free Shapes: "<<SeqLabels.Length()<<std::endl;
1302 for (i = 1; i<=SeqLabels.Length(); i++) {
1303 DumpShape(theDumpLog, SeqLabels.Value(i), level, deep);
1304 theDumpLog<<std::endl;
1309 //=======================================================================
1311 //purpose : override
1312 //=======================================================================
1314 Standard_OStream& XCAFDoc_ShapeTool::Dump(Standard_OStream& theDumpLog) const
1316 TDF_Attribute::Dump (theDumpLog);
1317 Dump (theDumpLog, Standard_False);
1321 //=======================================================================
1322 //function : DumpShape
1324 //=======================================================================
1326 void XCAFDoc_ShapeTool::DumpShape(Standard_OStream& theDumpLog, const TDF_Label& L,const Standard_Integer level,const Standard_Boolean deep)
1329 if(! XCAFDoc_ShapeTool::GetShape(L, S) ) return;
1330 for (Standard_Integer i=0; i<level; i++)
1333 if(XCAFDoc_ShapeTool::IsAssembly(L))
1335 theDumpLog<<"ASSEMBLY ";
1337 else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
1339 if(L.Father().Father().Father().IsRoot())
1340 theDumpLog<<"PART ";
1344 theDumpLog<<"INSTANCE ";
1346 TopAbs::Print(S.ShapeType(), theDumpLog);
1348 TCollection_AsciiString Entry;
1349 TDF_Tool::Entry(L, Entry);
1350 theDumpLog<<" "<<Entry;
1351 if(XCAFDoc_ShapeTool::IsReference(L))
1353 Handle(TDataStd_TreeNode) aRef;
1354 L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
1355 TDF_Tool::Entry(aRef->Father()->Label(), Entry);
1356 theDumpLog<<" (refers to "<<Entry<<")";
1358 //std::cout<<std::endl;
1359 Handle(TDataStd_Name) Name;
1360 if (L.FindAttribute(TDataStd_Name::GetID(),Name))
1361 theDumpLog<<" \""<<Name->Get()<<"\" ";
1364 theDumpLog<<"("<<*(void**)&S.TShape();
1365 if (! S.Location().IsIdentity())
1366 theDumpLog<<", "<< *(void**)&S.Location();
1371 //=======================================================================
1372 //function : IsExternRef
1374 //=======================================================================
1376 Standard_Boolean XCAFDoc_ShapeTool::IsExternRef(const TDF_Label& L)
1378 Handle(TDataStd_UAttribute) Uattr;
1379 return L.FindAttribute(XCAFDoc::ExternRefGUID(), Uattr);
1382 //=======================================================================
1383 //function : SetExternRefs
1385 //=======================================================================
1387 void XCAFDoc_ShapeTool::SetExternRefs(const TDF_Label& L,
1388 const TColStd_SequenceOfHAsciiString& SHAS) const
1390 TDF_Label ShapeLabel = L.NewChild();
1391 TDataStd_UAttribute::Set(ShapeLabel,XCAFDoc::ExternRefGUID());
1392 for(Standard_Integer i=1; i<=SHAS.Length(); i++) {
1393 TDF_Label tmplbl = ShapeLabel.FindChild(i,Standard_True);
1394 Handle(TCollection_HAsciiString) str = SHAS(i);
1395 TCollection_ExtendedString extstr(str->String());
1396 TDataStd_Name::Set(tmplbl,extstr);
1400 //=======================================================================
1401 //function : SetExternRefs
1403 //=======================================================================
1405 TDF_Label XCAFDoc_ShapeTool::SetExternRefs(const TColStd_SequenceOfHAsciiString& SHAS) const
1407 TDF_Label ShapeLabel;
1410 ShapeLabel = aTag.NewChild(Label());
1411 TDataStd_UAttribute::Set(ShapeLabel,XCAFDoc::ExternRefGUID());
1412 for(Standard_Integer i=1; i<=SHAS.Length(); i++) {
1413 TDF_Label tmplbl = ShapeLabel.FindChild(i,Standard_True);
1414 Handle(TCollection_HAsciiString) str = SHAS(i);
1415 TCollection_ExtendedString extstr(str->String());
1416 TDataStd_Name::Set(tmplbl,extstr);
1421 //=======================================================================
1422 //function : GetExternRefs
1424 //=======================================================================
1426 void XCAFDoc_ShapeTool::GetExternRefs(const TDF_Label& L,
1427 TColStd_SequenceOfHAsciiString& SHAS)
1429 Handle(TDataStd_Name) TDN;
1431 for(Standard_Integer i=1; i<=L.NbChildren(); i++) {
1432 tmplbl = L.FindChild(i);
1433 if(tmplbl.FindAttribute(TDataStd_Name::GetID(),TDN)) {
1434 TCollection_ExtendedString extstr = TDN->Get();
1435 Handle(TCollection_HAsciiString) str =
1436 new TCollection_HAsciiString(TCollection_AsciiString(extstr));
1442 // API: API work with SHUO (Specified Higher Usage Occurrance) structure
1444 //=======================================================================
1445 //function : GetSHUO
1447 //=======================================================================
1449 Standard_Boolean XCAFDoc_ShapeTool::GetSHUO (const TDF_Label& SHUOLabel,
1450 Handle(XCAFDoc_GraphNode)& aSHUOAttr)
1452 if ( !SHUOLabel.FindAttribute( XCAFDoc::SHUORefGUID(), aSHUOAttr ) )
1453 return Standard_False;
1454 return Standard_True;
1457 //=======================================================================
1458 //function : GetAllComponentSHUO
1460 //=======================================================================
1462 Standard_Boolean XCAFDoc_ShapeTool::GetAllComponentSHUO (const TDF_Label& theCompLabel,
1463 TDF_AttributeSequence& theSHUOAttrs)
1465 TDF_ChildIterator it(theCompLabel);
1466 for (; it.More(); it.Next()) {
1467 TDF_Label L = it.Value();
1468 Handle(XCAFDoc_GraphNode) aSHUOAttr;
1469 if ( GetSHUO( L, aSHUOAttr ) )
1470 theSHUOAttrs.Append( aSHUOAttr );
1472 return (theSHUOAttrs.Length() > 0);
1475 //=======================================================================
1476 //function : SetSHUO
1478 //=======================================================================
1480 Standard_Boolean XCAFDoc_ShapeTool::SetSHUO (const TDF_LabelSequence& labels,
1481 Handle(XCAFDoc_GraphNode)& MainSHUOAttr) const
1483 MainSHUOAttr.Nullify();
1484 // check number of labels
1485 if (labels.Length() < 2)
1486 return Standard_False;
1487 // check is all labels contains components of any assemblyies
1489 for (i = 1; i <= labels.Length(); i++)
1490 if ( !IsComponent(labels.Value(i)) )
1491 return Standard_False;
1494 TDF_Label UpperSubL = aTag.NewChild( labels( 1 ) );
1495 if (theAutoNaming) {
1496 TCollection_ExtendedString Entry("SHUO");
1497 TDataStd_Name::Set(UpperSubL, TCollection_ExtendedString( Entry ));
1499 Handle(XCAFDoc_GraphNode) aUpperSHUO;
1500 aUpperSHUO = XCAFDoc_GraphNode::Set( UpperSubL, XCAFDoc::SHUORefGUID() );
1501 // init out argument by main upper usage SHUO
1502 MainSHUOAttr = aUpperSHUO;
1503 // add other next_usage occurrences.
1504 for (i = 2; i <= labels.Length(); i++) {
1505 TDF_Label NextSubL = aTag.NewChild( labels( i ) );
1506 if (theAutoNaming) {
1507 TCollection_ExtendedString EntrySub("SHUO-");
1509 TDataStd_Name::Set(NextSubL, TCollection_ExtendedString( EntrySub ));
1511 Handle(XCAFDoc_GraphNode) aNextSHUO;
1512 aNextSHUO = XCAFDoc_GraphNode::Set( NextSubL, XCAFDoc::SHUORefGUID() );
1514 aUpperSHUO->SetChild( aNextSHUO );
1515 aNextSHUO->SetFather( aUpperSHUO );
1516 // now lets next_usage become upper_usage for next level of SHUO
1517 aUpperSHUO = aNextSHUO;
1518 UpperSubL = NextSubL;
1521 return Standard_True;
1524 //=======================================================================
1525 //function : GetSHUOUpperUsage
1527 //=======================================================================
1529 Standard_Boolean XCAFDoc_ShapeTool::GetSHUOUpperUsage (const TDF_Label& NextUsageL,
1530 TDF_LabelSequence& aLabels)
1532 Handle(XCAFDoc_GraphNode) aNextSHUO;
1533 if( !GetSHUO( NextUsageL, aNextSHUO ) || aNextSHUO->NbFathers()<1 )
1534 return Standard_False;
1536 // get upper_usage SHAO
1537 for (Standard_Integer i = 1; i <= aNextSHUO->NbFathers(); i++)
1538 aLabels.Append( aNextSHUO->GetFather(i)->Label() );
1539 return Standard_True;
1542 //=======================================================================
1543 //function : GetSHUONextUsage
1545 //=======================================================================
1547 Standard_Boolean XCAFDoc_ShapeTool::GetSHUONextUsage (const TDF_Label& UpperUsageL,
1548 TDF_LabelSequence& aLabels)
1550 Handle(XCAFDoc_GraphNode) aUpperSHUO;
1551 if ( !GetSHUO( UpperUsageL, aUpperSHUO ) || aUpperSHUO->NbChildren()<1 )
1552 return Standard_False;
1553 // get upper_usage SHAO
1554 for (Standard_Integer i = 1; i <= aUpperSHUO->NbChildren(); i++)
1555 aLabels.Append( aUpperSHUO->GetChild(i)->Label() );
1556 return Standard_True;
1559 //=======================================================================
1560 //function : RemoveSHUO
1562 //=======================================================================
1564 Standard_Boolean XCAFDoc_ShapeTool::RemoveSHUO (const TDF_Label& L) const
1566 L.ForgetAllAttributes (Standard_True);
1567 return Standard_True;
1570 //=======================================================================
1571 //function : checkForShape
1572 //purpose : auxilary
1573 //=======================================================================
1575 static Standard_Boolean checkForShape (const TopoDS_Shape& theShape,
1576 const TopoDS_Shape& theCurSh,
1577 const TDF_Label& theUserL,
1578 TDF_LabelSequence& theLabels)
1580 // the label of an assembly which contains this component
1581 TDF_Label aSuperUserL = theUserL.Father();
1582 TopLoc_Location aSupLoc, aCompLoc;
1583 aSupLoc = ::XCAFDoc_ShapeTool::GetLocation ( aSuperUserL );
1584 aCompLoc = ::XCAFDoc_ShapeTool::GetLocation ( theUserL );
1585 TopoDS_Shape aCopySh = theCurSh;
1586 aCompLoc = aCompLoc.Multiplied( theCurSh.Location() );
1587 aSupLoc = aSupLoc.Multiplied( aCompLoc );
1588 aCopySh.Location( aSupLoc );
1589 if ( aCopySh.IsSame( theShape ) ) {
1590 theLabels.Prepend( theUserL );
1591 return Standard_True;
1593 // try to search deeply (upper by assmebly structure)
1594 TDF_LabelSequence aNewLabels;
1595 for (Standard_Integer j = 1; j <= theLabels.Length(); j++)
1596 aNewLabels.Append( theLabels.Value( j ) );
1597 aNewLabels.Prepend( theUserL );
1598 TDF_LabelSequence aUsers;
1599 ::XCAFDoc_ShapeTool::GetUsers( aSuperUserL, aUsers );
1600 for (Standard_Integer i = 1; i <= aUsers.Length(); i++)
1601 if ( checkForShape( theShape, aCopySh, aUsers.Value( i ), aNewLabels ) ) {
1603 theLabels = aNewLabels;
1604 return Standard_True;
1606 return Standard_False;
1609 //=======================================================================
1610 //function : FindComponent
1612 //=======================================================================
1614 Standard_Boolean XCAFDoc_ShapeTool::FindComponent (const TopoDS_Shape& theShape,
1615 TDF_LabelSequence& theLabels) const
1618 // search for a top-level shape that corresponds to this component
1619 TopoDS_Shape S0 = theShape;
1620 TopLoc_Location loc;
1621 S0.Location ( loc );
1622 TDF_Label aRefL = FindShape( S0 );
1624 return Standard_False; // cannot find top-level shape.
1626 TDF_LabelSequence aUsers;
1627 ::XCAFDoc_ShapeTool::GetUsers( aRefL, aUsers );
1628 for (Standard_Integer i = 1; i <= aUsers.Length(); i++)
1629 if ( checkForShape( theShape, S0, aUsers.Value( i ), theLabels ) )
1632 return (theLabels.Length() > 0);
1635 //=======================================================================
1636 //function : getShapesOfSHUO
1637 //purpose : auxilary
1638 //=======================================================================
1640 static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
1641 const Handle(XCAFDoc_ShapeTool)& theSTool,
1642 const TDF_Label& theSHUOlab,
1643 TopoDS_Shape& theShape)
1645 Handle(XCAFDoc_GraphNode) SHUO;
1646 TDF_LabelSequence aLabSeq;
1647 theSTool->GetSHUONextUsage( theSHUOlab, aLabSeq );
1648 if (aLabSeq.Length() >= 1)
1649 for (Standard_Integer i = 1; i <= aLabSeq.Length(); i++) {
1650 TDF_Label aSubCompL = aLabSeq.Value( i );
1651 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSubCompL.Father() );
1652 // create new map of laocation (to not merge locations from different shapes)
1653 TopLoc_IndexedMapOfLocation aNewPrevLocMap;
1654 for (Standard_Integer m = 1; m <= theaPrevLocMap.Extent(); m++)
1655 aNewPrevLocMap.Add( theaPrevLocMap.FindKey( m ) );
1656 aNewPrevLocMap.Add( compLoc );
1657 // got for the new sublocations and corresponding shape
1658 getShapesOfSHUO( aNewPrevLocMap, theSTool, aSubCompL, theShape );
1661 TopoDS_Shape aSHUO_NUSh = theSTool->GetShape ( theSHUOlab.Father() );
1662 if ( aSHUO_NUSh.IsNull() ) return Standard_False;
1663 // cause got shape with location already.
1664 TopLoc_Location nullLoc;
1665 aSHUO_NUSh.Location ( nullLoc );
1666 // multiply the locations
1667 Standard_Integer intMapLenght = theaPrevLocMap.Extent();
1668 if ( intMapLenght < 1 )
1669 return Standard_False; // should not be, but to avoid exception...?
1670 TopLoc_Location SupcompLoc;
1671 SupcompLoc = theaPrevLocMap.FindKey( intMapLenght );
1672 if (intMapLenght > 1) {
1673 Standard_Integer l = intMapLenght - 1;
1675 SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
1679 aSHUO_NUSh.Location( SupcompLoc );
1680 theShape = aSHUO_NUSh;
1682 return (!theShape.IsNull());
1685 //=======================================================================
1686 //function : GetSHUOInstance
1688 //=======================================================================
1690 TopoDS_Shape XCAFDoc_ShapeTool::GetSHUOInstance (const Handle(XCAFDoc_GraphNode)& theSHUO) const
1692 TopoDS_Shape aShape;
1693 if (theSHUO.IsNull())
1696 TDF_Label aSHUOlab = theSHUO->Label();
1697 // get location of the assembly
1698 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father().Father() );
1699 // get location of the component
1700 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father() );
1701 TopLoc_IndexedMapOfLocation aPrevLocMap;
1702 // get previous setted location
1703 if ( !loc.IsIdentity() )
1704 aPrevLocMap.Add( loc );
1705 aPrevLocMap.Add( compLoc );
1706 // get shape by recurse method
1707 const Handle(XCAFDoc_ShapeTool)& STool = this;
1708 getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aShape );
1713 //=======================================================================
1714 //function : getUsersShapesOfSHUO
1715 //purpose : auxilary
1716 //=======================================================================
1718 static Standard_Boolean getUsersShapesOfSHUO (TopLoc_IndexedMapOfLocation& aPrevLocMap,
1719 const Handle(XCAFDoc_ShapeTool)& STool,
1720 const TDF_Label& aSHUOlab,
1721 const TDF_Label& theUserL,
1722 TopTools_SequenceOfShape& theSHUOShapeSeq)
1724 TopLoc_IndexedMapOfLocation aNewPrevLocMap;
1725 // get location of the assembly
1726 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( theUserL.Father() );
1727 // get location of the component
1728 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( theUserL );
1729 // get previous setted location
1730 aNewPrevLocMap.Add( loc );
1731 aNewPrevLocMap.Add( compLoc );
1733 for (i = 1; i <= aPrevLocMap.Extent(); i++)
1734 aNewPrevLocMap.Add( aPrevLocMap.FindKey(i) );
1735 TDF_Label L = theUserL.Father();
1736 TDF_LabelSequence usersLab;
1737 ::XCAFDoc_ShapeTool::GetUsers( L, usersLab );
1738 if (usersLab.Length() == 0) {
1739 TopoDS_Shape aShape;
1740 getShapesOfSHUO( aNewPrevLocMap, STool, aSHUOlab, aShape );
1741 if (!aShape.IsNull()) {
1742 theSHUOShapeSeq.Append(aShape);
1743 return Standard_True;
1746 // now iterates on users of this assembly as component
1747 for ( i = 1; i <= usersLab.Length(); i++ ) {
1748 TDF_Label aNewUserL = usersLab.Value(i);
1749 getUsersShapesOfSHUO( aNewPrevLocMap, STool, aSHUOlab, aNewUserL, theSHUOShapeSeq );
1752 return (theSHUOShapeSeq.Length() > 1);
1755 //=======================================================================
1756 //function : GetAllSHUOInstances
1757 //purpose : auxilary
1758 //=======================================================================
1760 Standard_Boolean XCAFDoc_ShapeTool::GetAllSHUOInstances (const Handle(XCAFDoc_GraphNode)& theSHUO,
1761 TopTools_SequenceOfShape& theSHUOShapeSeq) const
1763 if (theSHUO.IsNull())
1764 return Standard_False;
1766 TDF_Label aSHUOlab = theSHUO->Label();
1767 TopLoc_IndexedMapOfLocation aPrevLocMap;
1768 // get location of the assembly
1769 TopLoc_Location loc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father().Father() );
1770 // get location of the component
1771 TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSHUOlab.Father() );
1772 // get previous setted location
1773 if ( !loc.IsIdentity() )
1774 aPrevLocMap.Add( loc );
1775 aPrevLocMap.Add( compLoc );
1776 // get label of assembly
1777 TDF_Label L = aSHUOlab.Father().Father();
1778 TDF_LabelSequence usersLab;
1779 ::XCAFDoc_ShapeTool::GetUsers( L, usersLab );
1780 TopoDS_Shape aShape;
1781 const Handle(XCAFDoc_ShapeTool)& STool = this;
1782 if (usersLab.Length() == 0) {
1783 getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aShape );
1784 if (!aShape.IsNull()) {
1785 theSHUOShapeSeq.Append(aShape);
1786 return Standard_True;
1789 // now iterates on users of this assembly as component
1790 for (Standard_Integer i = 1; i <= usersLab.Length(); i++) {
1791 TDF_Label aUserL = usersLab.Value(i);
1792 getUsersShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aUserL, theSHUOShapeSeq );
1795 return (theSHUOShapeSeq.Length() > 1);
1798 //=======================================================================
1799 //function : SetInstanceSHUO
1801 //=======================================================================
1803 Handle(XCAFDoc_GraphNode) XCAFDoc_ShapeTool::SetInstanceSHUO (const TopoDS_Shape& theShape) const
1805 Handle(XCAFDoc_GraphNode) SHUO;
1806 TDF_LabelSequence aLabels;
1807 if ( FindComponent( theShape, aLabels ) )
1808 // set shuo structure on labels of component-assembly structure
1809 SetSHUO( aLabels, SHUO );
1813 //=======================================================================
1814 //function : FindSHUO
1816 //=======================================================================
1818 Standard_Boolean XCAFDoc_ShapeTool::FindSHUO (const TDF_LabelSequence& theLabels,
1819 Handle(XCAFDoc_GraphNode)& theSHUOAttr)
1821 TDF_AttributeSequence SHUOAttrs;
1822 TDF_Label aCompLabel = theLabels.Value(1);
1823 if (! ::XCAFDoc_ShapeTool::GetAllComponentSHUO( aCompLabel, SHUOAttrs ) )
1824 return Standard_False;
1825 // WARNING: manage that each SHUO upper_usage have only one SHUO next_usage
1826 for (Standard_Integer i = 1; i <= SHUOAttrs.Length(); i++) {
1827 TDF_LabelSequence aCondidate;
1828 Handle(XCAFDoc_GraphNode) anSHUO = Handle(XCAFDoc_GraphNode)::DownCast(SHUOAttrs.Value(i));
1829 aCondidate.Append( anSHUO->Label().Father() );
1830 while (anSHUO->NbChildren()) {
1831 anSHUO = anSHUO->GetChild( 1 );
1832 aCondidate.Append( anSHUO->Label().Father() );
1834 // check the label sequences
1835 Standard_Boolean isEqual = Standard_True;
1836 if (theLabels.Length() != aCondidate.Length())
1837 isEqual = Standard_False;
1839 for (Standard_Integer li = 1; li <= theLabels.Length(); li++)
1840 if ( theLabels.Value(li) != aCondidate.Value(li) ) {
1841 isEqual = Standard_False;
1847 theSHUOAttr = Handle(XCAFDoc_GraphNode)::DownCast(SHUOAttrs.Value(i));
1850 return ( !theSHUOAttr.IsNull() );
1853 //=======================================================================
1856 //=======================================================================
1857 Standard_Boolean XCAFDoc_ShapeTool::Expand (const TDF_Label& theShapeL)
1859 if (theShapeL.IsNull() || IsAssembly(theShapeL))
1860 return Standard_False;
1862 TopoDS_Shape aShape = GetShape(theShapeL);
1863 if (aShape.IsNull())
1864 return Standard_False;
1866 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1867 Standard_Boolean isExpandedType = aShapeType == TopAbs_COMPOUND || aShapeType == TopAbs_COMPSOLID ||
1868 aShapeType == TopAbs_SHELL || aShapeType == TopAbs_WIRE;
1871 TopoDS_Iterator anIter(aShape);
1872 for(; anIter.More(); anIter.Next())
1874 const TopoDS_Shape& aChildShape = anIter.Value();
1875 TDF_Label aChild, aPart;
1877 // Find child shape as subshape of expanded shape
1878 FindSubShape(theShapeL, aChildShape, aChild);
1879 Handle(TDataStd_Name) anAttr;
1880 //make child (if color isn't set or if it is compound)
1881 if (aChild.IsNull()) {
1882 aChild = AddSubShape(theShapeL, aChildShape);
1886 aChild.FindAttribute(TDataStd_Name::GetID(), anAttr);
1889 // Try to find child shape as already existed part
1890 aPart = FindShape(aChildShape.Located(TopLoc_Location()));
1891 if (aPart.IsNull()) {
1892 // Create new part to link child shape
1893 aPart = AddShape(aChildShape.Located(TopLoc_Location()), Standard_False, Standard_False);
1895 // Add shape manually, if already existed subshape found instead of creation of new part
1896 if (!aPart.IsNull() && !IsTopLevel(aPart)) {
1897 if (!GetReferredShape(aPart, aPart)) {
1899 aPart = aTag.NewChild(Label());
1900 SetShape(aPart, aChildShape.Located(TopLoc_Location()));
1905 if (!anAttr.IsNull()) {
1906 TDataStd_Name::Set(aPart, anAttr->Get());
1909 Standard_SStream Stream;
1910 TopAbs::Print(aChildShape.ShapeType(), Stream);
1911 TCollection_AsciiString aName(Stream.str().c_str());
1912 TDataStd_Name::Set(aPart, TCollection_ExtendedString(aName));
1914 MakeReference(aChild, aPart, aChildShape.Location());
1915 makeSubShape(theShapeL, aPart, aChildShape, aChildShape.Location());
1917 //set assembly attribute
1918 TDataStd_UAttribute::Set(theShapeL, XCAFDoc::AssemblyGUID());
1919 return Standard_True;
1921 return Standard_False;
1924 //=======================================================================
1925 //function : makeSubShape
1927 //=======================================================================
1929 void XCAFDoc_ShapeTool::makeSubShape (const TDF_Label& theMainShapeL,
1930 const TDF_Label& thePart,
1931 const TopoDS_Shape& theShape,
1932 const TopLoc_Location& theLoc)
1934 TopoDS_Iterator anIter(theShape);
1935 Standard_Boolean isCompoundPart = (GetShape(thePart).ShapeType() == TopAbs_COMPOUND);
1936 Standard_Boolean isAssembly = IsAssembly(thePart);
1938 for(; anIter.More(); anIter.Next()) {
1939 const TopoDS_Shape& aChildShape = anIter.Value();
1940 TDF_Label aChildLabel;
1941 FindSubShape(theMainShapeL, aChildShape, aChildLabel);
1942 if(!aChildLabel.IsNull()) {
1944 aChildLabel.ForgetAllAttributes();
1945 makeSubShape(theMainShapeL, thePart, aChildShape, theLoc);
1949 Handle(TDataStd_Name) anAttr;
1950 aChildLabel.FindAttribute(TDataStd_Name::GetID(), anAttr);
1951 TopLoc_Location aSubLoc;
1952 // Calculate location for subshapes of compound parts
1953 aSubLoc = aChildShape.Location();
1955 aSubLoc = theLoc.Inverted() * aSubLoc;
1957 TDF_Label aSubLabel;
1958 // Identical location and empty location are not the same for ShapeTool, so try to process both
1959 // in case of aSubLoc is not identical, the second Add try will not affect algorithm.
1960 Standard_Boolean isNewSubL;
1961 isNewSubL = AddSubShape(thePart, aChildShape.Located(aSubLoc), aSubLabel);
1962 if (aSubLabel.IsNull())
1964 isNewSubL = AddSubShape(thePart, aChildShape.Located(TopLoc_Location()), aSubLabel);
1967 //set name to sub shape
1968 if (!anAttr.IsNull()) {
1969 TDataStd_Name::Set(aSubLabel, anAttr->Get());
1972 Standard_SStream Stream;
1973 TopAbs::Print(aChildShape.ShapeType(), Stream);
1974 TCollection_AsciiString aName(Stream.str().c_str());
1975 TDataStd_Name::Set(aSubLabel, TCollection_ExtendedString(aName));
1977 // Create auxiliary link, it will be removed during moving attributes
1978 MakeReference(aSubLabel, aChildLabel, aChildShape.Location());
1981 aChildLabel.ForgetAllAttributes();
1985 makeSubShape(theMainShapeL, thePart, aChildShape, theLoc);
1989 //=======================================================================
1990 //function : updateComponent
1992 //=======================================================================
1994 Standard_Boolean XCAFDoc_ShapeTool::updateComponent(const TDF_Label& theItemLabel,
1995 TopoDS_Shape& theUpdatedShape,
1996 TDF_LabelMap& theUpdated) const
1998 if ( !IsAssembly(theItemLabel) )
1999 return Standard_False; // Do nothing for non-assemblies
2001 // Get the currently stored compound for the assembly
2002 TopoDS_Shape aCurrentRootShape;
2003 GetShape(theItemLabel, aCurrentRootShape);
2005 // Check if the given assembly is already updated
2006 if (theUpdated.Contains(theItemLabel)) {
2007 theUpdatedShape = aCurrentRootShape;
2008 return Standard_True;
2011 TopTools_MapOfOrientedShape aCurrentRootShapeMap (aCurrentRootShape.NbChildren());
2013 // Get components of the assembly
2014 TDF_LabelSequence aComponentLabs;
2015 GetComponents(theItemLabel, aComponentLabs);
2017 // This flag indicates whether to update the compound of the assembly
2018 Standard_Boolean isModified = Standard_False;
2020 // Compare the number of components in XDE structure with the number of
2021 // components in topological structure. A component may happen to be removed,
2022 // so we have to update the assembly compound
2023 const Standard_Integer aNumTopoComponents = aCurrentRootShape.NbChildren();
2025 if ( aNumTopoComponents != aComponentLabs.Length() )
2026 isModified = Standard_True;
2028 // Iterate over the assembly components. If at least one component is
2029 // modified (this is the recursive check), then the actually stored
2030 // compound has to be updated
2031 TopTools_ListOfShape aComponentShapes;
2033 for ( TDF_LabelSequence::Iterator aCompIt(aComponentLabs); aCompIt.More(); aCompIt.Next() )
2035 const TDF_Label& aComponentLab = aCompIt.Value();
2037 // Take the referred assembly item (ultimately, a part for an instance)
2038 TDF_Label aComponentRefLab;
2039 GetReferredShape(aComponentLab, aComponentRefLab);
2041 // Shape comes with some placement transformation here
2042 TopoDS_Shape aComponentShape;
2043 GetShape(aComponentLab, aComponentShape);
2044 TopLoc_Location aComponentLoc = aComponentShape.Location();
2046 // If the component is a sub-assembly, then its associated compound
2047 // has to be processed in the same manner
2048 if ( IsAssembly(aComponentRefLab) )
2051 if ( updateComponent(aComponentRefLab, aComponentShape, theUpdated) )
2053 isModified = Standard_True;
2054 aComponentShape.Location(aComponentLoc); // Apply placement
2059 // Search for a part in the actual compound of the ultimate assembly.
2060 // If the part is there, then the compound is up-to-date, so it does not require rebuilding
2063 if (aCurrentRootShapeMap.IsEmpty())
2065 // optimize search for next labels in aComponentLabs
2066 for (TopoDS_Iterator aTopoIt(aCurrentRootShape); aTopoIt.More(); aTopoIt.Next())
2068 aCurrentRootShapeMap.Add (aTopoIt.Value());
2071 if (!aCurrentRootShapeMap.Contains (aComponentShape))
2073 // Part has been modified somewhere, so the compound has to be rebuilt
2074 isModified = Standard_True;
2079 // Fill the list of shapes composing a new compound for the assembly
2080 aComponentShapes.Append(aComponentShape);
2083 // If any component is modified, we update the currently stored shape
2086 TopoDS_Compound anUpdatedCompound;
2088 aBB.MakeCompound(anUpdatedCompound);
2090 // Compose new compound
2091 for ( TopTools_ListIteratorOfListOfShape aShapeIt(aComponentShapes); aShapeIt.More(); aShapeIt.Next() )
2093 aBB.Add( anUpdatedCompound, aShapeIt.Value() );
2096 // Store the updated shape as an output
2097 theUpdatedShape = anUpdatedCompound;
2099 // Use topological naming services to store the updated shape in XDE
2100 TNaming_Builder NB(theItemLabel);
2101 NB.Generated(theUpdatedShape);
2105 theUpdated.Add(theItemLabel);
2110 //=======================================================================
2111 //function : GetNamedProperties
2113 //=======================================================================
2115 Handle(TDataStd_NamedData) XCAFDoc_ShapeTool::GetNamedProperties (const TDF_Label& theLabel,
2116 const Standard_Boolean theToCreate) const
2118 Handle(TDataStd_NamedData) aNamedProperty;
2119 if (!theLabel.FindAttribute(TDataStd_NamedData::GetID(), aNamedProperty) && theToCreate)
2121 aNamedProperty = TDataStd_NamedData::Set(theLabel);
2124 return aNamedProperty;
2127 //=======================================================================
2128 //function : GetNamedProperties
2130 //=======================================================================
2132 Handle(TDataStd_NamedData) XCAFDoc_ShapeTool::GetNamedProperties (const TopoDS_Shape& theShape,
2133 const Standard_Boolean theToCreate) const
2135 Handle(TDataStd_NamedData) aNamedProperty;
2137 if (!Search (theShape, aLabel))
2138 return aNamedProperty;
2140 aNamedProperty = GetNamedProperties (aLabel, theToCreate);
2142 return aNamedProperty;
2145 //=======================================================================
2146 //function : DumpJson
2148 //=======================================================================
2149 void XCAFDoc_ShapeTool::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
2151 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
2153 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
2155 for (XCAFDoc_DataMapOfShapeLabel::Iterator aShapeLabelIt (myShapeLabels); aShapeLabelIt.More(); aShapeLabelIt.Next())
2157 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &aShapeLabelIt.Key())
2159 TCollection_AsciiString aShapeLabel;
2160 TDF_Tool::Entry (aShapeLabelIt.Value(), aShapeLabel);
2161 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aShapeLabel)
2164 for (XCAFDoc_DataMapOfShapeLabel::Iterator aSubShapeIt (mySubShapes); aSubShapeIt.More(); aSubShapeIt.Next())
2166 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &aSubShapeIt.Key())
2168 TCollection_AsciiString aSubShape;
2169 TDF_Tool::Entry (aSubShapeIt.Value(), aSubShape);
2170 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aSubShape)
2173 for (XCAFDoc_DataMapOfShapeLabel::Iterator aSimpleShapeIt (mySimpleShapes); aSimpleShapeIt.More(); aSimpleShapeIt.Next())
2175 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &aSimpleShapeIt.Key())
2177 TCollection_AsciiString aSimpleShape;
2178 TDF_Tool::Entry (aSimpleShapeIt.Value(), aSimpleShape);
2179 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aSimpleShape)
2182 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, hasSimpleShapes)