1 // Created on: 1993-08-13
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
19 #include <Draw_Appli.hxx>
21 #include <OSD_Environment.hxx>
22 #include <OSD_Timer.hxx>
24 #include <Draw_Window.hxx>
25 #include <gp_Pnt2d.hxx>
27 #include <Standard_Stream.hxx>
28 #include <Standard_Version.hxx>
30 #include <Draw_Drawable3D.hxx>
31 #include <Draw_Interpretor.hxx>
32 #include <Draw_ProgressIndicator.hxx>
34 #include <Plugin_MapOfFunctions.hxx>
35 #include <OSD_SharedLibrary.hxx>
36 #include <Resource_Manager.hxx>
37 #include <Draw_Failure.hxx>
38 #include <TCollection_AsciiString.hxx>
39 #include <Standard_ErrorHandler.hxx>
43 // on MSVC, use #pragma to define name of the Tcl library to link with,
44 // depending on Tcl version number
46 // two helper macros are needed to convert version number macro to string literal
47 #define STRINGIZE1(a) #a
48 #define STRINGIZE2(a) STRINGIZE1(a)
49 #pragma comment (lib, "tcl" STRINGIZE2(TCL_MAJOR_VERSION) STRINGIZE2(TCL_MINOR_VERSION) ".lib")
50 #pragma comment (lib, "tk" STRINGIZE2(TCL_MAJOR_VERSION) STRINGIZE2(TCL_MINOR_VERSION) ".lib")
55 extern Standard_Boolean Draw_ParseFailed;
57 Standard_EXPORT Draw_Viewer dout;
58 Standard_EXPORT Draw_Interpretor theCommands;
59 Standard_EXPORT Standard_Boolean Draw_Batch;
60 Standard_EXPORT Standard_Boolean Draw_Spying = Standard_False;
61 Standard_EXPORT Standard_Boolean Draw_Chrono = Standard_False;
62 Standard_EXPORT Standard_Boolean Draw_VirtualWindows = Standard_False;
63 Standard_EXPORT Standard_Boolean ErrorMessages = Standard_True;
65 static const char* ColorNames[MAXCOLOR] = {
66 "White","Red","Green","Blue","Cyan","Gold","Magenta",
67 "Maroon","Orange","Pink","Salmon","Violet","Yellow","Khaki","Coral"
72 static ostream spystream(&Draw_Spyfile);
74 static Standard_Boolean XLoop;
76 static Handle(Draw_ProgressIndicator) PInd = NULL;
78 Standard_EXPORT Standard_Boolean Draw_Interprete(const char* command);
79 // true if complete command
81 // *******************************************************************
83 // *******************************************************************
85 extern console_semaphore_value volatile console_semaphore;
86 extern char console_command[1000];
89 static void ReadInitFile (const TCollection_AsciiString& theFileName)
91 TCollection_AsciiString aPath = theFileName;
95 aPath.ChangeAll ('\\', '/');
97 Sprintf(console_command, "source \"%.980s\"", aPath.ToCString());
98 console_semaphore = HAS_CONSOLE_COMMAND;
99 while (console_semaphore == HAS_CONSOLE_COMMAND)
103 cout << "Error while reading a script file." << endl;
108 char* com = new char [aPath.Length() + strlen ("source ") + 2];
109 Sprintf (com, "source %s", aPath.ToCString());
110 Draw_Interprete (com);
117 //=======================================================================
119 //purpose : Set/Get Progress Indicator
120 //=======================================================================
121 void Draw::SetProgressBar(const Handle(Draw_ProgressIndicator)& thePI)
126 Handle(Draw_ProgressIndicator) Draw::GetProgressBar()
132 /*--------------------------------------------------------*\
133 | exitProc: finalization handler for Tcl/Tk thread. Forces parent process to die
134 \*--------------------------------------------------------*/
135 void exitProc(ClientData /*dc*/)
138 for (Standard_Integer id = 0; id < MAXVIEW; id++)
144 // *******************************************************************
146 // *******************************************************************
148 //Standard_EXPORT void Draw_Appli(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lps
149 Standard_EXPORT void Draw_Appli(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszLine, int nShow,const FDraw_InitAppli Draw_InitAppli)
151 void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_InitAppli)
155 // prepend extra DLL search path to override system libraries like opengl32.dll
157 OSD_Environment aUserDllEnv ("CSF_UserDllPath");
158 TCollection_AsciiString aUserDllPath = aUserDllEnv.Value();
159 if (!aUserDllPath.IsEmpty())
161 // This function available since Win XP SP1 #if (_WIN32_WINNT >= 0x0502).
162 // We retrieve dynamically here (kernel32 should be always preloaded).
163 typedef BOOL (WINAPI *SetDllDirectoryA_t)(const char* thePathName);
164 HMODULE aKern32Module = GetModuleHandleA ("kernel32");
165 SetDllDirectoryA_t aFunc = (aKern32Module != NULL)
166 ? (SetDllDirectoryA_t )GetProcAddress (aKern32Module, "SetDllDirectoryA") : NULL;
169 aFunc (aUserDllPath.ToCString());
173 //std::cerr << "SetDllDirectoryA() is not available on this system!\n";
175 if (aKern32Module != NULL)
177 FreeLibrary (aKern32Module);
182 // *****************************************************************
184 // *****************************************************************
185 Draw_Batch = Standard_False;
186 TCollection_AsciiString aRunFile, aCommand;
188 Standard_Boolean isInteractiveForced = Standard_False;
191 // On NT command line arguments are in the lpzline and not in argv
193 const int MAXARGS = 1024;
194 const char* argv[MAXARGS];
195 for (const char* p = strtok(lpszLine, " \t"); p != NULL; p = strtok(NULL, " \t")) {
200 // parse command line
201 for (i = 1; i < argc; i++) {
202 if (strcasecmp (argv[i], "-h") == 0 || strcasecmp (argv[i], "--help") == 0)
204 cout << "Open CASCADE " << OCC_VERSION_STRING_EXT << " DRAW Test Harness" << endl << endl;
205 cout << "Options: " << endl;
206 cout << " -b: batch mode (no GUI, no viewers)" << endl;
207 cout << " -v: no GUI, use virtual (off-screen) windows for viewers" << endl;
208 cout << " -i: interactive mode" << endl;
209 cout << " -f file: execute script from file" << endl;
210 cout << " -c command args...: execute command (with optional arguments)" << endl << endl;
211 cout << "Options -b, -v, and -i are mutually exclusive." << endl;
212 cout << "If -c or -f are given, -v is default; otherwise default is -i." << endl;
213 cout << "Options -c and -f are alternatives and should be at the end " << endl;
214 cout << "of the command line. " << endl;
215 cout << "Option -c can accept set of commands separated by ';'." << endl;
218 else if (strcasecmp (argv[i], "-b") == 0)
219 Draw_Batch = Standard_True;
220 else if (strcasecmp (argv[i], "-v") == 0) {
221 // force virtual windows
222 Draw_VirtualWindows = Standard_True;
223 } else if (strcasecmp (argv[i], "-i") == 0) {
225 Draw_VirtualWindows = Standard_False;
226 isInteractiveForced = Standard_True;
227 } else if (strcasecmp (argv[i], "-f") == 0) { // -f option should be LAST!
228 Draw_VirtualWindows = !isInteractiveForced;
230 aRunFile = TCollection_AsciiString (argv[i]);
233 } else if (strcasecmp (argv[i], "-c") == 0) { // -c option should be LAST!
234 Draw_VirtualWindows = !isInteractiveForced;
236 aCommand = TCollection_AsciiString (argv[i]);
239 aCommand.AssignCat (" ");
240 aCommand.AssignCat (argv[i]);
244 cout << "Error: unsupported option " << argv[i] << endl;
248 // *****************************************************************
250 // *****************************************************************
251 OSD::SetSignal(Standard_False);
254 // in interactive mode, force Windows to report dll loading problems interactively
255 if ( ! Draw_VirtualWindows && ! Draw_Batch )
259 // *****************************************************************
260 // init X window and create display
261 // *****************************************************************
268 Draw_Batch=!Init_Appli(hInst, hPrevInst, nShow, hWnd);
270 Draw_Batch=!Init_Appli();
273 cout << "DRAW is running in batch mode" << endl;
278 for (i=0;i<MAXCOLOR;i++) {
279 if (!dout.DefineColor(i,ColorNames[i]))
280 cout <<"Could not allocate default color "<<ColorNames[i]<<endl;
284 // *****************************************************************
285 // set maximum precision for cout
286 // *****************************************************************
289 // *****************************************************************
291 // *****************************************************************
292 Draw::BasicCommands(theCommands);
293 Draw::VariableCommands(theCommands);
294 Draw::UnitCommands(theCommands);
295 if (!Draw_Batch) Draw::GraphicCommands(theCommands);
297 // *****************************************************************
299 // *****************************************************************
300 Draw_InitAppli(theCommands);
303 Tcl_CreateExitHandler(exitProc, 0);
306 // *****************************************************************
308 // *****************************************************************
311 if (getenv ("DRAWDEFAULT") == NULL)
313 if (getenv ("CASROOT") == NULL)
316 ReadInitFile ("ddefault");
318 cout << " the CASROOT variable is mandatory to Run OpenCascade "<< endl;
319 cout << "No default file" << endl;
324 TCollection_AsciiString aDefStr (getenv ("CASROOT"));
325 aDefStr += "/src/DrawResources/DrawDefault";
326 ReadInitFile (aDefStr);
331 ReadInitFile (getenv ("DRAWDEFAULT"));
334 // read commands from file
335 if (!aRunFile.IsEmpty()) {
336 ReadInitFile (aRunFile);
337 // provide a clean exit, this is useful for some analysis tools
338 if ( ! isInteractiveForced )
346 // execute command from command line
347 if (!aCommand.IsEmpty()) {
348 Draw_Interprete (aCommand.ToCString());
349 // provide a clean exit, this is useful for some analysis tools
350 if ( ! isInteractiveForced )
358 // *****************************************************************
360 // *****************************************************************
365 Run_Appli(Draw_Interprete);
376 } while ((cmd[i] != '\n') && (!cin.fail()));
378 } while (Draw_Interprete(cmd) != (unsigned int ) -2);
381 // Destruction de l'application
382 Destroy_Appli(hInst);
387 // User functions called before and after each command
388 void (*Draw_BeforeCommand)() = NULL;
389 void (*Draw_AfterCommand)(Standard_Integer) = NULL;
391 Standard_Boolean Draw_Interprete(const char* com)
394 static Standard_Boolean first = Standard_True;
395 static Tcl_DString command;
398 first = Standard_False;
399 Tcl_DStringInit(&command);
402 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 1)))
403 // OCC63: Since Tcl 8.1 it uses UTF-8 encoding for internal representation of strings
404 Tcl_ExternalToUtfDString ( NULL, com, -1, &command );
406 Tcl_DStringAppend(&command,com,-1);
409 if (!theCommands.Complete(Tcl_DStringValue(&command)))
410 return Standard_False;
412 // *******************************************************************
413 // Command interpreter
414 // *******************************************************************
416 // Standard_Integer i = 0;
417 // Standard_Integer j = 0;
419 Standard_Boolean wasspying = Draw_Spying;
422 Standard_Boolean hadchrono = Draw_Chrono;
423 if (hadchrono) tictac.Start();
425 if (Draw_BeforeCommand) (*Draw_BeforeCommand) ();
429 c = theCommands.RecordAndEval(Tcl_DStringValue(&command));
431 if (Draw_AfterCommand) (*Draw_AfterCommand)(c);
433 if (wasspying && Draw_Spying) {
434 if (c > 0) spystream << "# ";
435 spystream << Tcl_DStringValue(&command) << "\n";
440 if (*theCommands.Result())
441 cout << theCommands.Result() << endl;
443 if (Draw_Chrono && hadchrono) {
448 Tcl_DStringFree(&command);
450 return Standard_True;
457 Standard_Integer Tcl_AppInit (Tcl_Interp *)
468 Standard_Integer Draw_Call (char *c)
470 Standard_Integer r = theCommands.Eval(c);
471 cout << theCommands.Result() << endl;
475 //=================================================================================
477 //=================================================================================
478 void Draw::Load(Draw_Interpretor& theDI, const TCollection_AsciiString& theKey,
479 const TCollection_AsciiString& theResourceFileName,
480 TCollection_AsciiString& theDefaultsDirectory,
481 TCollection_AsciiString& theUserDefaultsDirectory,
482 const Standard_Boolean Verbose ) {
484 static Plugin_MapOfFunctions theMapOfFunctions;
487 if(!theMapOfFunctions.IsBound(theKey)) {
489 Handle(Resource_Manager) aPluginResource = new Resource_Manager(theResourceFileName.ToCString(), theDefaultsDirectory, theUserDefaultsDirectory, Verbose);
491 if(!aPluginResource->Find(theKey.ToCString())) {
492 Standard_SStream aMsg; aMsg << "Could not find the resource:";
493 aMsg << theKey.ToCString()<< endl;
494 cout << "could not find the resource:"<<theKey.ToCString()<< endl;
495 Draw_Failure::Raise(aMsg);
498 TCollection_AsciiString aPluginLibrary("");
500 aPluginLibrary += "lib";
502 aPluginLibrary += aPluginResource->Value(theKey.ToCString());
504 aPluginLibrary += ".dll";
506 aPluginLibrary += ".dylib";
507 #elif defined (HPUX) || defined(_hpux)
508 aPluginLibrary += ".sl";
510 aPluginLibrary += ".so";
512 OSD_SharedLibrary aSharedLibrary(aPluginLibrary.ToCString());
513 if(!aSharedLibrary.DlOpen(OSD_RTLD_LAZY)) {
514 TCollection_AsciiString error(aSharedLibrary.DlError());
515 Standard_SStream aMsg; aMsg << "Could not open: ";
516 aMsg << aPluginResource->Value(theKey.ToCString());
517 aMsg << "; reason: ";
518 aMsg << error.ToCString();
520 cout << "could not open: " << aPluginResource->Value(theKey.ToCString())<< " ; reason: "<< error.ToCString() << endl;
522 Draw_Failure::Raise(aMsg);
524 f = aSharedLibrary.DlSymb("PLUGINFACTORY");
526 TCollection_AsciiString error(aSharedLibrary.DlError());
527 Standard_SStream aMsg; aMsg << "Could not find the factory in: ";
528 aMsg << aPluginResource->Value(theKey.ToCString());
529 aMsg << error.ToCString();
530 Draw_Failure::Raise(aMsg);
532 theMapOfFunctions.Bind(theKey, f);
535 f = theMapOfFunctions(theKey);
537 // void (*fp) (Draw_Interpretor&, const TCollection_AsciiString&) = NULL;
538 // fp = (void (*)(Draw_Interpretor&, const TCollection_AsciiString&)) f;
539 // (*fp) (theDI, theKey);
541 void (*fp) (Draw_Interpretor&) = NULL;
542 fp = (void (*)(Draw_Interpretor&)) f;