0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / ShapeProcess / ShapeProcess_Context.cxx
CommitLineData
b311480e 1// Created on: 2000-08-21
2// Created by: Andrey BETENEV
973c2be1 3// Copyright (c) 2000-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
7fd59977 16
42cf5bc1 17#include <Message.hxx>
18#include <Message_Messenger.hxx>
19#include <Message_ProgressIndicator.hxx>
20#include <Resource_Manager.hxx>
21#include <ShapeProcess_Context.hxx>
7fd59977 22#include <Standard_ErrorHandler.hxx>
23#include <Standard_Failure.hxx>
42cf5bc1 24#include <Standard_Type.hxx>
25#include <TCollection_AsciiString.hxx>
7fd59977 26#include <TCollection_HAsciiString.hxx>
7fd59977 27
42cf5bc1 28#include <sys/stat.h>
25e59720 29IMPLEMENT_STANDARD_RTTIEXT(ShapeProcess_Context,Standard_Transient)
92efcf78 30
7fd59977 31//=======================================================================
32//function : ShapeProcess_Context
33//purpose :
34//=======================================================================
7fd59977 35ShapeProcess_Context::ShapeProcess_Context()
36{
37 myMessenger = Message::DefaultMessenger();
38 myTraceLev = 1;
39}
40
41//=======================================================================
42//function : ShapeProcess_Context
43//purpose :
44//=======================================================================
45
46ShapeProcess_Context::ShapeProcess_Context (const Standard_CString file,
47 const Standard_CString scope)
48{
49 Init ( file, scope );
50 myMessenger = Message::DefaultMessenger();
51 myTraceLev = 1;
52}
53
54//=======================================================================
55//function : Init
56//purpose :
57//=======================================================================
58
59Standard_Boolean ShapeProcess_Context::Init (const Standard_CString file,
60 const Standard_CString scope)
61{
62 myScope.Nullify();
63 myRC = LoadResourceManager ( file );
64 if ( scope && scope[0] ) {
65 SetScope ( scope );
66 }
67 return Standard_True; // myRC->Length() >0; NOT IMPLEMENTED
68}
69
70//=======================================================================
71//function : LoadResourceManager
72//purpose :
73//=======================================================================
74
b6d779d9 75Handle(Resource_Manager) ShapeProcess_Context::LoadResourceManager (const Standard_CString name)
7fd59977 76{
77 // Optimisation of loading resource file: file is load only once
78 // and reloaded only if file date has changed
79 static Handle(Resource_Manager) sRC;
b6d779d9 80 static Standard_Time sMtime, sUMtime;
81 static TCollection_AsciiString sName;
82
83 struct stat buf;
84 Standard_Time aMtime(0), aUMtime(0);
85 TCollection_AsciiString aPath,aUserPath;
86 Resource_Manager::GetResourcePath(aPath,name,Standard_False);
87 Resource_Manager::GetResourcePath(aUserPath,name,Standard_True);
88 if ( !aPath.IsEmpty() )
89 {
90 stat( aPath.ToCString(), &buf );
91 aMtime = (Standard_Time)buf.st_mtime;
92 }
93 if ( !aUserPath.IsEmpty() )
94 {
95 stat( aUserPath.ToCString(), &buf );
96 aUMtime = (Standard_Time)buf.st_mtime;
97 }
98
99 Standard_Boolean isFileModified = Standard_False;
100 if ( !sRC.IsNull() ) {
101 if ( sName.IsEqual ( name ) ) {
102 if ( sMtime != aMtime )
103 {
104 sMtime = aMtime;
105 isFileModified = Standard_True;
106 }
107 if ( sUMtime != aUMtime )
108 {
109 sUMtime = aUMtime;
110 isFileModified = Standard_True;
111 }
112 if (isFileModified)
113 sRC.Nullify();
7fd59977 114 }
b6d779d9 115 else
116 sRC.Nullify();
7fd59977 117 }
118 if ( sRC.IsNull() ) {
0797d9d3 119#ifdef OCCT_DEBUG
7fd59977 120 cout << "Info: ShapeProcess_Context: Reload Resource_Manager: "
b6d779d9 121 << sName.ToCString() << " -> " << name << endl;
7fd59977 122#endif
b6d779d9 123 sRC = new Resource_Manager ( name );
124 if (!isFileModified)
125 {
126 sName = name;
127 sMtime = aMtime;
128 sUMtime = aUMtime;
129 }
7fd59977 130 }
131 return sRC;
132}
133
134//=======================================================================
135//function : ResourceManager
136//purpose :
137//=======================================================================
138
139const Handle(Resource_Manager) &ShapeProcess_Context::ResourceManager () const
140{
141 return myRC;
142}
143
144//=======================================================================
145//function : SetScope
146//purpose :
147//=======================================================================
148
149void ShapeProcess_Context::SetScope (const Standard_CString scope)
150{
151 if ( myScope.IsNull() ) myScope = new TColStd_HSequenceOfHAsciiString;
152 Handle(TCollection_HAsciiString) str;
153 if ( myScope->Length() >0 ) {
154 str = new TCollection_HAsciiString ( myScope->Value ( myScope->Length() ) );
155 str->AssignCat ( "." );
156 str->AssignCat ( scope );
157 }
158 else str = new TCollection_HAsciiString ( scope );
159 myScope->Append ( str );
160}
161
162//=======================================================================
163//function : UnSetScope
164//purpose :
165//=======================================================================
166
167void ShapeProcess_Context::UnSetScope ()
168{
169 if ( ! myScope.IsNull() && myScope->Length() >0 )
170 myScope->Remove ( myScope->Length() );
171}
172
173//=======================================================================
174//function : IsParamSet
175//purpose :
176//=======================================================================
177
178static Handle(TCollection_HAsciiString) MakeName (const Handle(TColStd_HSequenceOfHAsciiString) &scope,
179 const Standard_CString param)
180{
181 Handle(TCollection_HAsciiString) str;
182 if ( ! scope.IsNull() && scope->Length() >0 ) {
183 str = new TCollection_HAsciiString ( scope->Value ( scope->Length() )->String() );
184 str->AssignCat (".");
185 str->AssignCat ( param );
186 }
187 else str = new TCollection_HAsciiString ( param );
188 return str;
189}
190
191Standard_Boolean ShapeProcess_Context::IsParamSet (const Standard_CString param) const
192{
193 return ! myRC.IsNull() && myRC->Find ( MakeName ( myScope, param )->ToCString() );
194}
195
196//=======================================================================
197//function : GetString
198//purpose :
199//=======================================================================
200
201Standard_Boolean ShapeProcess_Context::GetString (const Standard_CString param,
202 TCollection_AsciiString &str) const
203{
204 if ( myRC.IsNull() ) return Standard_False;
205 Handle(TCollection_HAsciiString) pname = MakeName ( myScope, param );
206 if ( ! myRC->Find ( pname->ToCString() ) ) {
0797d9d3 207#ifdef OCCT_DEBUG
7fd59977 208 cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << pname->ToCString() << " is not defined" << endl;
209#endif
210 return Standard_False;
211 }
212 str = myRC->Value ( pname->ToCString() );
213 return Standard_True;
214}
215
216//=======================================================================
217//function : GetReal
218//purpose :
219//=======================================================================
220
221Standard_Boolean ShapeProcess_Context::GetReal (const Standard_CString param,
222 Standard_Real &val) const
223{
224 if ( myRC.IsNull() ) return Standard_False;
225
226 TCollection_AsciiString str;
227 if ( ! GetString ( param, str ) ) return Standard_False;
228
229 if ( str.IsRealValue() ) {
230 val = str.RealValue();
231 return Standard_True;
232 }
233
234 // if not real, try to treat as alias "&param"
235 str.LeftAdjust();
236 if ( str.Value(1) == '&' ) {
237 TCollection_AsciiString ref = str.Split ( 1 );
238 ref.LeftAdjust();
239 ref.RightAdjust();
240 if ( ! myRC->Find ( ref.ToCString() ) ) {
0797d9d3 241#ifdef OCCT_DEBUG
7fd59977 242 cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << ref.ToCString() << " is not defined" << endl;
243#endif
244 return Standard_False;
245 }
246 str = myRC->Value ( ref.ToCString() );
247 if ( str.IsRealValue() ) {
248 val = str.RealValue();
249 return Standard_True;
250 }
251 }
0797d9d3 252#ifdef OCCT_DEBUG
7fd59977 253 cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << param << " is neither Real nor reference to Real";
254#endif
255 return Standard_False;
256}
257
258//=======================================================================
259//function : GetInteger
260//purpose :
261//=======================================================================
262
263Standard_Boolean ShapeProcess_Context::GetInteger (const Standard_CString param,
264 Standard_Integer &val) const
265{
266 if ( myRC.IsNull() ) return Standard_False;
267
268 TCollection_AsciiString str;
269 if ( ! GetString ( param, str ) ) return Standard_False;
270
271 if ( str.IsIntegerValue() ) {
272 val = str.IntegerValue();
273 return Standard_True;
274 }
275
276 // if not integer, try to treat as alias "&param"
277 str.LeftAdjust();
278 if ( str.Value(1) == '&' ) {
279 TCollection_AsciiString ref = str.Split ( 1 );
280 ref.LeftAdjust();
281 ref.RightAdjust();
282 if ( ! myRC->Find ( ref.ToCString() ) ) {
0797d9d3 283#ifdef OCCT_DEBUG
7fd59977 284 cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << ref.ToCString() << " is not defined" << endl;
285#endif
286 return Standard_False;
287 }
288 str = myRC->Value ( ref.ToCString() );
289 if ( str.IsIntegerValue() ) {
290 val = str.IntegerValue();
291 return Standard_True;
292 }
293 }
0797d9d3 294#ifdef OCCT_DEBUG
7fd59977 295 cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << param << " is neither Integer nor reference to Integer";
296#endif
297 return Standard_False;
298}
299
300//=======================================================================
301//function : GetBoolean
302//purpose :
303//=======================================================================
304
305Standard_Boolean ShapeProcess_Context::GetBoolean (const Standard_CString param,
306 Standard_Boolean &val) const
307{
308 if ( myRC.IsNull() ) return Standard_False;
309 try {
310 OCC_CATCH_SIGNALS
dde68833 311 val = myRC->Integer (MakeName (myScope, param)->ToCString()) != 0;
7fd59977 312 return Standard_True;
313 }
9775fa61 314 catch (Standard_Failure const& anException) {
0797d9d3 315#ifdef OCCT_DEBUG
7fd59977 316 cout << "Warning: ShapeProcess_Context::GetInteger(): " << param << ": ";
9775fa61 317 anException.Print(cout); cout << endl;
7fd59977 318#endif
9775fa61 319 (void)anException;
7fd59977 320 }
321 return Standard_False;
322}
323
324//=======================================================================
325//function : RealVal
326//purpose :
327//=======================================================================
328
329Standard_Real ShapeProcess_Context::RealVal (const Standard_CString param,
330 const Standard_Real def) const
331{
332 Standard_Real val;
333 return GetReal ( param, val ) ? val : def;
334}
335
336//=======================================================================
337//function : BooleanVal
338//purpose :
339//=======================================================================
340
341Standard_Boolean ShapeProcess_Context::BooleanVal (const Standard_CString param,
342 const Standard_Boolean def) const
343{
344 Standard_Boolean val;
345 return GetBoolean ( param, val ) ? val : def;
346}
347
348//=======================================================================
349//function : IntegerVal
350//purpose :
351//=======================================================================
352
353Standard_Integer ShapeProcess_Context::IntegerVal (const Standard_CString param,
354 const Standard_Integer def) const
355{
356 Standard_Integer val;
357 return GetInteger ( param, val ) ? val : def;
358}
359
360//=======================================================================
361//function : StringVal
362//purpose :
363//=======================================================================
364
365Standard_CString ShapeProcess_Context::StringVal (const Standard_CString param,
366 const Standard_CString def) const
367{
368 if ( myRC.IsNull() ) return def;
369 try {
370 OCC_CATCH_SIGNALS
371 return myRC->Value ( MakeName ( myScope, param )->ToCString() );
372 }
9775fa61 373 catch (Standard_Failure const& anException) {
0797d9d3 374#ifdef OCCT_DEBUG
7fd59977 375 cout << "Warning: ShapeProcess_Context::GetInteger(): " << param << ": ";
9775fa61 376 anException.Print(cout); cout << endl;
7fd59977 377#endif
9775fa61 378 (void)anException;
7fd59977 379 }
380 return def;
381}
382
383//=======================================================================
384//function : SetMessenger
385//purpose :
386//=======================================================================
387
388void ShapeProcess_Context::SetMessenger (const Handle(Message_Messenger)& messenger)
389{
390 if ( messenger.IsNull() )
391 myMessenger = Message::DefaultMessenger();
392 else
393 myMessenger = messenger;
394}
395
396//=======================================================================
397//function : Messenger
398//purpose :
399//=======================================================================
400
401Handle(Message_Messenger) ShapeProcess_Context::Messenger () const
402{
403 return myMessenger;
404}
405
b485ee79
KD
406//=======================================================================
407//function : SetProgress
408//purpose :
409//=======================================================================
410
411void ShapeProcess_Context::SetProgress (const Handle(Message_ProgressIndicator)& progress)
412{
413 myProgress = progress;
414}
415
416//=======================================================================
417//function : Progress
418//purpose :
419//=======================================================================
420
421Handle(Message_ProgressIndicator) ShapeProcess_Context::Progress() const
422{
423 return myProgress;
424}
425
7fd59977 426//=======================================================================
427//function : SetTraceLevel
428//purpose :
429//=======================================================================
430
431void ShapeProcess_Context::SetTraceLevel (const Standard_Integer tracelev)
432{
433 myTraceLev = tracelev;
434}
435
436//=======================================================================
437//function : TraceLevel
438//purpose :
439//=======================================================================
440
441Standard_Integer ShapeProcess_Context::TraceLevel () const
442{
443 return myTraceLev;
42cf5bc1 444}