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)
124 _ints[theID] = theValue;
129 //=======================================================================
132 * Purpose : Returns a int value associated with the given ID
134 //=======================================================================
135 int SALOMEDSImpl_AttributeParameter::GetInt(const string& theID)
137 if(!IsSet(theID, PT_INTEGER)) Standard_Failure::Raise("Invalid ID");
141 //=======================================================================
144 * Purpose : Associates a double value with the ID
146 //=======================================================================
147 void SALOMEDSImpl_AttributeParameter::SetReal(const string& theID, const double& theValue)
153 _reals[theID] = theValue;
158 //=======================================================================
161 * Purpose : Returns a double value associated with the given ID
163 //=======================================================================
164 double SALOMEDSImpl_AttributeParameter::GetReal(const string& theID)
166 if(!IsSet(theID, PT_REAL)) Standard_Failure::Raise("Invalid ID");
167 return _reals[theID];
170 //=======================================================================
172 * Function : SetString
173 * Purpose : Associates a string with the ID
175 //=======================================================================
176 void SALOMEDSImpl_AttributeParameter::SetString(const string& theID, const string& theValue)
182 _strings[theID] = theValue;
187 //=======================================================================
189 * Function : GetString
190 * Purpose : Returns a string associated with the given ID
192 //=======================================================================
193 string SALOMEDSImpl_AttributeParameter::GetString(const string& theID)
195 if(!IsSet(theID, PT_STRING)) Standard_Failure::Raise("Invalid ID");
196 return _strings[theID];
199 //=======================================================================
202 * Purpose : Associates a bool value with the ID
204 //=======================================================================
205 void SALOMEDSImpl_AttributeParameter::SetBool(const string& theID, const bool& theValue)
211 _bools[theID] = theValue;
216 //=======================================================================
219 * Purpose : Returns a bool value associated with the ID
221 //=======================================================================
222 bool SALOMEDSImpl_AttributeParameter::GetBool(const string& theID)
224 if(!IsSet(theID, PT_BOOLEAN)) Standard_Failure::Raise("Invalid ID");
225 return _bools[theID];
228 //=======================================================================
230 * Function : SetRealArray
231 * Purpose : Associates an array of double values with the given ID
233 //=======================================================================
234 void SALOMEDSImpl_AttributeParameter::SetRealArray(const string& theID, const vector<double>& theArray)
240 _realarrays[theID] = theArray;
245 //=======================================================================
247 * Function : GetRealArray
248 * Purpose : Returns double values associated with the ID
250 //=======================================================================
251 vector<double> SALOMEDSImpl_AttributeParameter::GetRealArray(const string& theID)
253 if(!IsSet(theID, PT_REALARRAY)) Standard_Failure::Raise("Invalid ID");
254 return _realarrays[theID];
258 //=======================================================================
260 * Function : SetIntArray
261 * Purpose : Associates an array of int values with the given ID
263 //=======================================================================
264 void SALOMEDSImpl_AttributeParameter::SetIntArray(const string& theID, const vector<int>& theArray)
270 _intarrays[theID] = theArray;
275 //=======================================================================
277 * Function : GetIntArray
278 * Purpose : Returns int values associated with the ID
280 //=======================================================================
281 vector<int> SALOMEDSImpl_AttributeParameter::GetIntArray(const string& theID)
283 if(!IsSet(theID, PT_INTARRAY)) Standard_Failure::Raise("Invalid ID");
284 return _intarrays[theID];
288 //=======================================================================
290 * Function : SetStrArray
291 * Purpose : Associates an array of string values with the given ID
293 //=======================================================================
294 void SALOMEDSImpl_AttributeParameter::SetStrArray(const string& theID, const vector<string>& theArray)
300 _strarrays[theID] = theArray;
305 //=======================================================================
307 * Function : GetStrArray
308 * Purpose : Returns string values associated with the ID
310 //=======================================================================
311 vector<string> SALOMEDSImpl_AttributeParameter::GetStrArray(const string& theID)
313 if(!IsSet(theID, PT_STRARRAY)) Standard_Failure::Raise("Invalid ID");
314 return _strarrays[theID];
318 //=======================================================================
321 * Purpose : Returns true if for the ID of given type was assigned \n
322 * a value in the attribute
324 //=======================================================================
325 bool SALOMEDSImpl_AttributeParameter::IsSet(const string& theID, const Parameter_Types theType)
329 if(_ints.find(theID) != _ints.end()) return true;
333 if(_reals.find(theID) != _reals.end()) return true;
337 if(_bools.find(theID) != _bools.end()) return true;
341 if(_strings.find(theID) != _strings.end()) return true;
345 if(_realarrays.find(theID) != _realarrays.end()) return true;
349 if(_intarrays.find(theID) != _intarrays.end()) return true;
353 if(_strarrays.find(theID) != _strarrays.end()) return true;
356 default: return false;
362 //=======================================================================
364 * Function : RemoveID
365 * Purpose : Removes a parameter with given ID
367 //=======================================================================
368 bool SALOMEDSImpl_AttributeParameter::RemoveID(const string& theID, const Parameter_Types theType)
375 if(_ints.erase(theID)) return true;
379 if(_reals.erase(theID)) return true;
383 if(_bools.erase(theID)) return true;
387 if(_strings.erase(theID)) return true;
391 if(_realarrays.erase(theID)) return true;
395 if(_intarrays.erase(theID)) return true;
399 if(_strarrays.erase(theID)) return true;
402 default: return false;
410 //=======================================================================
412 * Function : GetFather
413 * Purpose : Returns a father attribute for this attribute
415 //=======================================================================
416 Handle(SALOMEDSImpl_AttributeParameter) SALOMEDSImpl_AttributeParameter::GetFather()
418 Handle(SALOMEDSImpl_AttributeParameter) aFather;
419 TDF_Label L = Label();
420 if(L.IsRoot()) return aFather;
424 if(L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID(), aFather)) break;
430 //=======================================================================
432 * Function : HasFather
433 * Purpose : Returns True if the attribute has a father attribute
435 //=======================================================================
436 bool SALOMEDSImpl_AttributeParameter::HasFather()
438 TDF_Label L = Label();
439 if(L.IsRoot()) return false;
442 if(L.IsAttribute(SALOMEDSImpl_AttributeParameter::GetID())) return true;
448 //=======================================================================
451 * Purpose : Returns True is the attribute is highest in an hierachy
453 //=======================================================================
454 bool SALOMEDSImpl_AttributeParameter::IsRoot()
456 return ((HasFather())?false:true);
459 //=======================================================================
462 * Purpose : Clears the content of the attribute
464 //=======================================================================
465 void SALOMEDSImpl_AttributeParameter::Clear()
480 //=======================================================================
483 * Purpose : Returns an array of all ID's of the given type
485 //=======================================================================
486 vector<string> SALOMEDSImpl_AttributeParameter::GetIDs(const Parameter_Types theType)
489 vector<string> anArray;
495 anArray.resize(_ints.size());
496 for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++, i++)
497 anArray[i] = p->first;
503 anArray.resize(_reals.size());
504 for(map<string,double>::const_iterator p = _reals.begin(); p != _reals.end(); p++, i++)
505 anArray[i] = p->first;
511 anArray.resize(_bools.size());
512 for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++, i++)
513 anArray[i] = p->first;
518 if(_strings.size()) {
519 anArray.resize(_strings.size());
520 for(map<string,string>::const_iterator p = _strings.begin(); p!= _strings.end(); p++)
521 anArray[i] = p->first;
526 if(_realarrays.size()) {
527 anArray.resize(_realarrays.size());
528 for(map< string, vector<double> >::const_iterator p = _realarrays.begin(); p!= _realarrays.end(); p++)
529 anArray[i] = p->first;
534 if(_intarrays.size()) {
535 anArray.resize(_intarrays.size());
536 for(map< string, vector<int> >::const_iterator p = _intarrays.begin(); p!= _intarrays.end(); p++)
537 anArray[i] = p->first;
542 if(_strarrays.size()) {
543 anArray.resize(_strarrays.size());
544 for(map< string, vector<string> >::const_iterator p = _strarrays.begin(); p!= _strarrays.end(); p++)
545 anArray[i] = p->first;
549 default: return anArray;
555 //=======================================================================
558 * Purpose : Rteurns an GUID of the attribute
560 //=======================================================================
561 const Standard_GUID& SALOMEDSImpl_AttributeParameter::ID () const { return GetID(); }
564 Handle(TDF_Attribute) SALOMEDSImpl_AttributeParameter::NewEmpty () const
566 return new SALOMEDSImpl_AttributeParameter();
569 //=======================================================================
572 * Purpose : Restors the content of the attribute from another
574 //=======================================================================
575 void SALOMEDSImpl_AttributeParameter::Restore(const Handle(TDF_Attribute)& with)
577 Handle(SALOMEDSImpl_AttributeParameter) A = Handle(SALOMEDSImpl_AttributeParameter)::DownCast (with);
586 for(map<string,int>::const_iterator p = A->_ints.begin(); p!= A->_ints.end(); p++) _ints[p->first] = p->second;
587 for(map<string,double>::const_iterator p = A->_reals.begin(); p!= A->_reals.end(); p++) _reals[p->first] = p->second;
588 for(map<string,bool>::const_iterator p = A->_bools.begin(); p!= A->_bools.end(); p++) _bools[p->first] = p->second;
589 for(map<string,string>::const_iterator p = A->_strings.begin(); p!= A->_strings.end(); p++)
590 _strings[p->first] = p->second;
591 for(map< string,vector<double> >::const_iterator p = A->_realarrays.begin(); p!= A->_realarrays.end(); p++)
592 _realarrays[p->first] = p->second;
593 for(map< string,vector<int> >::const_iterator p = A->_intarrays.begin(); p!= A->_intarrays.end(); p++)
594 _intarrays[p->first] = p->second;
595 for(map< string,vector<string> >::const_iterator p = A->_strarrays.begin(); p!= A->_strarrays.end(); p++)
596 _strarrays[p->first] = p->second;
599 //=======================================================================
602 * Purpose : Pastes the content of attribute to another
604 //=======================================================================
605 void SALOMEDSImpl_AttributeParameter::Paste (const Handle(TDF_Attribute)& into,
606 const Handle(TDF_RelocationTable)& RT) const
611 //=======================================================================
614 * Purpose : Saves a content of the attribute as a string
616 //=======================================================================
617 TCollection_AsciiString SALOMEDSImpl_AttributeParameter::Save()
620 char *tmpBuffer = new char[255];
622 buffer << _ints.size() << " ";
624 for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++) {
625 buffer << convertString(p->first) << " " << p->second << " ";
628 buffer << _reals.size() << " ";
629 for(map<string,double>::const_iterator p =_reals.begin(); p != _reals.end(); p++) {
630 sprintf(tmpBuffer, "%.64e", p->second);
631 buffer << convertString(p->first) << " " << tmpBuffer << " ";
634 buffer << _bools.size() << " ";
635 for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++) {
636 buffer << convertString(p->first) << " " << p->second << " ";
639 buffer << _strings.size() << " ";
640 for(map<string,string>::const_iterator p = _strings.begin(); p != _strings.end(); p++) {
641 buffer << convertString(p->first) << " " << convertString(p->second) << " ";
644 buffer << _realarrays.size() << " ";
645 for(map< string,vector<double> >::const_iterator p = _realarrays.begin(); p != _realarrays.end(); p++) {
646 vector<double> v(p->second);
647 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
649 for(int i = 0; i<v.size(); i++) {
650 sprintf(tmpBuffer, " %.64e ", v[i]);
655 buffer << _intarrays.size() << " ";
656 for(map< string,vector<int> >::const_iterator p = _intarrays.begin(); p != _intarrays.end(); p++) {
657 vector<int> v(p->second);
658 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
660 for(int i = 0; i<v.size(); i++) {
661 sprintf(tmpBuffer, " %d ", v[i]);
666 buffer << _strarrays.size() << " ";
667 for(map< string,vector<string> >::const_iterator p = _strarrays.begin(); p != _strarrays.end(); p++) {
668 vector<string> v(p->second);
669 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
671 for(int i = 0; i<v.size(); i++) {
672 buffer << " " << convertString(v[i]) << " ";
678 TCollection_AsciiString AS((char*)buffer.rdbuf()->str());
683 //=======================================================================
686 * Purpose : Restores the attribute from the string
688 //=======================================================================
689 void SALOMEDSImpl_AttributeParameter::Load(const TCollection_AsciiString& theValue)
700 istrstream buffer(theValue.ToCString(), strlen(theValue.ToCString()));
707 for(int i = 1; i<=size; i++) {
709 _ints[restoreString(id)] = val;
713 for(int i = 1; i<=size; i++) {
714 buffer >> id >> val2;
715 _reals[restoreString(id)] = val2;
719 for(int i = 1; i<=size; i++) {
721 _bools[restoreString(id)] = val;
725 for(int i = 1; i<=size; i++) {
727 _strings[restoreString(id)] = restoreString(s);
731 for(int i = 1; i<=size; i++) {
735 for(int j = 0; j<val; j++) {
739 _realarrays[restoreString(id)] = v;
743 for(int i = 1; i<=size; i++) {
747 for(int j = 0; j<val; j++) {
751 _intarrays[restoreString(id)] = v;
755 for(int i = 1; i<=size; i++) {
759 for(int j = 0; j<val; j++) {
761 v[j] = restoreString(s);
763 _strarrays[restoreString(id)] = v;