1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
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>
34 IMPLEMENT_STANDARD_RTTIEXT(IFSelect_SessionPilot,IFSelect_Activator)
39 static int initactor = 0;
40 static TCollection_AsciiString nulword;
44 // Nb Maxi de words : cf thewords et method SetCommandLine
46 IFSelect_SessionPilot::IFSelect_SessionPilot (const Standard_CString prompt)
47 : theprompt (prompt) , thewords (0,MAXWORDS-1) , thewordeb (0,MAXWORDS-1)
49 if (theprompt.Length() == 0) theprompt.AssignCat ("Test-XSTEP>");
50 therecord = Standard_False; thenbwords = 0;
51 if (initactor) return; initactor = 1;
63 Handle(IFSelect_WorkSession) IFSelect_SessionPilot::Session () const
64 { return thesession; }
66 Handle(IFSelect_WorkLibrary) IFSelect_SessionPilot::Library () const
67 { return thesession->WorkLibrary(); }
69 Standard_Boolean IFSelect_SessionPilot::RecordMode () const
72 void IFSelect_SessionPilot::SetSession
73 (const Handle(IFSelect_WorkSession)& WS)
76 void IFSelect_SessionPilot::SetLibrary
77 (const Handle(IFSelect_WorkLibrary)& WL)
78 { if (!thesession.IsNull()) thesession->SetLibrary(WL); }
80 void IFSelect_SessionPilot::SetRecordMode (const Standard_Boolean mode)
84 void IFSelect_SessionPilot::SetCommandLine
85 (const TCollection_AsciiString& command)
87 Standard_Integer lc = command.Length();
88 if (lc > 200) cout<<" Commande TRES LONGUE : "<<lc<<" caracteres :"<<endl
89 <<command.ToCString()<<endl;
91 if (thecommand.Value(lc) <= ' ') { thecommand.Remove(lc); lc --; }
93 Standard_Integer i, nc = 0;
95 for (i = 1; i <= lc; i ++) {
96 char val = command.Value(i);
98 if (nc == 0) continue;
99 if (thenbwords >= MAXWORDS) { unarg[nc] = val; nc ++; continue; }
101 thewords(thenbwords).Clear(); thewords(thenbwords).AssignCat(unarg);
103 cout<<"thewords("<<thenbwords<<") ="<<unarg<<endl;
105 thenbwords ++; nc = 0;
108 if (nc == 0) thewordeb.SetValue (thenbwords,i);
109 if (nc > MAXCARS) { cout<<"Arg."<<thenbwords<<" > "<<MAXCARS<<" car.s, tronque"<<endl; continue; }
110 unarg[nc] = val; nc ++;
113 unarg[nc] = '\0'; thewords(thenbwords).Clear();
114 thewords(thenbwords).AssignCat(unarg);
116 cout<<"thewords("<<thenbwords<<")="<<unarg<<endl<<" .. Fin avec thenbwords="<<thenbwords+1<<endl;
122 char l0[80],l1[80],l2[80],l3[80],l4[80],l5[80],l6[80],l7[80],l8[80],l9[80];
123 char m0[80],m1[80],m2[80],m3[80],m4[80],m5[80],m6[80],m7[80],m8[80],m9[80];
125 (thecommand.ToCString(),"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
126 l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,m0,m1,m2,m3,m4,m5,m6,m7,m8,m9);
127 if (thenbwords < 0) thenbwords = 0;
128 if (thenbwords > MAXWORDS) thenbwords = MAXWORDS;
129 Standard_Integer nb = thewords.Upper();
130 for (i = 0; i <= nb; i ++) thewords(i).Clear();
131 switch (thenbwords) {
132 case 20 : thewords(19).AssignCat(m9);
133 case 19 : thewords(18).AssignCat(m8);
134 case 18 : thewords(17).AssignCat(m7);
135 case 17 : thewords(16).AssignCat(m6);
136 case 16 : thewords(15).AssignCat(m5);
137 case 15 : thewords(14).AssignCat(m4);
138 case 14 : thewords(13).AssignCat(m3);
139 case 13 : thewords(12).AssignCat(m2);
140 case 12 : thewords(11).AssignCat(m1);
141 case 11 : thewords(10).AssignCat(m0);
142 case 10 : thewords(9).AssignCat(l9);
143 case 9 : thewords(8).AssignCat(l8);
144 case 8 : thewords(7).AssignCat(l7);
145 case 7 : thewords(6).AssignCat(l6);
146 case 6 : thewords(5).AssignCat(l5);
147 case 5 : thewords(4).AssignCat(l4);
148 case 4 : thewords(3).AssignCat(l3);
149 case 3 : thewords(2).AssignCat(l2);
150 case 2 : thewords(1).AssignCat(l1);
151 case 1 : thewords(0).AssignCat(l0);
159 const TCollection_AsciiString& IFSelect_SessionPilot::CommandLine () const
160 { return thecommand; }
162 Standard_CString IFSelect_SessionPilot::CommandPart
163 (const Standard_Integer numarg) const
165 if (numarg <= 0) return thecommand.ToCString();
166 if (numarg >= thenbwords) return "";
167 return &(thecommand.ToCString())[thewordeb(numarg)-1];
170 Standard_Integer IFSelect_SessionPilot::NbWords () const
171 { return thenbwords; }
173 const TCollection_AsciiString& IFSelect_SessionPilot::Word
174 (const Standard_Integer num) const
175 { if (num < thenbwords) return thewords(num); return nulword; }
177 Standard_CString IFSelect_SessionPilot::Arg
178 (const Standard_Integer num) const
179 { return Word(num).ToCString(); }
181 Standard_Boolean IFSelect_SessionPilot::RemoveWord
182 (const Standard_Integer num)
184 if (num < 0 || num > thenbwords) return Standard_False;
185 Standard_Integer i; // svv Jan11 2000 : porting on DEC
186 for (i = num; i < thenbwords; i ++) {
188 thewords(i).AssignCat(thewords(i+1).ToCString());
190 thewords(thenbwords).Clear();
192 // Et refaire thecommand. Si num = 0, on supprime le debut (facile)
194 thecommand.Remove(1,thewordeb(1));
196 // Sinon, reconstituer, a partir des words
198 for (i = 0; i < thenbwords; i ++) {
199 if (i > 0) thecommand.AssignCat(" ");
200 thecommand.AssignCat(thewords(i));
204 return Standard_True;
207 Standard_Integer IFSelect_SessionPilot::NbCommands () const
208 { return thecomlist.Length(); }
210 const TCollection_AsciiString& IFSelect_SessionPilot::Command
211 (const Standard_Integer num) const
212 { return thecomlist(num); }
215 IFSelect_ReturnStatus IFSelect_SessionPilot::RecordItem
216 (const Handle(Standard_Transient)& item)
219 return (item.IsNull() ? IFSelect_RetFail : IFSelect_RetDone);
222 Handle(Standard_Transient) IFSelect_SessionPilot::RecordedItem () const
223 { return theobjrec; }
225 void IFSelect_SessionPilot::Clear ()
226 { thecomlist.Clear(); }
229 // #######################################################################
230 // ######## CONTROLE D EXECUTION
233 IFSelect_ReturnStatus IFSelect_SessionPilot::ReadScript
234 (const Standard_CString file)
236 FILE* fic; int lefic = 0;
237 if (file != NULL && file[0] != '\0') {
238 fic = OSD_OpenFile (file,"r");
240 else { cout<<" ... Script File "<<file<<" not found"<<endl; return IFSelect_RetFail; }
241 cout << " ... Reading Script File " << file << endl;
244 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
248 if (!lefic) std::cout << theprompt.ToCString();
250 if (fgets(ligne,100,fic) == NULL
255 if (ligne[0] == '\0') continue;
256 // On interprete cette commande
257 TCollection_AsciiString command(ligne);
258 if (lefic) cout<<file<<":"<<command; // le return est dans la ligne ... !
259 stat = Execute(command);
260 if (stat == IFSelect_RetStop) break;
261 if ((stat == IFSelect_RetError || stat == IFSelect_RetFail) && lefic)
262 { cout << " ... Error in Script File, abandon"<<endl; break; }
264 if (!lefic) return IFSelect_RetStop;
266 cout<<"End of Reading Script File " << file << endl;
267 if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return stat;
268 return IFSelect_RetVoid; // fin fichier : depiler
272 // On boucle sur la lecture jusqu a une commande de fin ou un EOF
274 IFSelect_ReturnStatus IFSelect_SessionPilot::Perform ()
276 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
277 if (thenbwords == 0) return stat;
278 if (thewords(0).Value(1) == '#') return stat; // commentaire
283 // Commande pour un Acteur
284 Handle(IFSelect_Activator) actor; Standard_Integer num;
285 if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
286 stat = actor->Do(num,this);
287 // Prise en compte des commandes a resultat
288 // Ici, resultat non nomme; Resultat nomme par commande x (plus loin)
289 if (!theobjrec.IsNull()) {
290 thesession->RemoveItem(theobjrec); //// depannage ?
291 Standard_Boolean addws = thesession->AddItem(theobjrec);
292 if (!addws) { cout<<"Could not add item to session, sorry"<<endl; return IFSelect_RetFail; }
295 if (stat == IFSelect_RetVoid || stat == IFSelect_RetDone) {
296 if (therecord) thecomlist.Append(thecommand);
298 else if (stat == IFSelect_RetError) cout<<"Error in Command : "<<thecommand<<endl;
299 else if (stat == IFSelect_RetFail) cout << "Execution Failure for : " <<thecommand<<endl;
302 cout << " Command : " << thewords(0) << " unknown" << endl;
303 return IFSelect_RetError; // pas reconnu donc incorrect
306 IFSelect_ReturnStatus IFSelect_SessionPilot::ExecuteAlias
307 (const TCollection_AsciiString& alias)
309 if (alias.Length() > 0) thewords(0) = alias;
313 IFSelect_ReturnStatus IFSelect_SessionPilot::Execute
314 (const TCollection_AsciiString& command)
316 SetCommandLine(command);
320 IFSelect_ReturnStatus IFSelect_SessionPilot::ExecuteCounter
321 (const Handle(IFSelect_SignCounter)& counter, const Standard_Integer numword,
322 const IFSelect_PrintCount mode)
324 if (counter.IsNull()) return IFSelect_RetError;
326 if (NbWords() <= numword) counter->AddModel (thesession->Model());
328 // on demande un givelist
329 Handle(TColStd_HSequenceOfTransient) list = thesession->GiveList (CommandPart(numword));
331 cout<<"Nothing selected from : "<<CommandPart(numword)<<endl;
332 return IFSelect_RetError;
334 counter->AddWithGraph (list,thesession->Graph());
336 counter->PrintList(Message::DefaultMessenger(),thesession->Model(),mode);
337 return IFSelect_RetVoid;
340 Standard_Integer IFSelect_SessionPilot::Number
341 (const Standard_CString val) const
343 Standard_Integer num = thesession->NumberFromLabel (val);
344 if (num < 0) cout<<" Label:"<<val<<" ->"<<-num<<" ent.s, refus"<<endl;
349 // #########################################################################
350 // ######## ACTIONS SPECIFIQUES DU PILOTE
352 #define MAXCOMPERLINE 5
353 #define LENGTHFORCOM 15
355 IFSelect_ReturnStatus IFSelect_SessionPilot::Do
356 (const Standard_Integer number,
357 const Handle(IFSelect_SessionPilot)& session)
359 // Commandes Propres : x, exit, undo, redo, ?, help
360 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
361 Standard_Integer argc = NbWords();
362 const Standard_CString arg1 = Word(1).ToCString();
363 Standard_Integer modhelp = -1;
365 case -1 : // **** HELP-XSNEW
367 cout<<" -- Commands candidate for xsnew --"<<endl;
368 // HELP : soit complet (par defaut) soit limite a xsnew
369 case 0 : { // **** HELP
370 Handle(TColStd_HSequenceOfAsciiString) list;
371 // Help complet : on donne la liste des commandes, sans plus (deja pas mal)
372 if (thenbwords <= 1) {
373 list = IFSelect_Activator::Commands(modhelp);
374 Standard_Integer nbcom = 0;
375 Standard_Integer nb = list->Length();
376 cout << " -- Liste des Commands Disponibles --"<<endl;
377 for (Standard_Integer i = 1; i <= nb; i ++) {
378 const TCollection_AsciiString& uncom = list->Value(i);
379 Standard_Integer loncom = uncom.Length();
381 if (nbcom > MAXCOMPERLINE) { cout<<endl; nbcom = 1; }
383 if (nbcom == MAXCOMPERLINE) continue;
384 for (Standard_Integer j = loncom; j < LENGTHFORCOM; j ++) cout<<" ";
386 if (nbcom > 0) cout<<endl;
387 cout<<"\nhelp * liste toutes les commandes avec un help sur chacune\n"
388 <<"help <com> liste la ou les commande debutant par <com>"
389 <<" avec un help sur chacune"<<endl;
391 // Un Help particulier
393 if (thewords(1).IsEqual("*")) list = IFSelect_Activator::Commands(modhelp);
395 else list = IFSelect_Activator::Commands(modhelp,thewords(1).ToCString());
397 Standard_Integer nb = list->Length();
398 for (Standard_Integer i = 1; i <= nb; i ++) {
399 Handle(IFSelect_Activator) actor; Standard_Integer num;
400 if (IFSelect_Activator::Select
401 (list->Value(i).ToCString(),num,actor)) {
402 if (IFSelect_Activator::Mode (list->Value(i).ToCString()) == 1)
403 cout<<"[xsnew name] ";
404 cout << list->Value(i) << " : " << actor->Help(num) << endl;
407 if (nb == 0 && thenbwords > 1) cout<<" Command "<<Word(1)<<" unknown. "
408 << " help (without command) lists all the commands" << endl;
410 return IFSelect_RetVoid;
412 case 1 : return IFSelect_RetStop; // **** Fin de session
413 case 2 : { // **** HELP
416 case 3 : { // **** COMMAND
417 if (argc < 2) { cout << "Donner une option :\n"
418 <<"a : analyse une ligne r : toggle record mode\n"
419 <<"l : list recorded c : clear f nom : sauver dans fichier de nom"
420 << endl; return IFSelect_RetVoid; }
422 case 'a' : { // **** command analyse
423 cout<<"Command n0 " << number <<" : "<< session->CommandLine()<<endl;
424 cout<<"Nb Words : " << argc-2 << " :\n";
425 for (Standard_Integer i = 2; i < argc; i ++) {
426 cout << " Word." << i-1 << " : " << session->Word(i) <<endl;
430 case 'c' : session->Clear(); break; // **** command clear
432 if (argc < 3) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
433 Standard_Integer nb = session->NbCommands();
434 if (nb == 0) { cout<<"Aucune commande enregistree"<<endl; break; }
435 cout << "Nb Commandes enregistrees : " << nb <<endl;
436 ofstream fout(Word(2).ToCString(),ios::out);
437 for (Standard_Integer i = 1; i <= nb; i ++)
438 fout<<session->Command(i)<<endl;
441 case 'l' : { // **** command list
442 if (session->RecordMode()) cout<<" -- Record Mode Actif"<<endl;
443 else cout<<" -- Record Mode Inactif"<<endl;
444 Standard_Integer nb = session->NbCommands();
445 cout << "Nb Commandes enregistrees : " << nb << " :"<<endl;
446 for (Standard_Integer i = 1; i <= nb; i ++) {
447 cout<<" "<<i<<" "<<session->Command(i)<<endl;
451 case 'r' : { // **** command record
452 Standard_Boolean mode = session->RecordMode();
453 if (mode) cout << " -- Record Mode a present Inactif" <<endl;
454 else cout << " -- Record Mode a present Actif" <<endl;
455 session->SetRecordMode(!mode);
458 default : cout << "Option de controle de commande non comprise"<<endl;
460 return IFSelect_RetVoid;
463 case 4 : { // **** FILE
464 if (argc < 2) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
465 return session->ReadScript
466 (TCollection_AsciiString(session->Word(1)).ToCString());
467 // On recopie la string parce que Word(1) change tout le temps !
470 case 5 : { // **** XSTEP
472 cout<<"xstep : prefixe neutre pour toute commande xstep-draw"<<endl
473 <<"xstep command args equivaut a command args"<<endl;
480 case 6 : { // **** XSNEW(variable)
482 cout<<"xsnew nomvar command [args] creates an item"<<endl
483 <<" nomvar : name of item (must be a new name) in the session"<<endl;
488 TCollection_AsciiString name = Word(1);
489 // Le nom ne doit pas etre deja pris !
490 if (thesession.IsNull()) { cout<<"Command with a Name and no Session defined !"<<endl; return IFSelect_RetFail; }
491 ////// if (thesession->NameIdent(thewords(0).ToCString()) > 0)
492 ////// { cout<<"Command with name:"<<thewords(0)<<", already taken"<<endl; return IFSelect_RetFail; }
493 RemoveWord(0); RemoveWord(0);
495 // Commande pour un Acteur
496 Handle(IFSelect_Activator) actor; Standard_Integer num;
497 if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
499 stat = actor->Do(num,this);
500 // Prise en compte des commandes a resultat
501 if (!theobjrec.IsNull()) {
502 thesession->RemoveItem(theobjrec); //// depannage ?
503 Standard_Boolean addws =
504 thesession->AddNamedItem(name.ToCString(),theobjrec);
506 if (!addws) { cout<<"Could not add named item:"<<name<<", sorry"<<endl; return IFSelect_RetFail; }
508 else cout<<"Remark : xsnew with name:"<<name<<" and no result"<<endl;
512 cout << " Command : " << thewords(0) << " unknown" << endl;
513 return IFSelect_RetError; // pas reconnu donc incorrect
516 default : return IFSelect_RetError;
522 Standard_CString IFSelect_SessionPilot::Help
523 (const Standard_Integer number) const
526 case 1 : return "exit ou x : Fin de session";
527 case 2 : return "Liste les commandes. ? <titre> : commandes debutant par <titre>";
528 case 3 : return "controle de commande. command tout court pour help complet";
529 case 4 : return "lit les commandes depuis un fichier";
530 case 5 : return "prefixe neutre pour xstep-draw";
531 case 6 : return "creation item : donner nom_item puis commande args";