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