1 // Created on: 2017-02-16
2 // Created by: Sergey NIKONOV
3 // Copyright (c) 2000-2017 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 <Standard_GUID.hxx>
17 #include <TDF_Data.hxx>
18 #include <TDF_Label.hxx>
19 #include <TDF_Tool.hxx>
20 #include <TDF_RelocationTable.hxx>
21 #include <TDocStd_Owner.hxx>
22 #include <TDocStd_Document.hxx>
23 #include <TNaming_NamedShape.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopTools_IndexedMapOfShape.hxx>
27 #include <XCAFDoc_AssemblyItemRef.hxx>
29 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_AssemblyItemRef, TDF_Attribute)
34 ExtraRef_SubshapeIndex
38 XCAFDoc_AssemblyItemRef::GetID()
40 static Standard_GUID s_ID("3F2E4CD6-169B-4747-A321-5670E4291F5D");
44 Handle(XCAFDoc_AssemblyItemRef)
45 XCAFDoc_AssemblyItemRef::Get(const TDF_Label& theLabel)
47 Handle(XCAFDoc_AssemblyItemRef) aThis;
48 theLabel.FindAttribute(XCAFDoc_AssemblyItemRef::GetID(), aThis);
52 Handle(XCAFDoc_AssemblyItemRef)
53 XCAFDoc_AssemblyItemRef::Set(const TDF_Label& theLabel,
54 const XCAFDoc_AssemblyItemId& theItemId)
56 Handle(XCAFDoc_AssemblyItemRef) aThis;
57 if (!theLabel.IsNull() && !theLabel.FindAttribute(XCAFDoc_AssemblyItemRef::GetID(), aThis))
59 aThis = new XCAFDoc_AssemblyItemRef();
60 aThis->SetItem(theItemId);
61 theLabel.AddAttribute(aThis);
66 Handle(XCAFDoc_AssemblyItemRef)
67 XCAFDoc_AssemblyItemRef::Set(const TDF_Label& theLabel,
68 const XCAFDoc_AssemblyItemId& theItemId,
69 const Standard_GUID& theAttrGUID)
71 Handle(XCAFDoc_AssemblyItemRef) aThis;
72 if (!theLabel.IsNull() && !theLabel.FindAttribute(XCAFDoc_AssemblyItemRef::GetID(), aThis))
74 aThis = new XCAFDoc_AssemblyItemRef();
75 aThis->SetItem(theItemId);
76 aThis->SetGUID(theAttrGUID);
77 theLabel.AddAttribute(aThis);
82 Handle(XCAFDoc_AssemblyItemRef)
83 XCAFDoc_AssemblyItemRef::Set(const TDF_Label& theLabel,
84 const XCAFDoc_AssemblyItemId& theItemId,
85 const Standard_Integer theShapeIndex)
87 Handle(XCAFDoc_AssemblyItemRef) aThis;
88 if (!theLabel.IsNull() && !theLabel.FindAttribute(XCAFDoc_AssemblyItemRef::GetID(), aThis))
90 aThis = new XCAFDoc_AssemblyItemRef();
91 aThis->SetItem(theItemId);
92 aThis->SetSubshapeIndex(theShapeIndex);
93 theLabel.AddAttribute(aThis);
98 XCAFDoc_AssemblyItemRef::XCAFDoc_AssemblyItemRef()
99 : myExtraRef(ExtraRef_None)
105 XCAFDoc_AssemblyItemRef::IsOrphan() const
107 if (myItemId.IsNull())
108 return Standard_True;
110 TDF_Label aRoot = Label().Root();
112 Handle(TDocStd_Owner) anOwner;
113 if (!aRoot.FindAttribute(TDocStd_Owner::GetID(), anOwner))
114 return Standard_True;
116 Handle(TDocStd_Document) aDoc = anOwner->GetDocument();
118 return Standard_True;
120 Handle(TDF_Data) aData = aDoc->GetData();
122 return Standard_True;
125 TDF_Tool::Label(aData, myItemId.GetPath().Last(), aLabel);
127 return Standard_True;
133 Handle(TDF_Attribute) anAttr;
134 if (!aLabel.FindAttribute(GetGUID(), anAttr))
135 return Standard_True;
137 else if (IsSubshapeIndex())
139 Handle(TNaming_NamedShape) aNamedShape;
140 if (!aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNamedShape))
141 return Standard_True;
143 TopoDS_Shape aShape = aNamedShape->Get();
144 TopTools_IndexedMapOfShape aMap;
145 TopExp::MapShapes(aShape, aMap);
146 Standard_Integer aSubshapeIndex = GetSubshapeIndex();
147 if (aSubshapeIndex < 1 || aMap.Size() < aSubshapeIndex)
148 return Standard_True;
152 return Standard_False;
156 XCAFDoc_AssemblyItemRef::HasExtraRef() const
158 return (myExtraRef != ExtraRef_None);
162 XCAFDoc_AssemblyItemRef::IsGUID() const
164 return (myExtraRef == ExtraRef_AttrGUID && Standard_GUID::CheckGUIDFormat(myExtraId.ToCString()));
168 XCAFDoc_AssemblyItemRef::IsSubshapeIndex() const
170 return (myExtraRef == ExtraRef_SubshapeIndex && myExtraId.IsIntegerValue());
173 const XCAFDoc_AssemblyItemId&
174 XCAFDoc_AssemblyItemRef::GetItem() const
180 XCAFDoc_AssemblyItemRef::GetGUID() const
183 return Standard_GUID(myExtraId.ToCString());
185 return Standard_GUID();
189 XCAFDoc_AssemblyItemRef::GetSubshapeIndex() const
191 if (IsSubshapeIndex())
192 return myExtraId.IntegerValue();
198 XCAFDoc_AssemblyItemRef::SetItem(const XCAFDoc_AssemblyItemId& theItemId)
201 myItemId = theItemId;
206 XCAFDoc_AssemblyItemRef::SetItem(const TColStd_ListOfAsciiString& thePath)
209 myItemId.Init(thePath);
214 XCAFDoc_AssemblyItemRef::SetItem(const TCollection_AsciiString& theString)
217 myItemId.Init(theString);
221 void XCAFDoc_AssemblyItemRef::SetGUID(const Standard_GUID& theAttrGUID)
224 myExtraRef = ExtraRef_AttrGUID;
225 Standard_Character aGUIDStr[Standard_GUID_SIZE + 1];
226 theAttrGUID.ToCString(aGUIDStr);
227 aGUIDStr[Standard_GUID_SIZE] = '\0';
229 myExtraId.AssignCat(aGUIDStr);
233 XCAFDoc_AssemblyItemRef::SetSubshapeIndex(Standard_Integer theSubshapeIndex)
236 myExtraRef = ExtraRef_SubshapeIndex;
238 myExtraId.AssignCat(theSubshapeIndex);
242 XCAFDoc_AssemblyItemRef::ClearExtraRef()
245 myExtraRef = ExtraRef_None;
250 XCAFDoc_AssemblyItemRef::ID() const
255 Handle(TDF_Attribute)
256 XCAFDoc_AssemblyItemRef::NewEmpty() const
258 return new XCAFDoc_AssemblyItemRef();
262 XCAFDoc_AssemblyItemRef::Restore(const Handle(TDF_Attribute)& theAttrFrom)
264 Handle(XCAFDoc_AssemblyItemRef) anOther = Handle(XCAFDoc_AssemblyItemRef)::DownCast(theAttrFrom);
265 if (!anOther.IsNull())
267 myItemId = anOther->myItemId;
268 myExtraRef = anOther->myExtraRef;
269 myExtraId = anOther->myExtraId;
274 XCAFDoc_AssemblyItemRef::Paste(const Handle(TDF_Attribute)& theAttrInto,
275 const Handle(TDF_RelocationTable)& /*theRT*/) const
277 Handle(XCAFDoc_AssemblyItemRef) anOther = Handle(XCAFDoc_AssemblyItemRef)::DownCast(theAttrInto);
278 if (!anOther.IsNull())
280 anOther->myItemId = myItemId;
281 anOther->myExtraRef = myExtraRef;
282 anOther->myExtraId = myExtraId;
287 XCAFDoc_AssemblyItemRef::Dump(Standard_OStream& theOS) const
289 theOS << "Path: " << myItemId.ToString();
291 theOS << "/GUID:" << myExtraId;
292 else if (IsSubshapeIndex())
293 theOS << "/Subshape: " << myExtraId;