1 // SALOME SALOMEDS : data structure of SALOME and sources of Salome data server
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOMEDS_SObject_i.cxx
25 // Author : Yves FRICAUD
29 #include <TDF_Tool.hxx>
30 #include <TDF_Attribute.hxx>
31 #include <TDF_Reference.hxx>
32 #include <Standard_GUID.hxx>
33 #include <Standard_NoSuchObject.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <TDataStd_Comment.hxx>
36 #include <TDataStd_Integer.hxx>
37 #include <TDataStd_Real.hxx>
38 #include <TDataStd_TreeNode.hxx>
39 #include <TDataStd_UAttribute.hxx>
41 #include <TCollection_AsciiString.hxx>
42 #include <TDF_AttributeIterator.hxx>
44 #include "SALOMEDS_SObject_i.hxx"
47 #include "SALOMEDS_Study_i.hxx"
48 #include "SALOMEDS_StudyManager_i.hxx"
49 #include "SALOMEDS_SComponent_i.hxx"
51 #include "SALOMEDS_AttributeComment_i.hxx"
53 #include "SALOMEDS_AttributeTreeNode_i.hxx"
54 #include "SALOMEDS_AttributeUserID_i.hxx"
56 #include "SALOMEDS_AttributePersistentRef_i.hxx"
57 #include "SALOMEDS_AttributeIOR_i.hxx"
58 #include "SALOMEDS_AttributeExternalFileDef_i.hxx"
59 #include "SALOMEDS_AttributeFileType_i.hxx"
60 #include "SALOMEDS_AttributeName_i.hxx"
61 #include "SALOMEDS_AttributeSequenceOfInteger_i.hxx"
62 #include "SALOMEDS_AttributeSequenceOfReal_i.hxx"
63 #include "SALOMEDS_AttributeTableOfInteger_i.hxx"
64 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
65 #include "SALOMEDS_AttributeTableOfString_i.hxx"
66 #include "SALOMEDS_AttributeInteger_i.hxx"
67 #include "SALOMEDS_AttributeReal_i.hxx"
68 #include "SALOMEDS_AttributeDrawable_i.hxx"
69 #include "SALOMEDS_AttributeSelectable_i.hxx"
70 #include "SALOMEDS_AttributeExpandable_i.hxx"
71 #include "SALOMEDS_AttributeOpened_i.hxx"
72 #include "SALOMEDS_AttributeTextColor_i.hxx"
73 #include "SALOMEDS_AttributeTextHighlightColor_i.hxx"
74 #include "SALOMEDS_AttributePixMap_i.hxx"
75 #include "SALOMEDS_AttributeTarget_i.hxx"
76 #include "SALOMEDS_AttributeLocalID_i.hxx"
77 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
78 #include "SALOMEDS_AttributePythonObject_i.hxx"
80 #include "SALOMEDS_AttributeGraphic_i.hxx"
81 #include "SALOMEDS_AttributeFlags_i.hxx"
83 #include "Utils_ExceptHandlers.hxx"
84 UNEXPECT_CATCH(GALockProtection, SALOMEDS::GenericAttribute::LockProtection);
86 #include "utilities.h"
89 using namespace SALOMEDS;
92 inline bool operator<(const Standard_GUID& theLeft, const Standard_GUID& theRight)
95 theLeft.ToCString(aLeft);
98 theRight.ToCString(aRight);
100 return strcmp(aLeft,aRight) < 0;
106 const char* Str(const TCollection_ExtendedString& theString)
108 return TCollection_AsciiString(theString).ToCString();
111 typedef std::string TAttributeID;
113 typedef Standard_GUID (*TGetGUID)();
114 typedef bool (*TIsCheckLockedStudy)();
115 typedef Handle(TDF_Attribute) (*TNewAttribute)();
116 typedef SALOMEDS_GenericAttribute_i* (*TNewInstance)(const Handle(TDF_Attribute)&, SALOMEDS_SObject_i*);
119 TAttrFun(const TGetGUID& theGetGUID,
120 const TIsCheckLockedStudy& theIsCheckLockedStudy,
121 const TNewAttribute& theNewAttribute,
122 const TNewInstance& theNewInstance):
123 myGetGUID(theGetGUID),
124 myIsCheckLockedStudy(theIsCheckLockedStudy),
125 myNewAttribute(theNewAttribute),
126 myNewInstance(theNewInstance)
131 TIsCheckLockedStudy myIsCheckLockedStudy;
132 TNewAttribute myNewAttribute;
133 TNewInstance myNewInstance;
136 typedef std::map<TAttributeID,TAttrFun> TAttrID2FunMap;
137 static TAttrID2FunMap __AttrID2FunMap__;
140 typedef std::map<Standard_GUID,TAttributeID> TGUID2AttrIDMap;
141 static TGUID2AttrIDMap __GUID2AttrIDMap__;
146 #define ADD_ATTRID2FUNMAP_ITEM(theName) \
147 __AttrID2FunMap__.insert( \
148 TAttrID2FunMap::value_type(#theName,TAttrFun( \
149 &(SALOMEDS_##theName##_i::GetGUID), \
150 &(SALOMEDS_##theName##_i::IsCheckLockedStudy), \
151 &(SALOMEDS_##theName##_i::NewAttribute), \
152 &(SALOMEDS_##theName##_i::NewInstance) \
155 ADD_ATTRID2FUNMAP_ITEM(AttributeName);
156 ADD_ATTRID2FUNMAP_ITEM(AttributeComment);
157 ADD_ATTRID2FUNMAP_ITEM(AttributeIOR);
158 ADD_ATTRID2FUNMAP_ITEM(AttributeReal);
159 ADD_ATTRID2FUNMAP_ITEM(AttributeInteger);
160 ADD_ATTRID2FUNMAP_ITEM(AttributeSequenceOfInteger);
161 ADD_ATTRID2FUNMAP_ITEM(AttributeSequenceOfReal);
162 ADD_ATTRID2FUNMAP_ITEM(AttributeTableOfInteger);
163 ADD_ATTRID2FUNMAP_ITEM(AttributeTableOfReal);
164 ADD_ATTRID2FUNMAP_ITEM(AttributeTableOfString);
165 ADD_ATTRID2FUNMAP_ITEM(AttributeLocalID);
166 ADD_ATTRID2FUNMAP_ITEM(AttributePythonObject);
168 ADD_ATTRID2FUNMAP_ITEM(AttributeUserID);
169 ADD_ATTRID2FUNMAP_ITEM(AttributeTreeNode);
171 ADD_ATTRID2FUNMAP_ITEM(AttributePersistentRef);
172 ADD_ATTRID2FUNMAP_ITEM(AttributeDrawable);
173 ADD_ATTRID2FUNMAP_ITEM(AttributeSelectable);
174 ADD_ATTRID2FUNMAP_ITEM(AttributeExpandable);
175 ADD_ATTRID2FUNMAP_ITEM(AttributeOpened);
176 ADD_ATTRID2FUNMAP_ITEM(AttributeTextColor);
177 ADD_ATTRID2FUNMAP_ITEM(AttributeTextHighlightColor);
178 ADD_ATTRID2FUNMAP_ITEM(AttributePixMap);
179 ADD_ATTRID2FUNMAP_ITEM(AttributeTarget);
180 ADD_ATTRID2FUNMAP_ITEM(AttributeStudyProperties);
181 ADD_ATTRID2FUNMAP_ITEM(AttributeExternalFileDef);
182 ADD_ATTRID2FUNMAP_ITEM(AttributeFileType);
184 ADD_ATTRID2FUNMAP_ITEM(AttributeGraphic);
185 ADD_ATTRID2FUNMAP_ITEM(AttributeFlags);
187 TAttrID2FunMap::const_iterator anIter = __AttrID2FunMap__.begin();
188 TAttrID2FunMap::const_iterator anEnd = __AttrID2FunMap__.end();
189 for(; anIter != anEnd; anIter++){
190 const TAttrID2FunMap::key_type& aKey = anIter->first;
191 const TAttrID2FunMap::data_type& aValue = anIter->second;
192 __GUID2AttrIDMap__[aValue.myGetGUID()] = aKey;
195 #undef ADD_ATTRID2FUNMAP_ITEM
200 static bool __IsInitilized__ = Init();
203 //============================================================================
204 bool GetAttrFun(const Standard_GUID& theGUID, TAttrFun& theAttrFun)
206 TGUID2AttrIDMap::const_iterator anIter = __GUID2AttrIDMap__.find(theGUID);
207 if(anIter != __GUID2AttrIDMap__.end())
209 const TAttributeID& anAttributeID = anIter->second;
210 TAttrID2FunMap::const_iterator anIter2 = __AttrID2FunMap__.find(anAttributeID);
211 if(anIter2 != __AttrID2FunMap__.end())
213 theAttrFun = anIter2->second;
221 //============================================================================
222 Standard_GUID GetGUID(const char* theType)
224 TAttrID2FunMap::const_iterator anIter = __AttrID2FunMap__.find(theType);
225 if(anIter != __AttrID2FunMap__.end()){
226 const TAttrID2FunMap::data_type& aValue = anIter->second;
227 return aValue.myGetGUID();
229 // create tree node GUID by name
230 if(strncmp(theType,"AttributeTreeNodeGUID",21) == 0){
231 char aGUIDString[40] = "";
232 sprintf(aGUIDString,&theType[21]);
236 return Standard_GUID();
240 //============================================================================
241 std::string GetType(const Handle(TDF_Attribute)& theAttr)
244 return CORBA::string_dup("");
246 Standard_GUID aGUID = theAttr->ID();
247 TGUID2AttrIDMap::const_iterator anIter = __GUID2AttrIDMap__.find(aGUID);
248 if(anIter != __GUID2AttrIDMap__.end())
250 const TAttributeID& anAttributeID = anIter->second;
251 return anAttributeID;
256 Handle(TDataStd_TreeNode) anAttr = Handle(TDataStd_TreeNode)::DownCast(theAttr);
257 if (!anAttr.IsNull()) {
259 anAttr->ID().ToCString(aGUID);
260 sprintf(aType, "AttributeTreeNodeGUID%s",aGUID);
265 Handle(TDataStd_UAttribute) anAttr = Handle(TDataStd_UAttribute)::DownCast(theAttr);
266 if (!anAttr.IsNull()) {
268 anAttr->ID().ToCString(aGUID);
269 sprintf(aType, "AttributeUserID_%s",aGUID);
278 //============================================================================
279 SALOMEDS_Study_i::TSObjectHolder
280 SALOMEDS_SObject_i::New(SALOMEDS_Study_i* theStudy,
281 const TDF_Label& theLabel)
283 SALOMEDS_Study_i::TSObjectHolder aSObjectHolder;
284 SALOMEDS_Study_i::TSObjectMap& anSObjectMap = theStudy->GetSObjectMap();
285 SALOMEDS_Study_i::TSObjectMap::const_iterator anIter = anSObjectMap.find(theLabel);
286 if(anIter != anSObjectMap.end())
287 aSObjectHolder = anIter->second;
289 TCollection_AsciiString anEntry;
290 TDF_Tool::Entry(theLabel,anEntry);
291 SALOMEDS_SObject_i* aSObject = new SALOMEDS_SObject_i(theStudy,theLabel);
292 aSObjectHolder.first = aSObject;
293 aSObjectHolder.second = aSObject->_this();
294 anSObjectMap[theLabel] = aSObjectHolder;
296 return aSObjectHolder;
300 SALOMEDS_SObject_i::NewPtr(SALOMEDS_Study_i* theStudy,
301 const TDF_Label& theLabel)
303 return New(theStudy,theLabel).first;
306 SALOMEDS::SObject_var
307 SALOMEDS_SObject_i::NewRef(SALOMEDS_Study_i* theStudy,
308 const TDF_Label& theLabel)
310 return New(theStudy,theLabel).second;
313 //============================================================================
314 /*! Function : constructor
317 //============================================================================
318 SALOMEDS_SObject_i::SALOMEDS_SObject_i(SALOMEDS_Study_i* theStudy,
319 const TDF_Label& theLabel):
325 //============================================================================
326 /*! Function : destructor
329 //============================================================================
330 SALOMEDS_SObject_i::~SALOMEDS_SObject_i()
335 //============================================================================
336 CORBA::ORB_var SALOMEDS_SObject_i::GetORB() const
338 return _study->GetORB();
342 //============================================================================
343 PortableServer::POA_var SALOMEDS_SObject_i::GetPOA() const
345 return _study->GetPOA();
349 //============================================================================
353 //============================================================================
354 char* SALOMEDS_SObject_i::GetID()
356 TCollection_AsciiString anEntry;
357 TDF_Tool::Entry(_lab,anEntry);
358 return CORBA::string_dup(anEntry.ToCString());
361 //============================================================================
365 //============================================================================
366 SALOMEDS::SComponent_ptr SALOMEDS_SObject_i::GetFatherComponent()
368 TDF_Label aSCompLabel = _lab;
369 while(!SALOMEDS_SComponent_i::IsA(aSCompLabel) && !aSCompLabel.IsRoot()){
370 aSCompLabel = aSCompLabel.Father();
372 return SALOMEDS_SComponent_i::NewRef(_study,aSCompLabel)._retn();
375 //============================================================================
379 //============================================================================
380 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::GetFather()
382 return SALOMEDS_SObject_i::NewRef(_study,_lab.Father())._retn();
385 //============================================================================
389 //============================================================================
390 SALOMEDS::Study_ptr SALOMEDS_SObject_i::GetStudy()
392 return _study->_this();
395 //============================================================================
396 /*! Function : ReferencedObject
399 //============================================================================
400 CORBA::Boolean SALOMEDS_SObject_i::ReferencedObject(SALOMEDS::SObject_out theSObject)
402 Handle(TDF_Reference) aRef;
403 if (!_lab.FindAttribute(TDF_Reference::GetID(),aRef))
406 theSObject = SALOMEDS_SObject_i::NewRef(_study,aRef->Get())._retn();
410 //============================================================================
414 //============================================================================
415 CORBA::Boolean SALOMEDS_SObject_i::FindSubObject(CORBA::Long theTag, SALOMEDS::SObject_out theSObject)
417 TDF_Label aLabel = _lab.FindChild(theTag,false);
421 theSObject = SALOMEDS_SObject_i::NewRef(_study,aLabel)._retn();
425 //============================================================================
429 //============================================================================
430 char* SALOMEDS_SObject_i::Name()
432 return CORBA::string_dup(_name.c_str());
435 //============================================================================
439 //============================================================================
440 void SALOMEDS_SObject_i::Name(const char* theName)
445 //============================================================================
449 //============================================================================
450 CORBA::Short SALOMEDS_SObject_i::Tag()
455 //============================================================================
459 //============================================================================
460 CORBA::Short SALOMEDS_SObject_i::Depth()
465 //============================================================================
469 //============================================================================
470 CORBA::Object_ptr SALOMEDS_SObject_i::GetObject()
473 Handle(SALOMEDS_IORAttribute) anAttr;
474 if(_lab.FindAttribute(SALOMEDS_IORAttribute::GetID(),anAttr)){
475 CORBA::ORB_var anORB = _study->GetStudyManager()->GetORB();
476 return anORB->string_to_object(Str(anAttr->Get()));
480 return CORBA::Object::_nil();
483 //============================================================================
487 //============================================================================
488 char* SALOMEDS_SObject_i::GetName() {
489 Handle(TDataStd_Name) anAttr;
490 if(_lab.FindAttribute(TDataStd_Name::GetID(),anAttr))
491 return CORBA::string_dup(Str(anAttr->Get()));
493 return CORBA::string_dup("");
496 //============================================================================
500 //============================================================================
501 char* SALOMEDS_SObject_i::GetComment() {
502 Handle(TDataStd_Comment) anAttr;
503 if(_lab.FindAttribute(TDataStd_Comment::GetID(), anAttr))
504 return CORBA::string_dup(Str(anAttr->Get()));
506 return CORBA::string_dup("");
509 //============================================================================
513 //============================================================================
514 char* SALOMEDS_SObject_i::GetIOR() {
515 Handle(SALOMEDS_IORAttribute) anAttr;
516 if(_lab.FindAttribute(SALOMEDS_IORAttribute::GetID(),anAttr))
517 return CORBA::string_dup(Str(anAttr->Get()));
519 return CORBA::string_dup("");
523 //============================================================================
524 /*! Function : GetAllAttributes
525 * Purpose : Returns list of all attributes for this sobject
527 //============================================================================
528 SALOMEDS_GenericAttribute_i*
529 SALOMEDS_SObject_i::_FindGenAttribute(const Handle(TDF_Attribute)& theAttr)
531 SALOMEDS_GenericAttribute_i* anGenAttr = NULL;
533 Standard_GUID aGUID = theAttr->ID();
535 TGUID2AttrIDMap::const_iterator anIter = __GUID2AttrIDMap__.find(aGUID);
536 if(anIter != __GUID2AttrIDMap__.end())
538 const ::TAttributeID& anAttributeID = anIter->second;
539 TAttrMap::const_iterator anIter = myAttrMap.find(anAttributeID);
540 if(anIter != myAttrMap.end())
541 anGenAttr = anIter->second;
543 if(anGenAttr != NULL){
544 if(anGenAttr->GetAttribute() != theAttr)
545 anGenAttr->SetAttribute(theAttr);
547 anGenAttr = _CreateGenAttribute(theAttr,anAttributeID.c_str());
555 SALOMEDS::ListOfAttributes* SALOMEDS_SObject_i::GetAllAttributes()
557 SALOMEDS::ListOfAttributes_var aSeqOfAttr = new SALOMEDS::ListOfAttributes;
558 if(_lab.NbAttributes() > 0){
559 Standard_Integer i = 0;
560 for(TDF_AttributeIterator iter(_lab); iter.More(); iter.Next()) {
561 Handle(TDF_Attribute) anAttr = iter.Value();
562 if(SALOMEDS_GenericAttribute_i* anGenAttr = _FindGenAttribute(anAttr))
564 aSeqOfAttr->length(++i);
565 aSeqOfAttr[i-1] = anGenAttr->_this();
570 return aSeqOfAttr._retn();
574 //============================================================================
575 /*! Function : FindAttribute
576 * Purpose : Find attribute of given type on this SObject
578 //============================================================================
579 SALOMEDS_GenericAttribute_i*
580 SALOMEDS_SObject_i::_CreateGenAttribute(const Handle(TDF_Attribute)& theAttr,
584 TAttrID2FunMap::const_iterator anIter = __AttrID2FunMap__.find(theType);
585 if(anIter != __AttrID2FunMap__.end()){
586 const TAttrID2FunMap::data_type& aValue = anIter->second;
588 if(aValue.myIsCheckLockedStudy())
589 _study->CheckLocked();
591 return aValue.myNewInstance(theAttr,this);
594 if(strncmp(theType,"AttributeTreeNode",17) == 0){
595 return new SALOMEDS_AttributeTreeNode_i(theAttr,this);
598 if(strncmp(theType,"AttributeUserID",15) == 0){
599 return new SALOMEDS_AttributeUserID_i(theAttr,this);
606 SALOMEDS_GenericAttribute_i*
607 SALOMEDS_SObject_i::_FindGenAttribute(const char* theType)
609 SALOMEDS_GenericAttribute_i* anGenAttr = NULL;
610 TAttrMap::const_iterator anIter = myAttrMap.find(theType);
611 if(anIter != myAttrMap.end())
612 anGenAttr = anIter->second;
614 Standard_GUID aGUID = ::GetGUID(theType);
615 Handle(TDF_Attribute) anAttr;
617 if(_lab.FindAttribute(aGUID,anAttr)){
618 if(anGenAttr != NULL){
619 if(anGenAttr->GetAttribute() != anAttr)
620 anGenAttr->SetAttribute(anAttr);
622 anGenAttr = _CreateGenAttribute(anAttr,theType);
624 if(anGenAttr != NULL)
625 myAttrMap[theType] = anGenAttr;
627 myAttrMap.erase(theType);
628 //if(anGenAttr != NULL)
629 // anGenAttr->Destroy();
636 SALOMEDS::GenericAttribute_ptr
637 SALOMEDS_SObject_i::_FindCORBAAttribute(const char* theType)
639 if(SALOMEDS_GenericAttribute_i* anGenAttr = _FindGenAttribute(theType))
640 return anGenAttr->_this();
641 return SALOMEDS::GenericAttribute::_nil();
646 SALOMEDS_SObject_i::FindAttribute(SALOMEDS::GenericAttribute_out theAttribute,
649 theAttribute = _FindCORBAAttribute(theType);
650 return !CORBA::is_nil(theAttribute);
654 //============================================================================
655 /*! Function : FindAttribute
656 * Purpose : Find attribute of given type on this SObject
658 //============================================================================
659 Handle(TDF_Attribute)
660 SALOMEDS_SObject_i::_AddAttribute(const char* theType)
662 Handle(TDF_Attribute) anAttr;
663 TAttrID2FunMap::const_iterator anIter = __AttrID2FunMap__.find(theType);
664 if(anIter != __AttrID2FunMap__.end()){
665 const TAttrID2FunMap::data_type& aValue = anIter->second;
667 if(aValue.myIsCheckLockedStudy())
668 _study->CheckLocked();
670 anAttr = aValue.myNewAttribute();
671 _lab.AddAttribute(anAttr);
675 if(strncmp(theType, "AttributeTreeNode",17) == 0){
677 if(strcmp(theType, "AttributeTreeNode") == 0){
678 aGUID = TDataStd_TreeNode::GetDefaultTreeID();
680 char aString[40] = "";
681 sprintf(aString, &theType[21]);
682 aGUID = Standard_GUID(aString); // create tree node GUID by name
684 if(!_lab.FindAttribute(aGUID,anAttr)){
685 _study->CheckLocked();
686 anAttr = TDataStd_TreeNode::Set(_lab,aGUID);
687 _lab.AddAttribute(anAttr);
692 if(strncmp(theType, "AttributeUserID",15) == 0){
693 Standard_GUID aGUID = SALOMEDS_AttributeUserID_i::GetGUID();
694 if(!_lab.FindAttribute(aGUID,anAttr)){
695 _study->CheckLocked();
696 anAttr = TDataStd_UAttribute::Set(_lab,aGUID);
697 _lab.AddAttribute(anAttr);
707 SALOMEDS::GenericAttribute_ptr
708 SALOMEDS_SObject_i::FindOrCreateAttribute(const char* theType)
710 if(SALOMEDS_GenericAttribute_i* anGenAttr = _FindGenAttribute(theType))
711 return anGenAttr->_this();
712 Handle(TDF_Attribute) anAttr = _AddAttribute(theType);
713 if(!anAttr.IsNull()){
714 if(SALOMEDS_GenericAttribute_i* anGenAttr = _CreateGenAttribute(anAttr,theType)){
715 return anGenAttr->_this();
718 return SALOMEDS::GenericAttribute::_nil();
722 //============================================================================
723 /*! Function : FindAttribute
724 * Purpose : Find attribute of given type on this SObject
726 //============================================================================
727 void SALOMEDS_SObject_i::RemoveAttribute(const char* theType)
729 _study->CheckLocked();
730 if(strcmp(theType, "AttributeIOR") == 0) { // postponed removing of CORBA objects
731 Handle(SALOMEDS_IORAttribute) anAttr;
732 if(_lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
733 _study->AddPostponed(Str(anAttr->Get()));
737 TAttrMap::iterator anIter = myAttrMap.find(theType);
738 if(anIter != myAttrMap.end()){
739 myAttrMap.erase(anIter);
741 _lab.ForgetAttribute(::GetGUID(theType));