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>
20 #include <Draw_Drawable3D.hxx>
21 #include <Draw_Failure.hxx>
22 #include <Draw_Interpretor.hxx>
23 #include <Draw_ProgressIndicator.hxx>
24 #include <Draw_Window.hxx>
25 #include <gp_Pnt2d.hxx>
27 #include <OSD_Environment.hxx>
28 #include <OSD_SharedLibrary.hxx>
29 #include <OSD_Timer.hxx>
30 #include <Plugin_MapOfFunctions.hxx>
31 #include <Resource_Manager.hxx>
32 #include <Standard_ErrorHandler.hxx>
33 #include <Standard_Stream.hxx>
34 #include <Standard_Version.hxx>
35 #include <TCollection_AsciiString.hxx>
38 // on MSVC, use #pragma to define name of the Tcl library to link with,
39 // depending on Tcl version number
41 // two helper macros are needed to convert version number macro to string literal
42 #define STRINGIZE1(a) #a
43 #define STRINGIZE2(a) STRINGIZE1(a)
44 #pragma comment (lib, "tcl" STRINGIZE2(TCL_MAJOR_VERSION) STRINGIZE2(TCL_MINOR_VERSION) ".lib")
45 #pragma comment (lib, "tk" STRINGIZE2(TCL_MAJOR_VERSION) STRINGIZE2(TCL_MINOR_VERSION) ".lib")
50 extern Standard_Boolean Draw_ParseFailed;
52 Standard_EXPORT Draw_Viewer dout;
53 Standard_EXPORT Draw_Interpretor theCommands;
54 Standard_EXPORT Standard_Boolean Draw_Batch;
55 Standard_EXPORT Standard_Boolean Draw_Spying = Standard_False;
56 Standard_EXPORT Standard_Boolean Draw_Chrono = Standard_False;
57 Standard_EXPORT Standard_Boolean Draw_VirtualWindows = Standard_False;
58 Standard_EXPORT Standard_Boolean ErrorMessages = Standard_True;
60 static const char* ColorNames[MAXCOLOR] = {
61 "White","Red","Green","Blue","Cyan","Gold","Magenta",
62 "Maroon","Orange","Pink","Salmon","Violet","Yellow","Khaki","Coral"
67 static ostream spystream(&Draw_Spyfile);
69 static Standard_Boolean XLoop;
71 static Handle(Draw_ProgressIndicator) PInd = NULL;
73 Standard_EXPORT Standard_Boolean Draw_Interprete(const char* command);
74 // true if complete command
76 // *******************************************************************
78 // *******************************************************************
80 extern console_semaphore_value volatile console_semaphore;
81 extern char console_command[1000];
84 static void ReadInitFile (const TCollection_AsciiString& theFileName)
86 TCollection_AsciiString aPath = theFileName;
90 aPath.ChangeAll ('\\', '/');
92 Sprintf(console_command, "source \"%.980s\"", aPath.ToCString());
93 console_semaphore = HAS_CONSOLE_COMMAND;
94 while (console_semaphore == HAS_CONSOLE_COMMAND)
98 cout << "Error while reading a script file." << endl;
103 char* com = new char [aPath.Length() + strlen ("source ") + 2];
104 Sprintf (com, "source %s", aPath.ToCString());
105 Draw_Interprete (com);
112 //=======================================================================
114 //purpose : Set/Get Progress Indicator
115 //=======================================================================
116 void Draw::SetProgressBar(const Handle(Draw_ProgressIndicator)& thePI)
121 Handle(Draw_ProgressIndicator) Draw::GetProgressBar()
127 /*--------------------------------------------------------*\
128 | exitProc: finalization handler for Tcl/Tk thread. Forces parent process to die
129 \*--------------------------------------------------------*/
130 void exitProc(ClientData /*dc*/)
133 for (Standard_Integer id = 0; id < MAXVIEW; id++)
139 // *******************************************************************
141 // *******************************************************************
143 //Standard_EXPORT void Draw_Appli(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lps
144 Standard_EXPORT void Draw_Appli(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszLine, int nShow,const FDraw_InitAppli Draw_InitAppli)
146 void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_InitAppli)
150 // prepend extra DLL search path to override system libraries like opengl32.dll
152 OSD_Environment aUserDllEnv ("CSF_UserDllPath");
153 TCollection_AsciiString aUserDllPath = aUserDllEnv.Value();
154 if (!aUserDllPath.IsEmpty())
156 // This function available since Win XP SP1 #if (_WIN32_WINNT >= 0x0502).
157 // We retrieve dynamically here (kernel32 should be always preloaded).
158 typedef BOOL (WINAPI *SetDllDirectoryA_t)(const char* thePathName);
159 HMODULE aKern32Module = GetModuleHandleA ("kernel32");
160 SetDllDirectoryA_t aFunc = (aKern32Module != NULL)
161 ? (SetDllDirectoryA_t )GetProcAddress (aKern32Module, "SetDllDirectoryA") : NULL;
164 aFunc (aUserDllPath.ToCString());
168 //std::cerr << "SetDllDirectoryA() is not available on this system!\n";
170 if (aKern32Module != NULL)
172 FreeLibrary (aKern32Module);
177 // *****************************************************************
179 // *****************************************************************
180 Draw_Batch = Standard_False;
181 TCollection_AsciiString aRunFile, aCommand;
183 Standard_Boolean isInteractiveForced = Standard_False;
186 // On NT command line arguments are in the lpzline and not in argv
188 const int MAXARGS = 1024;
189 const char* argv[MAXARGS];
190 for (const char* p = strtok(lpszLine, " \t"); p != NULL; p = strtok(NULL, " \t")) {
195 // parse command line
196 for (i = 1; i < argc; i++) {
197 if (strcasecmp (argv[i], "-h") == 0 || strcasecmp (argv[i], "--help") == 0)
199 cout << "Open CASCADE " << OCC_VERSION_STRING_EXT << " DRAW Test Harness" << endl << endl;
200 cout << "Options: " << endl;
201 cout << " -b: batch mode (no GUI, no viewers)" << endl;
202 cout << " -v: no GUI, use virtual (off-screen) windows for viewers" << endl;
203 cout << " -i: interactive mode" << endl;
204 cout << " -f file: execute script from file" << endl;
205 cout << " -c command args...: execute command (with optional arguments)" << endl << endl;
206 cout << "Options -b, -v, and -i are mutually exclusive." << endl;
207 cout << "If -c or -f are given, -v is default; otherwise default is -i." << endl;
208 cout << "Options -c and -f are alternatives and should be at the end " << endl;
209 cout << "of the command line. " << endl;
210 cout << "Option -c can accept set of commands separated by ';'." << endl;
213 else if (strcasecmp (argv[i], "-b") == 0)
214 Draw_Batch = Standard_True;
215 else if (strcasecmp (argv[i], "-v") == 0) {
216 // force virtual windows
217 Draw_VirtualWindows = Standard_True;
218 } else if (strcasecmp (argv[i], "-i") == 0) {
220 Draw_VirtualWindows = Standard_False;
221 isInteractiveForced = Standard_True;
222 } else if (strcasecmp (argv[i], "-f") == 0) { // -f option should be LAST!
223 Draw_VirtualWindows = !isInteractiveForced;
225 aRunFile = TCollection_AsciiString (argv[i]);
228 } else if (strcasecmp (argv[i], "-c") == 0) { // -c option should be LAST!
229 Draw_VirtualWindows = !isInteractiveForced;
231 aCommand = TCollection_AsciiString (argv[i]);
234 aCommand.AssignCat (" ");
235 aCommand.AssignCat (argv[i]);
239 cout << "Error: unsupported option " << argv[i] << endl;
243 // *****************************************************************
245 // *****************************************************************
246 OSD::SetSignal(Standard_False);
249 // in interactive mode, force Windows to report dll loading problems interactively
250 if ( ! Draw_VirtualWindows && ! Draw_Batch )
254 // *****************************************************************
255 // init X window and create display
256 // *****************************************************************
263 Draw_Batch=!Init_Appli(hInst, hPrevInst, nShow, hWnd);
265 Draw_Batch=!Init_Appli();
268 cout << "DRAW is running in batch mode" << endl;
273 for (i=0;i<MAXCOLOR;i++) {
274 if (!dout.DefineColor(i,ColorNames[i]))
275 cout <<"Could not allocate default color "<<ColorNames[i]<<endl;
279 // *****************************************************************
280 // set maximum precision for cout
281 // *****************************************************************
284 // *****************************************************************
286 // *****************************************************************
287 Draw::BasicCommands(theCommands);
288 Draw::VariableCommands(theCommands);
289 Draw::UnitCommands(theCommands);
290 if (!Draw_Batch) Draw::GraphicCommands(theCommands);
292 // *****************************************************************
294 // *****************************************************************
295 Draw_InitAppli(theCommands);
298 Tcl_CreateExitHandler(exitProc, 0);
301 // *****************************************************************
303 // *****************************************************************
306 if (getenv ("DRAWDEFAULT") == NULL)
308 if (getenv ("CASROOT") == NULL)
311 ReadInitFile ("ddefault");
313 cout << " the CASROOT variable is mandatory to Run OpenCascade "<< endl;
314 cout << "No default file" << endl;
319 TCollection_AsciiString aDefStr (getenv ("CASROOT"));
320 aDefStr += "/src/DrawResources/DrawDefault";
321 ReadInitFile (aDefStr);
326 ReadInitFile (getenv ("DRAWDEFAULT"));
329 // read commands from file
330 if (!aRunFile.IsEmpty()) {
331 ReadInitFile (aRunFile);
332 // provide a clean exit, this is useful for some analysis tools
333 if ( ! isInteractiveForced )
341 // execute command from command line
342 if (!aCommand.IsEmpty()) {
343 Draw_Interprete (aCommand.ToCString());
344 // provide a clean exit, this is useful for some analysis tools
345 if ( ! isInteractiveForced )
353 // *****************************************************************
355 // *****************************************************************
360 Run_Appli(Draw_Interprete);
371 } while ((cmd[i] != '\n') && (!cin.fail()));
373 } while (Draw_Interprete(cmd) != (unsigned int ) -2);
376 // Destruction de l'application
377 Destroy_Appli(hInst);
382 // User functions called before and after each command
383 void (*Draw_BeforeCommand)() = NULL;
384 void (*Draw_AfterCommand)(Standard_Integer) = NULL;
386 Standard_Boolean Draw_Interprete(const char* com)
389 static Standard_Boolean first = Standard_True;
390 static Tcl_DString command;
393 first = Standard_False;
394 Tcl_DStringInit(&command);
397 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 1)))
398 // OCC63: Since Tcl 8.1 it uses UTF-8 encoding for internal representation of strings
399 Tcl_ExternalToUtfDString ( NULL, com, -1, &command );
401 Tcl_DStringAppend(&command,com,-1);
404 if (!theCommands.Complete(Tcl_DStringValue(&command)))
405 return Standard_False;
407 // *******************************************************************
408 // Command interpreter
409 // *******************************************************************
411 // Standard_Integer i = 0;
412 // Standard_Integer j = 0;
414 Standard_Boolean wasspying = Draw_Spying;
417 Standard_Boolean hadchrono = Draw_Chrono;
418 if (hadchrono) tictac.Start();
420 if (Draw_BeforeCommand) (*Draw_BeforeCommand) ();
424 c = theCommands.RecordAndEval(Tcl_DStringValue(&command));
426 if (Draw_AfterCommand) (*Draw_AfterCommand)(c);
428 if (wasspying && Draw_Spying) {
429 if (c > 0) spystream << "# ";
430 spystream << Tcl_DStringValue(&command) << "\n";
435 if (*theCommands.Result())
436 cout << theCommands.Result() << endl;
438 if (Draw_Chrono && hadchrono) {
443 Tcl_DStringFree(&command);
445 return Standard_True;
452 Standard_Integer Tcl_AppInit (Tcl_Interp *)
463 Standard_Integer Draw_Call (char *c)
465 Standard_Integer r = theCommands.Eval(c);
466 cout << theCommands.Result() << endl;
470 //=================================================================================
472 //=================================================================================
473 void Draw::Load(Draw_Interpretor& theDI, const TCollection_AsciiString& theKey,
474 const TCollection_AsciiString& theResourceFileName,
475 TCollection_AsciiString& theDefaultsDirectory,
476 TCollection_AsciiString& theUserDefaultsDirectory,
477 const Standard_Boolean Verbose ) {
479 static Plugin_MapOfFunctions theMapOfFunctions;
482 if(!theMapOfFunctions.IsBound(theKey)) {
484 Handle(Resource_Manager) aPluginResource = new Resource_Manager(theResourceFileName.ToCString(), theDefaultsDirectory, theUserDefaultsDirectory, Verbose);
486 if(!aPluginResource->Find(theKey.ToCString())) {
487 Standard_SStream aMsg; aMsg << "Could not find the resource:";
488 aMsg << theKey.ToCString()<< endl;
489 cout << "could not find the resource:"<<theKey.ToCString()<< endl;
490 Draw_Failure::Raise(aMsg);
493 TCollection_AsciiString aPluginLibrary("");
494 #if !defined(_WIN32) || defined(__MINGW32__)
495 aPluginLibrary += "lib";
497 aPluginLibrary += aPluginResource->Value(theKey.ToCString());
499 aPluginLibrary += ".dll";
501 aPluginLibrary += ".dylib";
502 #elif defined (HPUX) || defined(_hpux)
503 aPluginLibrary += ".sl";
505 aPluginLibrary += ".so";
507 OSD_SharedLibrary aSharedLibrary(aPluginLibrary.ToCString());
508 if(!aSharedLibrary.DlOpen(OSD_RTLD_LAZY)) {
509 TCollection_AsciiString error(aSharedLibrary.DlError());
510 Standard_SStream aMsg; aMsg << "Could not open: ";
511 aMsg << aPluginResource->Value(theKey.ToCString());
512 aMsg << "; reason: ";
513 aMsg << error.ToCString();
515 cout << "could not open: " << aPluginResource->Value(theKey.ToCString())<< " ; reason: "<< error.ToCString() << endl;
517 Draw_Failure::Raise(aMsg);
519 f = aSharedLibrary.DlSymb("PLUGINFACTORY");
521 TCollection_AsciiString error(aSharedLibrary.DlError());
522 Standard_SStream aMsg; aMsg << "Could not find the factory in: ";
523 aMsg << aPluginResource->Value(theKey.ToCString());
524 aMsg << error.ToCString();
525 Draw_Failure::Raise(aMsg);
527 theMapOfFunctions.Bind(theKey, f);
530 f = theMapOfFunctions(theKey);
532 // void (*fp) (Draw_Interpretor&, const TCollection_AsciiString&) = NULL;
533 // fp = (void (*)(Draw_Interpretor&, const TCollection_AsciiString&)) f;
534 // (*fp) (theDI, theKey);
536 void (*fp) (Draw_Interpretor&) = NULL;
537 fp = (void (*)(Draw_Interpretor&)) f;