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