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