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