0027349: XtControl_Reader is not thread-safe
[occt.git] / src / XSControl / XSControl_WorkSession.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
7fd59977 14//:i1 pdn 03.04.99 BUC60301
15
42cf5bc1 16#include <Dico_DictionaryOfTransient.hxx>
17#include <Dico_IteratorOfDictionaryOfTransient.hxx>
18#include <Geom2d_Point.hxx>
19#include <Interface_Check.hxx>
20#include <Interface_CheckIterator.hxx>
7fd59977 21#include <Interface_Graph.hxx>
42cf5bc1 22#include <Interface_HGraph.hxx>
23#include <Interface_InterfaceModel.hxx>
24#include <Interface_IntVal.hxx>
25#include <Interface_Macros.hxx>
26#include <Message_Messenger.hxx>
42cf5bc1 27#include <Standard_ErrorHandler.hxx>
28#include <Standard_Failure.hxx>
7fd59977 29#include <TColStd_HSequenceOfTransient.hxx>
42cf5bc1 30#include <TopoDS_Shape.hxx>
7fd59977 31#include <Transfer_Binder.hxx>
32#include <Transfer_Finder.hxx>
42cf5bc1 33#include <Transfer_FinderProcess.hxx>
34#include <Transfer_ResultFromModel.hxx>
35#include <Transfer_ResultFromTransient.hxx>
7fd59977 36#include <Transfer_SimpleBinderOfTransient.hxx>
42cf5bc1 37#include <Transfer_TransientProcess.hxx>
38#include <TransferBRep.hxx>
39#include <XSControl_Controller.hxx>
40#include <XSControl_TransferReader.hxx>
42cf5bc1 41#include <XSControl_Vars.hxx>
42#include <XSControl_WorkSession.hxx>
7fd59977 43
92efcf78 44IMPLEMENT_STANDARD_RTTIEXT(XSControl_WorkSession,IFSelect_WorkSession)
45
7fd59977 46//=======================================================================
47//function : XSControl_WorkSession
48//purpose :
49//=======================================================================
7f56eba8 50
7fd59977 51XSControl_WorkSession::XSControl_WorkSession ()
7f56eba8 52: myTransferReader(new XSControl_TransferReader),
53 myTransferWriter(new XSControl_TransferWriter),
54 myVars(new XSControl_Vars)
7fd59977 55{
7fd59977 56}
57
58
59//=======================================================================
60//function : ClearData
61//purpose :
62//=======================================================================
63
64void XSControl_WorkSession::ClearData (const Standard_Integer mode)
65{
66 // 1-2-3-4 : standard IFSelect
67 if (mode >= 1 && mode <= 4) IFSelect_WorkSession::ClearData (mode);
68
69 // 5 : Transferts seuls
70 // 6 : Resultats forces seuls
71 // 7 : Management, y compris tous transferts (forces/calcules), views
72
73 if (mode == 5 || mode == 7) {
7f56eba8 74 myTransferReader->Clear(-1);
75 myTransferWriter->Clear(-1);
7fd59977 76 }
7f56eba8 77 if (mode == 6 && !myTransferReader.IsNull()) myTransferReader->Clear(1);
78 myTransferReader->SetGraph (HGraph());
7fd59977 79}
80
81
82//=======================================================================
83//function : SelectNorm
84//purpose :
85//=======================================================================
86
7f56eba8 87Standard_Boolean XSControl_WorkSession::SelectNorm(const Standard_CString normname)
7fd59977 88{
7f56eba8 89 // Old norm and results
90 myTransferReader->Clear(-1);
7fd59977 91 // ???? En toute rigueur, menage a faire dans XWS : virer les items
92 // ( a la limite, pourquoi pas, refaire XWS en entier)
93
7f56eba8 94 Handle(XSControl_Controller) newadapt = XSControl_Controller::Recorded (normname);
7fd59977 95 if (newadapt.IsNull()) return Standard_False;
7f56eba8 96 if (newadapt == myController) return Standard_True;
7fd59977 97 SetController (newadapt);
7fd59977 98 return Standard_True;
99}
100
101
102//=======================================================================
7fd59977 103//function : SetController
104//purpose :
105//=======================================================================
106
7f56eba8 107void XSControl_WorkSession::SetController(const Handle(XSControl_Controller)& ctl)
7fd59977 108{
7f56eba8 109 myController = ctl;
7fd59977 110
7f56eba8 111 SetLibrary ( myController->WorkLibrary() );
112 SetProtocol ( myController->Protocol() );
7fd59977 113
7f56eba8 114 ClearItems();
115 ClearFinalModifiers();
116 ClearShareOut(Standard_False);
117 ClearFile();
7fd59977 118
7f56eba8 119 // Set worksession parameters from teh controller
120 Handle(XSControl_WorkSession) aWorkSession(this);
121 myController->Customise (aWorkSession);
122
123 myTransferReader->SetController (myController);
124 myTransferWriter->SetController (myController);
7fd59977 125}
126
127
128//=======================================================================
129//function : SelectedNorm
130//purpose :
131//=======================================================================
132
133Standard_CString XSControl_WorkSession::SelectedNorm(const Standard_Boolean rsc) const
134{
135 //JR/Hp :
7f56eba8 136 Standard_CString astr = (Standard_CString ) (myController.IsNull() ? "" : myController->Name(rsc));
7fd59977 137 return astr ;
138}
7fd59977 139
140
141// ##########################################
142// ############ Contexte de Transfert ######
143// ##########################################
144
145
146//=======================================================================
7fd59977 147//function : SetAllContext
148//purpose :
149//=======================================================================
150
151void XSControl_WorkSession::SetAllContext(const Handle(Dico_DictionaryOfTransient)& context)
152{
7f56eba8 153 myContext = context;
154 myTransferReader->Context() = context;
7fd59977 155}
156
157
158//=======================================================================
159//function : ClearContext
160//purpose :
161//=======================================================================
162
163void XSControl_WorkSession::ClearContext ()
164{
7f56eba8 165 myContext.Nullify();
166 myTransferReader->Context().Nullify();
7fd59977 167}
168
169
170// ##########################################
171// ############ RESULTATS FORCES ######
172// ##########################################
173
174
175//=======================================================================
176//function : PrintTransferStatus
177//purpose :
178//=======================================================================
179
180Standard_Boolean XSControl_WorkSession::PrintTransferStatus(const Standard_Integer num,
181 const Standard_Boolean wri,
182 const Handle(Message_Messenger)& S) const
183{
7f56eba8 184 const Handle(Transfer_FinderProcess) &FP = myTransferWriter->FinderProcess();
185 Handle(Transfer_TransientProcess) TP = myTransferReader->TransientProcess();
7fd59977 186
187 Handle(Transfer_Binder) binder;
188 Handle(Transfer_Finder) finder;
189 Handle(Standard_Transient) ent;
190
191 // *** WRITE ***
192 if (wri) {
193 if (FP.IsNull()) return Standard_False;
194 if (num == 0 ) return Standard_False;
195
196 Standard_Integer ne=0, nr=0, max = FP->NbMapped() ,maxr = FP->NbRoots();
197 if (num > 0) {
198 if (num > max) return Standard_False;
199 ne = num;
200 finder = FP->Mapped(ne);
201 nr = FP->RootIndex(finder);
202 } else if (num < 0) {
203 nr = -num;
204 if (nr > maxr) return Standard_False;
205 finder = FP->Root(nr);
206 ne = FP->MapIndex(finder);
207 }
208
209 S<<"Transfer Write item n0."<<ne<<" of "<<max;
210 if (nr > 0) S<<" ** Transfer Root n0."<<ne; S<<endl;
211 ent = FP->FindTransient(finder);
212 S<<" -> Type "<<finder->DynamicType()->Name()<<endl;
213 FP->StartTrace (binder,finder,0,0); // pb sout/S
214 if (!ent.IsNull()) {
215 S<<" ** Resultat Transient, type "<<ent->DynamicType()->Name();
7f56eba8 216 const Handle(Interface_InterfaceModel) &model = Model();
7fd59977 217 if (!model.IsNull())
218 { S<<" In output Model, Entity "; model->Print(ent,S); }
219 S<<endl;
220 }
221 }
222
223 // *** READ ***
224 else {
225 if (TP.IsNull()) return Standard_False;
226 Handle(Interface_InterfaceModel) model = TP->Model();
227 if (model.IsNull()) cout<<"No Model"<<endl;
228 else if (model != Model()) cout<<"Model different from the session"<<endl;
229 if (num == 0) return Standard_False;
230
231 Standard_Integer ne=0, nr=0, max = TP->NbMapped() ,maxr = TP->NbRoots();
232 if (num > 0) {
233 if (num > max) return Standard_False;
234 ne = num;
235 ent = TP->Mapped(ne);
236 nr = TP->RootIndex(finder);
237 } else if (num < 0) {
238 nr = -num;
239 if (nr > maxr) return Standard_False;
240 ent = TP->Root(nr);
241 ne = TP->MapIndex(ent);
242 }
243
244 S<<"Transfer Read item n0."<<ne<<" of "<<max;
245 if (nr > 0) S<<" ** Transfer Root n0."<<ne; S<<endl;
246 if (!model.IsNull()) { S<<" In Model, Entity "; model->Print(ent,S); }
247 binder = TP->MapItem (ne);
248 S<<endl;
249 TP->StartTrace (binder,ent,0,0);
bc650d41 250
7fd59977 251 }
252
253// *** CHECK (commun READ+WRITE) ***
254 if (!binder.IsNull()) {
255 const Handle(Interface_Check) ch = binder->Check();
256 Standard_Integer i,nbw = ch->NbWarnings(), nbf = ch->NbFails();
257 if (nbw > 0) {
258 S<<" - Warnings : "<<nbw<<" :\n";
259 for (i = 1; i <= nbw; i ++) S<<ch->CWarning(i)<<endl;
260 }
261 if (nbf > 0) {
262 S<<" - Fails : "<<nbf<<" :\n";
263 for (i = 1; i <= nbf; i ++) S<<ch->CFail(i)<<endl;
264 }
265 }
266 return Standard_True;
267}
268
269
270//=======================================================================
271//function : InitTransferReader
272//purpose :
273//=======================================================================
274
275void XSControl_WorkSession::InitTransferReader(const Standard_Integer mode)
276{
7f56eba8 277 if (mode == 0 || mode == 5) myTransferReader->Clear(-1); // full clear
278 if (myTransferReader.IsNull()) SetTransferReader (new XSControl_TransferReader);
279 else SetTransferReader (myTransferReader);
7fd59977 280
281 // mode = 0 fait par SetTransferReader suite a Nullify
282 if (mode == 1) {
7f56eba8 283 if (!myTransferReader.IsNull()) myTransferReader->Clear(-1);
7fd59977 284 else SetTransferReader (new XSControl_TransferReader);
285 }
286 if (mode == 2) {
7f56eba8 287 Handle(Transfer_TransientProcess) TP = myTransferReader->TransientProcess();
7fd59977 288 if (TP.IsNull()) {
289 TP = new Transfer_TransientProcess;
7f56eba8 290 myTransferReader->SetTransientProcess(TP);
7fd59977 291 TP->SetGraph (HGraph());
292 }
7f56eba8 293 Handle(TColStd_HSequenceOfTransient) lis = myTransferReader->RecordedList();
7fd59977 294 Standard_Integer i, nb = lis->Length();
295 for (i = 1; i <= nb; i ++) TP->SetRoot(lis->Value(i));
296 }
297 if (mode == 3) {
7f56eba8 298 Handle(Transfer_TransientProcess) TP = myTransferReader->TransientProcess();
7fd59977 299 if (TP.IsNull()) return;
300 Standard_Integer i, nb = TP->NbRoots();
7f56eba8 301 for (i = 1; i <= nb; i ++) myTransferReader->RecordResult(TP->Root(i));
7fd59977 302 }
7f56eba8 303 if (mode == 4 || mode == 5) myTransferReader->BeginTransfer();
7fd59977 304}
305
306
307//=======================================================================
308//function : SetTransferReader
309//purpose :
310//=======================================================================
311
312void XSControl_WorkSession::SetTransferReader(const Handle(XSControl_TransferReader)& TR)
313{
7f56eba8 314 if (myTransferReader != TR) //i1 pdn 03.04.99 BUC60301
315 myTransferReader = TR;
7fd59977 316 if (TR.IsNull()) return;
7f56eba8 317 TR->SetController (myController);
7fd59977 318 TR->SetGraph (HGraph());
319 if (!TR->TransientProcess().IsNull()) return;
320 Handle(Transfer_TransientProcess) TP = new Transfer_TransientProcess
321 (Model().IsNull() ? 100 : Model()->NbEntities() + 100);
322 TP->SetGraph (HGraph());
323 TP->SetErrorHandle(Standard_True);
324 TR->SetTransientProcess(TP);
325}
326
327
328//=======================================================================
7fd59977 329//function : SetMapReader
330//purpose :
331//=======================================================================
332
7f56eba8 333Standard_Boolean XSControl_WorkSession::SetMapReader (const Handle(Transfer_TransientProcess)& TP)
7fd59977 334{
7f56eba8 335 if (TP.IsNull()) return Standard_False;
7fd59977 336 if (TP->Model().IsNull()) TP->SetModel (Model());
337 TP->SetGraph (HGraph());
338 if (TP->Model() != Model()) return Standard_False;
339// TR ne doit pas bouger, c est un "crochet" pour signatures, selections ...
340// En revanche, mieux vaut le RAZ
341// Handle(XSControl_TransferReader) TR = new XSControl_TransferReader;
7f56eba8 342 Handle(XSControl_TransferReader) TR = myTransferReader;
7fd59977 343 TR->Clear(-1);
344
345 SetTransferReader (TR); // avec le meme mais le reinitialise
346 TR->SetTransientProcess (TP); // et prend le nouveau TP
347 return Standard_True;
348}
349
350
351//=======================================================================
352//function : Result
353//purpose :
354//=======================================================================
355
356Handle(Standard_Transient) XSControl_WorkSession::Result
357 (const Handle(Standard_Transient)& ent, const Standard_Integer mode) const
358{
359 Standard_Integer ouca = (mode % 10);
360 Standard_Integer kica = (mode / 10);
361
362 Handle(Transfer_Binder) binder;
363 Handle(Transfer_ResultFromModel) resu;
364
7f56eba8 365 if (ouca != 1) resu = myTransferReader->FinalResult(ent);
7fd59977 366 if (mode == 20) return resu;
367
368 if (!resu.IsNull()) binder = resu->MainResult()->Binder();
369 if (binder.IsNull() && ouca > 0)
7f56eba8 370 binder = myTransferReader->TransientProcess()->Find(ent);
7fd59977 371
372 if (kica == 1) return binder;
373 DeclareAndCast(Transfer_SimpleBinderOfTransient,trb,binder);
374 if (!trb.IsNull()) return trb->Result();
375 return binder;
376}
377
378// ##########################################
379// ############ TRANSFERT #############
380// ##########################################
381
382
383//=======================================================================
384//function : TransferReadOne
385//purpose :
386//=======================================================================
387
7f56eba8 388Standard_Integer XSControl_WorkSession::TransferReadOne (const Handle(Standard_Transient)& ent)
7fd59977 389{
7fd59977 390 Handle(Interface_InterfaceModel) model = Model();
391 if (ent == model) return TransferReadRoots();
392
393 Handle(TColStd_HSequenceOfTransient) list = GiveList(ent);
7f56eba8 394 if (list->Length() == 1) return myTransferReader->TransferOne(list->Value(1));
395 else return myTransferReader->TransferList (list);
7fd59977 396}
397
398
399//=======================================================================
400//function : TransferReadRoots
401//purpose :
402//=======================================================================
403
7f56eba8 404Standard_Integer XSControl_WorkSession::TransferReadRoots ()
7fd59977 405{
7f56eba8 406 return myTransferReader->TransferRoots(Graph());
7fd59977 407}
408
409
410// ##########################################
411// ############ TRANSFERT WRITE
412// ##########################################
413
414//=======================================================================
415//function : NewModel
416//purpose :
417//=======================================================================
418
419Handle(Interface_InterfaceModel) XSControl_WorkSession::NewModel ()
420{
421 Handle(Interface_InterfaceModel) newmod;
7f56eba8 422 if (myController.IsNull()) return newmod;
423 newmod = myController->NewModel();
bc650d41 424
7fd59977 425 SetModel(newmod);
7f56eba8 426 if(!myTransferReader->TransientProcess().IsNull())
427 myTransferReader->TransientProcess()->Clear();
bc650d41 428 //clear all contains of WS
7f56eba8 429 myTransferReader->Clear(3);
430 myTransferWriter->Clear(-1);
bc650d41 431
7fd59977 432 return newmod;
433}
434
435
436//=======================================================================
7fd59977 437//function : TransferWriteShape
438//purpose :
439//=======================================================================
440
7f56eba8 441IFSelect_ReturnStatus XSControl_WorkSession::TransferWriteShape (const TopoDS_Shape& shape, const Standard_Boolean compgraph)
7fd59977 442{
443 IFSelect_ReturnStatus status;
7f56eba8 444 if (myController.IsNull()) return IFSelect_RetError;
445 const Handle(Interface_InterfaceModel) &model = Model();
7fd59977 446 if (model.IsNull()) return IFSelect_RetVoid;
447
7f56eba8 448 status = myTransferWriter->TransferWriteShape (model,shape);
7fd59977 449 // qui s occupe de tout, try/catch inclus
450
451 //skl insert param compgraph for XDE writing 10.12.2003
452 if(compgraph) ComputeGraph(Standard_True);
453
454 return status;
455}
456
457
458//=======================================================================
459//function : TransferWriteCheckList
460//purpose :
461//=======================================================================
462
463Interface_CheckIterator XSControl_WorkSession::TransferWriteCheckList () const
464{
7f56eba8 465 return myTransferWriter->ResultCheckList (Model());
7fd59977 466}
467
468
469//=======================================================================
470//function : ClearBinders
471//purpose :
472//=======================================================================
473
474void XSControl_WorkSession::ClearBinders()
475{
7f56eba8 476 const Handle(Transfer_FinderProcess) &FP = myTransferWriter->FinderProcess();
7fd59977 477 //Due to big number of chains of binders it is necessary to
478 //collect head binders of each chain in the sequence
479 TColStd_SequenceOfTransient aSeqBnd;
480 TColStd_SequenceOfTransient aSeqShapes;
481 Standard_Integer i =1;
482 for( ; i <= FP->NbMapped();i++) {
483 Handle(Transfer_Binder) bnd = FP->MapItem ( i );
484 if(!bnd.IsNull())
485 aSeqBnd.Append(bnd);
5b111128 486 Handle(Standard_Transient) ash (FP->Mapped(i));
7fd59977 487 aSeqShapes.Append(ash);
488 }
489 //removing finder process containing result of translation.
490 FP->Clear();
491 ClearData(1);
492 ClearData(5);
493
494 //removing each chain of binders
495 while(aSeqBnd.Length() >0) {
496 Handle(Transfer_Binder) aBnd = Handle(Transfer_Binder)::DownCast(aSeqBnd.Value(1));
497 Handle(Standard_Transient) ash =aSeqShapes.Value(1);
498 aSeqBnd.Remove(1);
499 aSeqShapes.Remove(1);
500 ash.Nullify();
501 while(!aBnd.IsNull()) {
502 Handle(Transfer_Binder) aBndNext = aBnd->NextResult();
503 aBnd.Nullify();
504 aBnd = aBndNext;
505 }
506
507 }
508
509}