0027460: Data Exchange - restore method XSControl_WorkSession::MapReader()
[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 <Dico_DictionaryOfTransient.hxx>
17 #include <Dico_IteratorOfDictionaryOfTransient.hxx>
18 #include <Geom2d_Point.hxx>
19 #include <Interface_Check.hxx>
20 #include <Interface_CheckIterator.hxx>
21 #include <Interface_Graph.hxx>
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>
27 #include <Standard_ErrorHandler.hxx>
28 #include <Standard_Failure.hxx>
29 #include <TColStd_HSequenceOfTransient.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <Transfer_Binder.hxx>
32 #include <Transfer_Finder.hxx>
33 #include <Transfer_FinderProcess.hxx>
34 #include <Transfer_ResultFromModel.hxx>
35 #include <Transfer_ResultFromTransient.hxx>
36 #include <Transfer_SimpleBinderOfTransient.hxx>
37 #include <Transfer_TransientProcess.hxx>
38 #include <TransferBRep.hxx>
39 #include <XSControl_Controller.hxx>
40 #include <XSControl_TransferReader.hxx>
41 #include <XSControl_Vars.hxx>
42 #include <XSControl_WorkSession.hxx>
43
44 IMPLEMENT_STANDARD_RTTIEXT(XSControl_WorkSession,IFSelect_WorkSession)
45
46 //=======================================================================
47 //function : XSControl_WorkSession
48 //purpose  : 
49 //=======================================================================
50
51 XSControl_WorkSession::XSControl_WorkSession ()
52 : myTransferReader(new XSControl_TransferReader),
53   myTransferWriter(new XSControl_TransferWriter),
54   myVars(new XSControl_Vars)
55 {
56 }
57
58
59 //=======================================================================
60 //function : ClearData
61 //purpose  : 
62 //=======================================================================
63
64 void  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) {
74     myTransferReader->Clear(-1);
75     myTransferWriter->Clear(-1);
76   }
77   if (mode == 6 && !myTransferReader.IsNull()) myTransferReader->Clear(1);
78   myTransferReader->SetGraph (HGraph());
79 }
80
81
82 //=======================================================================
83 //function : SelectNorm
84 //purpose  : 
85 //=======================================================================
86
87 Standard_Boolean  XSControl_WorkSession::SelectNorm(const Standard_CString normname)
88 {
89   // Old norm and results
90   myTransferReader->Clear(-1);
91   //  ????  En toute rigueur, menage a faire dans XWS : virer les items
92   //        ( a la limite, pourquoi pas, refaire XWS en entier)
93
94   Handle(XSControl_Controller) newadapt = XSControl_Controller::Recorded (normname);
95   if (newadapt.IsNull()) return Standard_False;
96   if (newadapt == myController) return Standard_True;
97   SetController (newadapt);
98   return Standard_True;
99 }
100
101
102 //=======================================================================
103 //function : SetController
104 //purpose  : 
105 //=======================================================================
106
107 void XSControl_WorkSession::SetController(const Handle(XSControl_Controller)& ctl)
108 {
109   myController = ctl;
110
111   SetLibrary   ( myController->WorkLibrary() );
112   SetProtocol  ( myController->Protocol() );
113
114   ClearItems();
115   ClearFinalModifiers();
116   ClearShareOut(Standard_False);
117   ClearFile();
118
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);
125 }
126
127
128 //=======================================================================
129 //function : SelectedNorm
130 //purpose  : 
131 //=======================================================================
132
133 Standard_CString XSControl_WorkSession::SelectedNorm(const Standard_Boolean rsc) const
134 {
135   //JR/Hp :
136   Standard_CString astr = (Standard_CString ) (myController.IsNull() ? "" : myController->Name(rsc));
137   return astr ;
138 }
139
140
141 //              ##########################################
142 //              ############  Contexte de Transfert ######
143 //              ##########################################
144
145
146 //=======================================================================
147 //function : SetAllContext
148 //purpose  : 
149 //=======================================================================
150
151 void XSControl_WorkSession::SetAllContext(const Handle(Dico_DictionaryOfTransient)& context)
152 {
153   myContext = context;
154   myTransferReader->Context() = context;
155 }
156
157
158 //=======================================================================
159 //function : ClearContext
160 //purpose  : 
161 //=======================================================================
162
163 void XSControl_WorkSession::ClearContext ()
164 {
165   myContext.Nullify();
166   myTransferReader->Context().Nullify();
167 }
168
169
170 //              ##########################################
171 //              ############    RESULTATS FORCES    ######
172 //              ##########################################
173
174
175 //=======================================================================
176 //function : PrintTransferStatus
177 //purpose  : 
178 //=======================================================================
179
180 Standard_Boolean  XSControl_WorkSession::PrintTransferStatus(const Standard_Integer num,
181                                                              const Standard_Boolean wri,
182                                                              const Handle(Message_Messenger)& S) const
183 {
184   const Handle(Transfer_FinderProcess)    &FP = myTransferWriter->FinderProcess();
185   Handle(Transfer_TransientProcess) TP = myTransferReader->TransientProcess();
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();
216       const Handle(Interface_InterfaceModel) &model = Model();
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);
250
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
275 void  XSControl_WorkSession::InitTransferReader(const Standard_Integer mode)
276 {
277   if (mode == 0 || mode == 5)  myTransferReader->Clear(-1);  // full clear
278   if (myTransferReader.IsNull()) SetTransferReader (new XSControl_TransferReader);
279   else SetTransferReader (myTransferReader);
280
281   // mode = 0 fait par SetTransferReader suite a Nullify
282   if (mode == 1) {
283     if (!myTransferReader.IsNull()) myTransferReader->Clear(-1);
284     else SetTransferReader (new XSControl_TransferReader);
285   }
286   if (mode == 2) {
287     Handle(Transfer_TransientProcess) TP = myTransferReader->TransientProcess();
288     if (TP.IsNull()) {
289       TP = new Transfer_TransientProcess;
290       myTransferReader->SetTransientProcess(TP);
291       TP->SetGraph (HGraph());
292     }
293     Handle(TColStd_HSequenceOfTransient) lis = myTransferReader->RecordedList();
294     Standard_Integer i, nb = lis->Length();
295     for (i = 1; i <= nb; i ++) TP->SetRoot(lis->Value(i));
296   }
297   if (mode == 3) {
298     Handle(Transfer_TransientProcess) TP = myTransferReader->TransientProcess();
299     if (TP.IsNull()) return;
300     Standard_Integer i, nb = TP->NbRoots();
301     for (i = 1; i <= nb; i ++) myTransferReader->RecordResult(TP->Root(i));
302   }
303   if (mode == 4 || mode == 5) myTransferReader->BeginTransfer();
304 }
305
306
307 //=======================================================================
308 //function : SetTransferReader
309 //purpose  : 
310 //=======================================================================
311
312 void XSControl_WorkSession::SetTransferReader(const Handle(XSControl_TransferReader)& TR)
313 {
314   if (myTransferReader != TR) //i1 pdn 03.04.99 BUC60301
315     myTransferReader = TR;
316   if (TR.IsNull()) return;
317   TR->SetController (myController);
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 //function : MapReader
329 //purpose  :
330 //=======================================================================
331
332 Handle(Transfer_TransientProcess) XSControl_WorkSession::MapReader() const
333 {
334   return myTransferReader->TransientProcess();
335 }
336
337 //=======================================================================
338 //function : SetMapReader
339 //purpose  : 
340 //=======================================================================
341
342 Standard_Boolean XSControl_WorkSession::SetMapReader (const Handle(Transfer_TransientProcess)& TP)
343 {
344   if (TP.IsNull()) return Standard_False;
345   if (TP->Model().IsNull()) TP->SetModel (Model());
346   TP->SetGraph (HGraph());
347   if (TP->Model() != Model()) return Standard_False;
348 //  TR ne doit pas bouger, c est un "crochet" pour signatures, selections ...
349 //  En revanche, mieux vaut le RAZ
350 //  Handle(XSControl_TransferReader) TR = new XSControl_TransferReader;
351   Handle(XSControl_TransferReader) TR = myTransferReader;
352   TR->Clear(-1);
353
354   SetTransferReader (TR);        // avec le meme mais le reinitialise
355   TR->SetTransientProcess (TP);  // et prend le nouveau TP
356   return Standard_True;
357 }
358
359
360 //=======================================================================
361 //function : Result
362 //purpose  : 
363 //=======================================================================
364
365 Handle(Standard_Transient)  XSControl_WorkSession::Result
366   (const Handle(Standard_Transient)& ent, const Standard_Integer mode) const
367 {
368   Standard_Integer ouca = (mode % 10);
369   Standard_Integer kica = (mode / 10);
370
371   Handle(Transfer_Binder) binder;
372   Handle(Transfer_ResultFromModel) resu;
373
374   if (ouca !=  1) resu = myTransferReader->FinalResult(ent);
375   if (mode == 20) return resu;
376
377   if (!resu.IsNull()) binder = resu->MainResult()->Binder();
378   if (binder.IsNull() && ouca > 0)
379     binder = myTransferReader->TransientProcess()->Find(ent);
380
381   if (kica == 1) return binder;
382   DeclareAndCast(Transfer_SimpleBinderOfTransient,trb,binder);
383   if (!trb.IsNull()) return trb->Result();
384   return binder;
385 }
386
387 //              ##########################################
388 //              ############    TRANSFERT    #############
389 //              ##########################################
390
391
392 //=======================================================================
393 //function : TransferReadOne
394 //purpose  : 
395 //=======================================================================
396
397 Standard_Integer XSControl_WorkSession::TransferReadOne (const Handle(Standard_Transient)& ent)
398 {
399   Handle(Interface_InterfaceModel) model = Model();
400   if (ent == model) return TransferReadRoots();
401
402   Handle(TColStd_HSequenceOfTransient) list = GiveList(ent);
403   if (list->Length() == 1) return myTransferReader->TransferOne(list->Value(1));
404   else return myTransferReader->TransferList (list);
405 }
406
407
408 //=======================================================================
409 //function : TransferReadRoots
410 //purpose  : 
411 //=======================================================================
412
413 Standard_Integer XSControl_WorkSession::TransferReadRoots ()
414 {
415   return myTransferReader->TransferRoots(Graph());
416 }
417
418
419 //              ##########################################
420 //              ############    TRANSFERT  WRITE
421 //              ##########################################
422
423 //=======================================================================
424 //function : NewModel
425 //purpose  : 
426 //=======================================================================
427
428 Handle(Interface_InterfaceModel) XSControl_WorkSession::NewModel ()
429 {
430   Handle(Interface_InterfaceModel) newmod;
431   if (myController.IsNull()) return newmod;
432   newmod = myController->NewModel();
433   
434   SetModel(newmod);
435   if(!myTransferReader->TransientProcess().IsNull())
436     myTransferReader->TransientProcess()->Clear();
437   //clear all contains of WS
438   myTransferReader->Clear(3);
439   myTransferWriter->Clear(-1);
440
441   return newmod;
442 }
443
444
445 //=======================================================================
446 //function : TransferWriteShape
447 //purpose  : 
448 //=======================================================================
449
450 IFSelect_ReturnStatus XSControl_WorkSession::TransferWriteShape (const TopoDS_Shape& shape, const Standard_Boolean compgraph)
451 {
452   IFSelect_ReturnStatus  status;
453   if (myController.IsNull()) return IFSelect_RetError;
454   const Handle(Interface_InterfaceModel) &model = Model();
455   if (model.IsNull()) return IFSelect_RetVoid;
456
457   status = myTransferWriter->TransferWriteShape (model,shape);
458   //  qui s occupe de tout, try/catch inclus
459
460   //skl insert param compgraph for XDE writing 10.12.2003
461   if(compgraph) ComputeGraph(Standard_True);
462
463   return status;
464 }
465
466
467 //=======================================================================
468 //function : TransferWriteCheckList
469 //purpose  : 
470 //=======================================================================
471
472 Interface_CheckIterator XSControl_WorkSession::TransferWriteCheckList () const
473 {
474   return myTransferWriter->ResultCheckList (Model());
475 }
476
477
478 //=======================================================================
479 //function : ClearBinders
480 //purpose  : 
481 //=======================================================================
482
483 void XSControl_WorkSession::ClearBinders()
484 {
485   const Handle(Transfer_FinderProcess) &FP = myTransferWriter->FinderProcess();
486   //Due to big number of chains of binders it is necessary to 
487   //collect head binders of each chain in the sequence
488   TColStd_SequenceOfTransient aSeqBnd;
489   TColStd_SequenceOfTransient aSeqShapes;
490   Standard_Integer i =1;
491   for( ; i <= FP->NbMapped();i++) {
492     Handle(Transfer_Binder) bnd = FP->MapItem ( i );
493     if(!bnd.IsNull())
494       aSeqBnd.Append(bnd);
495     Handle(Standard_Transient) ash (FP->Mapped(i));
496     aSeqShapes.Append(ash);
497   }
498   //removing finder process containing result of translation.
499   FP->Clear();
500   ClearData(1);
501   ClearData(5);
502   
503   //removing each chain of binders
504   while(aSeqBnd.Length() >0) {
505     Handle(Transfer_Binder) aBnd = Handle(Transfer_Binder)::DownCast(aSeqBnd.Value(1));
506     Handle(Standard_Transient) ash =aSeqShapes.Value(1);
507     aSeqBnd.Remove(1);
508     aSeqShapes.Remove(1);
509     ash.Nullify();
510     while(!aBnd.IsNull()) {
511       Handle(Transfer_Binder) aBndNext = aBnd->NextResult();
512       aBnd.Nullify();
513       aBnd = aBndNext;
514     }
515     
516   }
517
518 }