279c14db1c5fc38b269b1d7f24dd3cb62ff5ef7a
[occt.git] / src / TDataStd / TDataStd_Name.cxx
1 // Created on: 1997-07-31
2 // Created by: Denis PASCAL
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <TDataStd_Name.hxx>
18
19 #include <Standard_DomainError.hxx>
20 #include <Standard_Dump.hxx>
21 #include <Standard_GUID.hxx>
22 #include <Standard_Type.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TCollection_ExtendedString.hxx>
25 #include <TDF_Attribute.hxx>
26 #include <TDF_ChildIterator.hxx>
27 #include <TDF_Label.hxx>
28 #include <TDF_ListIteratorOfAttributeList.hxx>
29 #include <TDF_RelocationTable.hxx>
30 #include <TDF_Tool.hxx>
31
32 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_Name,TDF_Attribute)
33
34 //=======================================================================
35 //function : GetID
36 //purpose  : 
37 //=======================================================================
38 const Standard_GUID& TDataStd_Name::GetID () 
39 {
40   static Standard_GUID TDataStd_NameID("2a96b608-ec8b-11d0-bee7-080009dc3333");
41   return TDataStd_NameID;
42 }
43
44 //=======================================================================
45 //function : SetAttr
46 //purpose  : Implements Set functionality
47 //=======================================================================
48 static Handle(TDataStd_Name) SetAttr(const TDF_Label&       label,
49                                      const TCollection_ExtendedString& theString,
50                                      const Standard_GUID& theGuid) 
51 {
52   Handle(TDataStd_Name) N;
53   if (!label.FindAttribute(theGuid, N)) {
54     N = new TDataStd_Name ();
55     N->SetID(theGuid);
56     label.AddAttribute(N);
57   }
58   N->Set (theString); 
59   return N;
60 }
61
62 //=======================================================================
63 //function : Set
64 //purpose  : 
65 //=======================================================================
66 Handle(TDataStd_Name) TDataStd_Name::Set
67                                 (const TDF_Label&                  label,
68                                  const TCollection_ExtendedString& theString) 
69 {
70   return SetAttr(label, theString, GetID());
71 }
72
73 //=======================================================================
74 //function : Set
75 //purpose  : Set user defined attribute
76 //=======================================================================
77
78 Handle(TDataStd_Name) TDataStd_Name::Set (const TDF_Label&    label, 
79                                           const Standard_GUID& theGuid,
80                                           const TCollection_ExtendedString& theString) 
81 {
82   return SetAttr(label, theString, theGuid);
83 }
84 //=======================================================================
85 //function : TDataStd_Name
86 //purpose  : Empty Constructor
87 //=======================================================================
88
89 TDataStd_Name::TDataStd_Name () : myID(GetID())
90 {}
91
92 //=======================================================================
93 //function : Set
94 //purpose  : 
95 //=======================================================================
96 void TDataStd_Name::Set (const TCollection_ExtendedString& S) 
97 {
98   if(myString == S) return;
99  
100   Backup();
101   myString = S;
102 }
103
104
105
106 //=======================================================================
107 //function : Get
108 //purpose  : 
109 //=======================================================================
110 const TCollection_ExtendedString& TDataStd_Name::Get () const
111 {
112   return myString;
113 }
114
115 //=======================================================================
116 //function : SetID
117 //purpose  :
118 //=======================================================================
119
120 void TDataStd_Name::SetID( const Standard_GUID&  theGuid)
121 {  
122   if(myID == theGuid) return;
123
124   Backup();
125   myID = theGuid;
126 }
127
128 //=======================================================================
129 //function : SetID
130 //purpose  : sets default ID
131 //=======================================================================
132 void TDataStd_Name::SetID()
133 {
134   Backup();
135   myID = GetID();
136 }
137
138 // TDF_Attribute methods
139 //=======================================================================
140 //function : ID
141 //purpose  : 
142 //=======================================================================
143
144 const Standard_GUID& TDataStd_Name::ID () const { return myID; }
145
146 //=======================================================================
147 //function : NewEmpty
148 //purpose  : 
149 //=======================================================================
150
151 Handle(TDF_Attribute) TDataStd_Name::NewEmpty () const
152 {
153   return new TDataStd_Name();
154 }
155
156 //=======================================================================
157 //function : Restore
158 //purpose  : 
159 //=======================================================================
160
161 void TDataStd_Name::Restore(const Handle(TDF_Attribute)& with) 
162 {
163    Handle(TDataStd_Name) anAtt = Handle(TDataStd_Name)::DownCast (with);
164    myString = anAtt->Get();
165    myID = anAtt->ID();
166 }
167
168
169 //=======================================================================
170 //function : Paste
171 //purpose  : 
172 //=======================================================================
173
174 void TDataStd_Name::Paste (const Handle(TDF_Attribute)& into,
175                            const Handle(TDF_RelocationTable)&/* RT*/) const
176 {
177   Handle(TDataStd_Name) anAtt = Handle(TDataStd_Name)::DownCast (into);
178   anAtt->Set (myString);
179   anAtt->SetID(myID);
180 }
181
182 //=======================================================================
183 //function : Dump
184 //purpose  : 
185 //=======================================================================
186
187 Standard_OStream& TDataStd_Name::Dump (Standard_OStream& anOS) const
188 {
189   TDF_Attribute::Dump(anOS);
190   anOS << " Name=|"<<myString<<"|";
191   Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
192   myID.ToCString(sguid);
193   anOS << sguid << std::endl;
194   return anOS;
195 }
196
197 //=======================================================================
198 //function : DumpJson
199 //purpose  : 
200 //=======================================================================
201 void TDataStd_Name::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
202 {
203   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
204
205   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
206
207   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myString)
208   OCCT_DUMP_FIELD_VALUE_GUID (theOStream, myID)
209 }