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