2 // Copyright (c) 1998-1999 Matra Datavision
3 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <PlotMgt.hxx>
22 #include <PlotMgt_PlotterParameter.ixx>
23 #include <PlotMgt_PlotterTools.hxx>
25 // Removes str at index <idx2> and puts it before <idx1>
26 #define SWAP_STR(idx1,idx2) \
28 TCollection_AsciiString str; \
29 str = myDescription -> Value(idx2); \
30 myDescription -> Remove (idx2); \
31 myDescription -> InsertBefore (idx1, str); \
34 #define STRING_TRIM(aString) \
35 aString.LeftAdjust(); \
36 aString.RightAdjust();
38 #define FLAG_DIALOG 0x00000001
39 #define FLAG_MINVAL 0x00000002
40 #define FLAG_MAXVAL 0x00000004
41 #define FLAG_VALUES 0x00000008
42 #define FLAG_LENGTH 0x00000010
43 #define FLAG_DEFVAL 0x00000020
44 #define FLAG_NTYPE 0x00000040
46 //============================================================================
47 PlotMgt_PlotterParameter::PlotMgt_PlotterParameter (const TCollection_AsciiString& aName)
51 myType = PlotMgt_TOPP_Undefined;
53 myState = Standard_False;
54 myConfigState = Standard_False;
55 myIsModified = Standard_False;
56 //--------------------------------------------------------------
64 myMap = new TColStd_HSequenceOfAsciiString ();
65 myDescription = new TColStd_HSequenceOfAsciiString ();
66 //---- Find parameter's index in __PossibleParameters array ----
67 Standard_Integer i = 0;
68 while (__PossibleParameters[i]._new_name != NULL) {
69 if ( myName == __PossibleParameters[i]._new_name ||
70 (__PossibleParameters[i]._old_name && myName == __PossibleParameters[i]._old_name))
72 if (__PossibleParameters[i]._old_name)
73 myOldName = __PossibleParameters[i]._old_name;
74 myName = __PossibleParameters[i]._new_name;
81 cout << "PlotMgt_PlotterParameter WARNING: Unknown parameter'"
82 << myName << "'\n" << flush;
87 //============================================================================
88 #define PUT_INFO(aSign,aValue) { \
89 sprintf(aBuffer,"%s%s%s%s\n", myName.ToCString(), (aSign), \
90 _DELIM_SIGN, aValue.ToCString()); \
93 //============================================================================
94 Standard_Boolean PlotMgt_PlotterParameter::Save (OSD_File& aFile)
97 TCollection_AsciiString aLine = "";
98 if (NeedToBeSaved ()) {
99 //-----------------------------------------------------
100 if (!myOldName.IsEmpty ()) {
101 sprintf (aBuffer, "! %s (%s) parameter\n", myName.ToCString(),
102 myOldName.ToCString());
105 //------------ Write Type info ------------------------
106 if (myFlags & FLAG_NTYPE ) PUT_INFO(_TYPE_SIGN, PlotMgt::StringFromType (myType));
107 //------------ Write dialog info ----------------------
108 if (myFlags & FLAG_DIALOG) PUT_INFO(_DIALOG_SIGN, myDialog);
109 //------------ Write minval info ----------------------
110 if (myFlags & FLAG_MINVAL) PUT_INFO(_MINVAL_SIGN, myMinValue);
111 //------------ Write maxval info ----------------------
112 if (myFlags & FLAG_MAXVAL) PUT_INFO(_MAXVAL_SIGN, myMaxValue);
113 //------------ Write values info ----------------------
114 if (myFlags & FLAG_VALUES) PUT_INFO(_VALUES_SIGN, myValues);
115 //------------ Write defavl info ----------------------
116 if (myFlags & FLAG_DEFVAL) PUT_INFO("", myDefValue);
117 //------------ Write map info -------------------------
118 if (myFlags & FLAG_LENGTH && myMapLength && !myMap.IsNull()) {
119 Standard_Integer i, n = myMap -> Length();
120 aLine += myName; aLine += _LENGTH_SIGN;
121 aLine += _DELIM_SIGN; aLine += TCollection_AsciiString (myMapLength);
123 for (i = 1; i <= n; i++)
124 PUT_INFO("", myMap -> Value (i));
126 //------------ Append one new line --------------------
128 aFile.Write (aLine, aLine.Length ());
129 return (!aFile.Failed ());
131 return Standard_True;
134 //============================================================================
135 void PlotMgt_PlotterParameter::SetType (const PlotMgt_TypeOfPlotterParameter aType)
140 //============================================================================
141 void PlotMgt_PlotterParameter::SetState (const Standard_Boolean aState)
146 //============================================================================
147 TCollection_AsciiString PlotMgt_PlotterParameter::Name () const
152 //============================================================================
153 TCollection_AsciiString PlotMgt_PlotterParameter::OldName () const
158 //============================================================================
159 Handle(TColStd_HSequenceOfAsciiString)& PlotMgt_PlotterParameter::Description ()
161 return myDescription;
164 //============================================================================
165 Standard_Boolean PlotMgt_PlotterParameter::NeedToBeSaved () const
167 return (myConfigState && (myType != _T_UNK) && (myIsModified || myState));
170 //============================================================================
171 void PlotMgt_PlotterParameter::Dump () const
173 if (!myState || (myType == _T_UNK) ||
174 !myConfigState || !myIsModified)
177 cout << "!++++++++++++++++++++++++++++++++++++++++++++" << endl << flush;
178 cout << "! " << myName << " ";
179 if (!myOldName.IsEmpty())
180 cout << "(" << myOldName << ") ";
181 cout << "parameter" << endl << flush;
182 cout << myName << _TYPE_SIGN << _DELIM_SIGN
183 << PlotMgt::StringFromType (myType) << endl << flush;
184 if (myFlags & FLAG_DIALOG)
185 cout << myName << _DIALOG_SIGN << _DELIM_SIGN << myDialog << endl << flush;
186 if (myFlags & FLAG_MINVAL)
187 cout << myName << _MINVAL_SIGN << _DELIM_SIGN << myMinValue << endl << flush;
188 if (myFlags & FLAG_MAXVAL)
189 cout << myName << _MAXVAL_SIGN << _DELIM_SIGN << myMaxValue << endl << flush;
190 if (myFlags & FLAG_VALUES && (myType == _T_LSTR))
191 cout << myName << _VALUES_SIGN << _DELIM_SIGN << myValues << endl << flush;
192 if (myFlags & FLAG_DEFVAL)
193 cout << myName << _DELIM_SIGN << myDefValue << endl << flush;
194 if (myFlags & FLAG_LENGTH && myMapLength) {
195 cout << myName << _LENGTH_SIGN << _DELIM_SIGN << myMapLength << endl << flush;
196 Standard_Integer i, n = myMap -> Length();
197 for (i = 1; i <= n; i++)
198 cout << myName << _DELIM_SIGN << myMap -> Value (i) << endl << flush;
200 cout << "!++++++++++++++++++++++++++++++++++++++++++++" << endl << flush;
201 cout << endl << flush;
204 //============================================================================
205 void PlotMgt_PlotterParameter::PutCommandInfo (const Aspect_FStream& outStream) const
208 TCollection_AsciiString _setenv_ ("set Plot_");
209 TCollection_AsciiString _comment_ ("REM ######### ");
210 TCollection_AsciiString _delim_ (" = ");
212 TCollection_AsciiString _setenv_ ("setenv Plot_");
213 TCollection_AsciiString _comment_ ("############# ");
214 TCollection_AsciiString _delim_ (" ");
216 *outStream << _comment_ << "Parameter '" << myName << "' of type '"
217 << PlotMgt::StringFromType (myType) << "'" << endl;
219 if (myFlags & FLAG_DEFVAL)
220 *outStream << _setenv_ << myName << _delim_ << "'" << myDefValue << "'" << endl;
222 if (myFlags & FLAG_LENGTH && myMapLength) {
223 *outStream << _setenv_ << myName << "_Length" << _delim_ << myMapLength << endl;
224 Standard_Integer i, n = myMap -> Length();
225 for (i = 1; i <= n; i++)
226 *outStream << _setenv_ << myName << "_" << i << _delim_ << myMap -> Value (i) << endl;
230 //============================================================================
231 Standard_Boolean PlotMgt_PlotterParameter::CheckListValue ()
233 if (!(myFlags & FLAG_VALUES)) {
234 cout << "PlotMgt_PlotterParameter WARNING: Parameter '" << myName
235 << "' has type 'list_string', but does not have [.Values] "
236 << "descriptor" << endl << flush;
237 return Standard_False;
239 TCollection_AsciiString aValues = myValues, aToken, newValues;
240 Standard_Boolean commaFound, defValFound = Standard_False;
241 Standard_Integer idx, res;
242 STRING_TRIM(myDefValue);
243 STRING_TRIM(myValues);
244 if (myValues.IsEmpty()) {
245 if (!myDefValue.IsEmpty()) {
246 cout << "PlotMgt_PlotterParameter WARNING: Parameter '" << myName
247 << "' has type 'list_string', but [.Values] descriptor "
248 << "is empty. Defaulting to '" << myDefValue << "'" << endl << flush;
249 myValues = myDefValue;
250 return Standard_True;
252 cout << "PlotMgt_PlotterParameter WARNING: Parameter '" << myName
253 << "' has type 'list_string', but [.Values] descriptor "
254 << "and default values are empty." << endl << flush;
255 return Standard_False;
259 idx = aValues.Search (",");
260 commaFound = (idx != -1);
262 aToken = aValues.Token (",", 1);
263 res = aValues.Search (aToken);
265 aValues.Remove (1, res - 1);
266 aValues.Remove (1, aToken.Length() + 1);
271 // Still trying to find default value
273 defValFound = (aToken == myDefValue);
274 // Append new values with a token
275 if (!aToken.IsEmpty())
277 if (commaFound && !aToken.IsEmpty())
279 } while (commaFound);
280 // Check the default value
282 cout << "PlotMgt_PlotterParameter WARNING: Parameter '" << myName
283 << "' has type 'list_string'. ";
284 if (myFlags & FLAG_DEFVAL)
285 cout << "But the default value '" << myDefValue << "' is not found. ";
287 cout << "But does not have default value. ";
288 cout << "Defaulting to the first from the list." << endl << flush;
289 idx = newValues.Search (",");
290 if (idx != -1) myDefValue = newValues.Token (",", 1);
291 else myDefValue = newValues;
292 myFlags |= FLAG_DEFVAL;
294 // Use new values as default
295 myValues = newValues;
296 return Standard_True;
299 //============================================================================
300 void PlotMgt_PlotterParameter::ProcessParamVal (
301 const Standard_CString aParamSign,
302 const Standard_Integer aFlag,
303 TCollection_AsciiString& aValue)
305 Standard_Integer i, n = myDescription -> Length ();
306 Standard_Boolean fFound = Standard_False;
307 TCollection_AsciiString searchStr = myName, aLine;
309 searchStr += (Standard_CString ) (aParamSign ? aParamSign : "");
310 // searchStr += (aParamSign ? aParamSign : "");
311 searchStr += _DELIM_SIGN;
314 for (i = n; i >= 1; i--) {
315 aLine = myDescription -> Value(i);
316 Standard_Integer res = aLine.Search(searchStr);
318 aLine.Remove (1, searchStr.Length());
322 fFound = Standard_True;
323 // Remove all other <aParamSign> strings
324 Standard_Integer j = 1;
326 if (myDescription -> Value(j).Search(searchStr) != -1) {
327 myDescription -> Remove (j);
336 //============================================================================
337 void PlotMgt_PlotterParameter::Normalize ()
339 Standard_Integer n, i;
340 TCollection_AsciiString aMapLen;
341 // Quit if the parameter is not found in __PossibleParameters
343 goto _CLEAR_AND_EXIT;
344 // Check parameter's type in __PossibleParameters array
345 if (myType == _T_UNK || myType != __PossibleParameters[myIndex]._type) {
346 cout << "PlotMgt_PlotterParameter WARNING: Incorrect type of '"
347 << myName << "' parameter : '"
348 << PlotMgt::StringFromType (myType) << "' instead of '"
349 << PlotMgt::StringFromType (__PossibleParameters[myIndex]._type)
350 << "'. Using right TYPE.\n" << flush;
351 myType = __PossibleParameters[myIndex]._type;
352 myFlags |= FLAG_NTYPE;
354 // Change old names to new ones (if any)
355 if (!myOldName.IsEmpty ()) {
356 n = myDescription -> Length ();
357 TCollection_AsciiString aLine;
358 for (i = 1; i <= n; i++) {
359 aLine = myDescription -> Value (i);
360 Standard_Integer res = aLine.Search (myOldName);
362 aLine.Remove (1, myOldName.Length());
363 aLine.Insert (1, myName);
364 myDescription -> SetValue (i, aLine);
368 // Normalize parameter (build all necessary values)
369 ProcessParamVal (_DIALOG_SIGN, FLAG_DIALOG, myDialog);
370 ProcessParamVal (_MINVAL_SIGN, FLAG_MINVAL, myMinValue);
371 ProcessParamVal (_MAXVAL_SIGN, FLAG_MAXVAL, myMaxValue);
372 ProcessParamVal (_VALUES_SIGN, FLAG_VALUES, myValues);
373 ProcessParamVal (_LENGTH_SIGN, FLAG_LENGTH, aMapLen);
374 if ((myFlags & FLAG_LENGTH) && aMapLen.IsIntegerValue())
375 myMapLength = aMapLen.IntegerValue();
381 if (!__PossibleParameters[myIndex]._ismap) {
382 ProcessParamVal (NULL, FLAG_DEFVAL, myDefValue);
384 n = myDescription -> Length ();
385 // There must me at least <myMapLen> of default values
386 if (myMapLength > n || myMapLength == 0) {
387 cout << "PlotMgt_PlotterParameter WARNING: Bad Map description: present "
388 << "only " << n << " instead of " << myMapLength << " values in '"
389 << myName << "'" << endl;
390 goto _CLEAR_AND_EXIT;
392 // Put lines from the end of description
393 for (i = n - myMapLength + 1; i <= n; i++)
394 myMap -> Append (myDescription -> Value (i));
395 myFlags &= ~FLAG_DEFVAL;
396 // Leave only map values
397 TCollection_AsciiString remStr = (myName + _DELIM_SIGN), aLine;
398 n = myMap -> Length ();
399 for (i = 1; i <= n; i++) {
400 aLine = myMap -> Value (i);
401 if (aLine.Search(remStr) != -1) {
402 aLine.Remove (1, remStr.Length());
403 myMap -> SetValue (i, aLine);
409 ProcessParamVal (NULL, FLAG_DEFVAL, myDefValue);
414 // Check the configuration
415 if (myFlags & FLAG_DEFVAL && myDefValue.IsEmpty())
416 myFlags &= ~FLAG_DEFVAL;
417 if (myFlags & FLAG_DEFVAL && (myType == _T_INT)) {
418 if (!myDefValue.IsIntegerValue()) {
420 myFlags &= ~FLAG_DEFVAL;
423 if (myFlags & FLAG_DEFVAL && (myType == _T_REAL)) {
424 if (!myDefValue.IsRealValue()) {
426 myFlags &= ~FLAG_DEFVAL;
429 if (myFlags & FLAG_DEFVAL && (myType == _T_BOOL)) {
430 STRING_TRIM(myDefValue);
431 myDefValue.LowerCase ();
432 if (myDefValue.IsEmpty ())
433 myFlags &= ~FLAG_DEFVAL;
435 if (myType == _T_LSTR && !CheckListValue ())
436 goto _CLEAR_AND_EXIT;
437 // Indicate that parameter is configured properly
438 myConfigState = Standard_True;
441 // We do not need it anymore
442 myDescription->Clear ();
443 myDescription.Nullify();
446 //============================================================================
447 //============================================================================
448 //============================================================================
449 #define GET_BAD_TYPE_INFO(aGetType) \
450 cout << "PlotMgt_PlotterParameter ---> WARNING : '" << myName \
451 << "' of type '" << PlotMgt::StringFromType (myType) \
452 << "' requested about " << aGetType << " value" \
455 #define GET_EMPTY_VALUE_INFO(aValue) \
456 cout << "PlotMgt_PlotterParameter ---> WARNING : '" << myName \
457 << "' of type '" << PlotMgt::StringFromType (myType) \
458 << "' has no default value." \
459 << "Defaulting to " << aValue << "." << endl << flush;
461 //============================================================================
462 void PlotMgt_PlotterParameter::SValue (TCollection_AsciiString& aValue) const
465 if ((myType == _T_STR) || (myType == _T_LSTR)) {
466 if (myFlags & FLAG_DEFVAL && !myDefValue.IsEmpty()) {
469 GET_EMPTY_VALUE_INFO("empty string");
471 GET_BAD_TYPE_INFO("STRING");
474 //============================================================================
475 Standard_Boolean PlotMgt_PlotterParameter::BValue () const
477 if (myType == _T_BOOL) {
478 if (myFlags & FLAG_DEFVAL && !myDefValue.IsEmpty()) {
479 if (myDefValue.IsEqual("true") ||
480 myDefValue.IsIntegerValue() && myDefValue.IntegerValue())
481 return Standard_True;
483 GET_EMPTY_VALUE_INFO("FALSE");
485 GET_BAD_TYPE_INFO("BOOLEAN");
486 return Standard_False;
489 //============================================================================
490 Standard_Integer PlotMgt_PlotterParameter::IValue () const
492 if (myType == _T_INT) {
493 if (myFlags & FLAG_DEFVAL) {
494 return myDefValue.IntegerValue ();
496 GET_EMPTY_VALUE_INFO("0");
498 GET_BAD_TYPE_INFO("INTEGER");
502 //============================================================================
503 Standard_Real PlotMgt_PlotterParameter::RValue () const
505 if (myType == _T_REAL) {
506 if (myFlags & FLAG_DEFVAL) {
507 return myDefValue.RealValue ();
509 GET_EMPTY_VALUE_INFO("0.0");
511 GET_BAD_TYPE_INFO("REAL");
515 //============================================================================
516 void PlotMgt_PlotterParameter::LValues (Handle(TColStd_HSequenceOfAsciiString)& aList) const
518 if (!aList.IsNull ()) {
522 aList = new TColStd_HSequenceOfAsciiString ();
523 if (myType != _T_LSTR) {
524 GET_BAD_TYPE_INFO("LIST_STRING");
527 TCollection_AsciiString aToken, aValues = myValues;
528 Standard_Boolean commaFound;
529 Standard_Integer idx;
531 idx = aValues.Search (",");
532 commaFound = (idx != -1);
534 aToken = aValues.Token (",", 1);
535 aValues.Remove (1, aToken.Length() + 1);
539 aList -> Append (aToken);
540 } while (commaFound);
543 //============================================================================
544 Handle(TColStd_HSequenceOfAsciiString) PlotMgt_PlotterParameter::MValue () const
546 if (!(myFlags & FLAG_LENGTH && myMapLength))
547 GET_BAD_TYPE_INFO("MAP_VALUE");
551 //============================================================================
552 //============================================================================
553 //============================================================================
554 #define SET_VALUE() \
555 myDefValue = aValue; \
556 myFlags = FLAG_DEFVAL; \
557 myIsModified = Standard_True;
558 // myFlags != FLAG_DEFVAL;
560 #define SET_BAD_TYPE_INFO(aSetType) \
562 cout << "PlotMgt_PlotterParameter ---> WARNING : '" << myName \
563 << "' of type '" << PlotMgt::StringFromType (myType) \
564 << "' requested to set " << aSetType << " value" \
569 //============================================================================
570 void PlotMgt_PlotterParameter::SetSValue (const TCollection_AsciiString& aValue)
572 if (myType != _T_STR && myType != _T_LSTR)
573 SET_BAD_TYPE_INFO("STRING");
577 //============================================================================
578 void PlotMgt_PlotterParameter::SetBValue (const Standard_Boolean aValue)
580 if (myType != _T_BOOL)
581 SET_BAD_TYPE_INFO("BOOL");
583 myDefValue = (Standard_CString ) (aValue ? "true" : "false");
584 // myDefValue = (aValue ? "true" : "false");
585 // myFlags != FLAG_DEFVAL;
586 myFlags = FLAG_DEFVAL;
587 myIsModified = Standard_True;
590 //============================================================================
591 void PlotMgt_PlotterParameter::SetIValue (const Standard_Integer aValue)
593 if (myType != _T_INT)
594 SET_BAD_TYPE_INFO("INTEGER");
598 //============================================================================
599 void PlotMgt_PlotterParameter::SetRValue (const Standard_Real aValue)
601 if (myType != _T_REAL)
602 SET_BAD_TYPE_INFO("REAL");
606 //============================================================================
607 void PlotMgt_PlotterParameter::SetMValue (const Handle(TColStd_HSequenceOfAsciiString)& aMap)
609 if (!(myFlags & FLAG_LENGTH && myMapLength))
610 SET_BAD_TYPE_INFO("MAP_VALUE");
612 myMapLength = myMap -> Length ();
613 myIsModified = Standard_True;