Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_SObject.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //  File   : SALOMEDSImpl_SObject.cxx
3 //  Author : Sergey RUIN
4 //  Module : SALOME
5
6
7
8 #include "SALOMEDSImpl_SObject.hxx"
9 #include "SALOMEDSImpl_Attributes.hxx"
10 #include "SALOMEDSImpl_SComponent.hxx"
11 #include "SALOMEDSImpl_Study.hxx"
12
13 using namespace std;
14
15 #include <TDF_AttributeIterator.hxx>
16 #include <map>
17
18 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_SObject, MMgt_TShared )
19 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_SObject, MMgt_TShared )
20
21 //============================================================================
22 /*! Function : constructor
23  *  Purpose  : 
24  */
25 //============================================================================
26 SALOMEDSImpl_SObject::SALOMEDSImpl_SObject(const TDF_Label& theLabel)
27   :_lab(theLabel)
28 {
29   _value = "";
30   _type = "";
31   _name = "";
32 }
33
34 //============================================================================
35 /*! Function : Desctructor
36  *  Purpose  : 
37  */
38 //============================================================================    
39 SALOMEDSImpl_SObject::~SALOMEDSImpl_SObject() 
40 {}
41
42 //============================================================================
43 /*! Function : GetID
44  *  Purpose  : 
45  */
46 //============================================================================
47 TCollection_AsciiString SALOMEDSImpl_SObject::GetID()
48 {
49   TCollection_AsciiString anEntry;
50   TDF_Tool::Entry (_lab,anEntry);
51   return anEntry;
52 }
53   
54 //============================================================================
55 /*! Function : GetFatherComponent
56  *  Purpose  : 
57  */
58 //============================================================================
59 Handle(SALOMEDSImpl_SComponent) SALOMEDSImpl_SObject::GetFatherComponent()
60 {
61   TDF_Label LF = _lab;
62   while (!SALOMEDSImpl_SComponent::IsA(LF) && !LF.IsRoot()) {
63     LF = LF.Father();
64   }
65   
66   if(LF.IsRoot()) return NULL;
67   
68   return GetStudy()->GetSComponent(LF);
69 }
70   
71 //============================================================================
72 /*! Function : GetFather
73  *  Purpose  : 
74  */
75 //============================================================================
76 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_SObject::GetFather()
77 {
78   return GetStudy()->GetSObject(_lab.Father());    
79 }
80
81
82 //============================================================================
83 /*! Function : GetStudy
84  *  Purpose  : 
85  */
86 //============================================================================
87 Handle(SALOMEDSImpl_Study) SALOMEDSImpl_SObject::GetStudy()
88 {
89   return SALOMEDSImpl_Study::GetStudy(_lab);
90 }
91
92 //============================================================================
93 /*! Function : FindAttribute
94  *  Purpose  : Find attribute of given type on this SObject
95  */
96 //============================================================================
97 bool SALOMEDSImpl_SObject::FindAttribute(Handle(TDF_Attribute)& theAttribute, 
98                                          const TCollection_AsciiString& theTypeOfAttribute)
99 {
100   if(_lab.IsNull()) return Standard_False;
101   Standard_GUID aGUID = GetGUID(theTypeOfAttribute);
102   if (_lab.FindAttribute(aGUID, theAttribute)) return Standard_True;
103   return Standard_False;
104 }
105
106
107
108 //============================================================================
109 /*! Function : GetAllAttributes
110  *  Purpose  : Returns list of all attributes for this sobject
111  */
112 //============================================================================
113 Handle(TColStd_HSequenceOfTransient) SALOMEDSImpl_SObject::GetAllAttributes()
114 {
115   Standard_Integer NumAttr = _lab.NbAttributes();
116   Handle(TColStd_HSequenceOfTransient) SeqOfAttr = new TColStd_HSequenceOfTransient();
117   Handle(SALOMEDSImpl_GenericAttribute) anAttr;
118   if (NumAttr != 0) {
119     for(TDF_AttributeIterator iter(_lab);iter.More();iter.Next()) {
120       anAttr = Handle(SALOMEDSImpl_GenericAttribute)::DownCast(iter.Value());
121       if(!anAttr.IsNull() && anAttr->Type() != "AttributeReference")
122         SeqOfAttr->Append(anAttr);
123     }
124   }
125   return SeqOfAttr;
126 }
127
128
129 //============================================================================
130 /*! Function : ReferencedObject
131  *  Purpose  : 
132  */
133 //============================================================================
134 bool SALOMEDSImpl_SObject::ReferencedObject(Handle(SALOMEDSImpl_SObject)& theObject)
135 {
136   Handle(SALOMEDSImpl_AttributeReference) Ref;
137   if (!_lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(),Ref))
138     return false;
139   
140   theObject =  GetStudy()->GetSObject(Ref->Get());
141   return true;
142 }
143
144 //============================================================================
145 /*! Function : FindSubObject
146  *  Purpose  : 
147  */
148 //============================================================================
149 bool SALOMEDSImpl_SObject::FindSubObject(int theTag, Handle(SALOMEDSImpl_SObject)& theObject)
150 {
151   TDF_Label L = _lab.FindChild(theTag, false);
152   if (L.IsNull()) return false;
153   
154   theObject = GetStudy()->GetSObject(L);
155   return true;
156     
157 }  
158
159   
160 //============================================================================
161 /*! Function : GetName
162  *  Purpose  : 
163  */
164 //============================================================================
165 TCollection_AsciiString SALOMEDSImpl_SObject::GetName() 
166 {
167   TCollection_AsciiString aStr = "";
168   Handle(SALOMEDSImpl_AttributeName) aName;
169   if (_lab.FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aName)) {
170     aStr = aName->Value();
171   }
172   return aStr;
173 }
174
175 //============================================================================
176 /*! Function : GetComment
177  *  Purpose  : 
178  */
179 //============================================================================
180 TCollection_AsciiString SALOMEDSImpl_SObject::GetComment() 
181 {
182   TCollection_AsciiString aStr = "";
183   Handle(SALOMEDSImpl_AttributeComment) aComment;
184   if (_lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID(), aComment)) {
185     aStr = aComment->Value();
186   }
187   return aStr;
188 }
189
190 //============================================================================
191 /*! Function : GetIOR
192  *  Purpose  : 
193  */
194 //============================================================================
195 TCollection_AsciiString SALOMEDSImpl_SObject::GetIOR() 
196 {
197   TCollection_AsciiString aStr = "";
198   Handle(SALOMEDSImpl_AttributeIOR) anIOR;
199   if (_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anIOR)) {
200     aStr = anIOR->Value();
201   }
202   return aStr;
203 }
204
205
206 Standard_GUID SALOMEDSImpl_SObject::GetGUID(const TCollection_AsciiString& theType) 
207 {
208   __AttributeTypeToGUIDForSObject
209
210   if (strncmp(theType.ToCString(), "AttributeTreeNodeGUID",21) == 0) {
211     const char* aCType = theType.ToCString();
212     char* aGUIDString = new char[40]; 
213     sprintf(aGUIDString, &(aCType[21]));
214     Standard_GUID aGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
215     delete(aGUIDString);
216     return aGUID;
217   }
218   return Standard_GUID();
219 }
220
221