Salome HOME
merge from branch BR_For40_DSC tag mergeto_BR_Dev_For_4_0_16apr07
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_SObject.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
21 //  File   : SALOMEDSImpl_SObject.cxx
22 //  Author : Sergey RUIN
23 //  Module : SALOME
24
25
26
27 #include "SALOMEDSImpl_SObject.hxx"
28 #include "SALOMEDSImpl_Attributes.hxx"
29 #include "SALOMEDSImpl_SComponent.hxx"
30 #include "SALOMEDSImpl_Study.hxx"
31
32 using namespace std;
33
34 #include <TDF_AttributeIterator.hxx>
35 #include <map>
36
37 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_SObject, MMgt_TShared )
38 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_SObject, MMgt_TShared )
39
40 //============================================================================
41 /*! Function : constructor
42  *  Purpose  : 
43  */
44 //============================================================================
45 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject(const TDF_Label& theLabel)
46   :_lab(theLabel)
47 {
48   _value = "";
49   _type = "";
50   _name = "";
51 }
52
53 //============================================================================
54 /*! Function : Desctructor
55  *  Purpose  : 
56  */
57 //============================================================================    
58 SALOMEDSImpl_SObject::~SALOMEDSImpl_SObject() 
59 {}
60
61 //============================================================================
62 /*! Function : GetID
63  *  Purpose  : 
64  */
65 //============================================================================
66 TCollection_AsciiString SALOMEDSImpl_SObject::GetID()
67 {
68   TCollection_AsciiString anEntry;
69   TDF_Tool::Entry (_lab,anEntry);
70   return anEntry;
71 }
72   
73 //============================================================================
74 /*! Function : GetFatherComponent
75  *  Purpose  : 
76  */
77 //============================================================================
78 Handle(SALOMEDSImpl_SComponent) SALOMEDSImpl_SObject::GetFatherComponent()
79 {
80   TDF_Label LF = _lab;
81   while (!SALOMEDSImpl_SComponent::IsA(LF) && !LF.IsRoot()) {
82     LF = LF.Father();
83   }
84   
85   if(LF.IsRoot()) return NULL;
86   
87   return GetStudy()->GetSComponent(LF);
88 }
89   
90 //============================================================================
91 /*! Function : GetFather
92  *  Purpose  : 
93  */
94 //============================================================================
95 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_SObject::GetFather()
96 {
97   return GetStudy()->GetSObject(_lab.Father());    
98 }
99
100
101 //============================================================================
102 /*! Function : GetStudy
103  *  Purpose  : 
104  */
105 //============================================================================
106 Handle(SALOMEDSImpl_Study) SALOMEDSImpl_SObject::GetStudy()
107 {
108   return SALOMEDSImpl_Study::GetStudy(_lab);
109 }
110
111 //============================================================================
112 /*! Function : FindAttribute
113  *  Purpose  : Find attribute of given type on this SObject
114  */
115 //============================================================================
116 bool SALOMEDSImpl_SObject::FindAttribute(Handle(TDF_Attribute)& theAttribute, 
117                                          const TCollection_AsciiString& theTypeOfAttribute)
118 {
119   if(_lab.IsNull()) return Standard_False;
120   Standard_GUID aGUID = GetGUID(theTypeOfAttribute);
121   if (_lab.FindAttribute(aGUID, theAttribute)) return Standard_True;
122   return Standard_False;
123 }
124
125
126
127 //============================================================================
128 /*! Function : GetAllAttributes
129  *  Purpose  : Returns list of all attributes for this sobject
130  */
131 //============================================================================
132 Handle(TColStd_HSequenceOfTransient) SALOMEDSImpl_SObject::GetAllAttributes()
133 {
134   Standard_Integer NumAttr = _lab.NbAttributes();
135   Handle(TColStd_HSequenceOfTransient) SeqOfAttr = new TColStd_HSequenceOfTransient();
136   Handle(SALOMEDSImpl_GenericAttribute) anAttr;
137   if (NumAttr != 0) {
138     for(TDF_AttributeIterator iter(_lab);iter.More();iter.Next()) {
139       anAttr = Handle(SALOMEDSImpl_GenericAttribute)::DownCast(iter.Value());
140       if(!anAttr.IsNull() && anAttr->Type() != "AttributeReference")
141         SeqOfAttr->Append(anAttr);
142     }
143   }
144   return SeqOfAttr;
145 }
146
147
148 //============================================================================
149 /*! Function : ReferencedObject
150  *  Purpose  : 
151  */
152 //============================================================================
153 bool SALOMEDSImpl_SObject::ReferencedObject(Handle(SALOMEDSImpl_SObject)& theObject)
154 {
155   Handle(SALOMEDSImpl_AttributeReference) Ref;
156   if (!_lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(),Ref))
157     return false;
158   
159   theObject =  GetStudy()->GetSObject(Ref->Get());
160   return true;
161 }
162
163 //============================================================================
164 /*! Function : FindSubObject
165  *  Purpose  : 
166  */
167 //============================================================================
168 bool SALOMEDSImpl_SObject::FindSubObject(int theTag, Handle(SALOMEDSImpl_SObject)& theObject)
169 {
170   TDF_Label L = _lab.FindChild(theTag, false);
171   if (L.IsNull()) return false;
172   
173   theObject = GetStudy()->GetSObject(L);
174   return true;
175     
176 }  
177
178   
179 //============================================================================
180 /*! Function : GetName
181  *  Purpose  : 
182  */
183 //============================================================================
184 TCollection_AsciiString SALOMEDSImpl_SObject::GetName() 
185 {
186   TCollection_AsciiString aStr = "";
187   Handle(SALOMEDSImpl_AttributeName) aName;
188   if (_lab.FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aName)) {
189     aStr = aName->Value();
190   }
191   return aStr;
192 }
193
194 //============================================================================
195 /*! Function : GetComment
196  *  Purpose  : 
197  */
198 //============================================================================
199 TCollection_AsciiString SALOMEDSImpl_SObject::GetComment() 
200 {
201   TCollection_AsciiString aStr = "";
202   Handle(SALOMEDSImpl_AttributeComment) aComment;
203   if (_lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID(), aComment)) {
204     aStr = aComment->Value();
205   }
206   return aStr;
207 }
208
209 //============================================================================
210 /*! Function : GetIOR
211  *  Purpose  : 
212  */
213 //============================================================================
214 TCollection_AsciiString SALOMEDSImpl_SObject::GetIOR() 
215 {
216   TCollection_AsciiString aStr = "";
217   Handle(SALOMEDSImpl_AttributeIOR) anIOR;
218   if (_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anIOR)) {
219     aStr = anIOR->Value();
220   }
221   return aStr;
222 }
223
224
225 Standard_GUID SALOMEDSImpl_SObject::GetGUID(const TCollection_AsciiString& theType) 
226 {
227   __AttributeTypeToGUIDForSObject
228
229   if (strncmp(theType.ToCString(), "AttributeTreeNodeGUID",21) == 0) {
230     const char* aCType = theType.ToCString();
231     char* aGUIDString = new char[40]; 
232     sprintf(aGUIDString, &(aCType[21]));
233     Standard_GUID aGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
234     delete(aGUIDString);
235     return aGUID;
236   }
237   return Standard_GUID();
238 }
239
240