1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_AttributeParameter.cxx
21 // Author : Sergey RUIN
25 #include "SALOMEDSImpl_AttributeParameter.hxx"
26 #include <Standard_Failure.hxx>
36 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeParameter, SALOMEDSImpl_GenericAttribute )
37 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeParameter, SALOMEDSImpl_GenericAttribute )
40 // Purpose: Each character in the string is replaced by 3 characters: '%' and hex number
41 // of the character (2 characters)
42 string convertString(const string& S)
44 int length = S.size();
45 const char *s = S.c_str();
46 char *c = new char[3], *buffer = new char[length*3+1];
47 buffer[length*3] = (char)0;
48 for(int i = 0, pos = 0; i<length; i++, pos+=3) {
51 sprintf(c, "%x", val);
62 //Restors a string converted by the function convertString
63 string restoreString(const string& S)
65 int length = S.size();
66 char *c = new char[3], *buffer = new char[length/3+1];
67 buffer[length/3] = (char)0;
68 const char *s = S.c_str();
69 for(int i = 0, pos = 0; i<length; i+=3, pos++) {
73 int val = strtol(c, NULL, 16);
74 buffer[pos] = (char)val;
83 //=======================================================================
86 * Purpose : Returns GUID of the attribute
88 //=======================================================================
89 const Standard_GUID& SALOMEDSImpl_AttributeParameter::GetID ()
91 static Standard_GUID ParemeterID ("BA75F3A1-E40B-46b8-8D24-B1D3C3BB1A8C");
95 //=======================================================================
98 * Purpose : Adds an attribute to the label
100 //=======================================================================
101 Handle(SALOMEDSImpl_AttributeParameter) SALOMEDSImpl_AttributeParameter::Set (const TDF_Label& L)
103 Handle(SALOMEDSImpl_AttributeParameter) A;
104 if (!L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID(), A)) {
105 A = new SALOMEDSImpl_AttributeParameter();
112 //=======================================================================
115 * Purpose : Associates a integer value with the ID
117 //=======================================================================
118 void SALOMEDSImpl_AttributeParameter::SetInt(const string& theID, const int& theValue)
122 if(theID.size() == 0) return;
126 _ints[theID] = theValue;
131 //=======================================================================
134 * Purpose : Returns a int value associated with the given ID
136 //=======================================================================
137 int SALOMEDSImpl_AttributeParameter::GetInt(const string& theID)
139 if(!IsSet(theID, PT_INTEGER)) Standard_Failure::Raise("Invalid ID");
143 //=======================================================================
146 * Purpose : Associates a double value with the ID
148 //=======================================================================
149 void SALOMEDSImpl_AttributeParameter::SetReal(const string& theID, const double& theValue)
153 if(theID.size() == 0) return;
157 _reals[theID] = theValue;
162 //=======================================================================
165 * Purpose : Returns a double value associated with the given ID
167 //=======================================================================
168 double SALOMEDSImpl_AttributeParameter::GetReal(const string& theID)
170 if(!IsSet(theID, PT_REAL)) Standard_Failure::Raise("Invalid ID");
171 return _reals[theID];
174 //=======================================================================
176 * Function : SetString
177 * Purpose : Associates a string with the ID
179 //=======================================================================
180 void SALOMEDSImpl_AttributeParameter::SetString(const string& theID, const string& theValue)
184 if(theID.size() == 0) return;
188 _strings[theID] = theValue;
193 //=======================================================================
195 * Function : GetString
196 * Purpose : Returns a string associated with the given ID
198 //=======================================================================
199 string SALOMEDSImpl_AttributeParameter::GetString(const string& theID)
201 if(!IsSet(theID, PT_STRING)) Standard_Failure::Raise("Invalid ID");
202 return _strings[theID];
205 //=======================================================================
208 * Purpose : Associates a bool value with the ID
210 //=======================================================================
211 void SALOMEDSImpl_AttributeParameter::SetBool(const string& theID, const bool& theValue)
215 if(theID.size() == 0) return;
219 _bools[theID] = theValue;
224 //=======================================================================
227 * Purpose : Returns a bool value associated with the ID
229 //=======================================================================
230 bool SALOMEDSImpl_AttributeParameter::GetBool(const string& theID)
232 if(!IsSet(theID, PT_BOOLEAN)) Standard_Failure::Raise("Invalid ID");
233 return _bools[theID];
236 //=======================================================================
238 * Function : SetRealArray
239 * Purpose : Associates an array of double values with the given ID
241 //=======================================================================
242 void SALOMEDSImpl_AttributeParameter::SetRealArray(const string& theID, const vector<double>& theArray)
246 if(theID.size() == 0) return;
250 _realarrays[theID] = theArray;
255 //=======================================================================
257 * Function : GetRealArray
258 * Purpose : Returns double values associated with the ID
260 //=======================================================================
261 vector<double> SALOMEDSImpl_AttributeParameter::GetRealArray(const string& theID)
263 if(!IsSet(theID, PT_REALARRAY)) Standard_Failure::Raise("Invalid ID");
264 return _realarrays[theID];
268 //=======================================================================
270 * Function : SetIntArray
271 * Purpose : Associates an array of int values with the given ID
273 //=======================================================================
274 void SALOMEDSImpl_AttributeParameter::SetIntArray(const string& theID, const vector<int>& theArray)
278 if(theID.size() == 0) return;
282 _intarrays[theID] = theArray;
287 //=======================================================================
289 * Function : GetIntArray
290 * Purpose : Returns int values associated with the ID
292 //=======================================================================
293 vector<int> SALOMEDSImpl_AttributeParameter::GetIntArray(const string& theID)
295 if(!IsSet(theID, PT_INTARRAY)) Standard_Failure::Raise("Invalid ID");
296 return _intarrays[theID];
300 //=======================================================================
302 * Function : SetStrArray
303 * Purpose : Associates an array of string values with the given ID
305 //=======================================================================
306 void SALOMEDSImpl_AttributeParameter::SetStrArray(const string& theID, const vector<string>& theArray)
310 if(theID.size() == 0) return;
314 _strarrays[theID] = theArray;
319 //=======================================================================
321 * Function : GetStrArray
322 * Purpose : Returns string values associated with the ID
324 //=======================================================================
325 vector<string> SALOMEDSImpl_AttributeParameter::GetStrArray(const string& theID)
327 if(!IsSet(theID, PT_STRARRAY)) Standard_Failure::Raise("Invalid ID");
328 return _strarrays[theID];
332 //=======================================================================
335 * Purpose : Returns true if for the ID of given type was assigned \n
336 * a value in the attribute
338 //=======================================================================
339 bool SALOMEDSImpl_AttributeParameter::IsSet(const string& theID, const Parameter_Types theType)
343 if(_ints.find(theID) != _ints.end()) return true;
347 if(_reals.find(theID) != _reals.end()) return true;
351 if(_bools.find(theID) != _bools.end()) return true;
355 if(_strings.find(theID) != _strings.end()) return true;
359 if(_realarrays.find(theID) != _realarrays.end()) return true;
363 if(_intarrays.find(theID) != _intarrays.end()) return true;
367 if(_strarrays.find(theID) != _strarrays.end()) return true;
370 default: return false;
376 //=======================================================================
378 * Function : RemoveID
379 * Purpose : Removes a parameter with given ID
381 //=======================================================================
382 bool SALOMEDSImpl_AttributeParameter::RemoveID(const string& theID, const Parameter_Types theType)
389 if(_ints.erase(theID)) return true;
393 if(_reals.erase(theID)) return true;
397 if(_bools.erase(theID)) return true;
401 if(_strings.erase(theID)) return true;
405 if(_realarrays.erase(theID)) return true;
409 if(_intarrays.erase(theID)) return true;
413 if(_strarrays.erase(theID)) return true;
416 default: return false;
424 //=======================================================================
426 * Function : GetFather
427 * Purpose : Returns a father attribute for this attribute
429 //=======================================================================
430 Handle(SALOMEDSImpl_AttributeParameter) SALOMEDSImpl_AttributeParameter::GetFather()
432 Handle(SALOMEDSImpl_AttributeParameter) aFather;
433 TDF_Label L = Label();
434 if(L.IsRoot()) return aFather;
438 if(L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID(), aFather)) break;
444 //=======================================================================
446 * Function : HasFather
447 * Purpose : Returns True if the attribute has a father attribute
449 //=======================================================================
450 bool SALOMEDSImpl_AttributeParameter::HasFather()
452 TDF_Label L = Label();
453 if(L.IsRoot()) return false;
456 if(L.IsAttribute(SALOMEDSImpl_AttributeParameter::GetID())) return true;
462 //=======================================================================
465 * Purpose : Returns True is the attribute is highest in an hierachy
467 //=======================================================================
468 bool SALOMEDSImpl_AttributeParameter::IsRoot()
470 return ((HasFather())?false:true);
473 //=======================================================================
476 * Purpose : Clears the content of the attribute
478 //=======================================================================
479 void SALOMEDSImpl_AttributeParameter::Clear()
494 //=======================================================================
497 * Purpose : Returns an array of all ID's of the given type
499 //=======================================================================
500 vector<string> SALOMEDSImpl_AttributeParameter::GetIDs(const Parameter_Types theType)
503 vector<string> anArray;
509 anArray.resize(_ints.size());
510 for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++, i++)
511 anArray[i] = p->first;
517 anArray.resize(_reals.size());
518 for(map<string,double>::const_iterator p = _reals.begin(); p != _reals.end(); p++, i++)
519 anArray[i] = p->first;
525 anArray.resize(_bools.size());
526 for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++, i++)
527 anArray[i] = p->first;
532 if(_strings.size()) {
533 anArray.resize(_strings.size());
534 for(map<string,string>::const_iterator p = _strings.begin(); p!= _strings.end(); p++)
535 anArray[i] = p->first;
540 if(_realarrays.size()) {
541 anArray.resize(_realarrays.size());
542 for(map< string, vector<double> >::const_iterator p = _realarrays.begin(); p!= _realarrays.end(); p++)
543 anArray[i] = p->first;
548 if(_intarrays.size()) {
549 anArray.resize(_intarrays.size());
550 for(map< string, vector<int> >::const_iterator p = _intarrays.begin(); p!= _intarrays.end(); p++)
551 anArray[i] = p->first;
556 if(_strarrays.size()) {
557 anArray.resize(_strarrays.size());
558 for(map< string, vector<string> >::const_iterator p = _strarrays.begin(); p!= _strarrays.end(); p++)
559 anArray[i] = p->first;
563 default: return anArray;
569 //=======================================================================
572 * Purpose : Rteurns an GUID of the attribute
574 //=======================================================================
575 const Standard_GUID& SALOMEDSImpl_AttributeParameter::ID () const { return GetID(); }
578 Handle(TDF_Attribute) SALOMEDSImpl_AttributeParameter::NewEmpty () const
580 return new SALOMEDSImpl_AttributeParameter();
583 //=======================================================================
586 * Purpose : Restors the content of the attribute from another
588 //=======================================================================
589 void SALOMEDSImpl_AttributeParameter::Restore(const Handle(TDF_Attribute)& with)
591 Handle(SALOMEDSImpl_AttributeParameter) A = Handle(SALOMEDSImpl_AttributeParameter)::DownCast (with);
600 for(map<string,int>::const_iterator p = A->_ints.begin(); p!= A->_ints.end(); p++)
601 if(p->first.size()) _ints[p->first] = p->second;
602 for(map<string,double>::const_iterator p = A->_reals.begin(); p!= A->_reals.end(); p++)
603 if(p->first.size()) _reals[p->first] = p->second;
604 for(map<string,bool>::const_iterator p = A->_bools.begin(); p!= A->_bools.end(); p++)
605 if(p->first.size()) _bools[p->first] = p->second;
606 for(map<string,string>::const_iterator p = A->_strings.begin(); p!= A->_strings.end(); p++)
607 if(p->first.size()) _strings[p->first] = p->second;
608 for(map< string,vector<double> >::const_iterator p = A->_realarrays.begin(); p!= A->_realarrays.end(); p++)
609 if(p->first.size()) _realarrays[p->first] = p->second;
610 for(map< string,vector<int> >::const_iterator p = A->_intarrays.begin(); p!= A->_intarrays.end(); p++)
611 if(p->first.size()) _intarrays[p->first] = p->second;
612 for(map< string,vector<string> >::const_iterator p = A->_strarrays.begin(); p!= A->_strarrays.end(); p++)
613 if(p->first.size()) _strarrays[p->first] = p->second;
616 //=======================================================================
619 * Purpose : Pastes the content of attribute to another
621 //=======================================================================
622 void SALOMEDSImpl_AttributeParameter::Paste (const Handle(TDF_Attribute)& into,
623 const Handle(TDF_RelocationTable)& RT) const
628 //=======================================================================
631 * Purpose : Saves a content of the attribute as a string
633 //=======================================================================
634 TCollection_AsciiString SALOMEDSImpl_AttributeParameter::Save()
637 char *tmpBuffer = new char[255];
639 buffer << _ints.size() << " ";
641 for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++) {
642 buffer << convertString(p->first) << " " << p->second << " ";
645 buffer << _reals.size() << " ";
646 for(map<string,double>::const_iterator p =_reals.begin(); p != _reals.end(); p++) {
647 sprintf(tmpBuffer, "%.64e", p->second);
648 buffer << convertString(p->first) << " " << tmpBuffer << " ";
651 buffer << _bools.size() << " ";
652 for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++) {
653 buffer << convertString(p->first) << " " << p->second << " ";
656 buffer << _strings.size() << " ";
657 for(map<string,string>::const_iterator p = _strings.begin(); p != _strings.end(); p++) {
658 buffer << convertString(p->first) << " " << convertString(p->second) << " ";
661 buffer << _realarrays.size() << " ";
662 for(map< string,vector<double> >::const_iterator p = _realarrays.begin(); p != _realarrays.end(); p++) {
663 vector<double> v(p->second);
664 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
666 for(int i = 0; i<v.size(); i++) {
667 sprintf(tmpBuffer, " %.64e ", v[i]);
672 buffer << _intarrays.size() << " ";
673 for(map< string,vector<int> >::const_iterator p = _intarrays.begin(); p != _intarrays.end(); p++) {
674 vector<int> v(p->second);
675 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
677 for(int i = 0; i<v.size(); i++) {
678 sprintf(tmpBuffer, " %d ", v[i]);
683 buffer << _strarrays.size() << " ";
684 for(map< string,vector<string> >::const_iterator p = _strarrays.begin(); p != _strarrays.end(); p++) {
685 vector<string> v(p->second);
686 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
688 for(int i = 0; i<v.size(); i++) {
689 buffer << " " << convertString(v[i]) << " ";
695 TCollection_AsciiString AS((char*)buffer.rdbuf()->str());
700 //=======================================================================
703 * Purpose : Restores the attribute from the string
705 //=======================================================================
706 void SALOMEDSImpl_AttributeParameter::Load(const TCollection_AsciiString& theValue)
717 istrstream buffer(theValue.ToCString(), strlen(theValue.ToCString()));
724 for(int i = 1; i<=size; i++) {
726 _ints[restoreString(id)] = val;
730 for(int i = 1; i<=size; i++) {
731 buffer >> id >> val2;
732 _reals[restoreString(id)] = val2;
736 for(int i = 1; i<=size; i++) {
738 _bools[restoreString(id)] = val;
742 for(int i = 1; i<=size; i++) {
744 _strings[restoreString(id)] = restoreString(s);
748 for(int i = 1; i<=size; i++) {
752 for(int j = 0; j<val; j++) {
756 _realarrays[restoreString(id)] = v;
760 for(int i = 1; i<=size; i++) {
764 for(int j = 0; j<val; j++) {
768 _intarrays[restoreString(id)] = v;
772 for(int i = 1; i<=size; i++) {
776 for(int j = 0; j<val; j++) {
778 v[j] = restoreString(s);
780 _strarrays[restoreString(id)] = v;