b83c08465ddef6c42a8cd994ba59f0a058449c0f
[occt.git] / src / Draw / Draw_BasicCommands.cxx
1 // File:        Draw_BasicCommands.cxx
2 // Created:     Thu Feb 23 18:21:17 1995
3 // Author:      Remi LEQUETTE
4 //              <rle@bravox>
5
6 #include <Standard_Macro.hxx>
7 #include <Standard_Stream.hxx>
8 #include <Standard_SStream.hxx>
9
10 #include <Draw.ixx>
11 #include <Draw_Appli.hxx>
12 #include <Draw_Printer.hxx>
13
14 #include <Message.hxx>
15 #include <Message_Messenger.hxx>
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #if defined(HAVE_TIME_H) || defined(WNT)
22 # include <time.h>
23 #endif
24
25 #ifdef HAVE_SIGNAL_H
26 # include <signal.h>
27 #endif
28
29 #ifndef WNT
30 # include <sys/resource.h>
31 # ifdef HAVE_STRINGS_H
32 #  include <strings.h>
33 # endif
34 #else
35 //WNT
36 extern Standard_Boolean Draw_Batch;
37 #include <windows.h>
38 #include <winbase.h>
39 #include <process.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #ifdef HAVE_LIMITS 
43 # include <limits>
44 #elif defined (HAVE_LIMITS_H)
45 # include <limits.h>
46 #endif
47
48 #ifdef WNT
49 # include <limits>
50 #endif
51
52 static clock_t MDTV_CPU_LIMIT;   // Cpu_limit in Sec.
53 static clock_t MDTV_CPU_CURRENT; // cpu time already used at last
54                                  // cpulimit call. (sec.) 
55 //#define strcasecmp strcmp Already defined
56 #define RLIM_INFINITY   0x7fffffff
57 #endif
58
59 #include <Draw_Chronometer.hxx>
60
61 #if defined (__hpux) || defined ( HPUX )
62 #define RLIM_INFINITY   0x7fffffff
63 #define RLIMIT_CPU      0
64 #endif
65
66
67
68 //=======================================================================
69 // chronom
70 //=======================================================================
71
72 extern Standard_Boolean Draw_Chrono;
73
74 static Standard_Integer chronom(Draw_Interpretor& di,
75                                 Standard_Integer n,const char** a)
76 {
77   if ((n == 1) || (*a[1] == '0') || (*a[1] == '1')) {
78     if (n == 1)
79       Draw_Chrono = !Draw_Chrono;
80     else
81       Draw_Chrono = (*a[1] == '1');
82
83     if (Draw_Chrono) di << "Chronometers activated."<<"\n";
84     else di << "Chronometers desactivated."<<"\n";
85   }
86   else {
87     Handle(Draw_Drawable3D) D = Draw::Get(a[1]);
88     Handle(Draw_Chronometer) C;
89     if (!D.IsNull()) {
90       C = Handle(Draw_Chronometer)::DownCast(D);
91     }
92     if (C.IsNull()) {
93       C = new Draw_Chronometer();
94     Draw::Set(a[1],C,Standard_False);
95     }
96     if (n <= 2) {
97       C->Timer().Reset();
98     }
99     else {
100       if (!strcasecmp(a[2],"reset"))
101         C->Timer().Reset();
102       if (!strcasecmp(a[2],"start"))
103         C->Timer().Start();
104       if (!strcasecmp(a[2],"stop"))
105         C->Timer().Stop();
106       if (!strcasecmp(a[2],"show"))
107         C->Timer().Show();
108     }
109   }
110   return 0;
111 }
112
113 static Standard_Integer dchronom(Draw_Interpretor& I,
114                                  Standard_Integer n,const char** a)
115 {
116   if ((n == 1) || (*a[1] == '0') || (*a[1] == '1')) {
117     if (n == 1)
118       Draw_Chrono = !Draw_Chrono;
119     else
120       Draw_Chrono = (*a[1] == '1');
121
122     if (Draw_Chrono) I << "Chronometers activated."<<"\n";
123     else I << "Chronometers desactivated."<<"\n";
124   }
125   else {
126     Handle(Draw_Drawable3D) D = Draw::Get(a[1]);
127     Handle(Draw_Chronometer) C;
128     if (!D.IsNull()) {
129       C = Handle(Draw_Chronometer)::DownCast(D);
130     }
131     if (C.IsNull()) {
132       C = new Draw_Chronometer();
133     Draw::Set(a[1],C,Standard_False);
134     }
135     if (n <= 2) {
136       C->Timer().Reset();
137     }
138     else {
139       if (!strcasecmp(a[2],"reset"))
140         C->Timer().Reset();
141       if (!strcasecmp(a[2],"start"))
142         C->Timer().Start();
143       if (!strcasecmp(a[2],"stop"))
144         C->Timer().Stop();
145       if (!strcasecmp(a[2],"show")) {
146         Standard_SStream ss;
147         C->Timer().Show(ss);
148         I << ss;
149       }
150     }
151   }
152   return 0;
153 }
154
155
156
157 //=======================================================================
158 //function : ifbatch
159 //purpose  : 
160 //=======================================================================
161
162 static Standard_Integer ifbatch(Draw_Interpretor& DI, Standard_Integer , const char** )
163 {
164   if (Draw_Batch)
165     DI << "1";
166   else
167     DI << "0";
168   
169   return 0;
170 }
171
172 //=======================================================================
173 //function : spy
174 //purpose  : 
175 //=======================================================================
176
177 extern Standard_Boolean Draw_Spying;
178 extern filebuf Draw_Spyfile;
179
180 static Standard_Integer spy(Draw_Interpretor& di, Standard_Integer n, const char** a)
181 {
182   if (Draw_Spying) 
183     Draw_Spyfile.close();
184   Draw_Spying = Standard_False;
185   if (n > 1) {
186     if (!Draw_Spyfile.open(a[1],ios::out)) {
187       di << "Cannot open "<<a[1]<<" for writing"<<"\n";
188       return 1;
189     }
190     Draw_Spying = Standard_True;
191   }
192   return 0;
193 }
194
195 //=======================================================================
196 //function : wait
197 //purpose  : 
198 //=======================================================================
199
200 static Standard_Integer Draw_wait(Draw_Interpretor& , Standard_Integer n, const char** a)
201 {
202   Standard_Integer w = 10;
203   if (n > 1)
204     w = atoi(a[1]);
205   time_t ct = time(NULL) + w;
206   while (time(NULL) < ct) {};
207   return 0;
208 }
209
210
211 static Standard_Integer cpulimit(Draw_Interpretor& di, Standard_Integer n, const char** a)
212 {
213 #ifndef WNT
214   rlimit rlp;
215   rlp.rlim_max = RLIM_INFINITY;
216   if (n <= 1)
217     rlp.rlim_cur = RLIM_INFINITY;
218   else
219     rlp.rlim_cur = atoi(a[1]);
220
221   int status;
222   status=setrlimit(RLIMIT_CPU,&rlp);
223   if (status !=0)
224     di << "status cpulimit setrlimit : " << status << "\n";
225
226 #else
227 //WNT
228   static int first=1;
229 /*
230   unsigned int __stdcall CpuFunc(void * );
231   unsigned ThreadID;
232
233   if (n <= 1) MDTV_CPU_LIMIT = RLIM_INFINITY;
234   else {
235   
236           MDTV_CPU_LIMIT = atoi(a[1]);
237           MDTV_CPU_CURRENT = clock()/1000;
238
239           if (first) // Lancer le thread au 1er appel seulement.
240           {
241                   first=0 ;
242                   _beginthreadex(NULL,0,CpuFunc,NULL,0,&ThreadID);
243           }
244   }
245
246 */
247 #endif
248
249   return 0;
250 }
251
252
253 void Draw::BasicCommands(Draw_Interpretor& theCommands)
254 {
255   static Standard_Boolean Done = Standard_False;
256   if (Done) return;
257   Done = Standard_True;
258
259   const char* g = "DRAW General Commands";
260   
261   theCommands.Add("batch", "returns 1 in batch mode",
262                   __FILE__,ifbatch,g);
263   theCommands.Add("spy","spy [file], Save commands in file. no file close",
264                   __FILE__,spy,g);
265   theCommands.Add("wait","wait [time(10)], wait time seconds",
266                   __FILE__,Draw_wait,g);
267   theCommands.Add("cpulimit","cpulimit [nbseconds], no args remove limits",
268                   __FILE__,cpulimit,g);
269   theCommands.Add("chrono","chrono [ name start/stop/reset/show]",
270                   __FILE__,chronom,g);
271   theCommands.Add("dchrono","dchrono [ name start/stop/reset/show]",
272                   __FILE__,dchronom,g);
273 }