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>
37 static int initactor = 0;
40 static TCollection_AsciiString nulword;
42 // Nb Maxi de words : cf thewords et method SetCommandLine
44 IFSelect_SessionPilot::IFSelect_SessionPilot (const Standard_CString prompt)
45 : theprompt (prompt) , thewords (0,MAXWORDS-1) , thewordeb (0,MAXWORDS-1)
47 if (theprompt.Length() == 0) theprompt.AssignCat ("Test-XSTEP>");
48 therecord = Standard_False; thenbwords = 0;
49 if (initactor) return; initactor = 1;
58 trace = getenv("DEBUGMODE");
62 Handle(IFSelect_WorkSession) IFSelect_SessionPilot::Session () const
63 { return thesession; }
65 Handle(IFSelect_WorkLibrary) IFSelect_SessionPilot::Library () const
66 { return thesession->WorkLibrary(); }
68 Standard_Boolean IFSelect_SessionPilot::RecordMode () const
71 void IFSelect_SessionPilot::SetSession
72 (const Handle(IFSelect_WorkSession)& WS)
75 void IFSelect_SessionPilot::SetLibrary
76 (const Handle(IFSelect_WorkLibrary)& WL)
77 { if (!thesession.IsNull()) thesession->SetLibrary(WL); }
79 void IFSelect_SessionPilot::SetRecordMode (const Standard_Boolean mode)
83 void IFSelect_SessionPilot::SetCommandLine
84 (const TCollection_AsciiString& command)
86 Standard_Integer lc = command.Length();
87 if (lc > 200) cout<<" Commande TRES LONGUE : "<<lc<<" caracteres :"<<endl
88 <<command.ToCString()<<endl;
90 if (thecommand.Value(lc) <= ' ') { thecommand.Remove(lc); lc --; }
92 Standard_Integer i, nc = 0;
94 for (i = 1; i <= lc; i ++) {
95 char val = command.Value(i);
97 if (nc == 0) continue;
98 if (thenbwords >= MAXWORDS) { unarg[nc] = val; nc ++; continue; }
100 thewords(thenbwords).Clear(); thewords(thenbwords).AssignCat(unarg);
101 if (trace) cout<<"thewords("<<thenbwords<<") ="<<unarg<<endl;
102 thenbwords ++; nc = 0;
105 if (nc == 0) thewordeb.SetValue (thenbwords,i);
106 if (nc > MAXCARS) { cout<<"Arg."<<thenbwords<<" > "<<MAXCARS<<" car.s, tronque"<<endl; continue; }
107 unarg[nc] = val; nc ++;
110 unarg[nc] = '\0'; thewords(thenbwords).Clear();
111 thewords(thenbwords).AssignCat(unarg);
112 if (trace) cout<<"thewords("<<thenbwords<<")="<<unarg<<endl<<" .. Fin avec thenbwords="<<thenbwords+1<<endl;
117 char l0[80],l1[80],l2[80],l3[80],l4[80],l5[80],l6[80],l7[80],l8[80],l9[80];
118 char m0[80],m1[80],m2[80],m3[80],m4[80],m5[80],m6[80],m7[80],m8[80],m9[80];
120 (thecommand.ToCString(),"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
121 l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,m0,m1,m2,m3,m4,m5,m6,m7,m8,m9);
122 if (thenbwords < 0) thenbwords = 0;
123 if (thenbwords > MAXWORDS) thenbwords = MAXWORDS;
124 Standard_Integer nb = thewords.Upper();
125 for (i = 0; i <= nb; i ++) thewords(i).Clear();
126 switch (thenbwords) {
127 case 20 : thewords(19).AssignCat(m9);
128 case 19 : thewords(18).AssignCat(m8);
129 case 18 : thewords(17).AssignCat(m7);
130 case 17 : thewords(16).AssignCat(m6);
131 case 16 : thewords(15).AssignCat(m5);
132 case 15 : thewords(14).AssignCat(m4);
133 case 14 : thewords(13).AssignCat(m3);
134 case 13 : thewords(12).AssignCat(m2);
135 case 12 : thewords(11).AssignCat(m1);
136 case 11 : thewords(10).AssignCat(m0);
137 case 10 : thewords(9).AssignCat(l9);
138 case 9 : thewords(8).AssignCat(l8);
139 case 8 : thewords(7).AssignCat(l7);
140 case 7 : thewords(6).AssignCat(l6);
141 case 6 : thewords(5).AssignCat(l5);
142 case 5 : thewords(4).AssignCat(l4);
143 case 4 : thewords(3).AssignCat(l3);
144 case 3 : thewords(2).AssignCat(l2);
145 case 2 : thewords(1).AssignCat(l1);
146 case 1 : thewords(0).AssignCat(l0);
154 const TCollection_AsciiString& IFSelect_SessionPilot::CommandLine () const
155 { return thecommand; }
157 Standard_CString IFSelect_SessionPilot::CommandPart
158 (const Standard_Integer numarg) const
160 if (numarg <= 0) return thecommand.ToCString();
161 if (numarg >= thenbwords) return "";
162 return &(thecommand.ToCString())[thewordeb(numarg)-1];
165 Standard_Integer IFSelect_SessionPilot::NbWords () const
166 { return thenbwords; }
168 const TCollection_AsciiString& IFSelect_SessionPilot::Word
169 (const Standard_Integer num) const
170 { if (num < thenbwords) return thewords(num); return nulword; }
172 Standard_CString IFSelect_SessionPilot::Arg
173 (const Standard_Integer num) const
174 { return Word(num).ToCString(); }
176 Standard_Boolean IFSelect_SessionPilot::RemoveWord
177 (const Standard_Integer num)
179 if (num < 0 || num > thenbwords) return Standard_False;
180 Standard_Integer i; // svv Jan11 2000 : porting on DEC
181 for (i = num; i < thenbwords; i ++) {
183 thewords(i).AssignCat(thewords(i+1).ToCString());
185 thewords(thenbwords).Clear();
187 // Et refaire thecommand. Si num = 0, on supprime le debut (facile)
189 thecommand.Remove(1,thewordeb(1));
191 // Sinon, reconstituer, a partir des words
193 for (i = 0; i < thenbwords; i ++) {
194 if (i > 0) thecommand.AssignCat(" ");
195 thecommand.AssignCat(thewords(i));
199 return Standard_True;
202 Standard_Integer IFSelect_SessionPilot::NbCommands () const
203 { return thecomlist.Length(); }
205 const TCollection_AsciiString& IFSelect_SessionPilot::Command
206 (const Standard_Integer num) const
207 { return thecomlist(num); }
210 IFSelect_ReturnStatus IFSelect_SessionPilot::RecordItem
211 (const Handle(Standard_Transient)& item)
214 return (item.IsNull() ? IFSelect_RetFail : IFSelect_RetDone);
217 Handle(Standard_Transient) IFSelect_SessionPilot::RecordedItem () const
218 { return theobjrec; }
220 void IFSelect_SessionPilot::Clear ()
221 { thecomlist.Clear(); }
224 // #######################################################################
225 // ######## CONTROLE D EXECUTION
228 IFSelect_ReturnStatus IFSelect_SessionPilot::ReadScript
229 (const Standard_CString file)
231 FILE* fic; int lefic = 0;
232 if (file != NULL && file[0] != '\0') {
233 fic = OSD_OpenFile (file,"r");
235 else { cout<<" ... Script File "<<file<<" not found"<<endl; return IFSelect_RetFail; }
236 cout << " ... Reading Script File " << file << endl;
239 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
243 if (!lefic) std::cout << theprompt.ToCString();
245 if (fgets(ligne,100,fic) == NULL
250 if (ligne[0] == '\0') continue;
251 // On interprete cette commande
252 TCollection_AsciiString command(ligne);
253 if (lefic) cout<<file<<":"<<command; // le return est dans la ligne ... !
254 stat = Execute(command);
255 if (stat == IFSelect_RetStop) break;
256 if ((stat == IFSelect_RetError || stat == IFSelect_RetFail) && lefic)
257 { cout << " ... Error in Script File, abandon"<<endl; break; }
259 if (!lefic) return IFSelect_RetStop;
261 cout<<"End of Reading Script File " << file << endl;
262 if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return stat;
263 return IFSelect_RetVoid; // fin fichier : depiler
267 // On boucle sur la lecture jusqu a une commande de fin ou un EOF
269 IFSelect_ReturnStatus IFSelect_SessionPilot::Perform ()
271 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
272 if (thenbwords == 0) return stat;
273 if (thewords(0).Value(1) == '#') return stat; // commentaire
278 // Commande pour un Acteur
279 Handle(IFSelect_Activator) actor; Standard_Integer num;
280 if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
281 stat = actor->Do(num,this);
282 // Prise en compte des commandes a resultat
283 // Ici, resultat non nomme; Resultat nomme par commande x (plus loin)
284 if (!theobjrec.IsNull()) {
285 thesession->RemoveItem(theobjrec); //// depannage ?
286 Standard_Boolean addws = thesession->AddItem(theobjrec);
287 if (!addws) { cout<<"Could not add item to session, sorry"<<endl; return IFSelect_RetFail; }
290 if (stat == IFSelect_RetVoid || stat == IFSelect_RetDone) {
291 if (therecord) thecomlist.Append(thecommand);
293 else if (stat == IFSelect_RetError) cout<<"Error in Command : "<<thecommand<<endl;
294 else if (stat == IFSelect_RetFail) cout << "Execution Failure for : " <<thecommand<<endl;
297 cout << " Command : " << thewords(0) << " unknown" << endl;
298 return IFSelect_RetError; // pas reconnu donc incorrect
301 IFSelect_ReturnStatus IFSelect_SessionPilot::ExecuteAlias
302 (const TCollection_AsciiString& alias)
304 if (alias.Length() > 0) thewords(0) = alias;
308 IFSelect_ReturnStatus IFSelect_SessionPilot::Execute
309 (const TCollection_AsciiString& command)
311 SetCommandLine(command);
315 IFSelect_ReturnStatus IFSelect_SessionPilot::ExecuteCounter
316 (const Handle(IFSelect_SignCounter)& counter, const Standard_Integer numword,
317 const IFSelect_PrintCount mode)
319 if (counter.IsNull()) return IFSelect_RetError;
321 if (NbWords() <= numword) counter->AddModel (thesession->Model());
323 // on demande un givelist
324 Handle(TColStd_HSequenceOfTransient) list = thesession->GiveList (CommandPart(numword));
326 cout<<"Nothing selected from : "<<CommandPart(numword)<<endl;
327 return IFSelect_RetError;
329 counter->AddWithGraph (list,thesession->Graph());
331 counter->PrintList(Message::DefaultMessenger(),thesession->Model(),mode);
332 return IFSelect_RetVoid;
335 Standard_Integer IFSelect_SessionPilot::Number
336 (const Standard_CString val) const
338 Standard_Integer num = thesession->NumberFromLabel (val);
339 if (num < 0) cout<<" Label:"<<val<<" ->"<<-num<<" ent.s, refus"<<endl;
344 // #########################################################################
345 // ######## ACTIONS SPECIFIQUES DU PILOTE
347 #define MAXCOMPERLINE 5
348 #define LENGTHFORCOM 15
350 IFSelect_ReturnStatus IFSelect_SessionPilot::Do
351 (const Standard_Integer number,
352 const Handle(IFSelect_SessionPilot)& session)
354 // Commandes Propres : x, exit, undo, redo, ?, help
355 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
356 Standard_Integer argc = NbWords();
357 const Standard_CString arg1 = Word(1).ToCString();
358 Standard_Integer modhelp = -1;
360 case -1 : // **** HELP-XSNEW
362 cout<<" -- Commands candidate for xsnew --"<<endl;
363 // HELP : soit complet (par defaut) soit limite a xsnew
364 case 0 : { // **** HELP
365 Handle(TColStd_HSequenceOfAsciiString) list;
366 // Help complet : on donne la liste des commandes, sans plus (deja pas mal)
367 if (thenbwords <= 1) {
368 list = IFSelect_Activator::Commands(modhelp);
369 Standard_Integer nbcom = 0;
370 Standard_Integer nb = list->Length();
371 cout << " -- Liste des Commands Disponibles --"<<endl;
372 for (Standard_Integer i = 1; i <= nb; i ++) {
373 const TCollection_AsciiString& uncom = list->Value(i);
374 Standard_Integer loncom = uncom.Length();
376 if (nbcom > MAXCOMPERLINE) { cout<<endl; nbcom = 1; }
378 if (nbcom == MAXCOMPERLINE) continue;
379 for (Standard_Integer j = loncom; j < LENGTHFORCOM; j ++) cout<<" ";
381 if (nbcom > 0) cout<<endl;
382 cout<<"\nhelp * liste toutes les commandes avec un help sur chacune\n"
383 <<"help <com> liste la ou les commande debutant par <com>"
384 <<" avec un help sur chacune"<<endl;
386 // Un Help particulier
388 if (thewords(1).IsEqual("*")) list = IFSelect_Activator::Commands(modhelp);
390 else list = IFSelect_Activator::Commands(modhelp,thewords(1).ToCString());
392 Standard_Integer nb = list->Length();
393 for (Standard_Integer i = 1; i <= nb; i ++) {
394 Handle(IFSelect_Activator) actor; Standard_Integer num;
395 if (IFSelect_Activator::Select
396 (list->Value(i).ToCString(),num,actor)) {
397 if (IFSelect_Activator::Mode (list->Value(i).ToCString()) == 1)
398 cout<<"[xsnew name] ";
399 cout << list->Value(i) << " : " << actor->Help(num) << endl;
402 if (nb == 0 && thenbwords > 1) cout<<" Command "<<Word(1)<<" unknown. "
403 << " help (without command) lists all the commands" << endl;
405 return IFSelect_RetVoid;
407 case 1 : return IFSelect_RetStop; // **** Fin de session
408 case 2 : { // **** HELP
411 case 3 : { // **** COMMAND
412 if (argc < 2) { cout << "Donner une option :\n"
413 <<"a : analyse une ligne r : toggle record mode\n"
414 <<"l : list recorded c : clear f nom : sauver dans fichier de nom"
415 << endl; return IFSelect_RetVoid; }
417 case 'a' : { // **** command analyse
418 cout<<"Command n0 " << number <<" : "<< session->CommandLine()<<endl;
419 cout<<"Nb Words : " << argc-2 << " :\n";
420 for (Standard_Integer i = 2; i < argc; i ++) {
421 cout << " Word." << i-1 << " : " << session->Word(i) <<endl;
425 case 'c' : session->Clear(); break; // **** command clear
427 if (argc < 3) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
428 Standard_Integer nb = session->NbCommands();
429 if (nb == 0) { cout<<"Aucune commande enregistree"<<endl; break; }
430 cout << "Nb Commandes enregistrees : " << nb <<endl;
431 ofstream fout(Word(2).ToCString(),ios::out);
432 for (Standard_Integer i = 1; i <= nb; i ++)
433 fout<<session->Command(i)<<endl;
436 case 'l' : { // **** command list
437 if (session->RecordMode()) cout<<" -- Record Mode Actif"<<endl;
438 else cout<<" -- Record Mode Inactif"<<endl;
439 Standard_Integer nb = session->NbCommands();
440 cout << "Nb Commandes enregistrees : " << nb << " :"<<endl;
441 for (Standard_Integer i = 1; i <= nb; i ++) {
442 cout<<" "<<i<<" "<<session->Command(i)<<endl;
446 case 'r' : { // **** command record
447 Standard_Boolean mode = session->RecordMode();
448 if (mode) cout << " -- Record Mode a present Inactif" <<endl;
449 else cout << " -- Record Mode a present Actif" <<endl;
450 session->SetRecordMode(!mode);
453 default : cout << "Option de controle de commande non comprise"<<endl;
455 return IFSelect_RetVoid;
458 case 4 : { // **** FILE
459 if (argc < 2) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
460 return session->ReadScript
461 (TCollection_AsciiString(session->Word(1)).ToCString());
462 // On recopie la string parce que Word(1) change tout le temps !
465 case 5 : { // **** XSTEP
467 cout<<"xstep : prefixe neutre pour toute commande xstep-draw"<<endl
468 <<"xstep command args equivaut a command args"<<endl;
475 case 6 : { // **** XSNEW(variable)
477 cout<<"xsnew nomvar command [args] creates an item"<<endl
478 <<" nomvar : name of item (must be a new name) in the session"<<endl;
483 TCollection_AsciiString name = Word(1);
484 // Le nom ne doit pas etre deja pris !
485 if (thesession.IsNull()) { cout<<"Command with a Name and no Session defined !"<<endl; return IFSelect_RetFail; }
486 ////// if (thesession->NameIdent(thewords(0).ToCString()) > 0)
487 ////// { cout<<"Command with name:"<<thewords(0)<<", already taken"<<endl; return IFSelect_RetFail; }
488 RemoveWord(0); RemoveWord(0);
490 // Commande pour un Acteur
491 Handle(IFSelect_Activator) actor; Standard_Integer num;
492 if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
494 stat = actor->Do(num,this);
495 // Prise en compte des commandes a resultat
496 if (!theobjrec.IsNull()) {
497 thesession->RemoveItem(theobjrec); //// depannage ?
498 Standard_Boolean addws =
499 thesession->AddNamedItem(name.ToCString(),theobjrec);
501 if (!addws) { cout<<"Could not add named item:"<<name<<", sorry"<<endl; return IFSelect_RetFail; }
503 else cout<<"Remark : xsnew with name:"<<name<<" and no result"<<endl;
507 cout << " Command : " << thewords(0) << " unknown" << endl;
508 return IFSelect_RetError; // pas reconnu donc incorrect
511 default : return IFSelect_RetError;
517 Standard_CString IFSelect_SessionPilot::Help
518 (const Standard_Integer number) const
521 case 1 : return "exit ou x : Fin de session";
522 case 2 : return "Liste les commandes. ? <titre> : commandes debutant par <titre>";
523 case 3 : return "controle de commande. command tout court pour help complet";
524 case 4 : return "lit les commandes depuis un fichier";
525 case 5 : return "prefixe neutre pour xstep-draw";
526 case 6 : return "creation item : donner nom_item puis commande args";