4aaedfde4091d5c108ee9b528889e810d88c31d9
[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 //=======================================================================
329 //function : SetMapReader
330 //purpose  : 
331 //=======================================================================
332
333 Standard_Boolean XSControl_WorkSession::SetMapReader (const Handle(Transfer_TransientProcess)& TP)
334 {
335   if (TP.IsNull()) return Standard_False;
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;
342   Handle(XSControl_TransferReader) TR = myTransferReader;
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
356 Handle(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
365   if (ouca !=  1) resu = myTransferReader->FinalResult(ent);
366   if (mode == 20) return resu;
367
368   if (!resu.IsNull()) binder = resu->MainResult()->Binder();
369   if (binder.IsNull() && ouca > 0)
370     binder = myTransferReader->TransientProcess()->Find(ent);
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
388 Standard_Integer XSControl_WorkSession::TransferReadOne (const Handle(Standard_Transient)& ent)
389 {
390   Handle(Interface_InterfaceModel) model = Model();
391   if (ent == model) return TransferReadRoots();
392
393   Handle(TColStd_HSequenceOfTransient) list = GiveList(ent);
394   if (list->Length() == 1) return myTransferReader->TransferOne(list->Value(1));
395   else return myTransferReader->TransferList (list);
396 }
397
398
399 //=======================================================================
400 //function : TransferReadRoots
401 //purpose  : 
402 //=======================================================================
403
404 Standard_Integer XSControl_WorkSession::TransferReadRoots ()
405 {
406   return myTransferReader->TransferRoots(Graph());
407 }
408
409
410 //              ##########################################
411 //              ############    TRANSFERT  WRITE
412 //              ##########################################
413
414 //=======================================================================
415 //function : NewModel
416 //purpose  : 
417 //=======================================================================
418
419 Handle(Interface_InterfaceModel) XSControl_WorkSession::NewModel ()
420 {
421   Handle(Interface_InterfaceModel) newmod;
422   if (myController.IsNull()) return newmod;
423   newmod = myController->NewModel();
424   
425   SetModel(newmod);
426   if(!myTransferReader->TransientProcess().IsNull())
427     myTransferReader->TransientProcess()->Clear();
428   //clear all contains of WS
429   myTransferReader->Clear(3);
430   myTransferWriter->Clear(-1);
431
432   return newmod;
433 }
434
435
436 //=======================================================================
437 //function : TransferWriteShape
438 //purpose  : 
439 //=======================================================================
440
441 IFSelect_ReturnStatus XSControl_WorkSession::TransferWriteShape (const TopoDS_Shape& shape, const Standard_Boolean compgraph)
442 {
443   IFSelect_ReturnStatus  status;
444   if (myController.IsNull()) return IFSelect_RetError;
445   const Handle(Interface_InterfaceModel) &model = Model();
446   if (model.IsNull()) return IFSelect_RetVoid;
447
448   status = myTransferWriter->TransferWriteShape (model,shape);
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
463 Interface_CheckIterator XSControl_WorkSession::TransferWriteCheckList () const
464 {
465   return myTransferWriter->ResultCheckList (Model());
466 }
467
468
469 //=======================================================================
470 //function : ClearBinders
471 //purpose  : 
472 //=======================================================================
473
474 void XSControl_WorkSession::ClearBinders()
475 {
476   const Handle(Transfer_FinderProcess) &FP = myTransferWriter->FinderProcess();
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);
486     Handle(Standard_Transient) ash (FP->Mapped(i));
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 }