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.
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>
41 #include <XSDRAW_Vars.hxx>
44 //#include <XSDRAW_Shape.hxx>
45 static int deja = 0, dejald = 0;
47 //static int okxset = 0;
49 static NCollection_DataMap<TCollection_AsciiString, Standard_Integer> theolds;
50 static Handle(TColStd_HSequenceOfAsciiString) thenews;
52 static Handle(IFSelect_SessionPilot) thepilot; // detient Session, Model
54 static Standard_Integer XSTEPDRAWRUN (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
56 TCollection_AsciiString mess;
57 for (Standard_Integer i = 0; i < argc; i ++) {
58 mess.AssignCat(argv[i]); mess.AssignCat(" ");
61 const Handle(Message_Messenger)& aMsgMgr = Message::DefaultMessenger();
62 Message_SequenceOfPrinters aPrinters;
63 aPrinters.Append (aMsgMgr->ChangePrinters());
64 aMsgMgr->AddPrinter (new Draw_Printer (di));
66 IFSelect_ReturnStatus stat = thepilot->Execute (mess.ToCString());
68 aMsgMgr->RemovePrinters (STANDARD_TYPE(Draw_Printer));
69 aMsgMgr->ChangePrinters().Append (aPrinters);
71 if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return 1;
75 void XSDRAW::ChangeCommand
76 (const Standard_CString oldname, const Standard_CString newname)
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();
85 theolds.Bind(oldname,num);
88 void XSDRAW::RemoveCommand
89 (const Standard_CString oldname)
91 ChangeCommand (oldname,"");
94 Standard_Boolean XSDRAW::LoadSession ()
96 if (deja) return Standard_False;
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);
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;
110 void XSDRAW::LoadDraw (Draw_Interpretor& theCommands)
117 // Pour tout faire d un coup : BRepTest & cie:
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");
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;
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());
146 Standard_Integer XSDRAW::Execute
147 (const Standard_CString command, const Standard_CString varname)
150 Sprintf (mess,command,varname);
151 thepilot->Execute (mess);
155 Handle(IFSelect_SessionPilot) XSDRAW::Pilot ()
158 Handle(XSControl_WorkSession) XSDRAW::Session ()
159 { return XSControl::Session(thepilot); }
161 void XSDRAW::SetController (const Handle(XSControl_Controller)& control)
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;
170 Handle(XSControl_Controller) XSDRAW::Controller ()
171 { return Session()->NormAdaptor(); }
173 Standard_Boolean XSDRAW::SetNorm
174 (const Standard_CString norm)
176 return Session()->SelectNorm (norm);
179 Handle(Interface_Protocol) XSDRAW::Protocol ()
180 { return thepilot->Session()->Protocol(); }
182 Handle(Interface_InterfaceModel) XSDRAW::Model ()
183 { return thepilot->Session()->Model(); }
185 void XSDRAW::SetModel
186 (const Handle(Interface_InterfaceModel)& model, const Standard_CString file)
188 thepilot->Session()->SetModel(model);
189 if (file && file[0] != '\0') thepilot->Session()->SetLoadedFile(file);
192 Handle(Interface_InterfaceModel) XSDRAW::NewModel ()
193 { return Session()->NewModel(); }
195 Handle(Standard_Transient) XSDRAW::Entity (const Standard_Integer num)
196 { return thepilot->Session()->StartingEntity(num); }
198 Standard_Integer XSDRAW::Number (const Handle(Standard_Transient)& ent)
199 { return thepilot->Session()->StartingNumber(ent); }
201 void XSDRAW::SetTransferProcess (const Handle(Standard_Transient)& ATP)
203 DeclareAndCast(Transfer_FinderProcess,FP,ATP);
204 DeclareAndCast(Transfer_TransientProcess,TP,ATP);
206 // Cas FinderProcess ==> TransferWriter
207 if (!FP.IsNull()) Session()->SetMapWriter(FP);
209 // Cas TransientProcess ==> TransferReader
211 if (!TP->Model().IsNull() && TP->Model() != Session()->Model())
212 Session()->SetModel (TP->Model());
213 Session()->SetMapReader(TP);
217 Handle(Transfer_TransientProcess) XSDRAW::TransientProcess ()
218 { return Session()->TransferReader()->TransientProcess(); }
220 Handle(Transfer_FinderProcess) XSDRAW::FinderProcess ()
221 { return Session()->TransferWriter()->FinderProcess(); }
224 void XSDRAW::InitTransferReader (const Standard_Integer mode)
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);
232 Handle(XSControl_TransferReader) XSDRAW::TransferReader ()
233 { return Session()->TransferReader(); }
236 // ############ AUXILIAIRES #############
238 Handle(Standard_Transient) XSDRAW::GetEntity (const Standard_CString name)
239 { return IFSelect_Functions::GiveEntity (Session(),name); }
241 Standard_Integer XSDRAW::GetEntityNumber (const Standard_CString name)
242 { return IFSelect_Functions::GiveEntityNumber (Session(),name); }
245 Handle(TColStd_HSequenceOfTransient) XSDRAW::GetList
246 (const Standard_CString first, const Standard_CString second)
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);
257 // return IFSelect_Functions::GiveList (Session(),first,second);
258 return IFSelect_Functions::GiveList (Session(),first,second);
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); }
269 Standard_Integer XSDRAW::MoreShapes
270 (Handle(TopTools_HSequenceOfShape)& list, const Standard_CString name)
271 { return XSControl_FuncShape::MoreShapes (XSDRAW::Session(),list,name); }
274 // FONCTION POUR LE DEBUG
276 Standard_Integer XSDRAW_WHAT (const Handle(Standard_Transient)& ent)
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, std::cout, 0);
283 std::cout<<" -- Recorded Type:"<<model->TypeName (ent)<<std::endl;
284 return model->Number(ent);