0024023: Revamp the OCCT Handle -- general
[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 <XSControl_WorkSession.ixx>
17 #include <Standard_ErrorHandler.hxx>
18 #include <Standard_Failure.hxx>
19 #include <Interface_HGraph.hxx>
20 #include <Interface_Graph.hxx>
21
22 #include <MoniTool_Profile.hxx>
23
24 #include <Transfer_TransientProcess.hxx>
25 #include <Transfer_ResultFromModel.hxx>
26 #include <Transfer_ResultFromTransient.hxx>
27 #include <TColStd_HSequenceOfTransient.hxx>
28
29 #include <TransferBRep.hxx>
30 #include <Transfer_Binder.hxx>
31 #include <Transfer_Finder.hxx>
32 #include <Transfer_SimpleBinderOfTransient.hxx>
33
34 // tpent
35 #include <Interface_IntVal.hxx>
36 #include <Geom2d_Point.hxx>
37 #include <Dico_DictionaryOfTransient.hxx>
38 #include <Dico_IteratorOfDictionaryOfTransient.hxx>
39
40 #include <Interface_Macros.hxx>
41 #include <Interface_Check.hxx>
42
43 #include <Message_Messenger.hxx>
44
45 //=======================================================================
46 //function : XSControl_WorkSession
47 //purpose  : 
48 //=======================================================================
49
50 XSControl_WorkSession::XSControl_WorkSession ()
51 {
52   theModeWriteShape = 0;
53   theTransferRead  = new XSControl_TransferReader;
54   theTransferWrite = new XSControl_TransferWriter;
55   theVars = new XSControl_Vars;
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     theTransferRead->Clear(-1);
75     theTransferWrite->Clear(-1);
76   }
77   if (mode == 6 && !theTransferRead.IsNull()) theTransferRead->Clear(1);
78   theTransferRead->SetGraph (HGraph());
79 }
80
81
82 //=======================================================================
83 //function : SelectNorm
84 //purpose  : 
85 //=======================================================================
86
87 Standard_Boolean  XSControl_WorkSession::SelectNorm(const Standard_CString normname,
88                                                     const Standard_CString profile)
89 {
90   //   RAZ ancienne norme  et resultats
91   theTransferRead->Clear(-1);
92   //  ????  En toute rigueur, menage a faire dans XWS : virer les items
93   //        ( a la limite, pourquoi pas, refaire XWS en entier)
94
95   Handle(XSControl_Controller) newadapt =
96     XSControl_Controller::Recorded (normname);
97   if (newadapt.IsNull()) return Standard_False;
98   if (newadapt == theController) return Standard_True;
99   SetController (newadapt);
100   if (profile && profile[0] != '\0') newadapt->Profile()->SetCurrent(profile);
101
102 //  cout<<"##########################\n"
103 //      <<"##  Select Norm : "<<normname<<"\n"
104 //      <<"##########################"<<endl;
105   return Standard_True;
106 }
107
108
109 //=======================================================================
110 //function : SelectProfile
111 //purpose  : 
112 //=======================================================================
113
114 Standard_Boolean  XSControl_WorkSession::SelectProfile(const Standard_CString profile)
115 {
116   return theController->Profile()->SetCurrent(profile);
117 }
118
119
120 //=======================================================================
121 //function : SetController
122 //purpose  : 
123 //=======================================================================
124
125 void  XSControl_WorkSession::SetController(const Handle(XSControl_Controller)& ctl)
126 {
127   theController = ctl;
128
129   SetLibrary   ( theController->WorkLibrary() );
130   SetProtocol  ( theController->Protocol() );
131   ClearItems();  ClearFinalModifiers();
132   ClearShareOut(Standard_False);  ClearFile();
133   Handle(XSControl_WorkSession) aWorkSession = this;
134   theController->Customise (aWorkSession);
135   SetSignType  ( theController->SignType() );
136   theTransferRead->SetController (theController);
137   theTransferWrite->SetController (theController);
138
139   AdaptNorm ();
140 }
141
142
143 //=======================================================================
144 //function : AdaptNorm
145 //purpose  : 
146 //=======================================================================
147
148 void XSControl_WorkSession::AdaptNorm ()
149 {
150 }
151
152
153 //=======================================================================
154 //function : SelectedNorm
155 //purpose  : 
156 //=======================================================================
157
158 Standard_CString XSControl_WorkSession::SelectedNorm(const Standard_Boolean rsc) const
159 {
160   //JR/Hp :
161   Standard_CString astr = (Standard_CString ) (theController.IsNull() ? "" : theController->Name(rsc));
162   return astr ;
163 }
164 //      {  return (theController.IsNull() ? "" : theController->Name(rsc));  }
165
166
167 //=======================================================================
168 //function : NormAdaptor
169 //purpose  : 
170 //=======================================================================
171
172 Handle(XSControl_Controller) XSControl_WorkSession::NormAdaptor () const
173 {
174   return theController;
175 }
176
177
178 //              ##########################################
179 //              ############  Contexte de Transfert ######
180 //              ##########################################
181
182
183 //=======================================================================
184 //function : Context
185 //purpose  : 
186 //=======================================================================
187
188 Handle(Dico_DictionaryOfTransient) XSControl_WorkSession::Context () const
189 {
190   return theContext;
191 }
192
193
194 //=======================================================================
195 //function : SetAllContext
196 //purpose  : 
197 //=======================================================================
198
199 void XSControl_WorkSession::SetAllContext(const Handle(Dico_DictionaryOfTransient)& context)
200 {
201   theContext = context;
202   theTransferRead->Context() = context;
203 }
204
205
206 //=======================================================================
207 //function : ClearContext
208 //purpose  : 
209 //=======================================================================
210
211 void XSControl_WorkSession::ClearContext ()
212 {
213   Handle(Dico_DictionaryOfTransient) nulctx;
214   SetAllContext(nulctx);
215 }
216
217
218 //              ##########################################
219 //              ############    RESULTATS FORCES    ######
220 //              ##########################################
221
222
223 //=======================================================================
224 //function : PrintTransferStatus
225 //purpose  : 
226 //=======================================================================
227
228 Standard_Boolean  XSControl_WorkSession::PrintTransferStatus(const Standard_Integer num,
229                                                              const Standard_Boolean wri,
230                                                              const Handle(Message_Messenger)& S) const
231 {
232   Handle(Transfer_FinderProcess)    FP = MapWriter();
233   Handle(Transfer_TransientProcess) TP = MapReader();
234
235   Handle(Transfer_Binder) binder;
236   Handle(Transfer_Finder) finder;
237   Handle(Standard_Transient) ent;
238
239   //   ***   WRITE  ***
240   if (wri) {
241     if (FP.IsNull()) return Standard_False;
242     if (num == 0 ) return Standard_False;
243
244     Standard_Integer ne=0, nr=0, max = FP->NbMapped() ,maxr = FP->NbRoots();
245     if (num > 0) {
246       if (num > max) return Standard_False;
247       ne = num;
248       finder = FP->Mapped(ne);
249       nr = FP->RootIndex(finder);
250     } else if (num < 0) {
251       nr = -num;
252       if (nr > maxr) return Standard_False;
253       finder = FP->Root(nr);
254       ne  = FP->MapIndex(finder);
255     }
256
257     S<<"Transfer Write item n0."<<ne<<" of "<<max;
258     if (nr > 0) S<<"  ** Transfer Root n0."<<ne;  S<<endl;
259     ent = FP->FindTransient(finder);
260     S<<" -> Type "<<finder->DynamicType()->Name()<<endl;
261     FP->StartTrace (binder,finder,0,0);  // pb sout/S
262     if (!ent.IsNull()) {
263       S<<" ** Resultat Transient, type "<<ent->DynamicType()->Name();
264       Handle(Interface_InterfaceModel) model = Model();
265       if (!model.IsNull())
266         {  S<<" In output Model, Entity ";  model->Print(ent,S);  }
267       S<<endl;
268     }
269   }
270
271   //    ***   READ   ***
272   else {
273     if (TP.IsNull()) return Standard_False;
274     Handle(Interface_InterfaceModel) model = TP->Model();
275     if (model.IsNull()) cout<<"No Model"<<endl;
276     else if (model != Model()) cout<<"Model different from the session"<<endl;
277     if (num == 0) return Standard_False;
278
279     Standard_Integer  ne=0, nr=0, max = TP->NbMapped() ,maxr = TP->NbRoots();
280     if (num > 0) {
281       if (num > max) return Standard_False;
282       ne = num;
283       ent = TP->Mapped(ne);
284       nr = TP->RootIndex(finder);
285     } else if (num < 0) {
286       nr = -num;
287       if (nr > maxr) return Standard_False;
288       ent = TP->Root(nr);
289       ne  = TP->MapIndex(ent);
290     }
291
292     S<<"Transfer Read item n0."<<ne<<" of "<<max;
293     if (nr > 0) S<<"  ** Transfer Root n0."<<ne;  S<<endl;
294     if (!model.IsNull())  {  S<<" In Model, Entity ";  model->Print(ent,S); }
295     binder = TP->MapItem (ne);
296     S<<endl;
297     TP->StartTrace (binder,ent,0,0);
298
299   }
300
301 //   ***   CHECK (commun READ+WRITE)   ***
302   if (!binder.IsNull()) {
303     const Handle(Interface_Check) ch = binder->Check();
304     Standard_Integer i,nbw = ch->NbWarnings(), nbf = ch->NbFails();
305     if (nbw > 0) {
306       S<<" - Warnings : "<<nbw<<" :\n";
307       for (i = 1; i <= nbw; i ++) S<<ch->CWarning(i)<<endl;
308     }
309     if (nbf > 0) {
310       S<<" - Fails : "<<nbf<<" :\n";
311       for (i = 1; i <= nbf; i ++) S<<ch->CFail(i)<<endl;
312     }
313   }
314   return Standard_True;
315 }
316
317
318 //=======================================================================
319 //function : InitTransferReader
320 //purpose  : 
321 //=======================================================================
322
323 void  XSControl_WorkSession::InitTransferReader(const Standard_Integer mode)
324 {
325   if (mode == 0 || mode == 5)  theTransferRead->Clear(-1);  // full clear
326   if (theTransferRead.IsNull()) SetTransferReader (new XSControl_TransferReader);
327   else SetTransferReader (theTransferRead);
328
329   // mode = 0 fait par SetTransferReader suite a Nullify
330   if (mode == 1) {
331     if (!theTransferRead.IsNull()) theTransferRead->Clear(-1);
332     else SetTransferReader (new XSControl_TransferReader);
333   }
334   if (mode == 2) {
335     Handle(Transfer_TransientProcess) TP = theTransferRead->TransientProcess();
336     if (TP.IsNull()) {
337       TP = new Transfer_TransientProcess;
338       theTransferRead->SetTransientProcess(TP);
339       TP->SetGraph (HGraph());
340     }
341     Handle(TColStd_HSequenceOfTransient) lis = theTransferRead->RecordedList();
342     Standard_Integer i, nb = lis->Length();
343     for (i = 1; i <= nb; i ++) TP->SetRoot(lis->Value(i));
344   }
345   if (mode == 3) {
346     Handle(Transfer_TransientProcess) TP = theTransferRead->TransientProcess();
347     if (TP.IsNull()) return;
348     Standard_Integer i, nb = TP->NbRoots();
349     for (i = 1; i <= nb; i ++) theTransferRead->RecordResult(TP->Root(i));
350   }
351   if (mode == 4 || mode == 5) theTransferRead->BeginTransfer();
352 }
353
354
355 //=======================================================================
356 //function : SetTransferReader
357 //purpose  : 
358 //=======================================================================
359
360 void XSControl_WorkSession::SetTransferReader(const Handle(XSControl_TransferReader)& TR)
361 {
362   if (theTransferRead != TR) //i1 pdn 03.04.99 BUC60301
363     theTransferRead = TR;
364   if (TR.IsNull()) return;
365   TR->SetController (theController);
366   TR->SetGraph (HGraph());
367   if (!TR->TransientProcess().IsNull()) return;
368   Handle(Transfer_TransientProcess) TP = new Transfer_TransientProcess
369     (Model().IsNull() ? 100 : Model()->NbEntities() + 100);
370   TP->SetGraph (HGraph());
371   TP->SetErrorHandle(Standard_True);
372   TR->SetTransientProcess(TP);
373 }
374
375
376 //=======================================================================
377 //function : TransferReader
378 //purpose  : 
379 //=======================================================================
380
381 Handle(XSControl_TransferReader) XSControl_WorkSession::TransferReader () const
382 {
383   return theTransferRead;
384 }
385
386
387 //=======================================================================
388 //function : MapReader
389 //purpose  : 
390 //=======================================================================
391
392 Handle(Transfer_TransientProcess) XSControl_WorkSession::MapReader () const
393 {
394   return theTransferRead->TransientProcess();
395 }
396
397
398 //=======================================================================
399 //function : SetMapReader
400 //purpose  : 
401 //=======================================================================
402
403 Standard_Boolean  XSControl_WorkSession::SetMapReader
404   (const Handle(Transfer_TransientProcess)& TP)
405 {
406   if ( TP.IsNull())  return Standard_False;
407   if (TP->Model().IsNull()) TP->SetModel (Model());
408   TP->SetGraph (HGraph());
409   if (TP->Model() != Model()) return Standard_False;
410 //  TR ne doit pas bouger, c est un "crochet" pour signatures, selections ...
411 //  En revanche, mieux vaut le RAZ
412 //  Handle(XSControl_TransferReader) TR = new XSControl_TransferReader;
413   Handle(XSControl_TransferReader) TR = theTransferRead;
414   TR->Clear(-1);
415
416   SetTransferReader (TR);        // avec le meme mais le reinitialise
417   TR->SetTransientProcess (TP);  // et prend le nouveau TP
418   return Standard_True;
419 }
420
421
422 //=======================================================================
423 //function : Result
424 //purpose  : 
425 //=======================================================================
426
427 Handle(Standard_Transient)  XSControl_WorkSession::Result
428   (const Handle(Standard_Transient)& ent, const Standard_Integer mode) const
429 {
430   Standard_Integer ouca = (mode % 10);
431   Standard_Integer kica = (mode / 10);
432
433   Handle(Transfer_Binder) binder;
434   Handle(Transfer_ResultFromModel) resu;
435
436   if (ouca !=  1) resu = theTransferRead->FinalResult(ent);
437   if (mode == 20) return resu;
438
439   if (!resu.IsNull()) binder = resu->MainResult()->Binder();
440   if (binder.IsNull() && ouca > 0)
441     binder = theTransferRead->TransientProcess()->Find(ent);
442
443   if (kica == 1) return binder;
444   DeclareAndCast(Transfer_SimpleBinderOfTransient,trb,binder);
445   if (!trb.IsNull()) return trb->Result();
446   return binder;
447 }
448
449 //              ##########################################
450 //              ############    TRANSFERT    #############
451 //              ##########################################
452
453
454 //=======================================================================
455 //function : TransferReadOne
456 //purpose  : 
457 //=======================================================================
458
459 Standard_Integer  XSControl_WorkSession::TransferReadOne
460   (const Handle(Standard_Transient)& ent)
461 {
462   Handle(Interface_InterfaceModel) model = Model();
463   if (ent == model) return TransferReadRoots();
464
465   Handle(TColStd_HSequenceOfTransient) list = GiveList(ent);
466   if (list->Length() == 1) return theTransferRead->TransferOne(list->Value(1));
467   else return theTransferRead->TransferList (list);
468 }
469
470
471 //=======================================================================
472 //function : TransferReadRoots
473 //purpose  : 
474 //=======================================================================
475
476 Standard_Integer  XSControl_WorkSession::TransferReadRoots ()
477 {
478   return theTransferRead->TransferRoots(Graph());
479 }
480
481
482 //              ##########################################
483 //              ############    TRANSFERT  WRITE
484 //              ##########################################
485
486 //=======================================================================
487 //function : NewModel
488 //purpose  : 
489 //=======================================================================
490
491 Handle(Interface_InterfaceModel) XSControl_WorkSession::NewModel ()
492 {
493   Handle(Interface_InterfaceModel) newmod;
494   if (theController.IsNull()) return newmod;
495   newmod = theController->NewModel();
496   
497   SetModel(newmod);
498   if(!MapReader().IsNull())
499     MapReader()->Clear();
500   //clear all contains of WS
501   theTransferRead->Clear(3);
502   theTransferWrite->Clear(-1);
503
504   return newmod;
505 }
506
507
508 //=======================================================================
509 //function : TransferWriter
510 //purpose  : 
511 //=======================================================================
512
513 Handle(XSControl_TransferWriter) XSControl_WorkSession::TransferWriter () const
514 {
515   return theTransferWrite;
516 }
517
518
519 //=======================================================================
520 //function : MapWriter
521 //purpose  : 
522 //=======================================================================
523
524 Handle(Transfer_FinderProcess) XSControl_WorkSession::MapWriter () const
525 {
526   return theTransferWrite->FinderProcess();
527 }
528
529
530 //=======================================================================
531 //function : SetMapWriter
532 //purpose  : 
533 //=======================================================================
534
535 Standard_Boolean XSControl_WorkSession::SetMapWriter
536   (const Handle(Transfer_FinderProcess)& FP)
537 {
538   if ( FP.IsNull())  return Standard_False;
539   theTransferWrite->SetFinderProcess (FP);
540   return Standard_True;
541 }
542
543
544 //=======================================================================
545 //function : SetModeWriteShape
546 //purpose  : 
547 //=======================================================================
548
549 void XSControl_WorkSession::SetModeWriteShape(const Standard_Integer mode)
550 {
551   theTransferWrite->SetTransferMode(mode);
552 }
553
554
555 //=======================================================================
556 //function : ModeWriteShape
557 //purpose  : 
558 //=======================================================================
559
560 Standard_Integer XSControl_WorkSession::ModeWriteShape () const
561 {
562   return theTransferWrite->TransferMode();
563 }
564
565
566 //=======================================================================
567 //function : TransferWriteShape
568 //purpose  : 
569 //=======================================================================
570
571 IFSelect_ReturnStatus XSControl_WorkSession::TransferWriteShape
572   (const TopoDS_Shape& shape, const Standard_Boolean compgraph)
573 {
574   IFSelect_ReturnStatus  status;
575   if (theController.IsNull()) return IFSelect_RetError;
576   Handle(Interface_InterfaceModel) model = Model();
577   if (model.IsNull()) return IFSelect_RetVoid;
578
579   status = theTransferWrite->TransferWriteShape (model,shape);
580   //  qui s occupe de tout, try/catch inclus
581
582   //skl insert param compgraph for XDE writing 10.12.2003
583   if(compgraph) ComputeGraph(Standard_True);
584
585   return status;
586 }
587
588
589 //=======================================================================
590 //function : TransferWriteCheckList
591 //purpose  : 
592 //=======================================================================
593
594 Interface_CheckIterator XSControl_WorkSession::TransferWriteCheckList () const
595 {
596   return theTransferWrite->ResultCheckList (Model());
597 }
598
599
600 //=======================================================================
601 //function : Vars
602 //purpose  : 
603 //=======================================================================
604
605 Handle(XSControl_Vars) XSControl_WorkSession::Vars () const
606 {
607   return theVars;
608 }
609
610
611 //=======================================================================
612 //function : SetVars
613 //purpose  : 
614 //=======================================================================
615
616 void XSControl_WorkSession::SetVars (const Handle(XSControl_Vars)& newvars)
617 {
618   theVars = newvars;
619 }
620
621
622 //=======================================================================
623 //function : ClearBinders
624 //purpose  : 
625 //=======================================================================
626
627 void XSControl_WorkSession::ClearBinders()
628 {
629   Handle(Transfer_FinderProcess) FP = theTransferWrite->FinderProcess();
630   //Due to big number of chains of binders it is necessary to 
631   //collect head binders of each chain in the sequence
632   TColStd_SequenceOfTransient aSeqBnd;
633   TColStd_SequenceOfTransient aSeqShapes;
634   Standard_Integer i =1;
635   for( ; i <= FP->NbMapped();i++) {
636     Handle(Transfer_Binder) bnd = FP->MapItem ( i );
637     if(!bnd.IsNull())
638       aSeqBnd.Append(bnd);
639     Handle(Standard_Transient) ash = FP->Mapped(i);
640     aSeqShapes.Append(ash);
641   }
642   //removing finder process containing result of translation.
643   FP->Clear();
644   ClearData(1);
645   ClearData(5);
646   
647   //removing each chain of binders
648   while(aSeqBnd.Length() >0) {
649     Handle(Transfer_Binder) aBnd = Handle(Transfer_Binder)::DownCast(aSeqBnd.Value(1));
650     Handle(Standard_Transient) ash =aSeqShapes.Value(1);
651     aSeqBnd.Remove(1);
652     aSeqShapes.Remove(1);
653     ash.Nullify();
654     while(!aBnd.IsNull()) {
655       Handle(Transfer_Binder) aBndNext = aBnd->NextResult();
656       aBnd.Nullify();
657       aBnd = aBndNext;
658     }
659     
660   }
661
662 }
663
664
665 //=======================================================================
666 //function : Destroy
667 //purpose  : 
668 //=======================================================================
669
670 void XSControl_WorkSession::Destroy()
671 {
672   ClearBinders();
673 }
674
675