0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / IFSelect / IFSelect_SessionPilot.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <IFSelect_Activator.hxx>
16 #include <IFSelect_Selection.hxx>
17 #include <IFSelect_SessionPilot.hxx>
18 #include <IFSelect_SignCounter.hxx>
19 #include <IFSelect_WorkLibrary.hxx>
20 #include <IFSelect_WorkSession.hxx>
21 #include <Interface_EntityIterator.hxx>
22 #include <Interface_InterfaceModel.hxx>
23 #include <Interface_Macros.hxx>
24 #include <Message.hxx>
25 #include <Message_Messenger.hxx>
26 #include <OSD_OpenFile.hxx>
27 #include <Standard_Stream.hxx>
28 #include <Standard_Transient.hxx>
29 #include <Standard_Type.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <TColStd_HSequenceOfAsciiString.hxx>
32
33 #include <stdio.h>
34 IMPLEMENT_STANDARD_RTTIEXT(IFSelect_SessionPilot,IFSelect_Activator)
35
36 #define MAXWORDS 200
37 #define MAXCARS 1000
38
39 static int THE_IFSelect_SessionPilot_initactor = 0;
40 static TCollection_AsciiString nulword;
41
42 //#define DEBUG_TRACE
43
44 // Nb Maxi de words : cf thewords et method SetCommandLine
45
46 IFSelect_SessionPilot::IFSelect_SessionPilot (const Standard_CString prompt)
47 : theprompt (prompt),
48   thewords  (0, MAXWORDS - 1),
49   thewordeb (0, MAXWORDS - 1)
50 {
51   if (theprompt.Length() == 0)
52   {
53     theprompt.AssignCat ("Test-XSTEP>");
54   }
55   therecord = Standard_False;
56   thenbwords = 0;
57   if (THE_IFSelect_SessionPilot_initactor)
58   {
59     return;
60   }
61
62   THE_IFSelect_SessionPilot_initactor = 1;
63   Add (1,"x");
64   Add (1,"exit");
65   Add (2,"?");
66   Add (2,"xhelp");
67   Add (3,"xcommand");
68   Add (4,"xsource");
69   Add (5,"xstep");
70   Add (6,"xnew");
71 }
72
73
74     Handle(IFSelect_WorkSession)  IFSelect_SessionPilot::Session () const 
75       {  return thesession;  }
76
77     Handle(IFSelect_WorkLibrary)  IFSelect_SessionPilot::Library () const 
78       {  return thesession->WorkLibrary();  }
79
80     Standard_Boolean  IFSelect_SessionPilot::RecordMode () const 
81       {  return therecord;  }
82
83     void  IFSelect_SessionPilot::SetSession
84   (const Handle(IFSelect_WorkSession)& WS)
85       {  thesession = WS;  }
86
87     void  IFSelect_SessionPilot::SetLibrary
88   (const Handle(IFSelect_WorkLibrary)& WL)
89       {  if (!thesession.IsNull()) thesession->SetLibrary(WL);  }
90
91     void  IFSelect_SessionPilot::SetRecordMode (const Standard_Boolean mode)
92       {  therecord = mode;  }
93
94
95     void  IFSelect_SessionPilot::SetCommandLine
96   (const TCollection_AsciiString& command)
97 {
98   Standard_Integer lc = command.Length();
99   if (lc > 200) std::cout<<" Commande TRES LONGUE : "<<lc<<" caracteres :"<<std::endl
100     <<command.ToCString()<<std::endl;
101   thecommand = command;
102   if (thecommand.Value(lc) <= ' ')  {  thecommand.Remove(lc);  lc --;  }
103   thenbwords = 0;
104   Standard_Integer i, nc = 0;
105   char unarg[MAXCARS];
106   for (i = 1; i <= lc; i ++) {
107     char val = command.Value(i);
108     if (val <= ' ') {
109       if (nc == 0) continue;
110       if (thenbwords >= MAXWORDS) {  unarg[nc] = val;  nc ++;  continue;  }
111       unarg[nc] = '\0';
112       thewords(thenbwords).Clear();  thewords(thenbwords).AssignCat(unarg);
113 #ifdef DEBUG_TRACE
114       std::cout<<"thewords("<<thenbwords<<") ="<<unarg<<std::endl;
115 #endif
116       thenbwords ++; nc = 0;
117       continue;
118     }
119     if (nc == 0) thewordeb.SetValue (thenbwords,i);
120     if (nc > MAXCARS) {  std::cout<<"Arg."<<thenbwords<<" > "<<MAXCARS<<" car.s, tronque"<<std::endl; continue;  }
121     unarg[nc] = val;  nc ++;
122   }
123   if (nc > 0) {
124     unarg[nc] = '\0'; thewords(thenbwords).Clear();
125     thewords(thenbwords).AssignCat(unarg);
126 #ifdef DEBUG_TRACE
127     std::cout<<"thewords("<<thenbwords<<")="<<unarg<<std::endl<<" .. Fin avec thenbwords="<<thenbwords+1<<std::endl;
128 #endif
129     thenbwords ++;
130   }
131 /*
132     aligner sur MAXWORDS
133   char l0[80],l1[80],l2[80],l3[80],l4[80],l5[80],l6[80],l7[80],l8[80],l9[80];
134   char m0[80],m1[80],m2[80],m3[80],m4[80],m5[80],m6[80],m7[80],m8[80],m9[80];
135   thenbwords = sscanf
136     (thecommand.ToCString(),"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
137      l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,m0,m1,m2,m3,m4,m5,m6,m7,m8,m9);
138   if (thenbwords <  0) thenbwords = 0;
139   if (thenbwords > MAXWORDS) thenbwords = MAXWORDS;
140   Standard_Integer nb = thewords.Upper();
141   for (i = 0; i <= nb; i ++) thewords(i).Clear();
142   switch (thenbwords) {
143     case 20 : thewords(19).AssignCat(m9);
144     case 19 : thewords(18).AssignCat(m8);
145     case 18 : thewords(17).AssignCat(m7);
146     case 17 : thewords(16).AssignCat(m6);
147     case 16 : thewords(15).AssignCat(m5);
148     case 15 : thewords(14).AssignCat(m4);
149     case 14 : thewords(13).AssignCat(m3);
150     case 13 : thewords(12).AssignCat(m2);
151     case 12 : thewords(11).AssignCat(m1);
152     case 11 : thewords(10).AssignCat(m0);
153     case 10 : thewords(9).AssignCat(l9);
154     case  9 : thewords(8).AssignCat(l8);
155     case  8 : thewords(7).AssignCat(l7);
156     case  7 : thewords(6).AssignCat(l6);
157     case  6 : thewords(5).AssignCat(l5);
158     case  5 : thewords(4).AssignCat(l4);
159     case  4 : thewords(3).AssignCat(l3);
160     case  3 : thewords(2).AssignCat(l2);
161     case  2 : thewords(1).AssignCat(l1);
162     case  1 : thewords(0).AssignCat(l0);
163     default : break;
164   }
165 */
166   thenumrec = 0;
167   theobjrec.Nullify();
168 }
169
170     const TCollection_AsciiString&  IFSelect_SessionPilot::CommandLine () const
171       {  return thecommand;  }
172
173     Standard_CString  IFSelect_SessionPilot::CommandPart
174   (const Standard_Integer numarg) const
175 {
176   if (numarg <= 0) return thecommand.ToCString();
177   if (numarg >= thenbwords) return "";
178   return &(thecommand.ToCString())[thewordeb(numarg)-1];
179 }
180
181     Standard_Integer  IFSelect_SessionPilot::NbWords () const 
182       {  return thenbwords;  }
183
184     const TCollection_AsciiString&  IFSelect_SessionPilot::Word
185   (const Standard_Integer num) const 
186       {  if (num < thenbwords) return thewords(num);  return nulword;  }
187
188     Standard_CString  IFSelect_SessionPilot::Arg
189   (const Standard_Integer num) const 
190       {  return Word(num).ToCString();  }
191
192     Standard_Boolean  IFSelect_SessionPilot::RemoveWord
193   (const Standard_Integer num)
194 {
195   if (num < 0 || num > thenbwords) return Standard_False;
196   Standard_Integer i; // svv Jan11 2000 : porting on DEC
197   for (i = num; i < thenbwords; i ++) {
198     thewords(i).Clear();
199     thewords(i).AssignCat(thewords(i+1).ToCString());
200   }
201   thewords(thenbwords).Clear();
202   thenbwords --;
203 //  Et refaire thecommand. Si num = 0, on supprime le debut (facile)
204   if (num == 0) {
205     thecommand.Remove(1,thewordeb(1));
206   } else {
207 //   Sinon, reconstituer, a partir des words
208     thecommand.Clear();
209     for (i = 0; i < thenbwords; i ++) {
210       if (i > 0) thecommand.AssignCat(" ");
211       thecommand.AssignCat(thewords(i));
212     }
213   }
214
215   return Standard_True;
216 }
217
218     Standard_Integer  IFSelect_SessionPilot::NbCommands () const 
219       {  return thecomlist.Length();  }
220
221     const TCollection_AsciiString&  IFSelect_SessionPilot::Command
222   (const Standard_Integer num) const 
223       {  return thecomlist(num);  }
224
225
226     IFSelect_ReturnStatus  IFSelect_SessionPilot::RecordItem
227   (const Handle(Standard_Transient)& item)
228 {
229   theobjrec = item;
230   return (item.IsNull() ? IFSelect_RetFail : IFSelect_RetDone);
231 }
232
233     Handle(Standard_Transient)  IFSelect_SessionPilot::RecordedItem () const
234       {  return theobjrec;  }
235
236     void  IFSelect_SessionPilot::Clear ()
237       { thecomlist.Clear(); }
238
239
240 //  #######################################################################
241 //  ########        CONTROLE D EXECUTION
242
243
244     IFSelect_ReturnStatus  IFSelect_SessionPilot::ReadScript
245   (const Standard_CString file)
246 {
247   FILE* fic; int lefic = 0;
248   if (file != NULL && file[0] != '\0') {
249     fic = OSD_OpenFile (file,"r");
250     if (fic) lefic = 1;
251     else { std::cout<<" ...   Script File "<<file<<" not found"<<std::endl; return IFSelect_RetFail; }
252     std::cout << " ...   Reading Script File " << file << std::endl;
253   }
254   else fic = stdin;
255   IFSelect_ReturnStatus stat = IFSelect_RetVoid;
256
257   for (;;) {
258     char ligne[100];
259     if (!lefic) std::cout << theprompt.ToCString();
260     ligne[0] = '\0';
261     if (fgets(ligne,100,fic) == NULL
262      || feof(fic) != 0)
263     {
264       break;
265     }
266     if (ligne[0] == '\0') continue;
267 //    On interprete cette commande
268     TCollection_AsciiString command(ligne);
269     if (lefic) std::cout<<file<<":"<<command;  // le return est dans la ligne ... !
270     stat = Execute(command);
271     if (stat == IFSelect_RetStop) break;
272     if ((stat == IFSelect_RetError || stat == IFSelect_RetFail) && lefic)
273       { std::cout << " ...   Error in Script File, abandon"<<std::endl;  break; }
274   }
275   if (!lefic) return IFSelect_RetStop;
276   fclose(fic);
277   std::cout<<"End of Reading Script File " << file << std::endl;
278   if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return stat;
279   return IFSelect_RetVoid;        // fin fichier : depiler
280 }
281
282
283 //  On boucle sur la lecture jusqu a une commande de fin ou un EOF
284
285     IFSelect_ReturnStatus  IFSelect_SessionPilot::Perform ()
286 {
287   IFSelect_ReturnStatus stat = IFSelect_RetVoid;
288   if (thenbwords == 0) return stat;
289   if (thewords(0).Value(1) == '#') return stat;  // commentaire
290
291   theobjrec.Nullify();
292 //  Est-ce un nom ?
293
294 //  Commande pour un Acteur
295   Handle(IFSelect_Activator) actor;  Standard_Integer num;
296   if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
297     stat = actor->Do(num,this);
298 //  Prise en compte des commandes a resultat
299 //  Ici, resultat non nomme;  Resultat nomme par commande x (plus loin)
300     if (!theobjrec.IsNull()) {
301       thesession->RemoveItem(theobjrec);  //// depannage ?
302       Standard_Integer addws = thesession->AddItem(theobjrec);
303       if (addws == 0) { std::cout<<"Could not add item to session, sorry"<<std::endl; return IFSelect_RetFail; }
304     }
305
306     if (stat == IFSelect_RetVoid || stat == IFSelect_RetDone) {
307       if (therecord) thecomlist.Append(thecommand);
308     }
309     else if (stat == IFSelect_RetError) std::cout<<"Error in Command : "<<thecommand<<std::endl;
310     else if (stat == IFSelect_RetFail) std::cout << "Execution Failure for : " <<thecommand<<std::endl;
311     return stat;
312   }
313   std::cout << " Command : " << thewords(0) << " unknown" << std::endl;
314   return IFSelect_RetError;    // pas reconnu donc incorrect
315 }
316
317     IFSelect_ReturnStatus  IFSelect_SessionPilot::ExecuteAlias
318   (const TCollection_AsciiString& alias)
319 {
320   if (alias.Length() > 0) thewords(0) = alias;
321   return Perform();
322 }
323
324     IFSelect_ReturnStatus  IFSelect_SessionPilot::Execute
325   (const TCollection_AsciiString& command)
326 {
327   SetCommandLine(command);
328   return Perform();
329 }
330
331     IFSelect_ReturnStatus  IFSelect_SessionPilot::ExecuteCounter
332   (const Handle(IFSelect_SignCounter)& counter, const Standard_Integer numword,
333    const IFSelect_PrintCount mode)
334 {
335   if (counter.IsNull()) return IFSelect_RetError;
336   counter->Clear();
337   if (NbWords() <= numword) counter->AddModel (thesession->Model());
338   else {
339 //   on demande un givelist
340     Handle(TColStd_HSequenceOfTransient) list = thesession->GiveList (CommandPart(numword));
341     if (list.IsNull()) {
342       std::cout<<"Nothing selected from : "<<CommandPart(numword)<<std::endl;
343       return IFSelect_RetError;
344     }
345     counter->AddWithGraph (list,thesession->Graph());
346   }
347   Message_Messenger::StreamBuffer sout = Message::SendInfo();
348   counter->PrintList (sout, thesession->Model(), mode);
349   return IFSelect_RetVoid;
350 }
351
352     Standard_Integer  IFSelect_SessionPilot::Number
353   (const Standard_CString val) const
354 {
355   Standard_Integer num = thesession->NumberFromLabel (val);
356   if (num < 0)  std::cout<<" Label:"<<val<<" ->"<<-num<<" ent.s, refus"<<std::endl;
357   return num;
358 }
359
360
361 //  #########################################################################
362 //  ########        ACTIONS SPECIFIQUES DU PILOTE
363
364 #define MAXCOMPERLINE  5
365 #define LENGTHFORCOM  15
366
367     IFSelect_ReturnStatus  IFSelect_SessionPilot::Do
368   (const Standard_Integer number,
369    const Handle(IFSelect_SessionPilot)& session)
370 {
371 //                  Commandes Propres : x, exit, undo, redo, ?, help
372   IFSelect_ReturnStatus  stat = IFSelect_RetVoid;
373   Standard_Integer       argc = NbWords();
374   const Standard_CString arg1 = Word(1).ToCString();
375   Standard_Integer       modhelp = -1;
376   switch (number) {
377     case -1 :                                 //        ****     HELP-XSNEW
378       modhelp = 1;
379       std::cout<<"  --  Commands candidate for  xsnew  --"<<std::endl;
380 //  HELP : soit complet (par defaut)  soit limite a xsnew
381       Standard_FALLTHROUGH
382     case  0 : {                               //        ****     HELP
383       Handle(TColStd_HSequenceOfAsciiString) list;
384 //    Help complet : on donne la liste des commandes, sans plus (deja pas mal)
385       if (thenbwords <= 1) {
386         list = IFSelect_Activator::Commands(modhelp);
387         Standard_Integer nbcom = 0;
388         Standard_Integer nb = list->Length();
389         std::cout << " -- Liste des Commands Disponibles --"<<std::endl;
390         for (Standard_Integer i = 1; i <= nb; i ++) {
391           const TCollection_AsciiString& uncom = list->Value(i);
392           Standard_Integer loncom = uncom.Length();
393           nbcom ++;
394           if (nbcom > MAXCOMPERLINE) { std::cout<<std::endl; nbcom = 1; }
395           std::cout<<" "<<uncom;
396           if (nbcom == MAXCOMPERLINE) continue;
397           for (Standard_Integer j = loncom; j < LENGTHFORCOM; j ++) std::cout<<" ";
398         }
399         if (nbcom > 0) std::cout<<std::endl;
400         std::cout<<"\nhelp *  liste toutes les commandes avec un help sur chacune\n"
401           <<"help <com> liste la ou les commande debutant par <com>"
402           <<" avec un help sur chacune"<<std::endl;
403
404 //    Un Help particulier
405       } else {
406         if (thewords(1).IsEqual("*")) list = IFSelect_Activator::Commands(modhelp);
407
408         else list = IFSelect_Activator::Commands(modhelp,thewords(1).ToCString());
409
410         Standard_Integer nb = list->Length();
411         for (Standard_Integer i = 1; i <= nb; i ++) {
412           Handle(IFSelect_Activator) actor;  Standard_Integer num;
413           if (IFSelect_Activator::Select
414               (list->Value(i).ToCString(),num,actor)) {
415             if (IFSelect_Activator::Mode (list->Value(i).ToCString()) == 1)
416               std::cout<<"[xsnew name] ";
417             std::cout << list->Value(i) << "    : " << actor->Help(num) << std::endl;
418           }
419         }
420         if (nb == 0 && thenbwords > 1) std::cout<<" Command "<<Word(1)<<" unknown. "
421           << " help (without command) lists all the commands" << std::endl;
422       }
423       return IFSelect_RetVoid;
424     }
425     case  1 : return IFSelect_RetStop;        //        ****     Fin de session
426     case  2 : {                               //        ****     HELP
427       return Do(0,this);
428     }
429     case  3 : {                               //        ****     COMMAND
430       if (argc < 2) { std::cout << "Donner une option :\n"
431          <<"a : analyse une ligne  r : toggle record mode\n"
432          <<"l : list recorded  c : clear  f nom : sauver dans fichier de nom"
433          << std::endl; return IFSelect_RetVoid; }
434       switch (arg1[0]) {
435         case 'a' : {                          //        ****    command analyse
436           std::cout<<"Command n0 " << number <<" : "<< session->CommandLine()<<std::endl;
437           std::cout<<"Nb Words : " << argc-2 << " :\n";
438           for (Standard_Integer i = 2; i < argc; i ++) {
439             std::cout << " Word." << i-1 << " : " << session->Word(i) <<std::endl;
440           }
441           break;
442         }
443         case 'c' : session->Clear();  break;  //        ****    command clear
444         case 'f' : {
445           if (argc < 3) { std::cout<<"Donner nom de fichier"<<std::endl; return IFSelect_RetError; }
446           Standard_Integer nb = session->NbCommands();
447           if (nb == 0) { std::cout<<"Aucune commande enregistree"<<std::endl; break; }
448           std::cout << "Nb Commandes enregistrees : " << nb <<std::endl;
449           std::ofstream fout(Word(2).ToCString(),std::ios::out);
450           for (Standard_Integer i = 1; i <= nb; i ++)
451             fout<<session->Command(i)<<std::endl;
452           break;
453         }
454         case 'l' : {                          //        ****    command list
455           if (session->RecordMode()) std::cout<<"  -- Record Mode Actif"<<std::endl;
456           else                       std::cout<<"  -- Record Mode Inactif"<<std::endl;
457           Standard_Integer nb = session->NbCommands();
458           std::cout << "Nb Commandes enregistrees : " << nb << " :"<<std::endl;
459           for (Standard_Integer i = 1; i <= nb; i ++) {
460             std::cout<<"  "<<i<<"       "<<session->Command(i)<<std::endl;
461           }
462           break;
463         }
464         case 'r' : {                          //        ****    command record
465           Standard_Boolean mode = session->RecordMode();
466           if (mode) std::cout << " -- Record Mode a present Inactif" <<std::endl;
467           else      std::cout << " -- Record Mode a present Actif"   <<std::endl;
468           session->SetRecordMode(!mode);
469           break;
470         }
471         default  : std::cout << "Option de controle de commande non comprise"<<std::endl;
472       }
473       return IFSelect_RetVoid;
474     }
475
476     case  4 : {                               //        ****     FILE
477       if (argc < 2) { std::cout<<"Donner nom de fichier"<<std::endl; return IFSelect_RetError; }
478       return session->ReadScript
479         (TCollection_AsciiString(session->Word(1)).ToCString());
480 //          On recopie la string parce que Word(1) change tout le temps !
481     }
482
483     case  5 : {                               //        ****     XSTEP
484       if (argc < 2) {
485         std::cout<<"xstep : prefixe neutre pour toute commande xstep-draw"<<std::endl
486           <<"xstep command args  equivaut a  command args"<<std::endl;
487         return Do(2,this);
488       } else {
489         RemoveWord(0);
490         return Perform();
491       }
492     }
493     case  6 : {                               //        ****    XSNEW(variable)
494       if (argc < 3) {
495         std::cout<<"xsnew nomvar command [args]   creates an item"<<std::endl
496           <<"  nomvar : name of item (must be a new name) in the session"<<std::endl;
497         return Do (-1,this);
498       } else {
499
500         theobjrec.Nullify();
501         TCollection_AsciiString name = Word(1);
502 //  Le nom ne doit pas etre deja pris !
503           if (thesession.IsNull()) { std::cout<<"Command with a Name and no Session defined !"<<std::endl; return IFSelect_RetFail; }
504 //////    if (thesession->NameIdent(thewords(0).ToCString()) > 0)
505 //////      { std::cout<<"Command with name:"<<thewords(0)<<", already taken"<<std::endl; return IFSelect_RetFail; }
506         RemoveWord(0);  RemoveWord(0);
507
508 //  Commande pour un Acteur
509         Handle(IFSelect_Activator) actor;  Standard_Integer num;
510         if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
511           theobjrec.Nullify();
512           stat = actor->Do(num,this);
513 //  Prise en compte des commandes a resultat
514           if (!theobjrec.IsNull()) {
515             thesession->RemoveItem(theobjrec);  //// depannage ?
516             Standard_Integer addws =
517               thesession->AddNamedItem(name.ToCString(),theobjrec);
518             theobjrec.Nullify();
519             if (addws == 0) { std::cout<<"Could not add named item:"<<name<<", sorry"<<std::endl; return IFSelect_RetFail; }
520           }
521           else std::cout<<"Remark : xsnew with name:"<<name<<" and no result"<<std::endl;
522
523           return stat;
524         }
525         std::cout << " Command : " << thewords(0) << " unknown" << std::endl;
526         return IFSelect_RetError;    // pas reconnu donc incorrect
527       }
528     }
529     default : return IFSelect_RetError;
530   }
531 }
532
533
534
535     Standard_CString  IFSelect_SessionPilot::Help
536   (const Standard_Integer number) const
537 {
538   switch (number) {
539     case  1 : return "exit ou x : Fin de session";
540     case  2 : return "Liste les commandes. ? <titre> : commandes debutant par <titre>";
541     case  3 : return "controle de commande. command tout court pour help complet";
542     case  4 : return "lit les commandes depuis un fichier";
543     case  5 : return "prefixe neutre pour xstep-draw";
544     case  6 : return "creation item : donner nom_item puis commande args";
545     default : return "";
546   }
547 }