0030697: Draw Harness - Draw_Printer should not be set to Message::DefaultMessenger...
[occt.git] / src / XSDRAW / XSDRAW.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 <DBRep.hxx>
16 #include <Draw_Appli.hxx>
17 #include <Draw_Printer.hxx>
18 #include <IFSelect_Functions.hxx>
19 #include <IFSelect_SessionPilot.hxx>
20 #include <Interface_InterfaceModel.hxx>
21 #include <Interface_Macros.hxx>
22 #include <Interface_Protocol.hxx>
23 #include <Message.hxx>
24 #include <Message_Messenger.hxx>
25 #include <Message_PrinterOStream.hxx>
26 #include <Standard_Transient.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TColStd_HSequenceOfAsciiString.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <Transfer_FinderProcess.hxx>
31 #include <Transfer_TransientProcess.hxx>
32 #include <TransferBRep.hxx>
33 #include <XSControl.hxx>
34 #include <XSControl_Controller.hxx>
35 #include <XSControl_FuncShape.hxx>
36 #include <XSControl_Functions.hxx>
37 #include <XSControl_TransferReader.hxx>
38 #include <XSControl_TransferWriter.hxx>
39 #include <XSControl_WorkSession.hxx>
40 #include <XSDRAW.hxx>
41 #include <XSDRAW_Vars.hxx>
42
43 #include <stdio.h>
44 //#include <XSDRAW_Shape.hxx>
45 static int deja = 0, dejald = 0;
46 //unused variable 
47 //static int okxset = 0;
48
49 static NCollection_DataMap<TCollection_AsciiString, Standard_Integer> theolds;
50 static Handle(TColStd_HSequenceOfAsciiString) thenews;
51
52 static Handle(IFSelect_SessionPilot)    thepilot;  // detient Session, Model
53
54 static Standard_Integer XSTEPDRAWRUN (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
55 {
56   TCollection_AsciiString mess;
57   for (Standard_Integer i = 0; i < argc; i ++) {
58     mess.AssignCat(argv[i]);  mess.AssignCat(" ");
59   }
60
61   const Handle(Message_Messenger)& aMsgMgr = Message::DefaultMessenger();
62   Message_SequenceOfPrinters aPrinters;
63   aPrinters.Append (aMsgMgr->ChangePrinters());
64   aMsgMgr->AddPrinter (new Draw_Printer (di));
65
66   IFSelect_ReturnStatus stat = thepilot->Execute (mess.ToCString());
67
68   aMsgMgr->RemovePrinters (STANDARD_TYPE(Draw_Printer));
69   aMsgMgr->ChangePrinters().Append (aPrinters);
70
71   if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return 1;
72   else return 0;
73 }
74
75     void  XSDRAW::ChangeCommand
76   (const Standard_CString oldname, const Standard_CString newname)
77 {
78   Standard_Integer num = 0;
79   if (newname[0] != '\0') {
80     if (thenews.IsNull()) thenews = new TColStd_HSequenceOfAsciiString();
81     TCollection_AsciiString newstr(newname);
82     thenews->Append(newstr);
83     num = thenews->Length();
84   }
85   theolds.Bind(oldname,num);
86 }
87
88     void  XSDRAW::RemoveCommand
89   (const Standard_CString oldname)
90 {
91   ChangeCommand (oldname,"");
92 }
93
94     Standard_Boolean  XSDRAW::LoadSession ()
95 {
96   if (deja) return Standard_False;
97   deja = 1;
98   thepilot   = new IFSelect_SessionPilot("XSTEP-DRAW>");
99   Handle(XSControl_WorkSession) WS = new XSControl_WorkSession;
100   WS->SetVars (new XSDRAW_Vars);
101   thepilot->SetSession (WS);
102
103   IFSelect_Functions::Init();
104   XSControl_Functions::Init();
105   XSControl_FuncShape::Init();
106 //  XSDRAW_Shape::Init();   passe a present par theCommands
107   return Standard_True;
108 }
109
110 void XSDRAW::LoadDraw (Draw_Interpretor& theCommands)
111 {
112   if (dejald)
113   {
114     return;
115   }
116   dejald = 1;
117 //  Pour tout faire d un coup : BRepTest & cie:
118   LoadSession();
119
120   //skl: we make remove commands "x" and "exit" in order to this commands are
121   //     performed not in IFSelect_SessionPilot but in standard Tcl interpretor
122   XSDRAW::RemoveCommand("x");
123   XSDRAW::RemoveCommand("exit");
124
125 //  if (!getenv("WBHOSTTOP")) XSDRAW::RemoveCommand("xsnew");
126   Handle(TColStd_HSequenceOfAsciiString) list =
127     IFSelect_Activator::Commands(0);
128   TCollection_AsciiString com;
129   Standard_Integer i, nb = list->Length();
130   for (i = 1; i <= nb; i ++) {
131     Handle(IFSelect_Activator) act;
132     Standard_Integer nact, num = -1;
133     char help[200];
134     com = list->Value(i);
135     if (!theolds.IsEmpty())
136       theolds.Find(com, num);
137     if (num == 0) continue;
138     if (!IFSelect_Activator::Select(com.ToCString(),nact,act))
139       Sprintf (help,"type :  xhelp %s for help",com.ToCString());
140     else if (!act.IsNull()) strcpy(help,act->Help(nact));
141     if (num < 0) theCommands.Add (com.ToCString(),help,XSTEPDRAWRUN,act->Group());
142     else theCommands.Add (thenews->Value(num).ToCString(),help,XSTEPDRAWRUN,act->Group());
143   }
144 }
145
146     Standard_Integer  XSDRAW::Execute
147     (const Standard_CString command, const Standard_CString varname)
148 {
149   char mess[100];
150   Sprintf (mess,command,varname);
151   thepilot->Execute (mess);
152   return 1;  // stat ?
153 }
154
155     Handle(IFSelect_SessionPilot)  XSDRAW::Pilot ()
156       {  return thepilot;  }
157
158     Handle(XSControl_WorkSession)  XSDRAW::Session ()
159       {  return XSControl::Session(thepilot);  }
160
161     void  XSDRAW::SetController (const Handle(XSControl_Controller)& control)
162 {
163   if (thepilot.IsNull()) XSDRAW::LoadSession();
164   if (control.IsNull()) std::cout<<"XSTEP Controller not defined"<<std::endl;
165   else if (!Session().IsNull()) Session()->SetController (control);
166   else std::cout<<"XSTEP Session badly or not defined"<<std::endl;
167 }
168
169
170     Handle(XSControl_Controller) XSDRAW::Controller ()
171       {  return Session()->NormAdaptor();  }
172
173     Standard_Boolean  XSDRAW::SetNorm
174   (const Standard_CString norm)
175 {
176   return Session()->SelectNorm (norm);
177 }
178
179     Handle(Interface_Protocol)   XSDRAW::Protocol ()
180       {  return thepilot->Session()->Protocol();  }
181
182     Handle(Interface_InterfaceModel)  XSDRAW::Model ()
183       {  return thepilot->Session()->Model();  }
184
185     void  XSDRAW::SetModel
186   (const Handle(Interface_InterfaceModel)& model, const Standard_CString file)
187 {
188   thepilot->Session()->SetModel(model);
189   if (file && file[0] != '\0') thepilot->Session()->SetLoadedFile(file);
190 }
191
192     Handle(Interface_InterfaceModel)  XSDRAW::NewModel ()
193       {  return Session()->NewModel();  }
194
195     Handle(Standard_Transient)  XSDRAW::Entity (const Standard_Integer num)
196       {  return thepilot->Session()->StartingEntity(num);  }
197
198     Standard_Integer  XSDRAW::Number (const Handle(Standard_Transient)& ent)
199       {  return thepilot->Session()->StartingNumber(ent);  }
200
201     void  XSDRAW::SetTransferProcess (const Handle(Standard_Transient)& ATP)
202 {
203   DeclareAndCast(Transfer_FinderProcess,FP,ATP);
204   DeclareAndCast(Transfer_TransientProcess,TP,ATP);
205
206 //   Cas FinderProcess    ==> TransferWriter
207   if (!FP.IsNull())  Session()->SetMapWriter(FP);
208
209 //   Cas TransientProcess ==> TransferReader
210   if (!TP.IsNull()) {
211     if (!TP->Model().IsNull() && TP->Model() != Session()->Model())
212       Session()->SetModel (TP->Model());
213     Session()->SetMapReader(TP);
214   }
215 }
216
217     Handle(Transfer_TransientProcess)  XSDRAW::TransientProcess  ()
218       {  return Session()->TransferReader()->TransientProcess();  }
219
220     Handle(Transfer_FinderProcess)  XSDRAW::FinderProcess  ()
221       {  return Session()->TransferWriter()->FinderProcess();  }
222
223
224     void XSDRAW::InitTransferReader (const Standard_Integer mode)
225 {
226 //   0 nullify  1 clear
227 //   2 init TR avec contenu TP (roots)  3 init TP avec contenu TR
228 //   4 init avec model (debut scratch)
229   Session()->InitTransferReader(mode);
230 }
231
232     Handle(XSControl_TransferReader)  XSDRAW::TransferReader ()
233       {  return Session()->TransferReader();  }
234
235
236 //  ############  AUXILIAIRES  #############
237
238     Handle(Standard_Transient)  XSDRAW::GetEntity (const Standard_CString name)
239       {  return  IFSelect_Functions::GiveEntity (Session(),name);  }
240
241     Standard_Integer  XSDRAW::GetEntityNumber  (const Standard_CString name)
242       {  return  IFSelect_Functions::GiveEntityNumber (Session(),name);  }
243
244
245     Handle(TColStd_HSequenceOfTransient)  XSDRAW::GetList
246   (const Standard_CString first, const Standard_CString second)
247 {
248   Handle(TColStd_HSequenceOfTransient) list;
249   if (!first || first[0] == '\0') {
250     char ligne[80];  ligne[0] = '\0'; char truc;
251 //    std::cin.clear();  std::cin.get (ligne,79,'\n');
252     std::cin >> ligne;  Standard_Size ln = strlen(ligne);
253     char *ff = &ligne[0], *ss = NULL;
254     std::cin.get(truc);  if (truc != '\n') { std::cin>>&ligne[ln+1]; ss = &ligne[ln+1]; }
255     return  XSDRAW::GetList (ff,ss);
256   }
257 //  return IFSelect_Functions::GiveList (Session(),first,second);
258   return IFSelect_Functions::GiveList (Session(),first,second);
259 }
260
261
262     Standard_Boolean  XSDRAW::FileAndVar
263   (const Standard_CString file, const Standard_CString var,
264    const Standard_CString def,
265    TCollection_AsciiString& resfile,   TCollection_AsciiString& resvar)
266 {  return XSControl_FuncShape::FileAndVar
267      (XSDRAW::Session(),file,var,def,resfile,resvar); }
268
269     Standard_Integer  XSDRAW::MoreShapes
270   (Handle(TopTools_HSequenceOfShape)& list, const Standard_CString name)
271 {  return XSControl_FuncShape::MoreShapes  (XSDRAW::Session(),list,name);  }
272
273
274 //  FONCTION POUR LE DEBUG
275
276 Standard_Integer XSDRAW_WHAT (const Handle(Standard_Transient)& ent)
277 {
278   if (ent.IsNull()) { std::cout<<"(Null Handle)"<<std::endl; return 0; }
279   Handle(Interface_InterfaceModel) model = XSDRAW::Model();
280   if (model.IsNull()) { std::cout<<"(No model)  Type:"<<ent->DynamicType()->Name()<<std::endl; return 0; }
281   std::cout<<" Num/Id :";  
282   model->Print (ent, Message::DefaultMessenger(), 0);
283   std::cout<<"  --  Recorded Type:"<<model->TypeName (ent)<<std::endl;
284   return model->Number(ent);
285 }