0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[occt.git] / src / TransferBRep / TransferBRep_Reader.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 #include <TransferBRep_Reader.ixx>
15
16 #include <Interface_Macros.hxx>
17 #include <Interface_CheckTool.hxx>
18 #include <Transfer_TransferOutput.hxx>
19 #include <Transfer_TransientProcess.hxx>
20 #include <TransferBRep.hxx>
21
22 #include <TopoDS_Compound.hxx>
23 #include <BRep_Builder.hxx>
24 #include <Message_Messenger.hxx>
25
26 TransferBRep_Reader::TransferBRep_Reader ()
27     : theDone (Standard_False) , theFilest (0) , theNewpr (Standard_False)
28       {    theShapes = new TopTools_HSequenceOfShape();  theTransi = new TColStd_HSequenceOfTransient();  }
29
30     void  TransferBRep_Reader::SetProtocol
31   (const Handle(Interface_Protocol)& protocol)
32       {  theProto = protocol;  }
33
34     Handle(Interface_Protocol)  TransferBRep_Reader::Protocol () const
35       {  return theProto;  }
36
37     void  TransferBRep_Reader::SetActor
38   (const Handle(Transfer_ActorOfTransientProcess)& actor)
39       {  theActor = actor;  }
40
41     Handle(Transfer_ActorOfTransientProcess) TransferBRep_Reader::Actor () const
42       {  return theActor;  }
43
44     void  TransferBRep_Reader::SetFileStatus (const Standard_Integer status)
45       {  theFilest = status;  }
46
47     Standard_Integer  TransferBRep_Reader::FileStatus () const
48       { return theFilest;  }
49
50     Standard_Boolean  TransferBRep_Reader::FileNotFound () const
51       {  return (theFilest < 0);  }
52
53     Standard_Boolean  TransferBRep_Reader::SyntaxError () const
54       {  return (theFilest > 0);  }
55
56
57     void  TransferBRep_Reader::SetModel
58   (const Handle(Interface_InterfaceModel)& model)
59 {
60   theModel = model;
61   Clear();
62 }
63
64     Handle(Interface_InterfaceModel)  TransferBRep_Reader::Model () const
65       {  return theModel;  }
66
67
68     void  TransferBRep_Reader::Clear ()
69 {
70   theDone = Standard_False;
71   theShapes->Clear();  theTransi->Clear();
72 }
73
74     Standard_Boolean  TransferBRep_Reader::CheckStatusModel
75   (const Standard_Boolean withprint) const
76 {
77   Interface_CheckTool cht (theModel,theProto);
78   Interface_CheckIterator chl = cht.CompleteCheckList();
79   if ( withprint && ! theProc.IsNull() ) 
80     chl.Print (theProc->Messenger(), theModel, Standard_False);
81   return chl.IsEmpty(Standard_True);
82 }
83
84     Interface_CheckIterator  TransferBRep_Reader::CheckListModel () const
85 {
86   Interface_CheckTool cht (theModel,theProto);
87   Interface_CheckIterator chl = cht.CompleteCheckList();
88   return chl;
89 }
90
91     Standard_Boolean&  TransferBRep_Reader::ModeNewTransfer ()
92       {  return theNewpr;  }
93
94     Standard_Boolean  TransferBRep_Reader::BeginTransfer ()
95 {
96   theDone = Standard_False;
97   if (theModel.IsNull()) return Standard_False;
98
99   if (theNewpr || theProc.IsNull())
100     theProc = new Transfer_TransientProcess (theModel->NbEntities());
101   else theProc->Clear();
102   theProc->SetErrorHandle(Standard_True);
103   theProc->SetModel (theModel);
104   PrepareTransfer();
105   theProc->SetActor (theActor);
106   return Standard_True;
107 }
108
109     void  TransferBRep_Reader::EndTransfer ()
110 {
111   theShapes->Append ( TransferBRep::Shapes (theProc,Standard_True) );
112   Standard_Integer i,nb = theProc->NbRoots();
113   for (i = 1; i <= nb; i ++) {
114     Handle(Standard_Transient) ent = theProc->Root(i);
115     Handle(Standard_Transient) res = theProc->FindTransient(ent);
116     if (!res.IsNull()) theTransi->Append (res);
117   }
118   theDone = Standard_True;
119 }
120
121
122     void  TransferBRep_Reader::PrepareTransfer ()    {  }
123
124     void  TransferBRep_Reader::TransferRoots ()
125 {
126   Clear();
127   if (!BeginTransfer()) return;
128   Transfer_TransferOutput TP (theProc,theModel);
129
130   TP.TransferRoots(theProto);
131   EndTransfer();
132 }
133
134     Standard_Boolean TransferBRep_Reader::Transfer (const Standard_Integer num)
135 {
136   if (!BeginTransfer()) return Standard_False;
137   if (num <= 0 || num > theModel->NbEntities()) return Standard_False;
138   Handle(Standard_Transient) ent = theModel->Value(num);
139   Transfer_TransferOutput TP (theProc,theModel);
140
141   if (theProc->TraceLevel() > 1) {
142     Handle(Message_Messenger) sout = theProc->Messenger();
143     sout<<"--  Transfer(Read) : ";  
144     theModel->Print (ent,sout);  
145     sout<<endl;
146   }
147   TP.Transfer(ent);
148   theProc->SetRoot(ent);
149   EndTransfer();
150   return Standard_True;
151 }
152
153     void  TransferBRep_Reader::TransferList
154   (const Handle(TColStd_HSequenceOfTransient)& list)
155 {
156   if (!BeginTransfer()) return;
157   if (list.IsNull()) return;
158   Transfer_TransferOutput TP (theProc,theModel);
159   Standard_Integer i, nb = list->Length();
160   Handle(Message_Messenger) sout = theProc->Messenger();
161
162   if (theProc->TraceLevel() > 1) 
163     sout<<"--  Transfer(Read-List) : "<<nb<<" Items"<<endl;
164   for (i = 1; i <= nb; i ++) {
165     Handle(Standard_Transient) ent = list->Value(i);
166     if (theModel->Number(ent) == 0) continue;
167
168     if (theProc->TraceLevel() > 1) 
169     {
170       sout<<"--  Transfer(Read-List), Item "<<i<<" : ";  
171       theModel->Print (ent,sout);  
172       sout<<endl;
173     }
174     TP.Transfer(ent);
175     theProc->SetRoot(ent);
176   }
177   EndTransfer();
178 }
179
180     Standard_Boolean  TransferBRep_Reader::IsDone () const
181       {  return theDone;  }
182
183 //   ######    RESULTAT : SHAPES    ######
184
185     Standard_Integer  TransferBRep_Reader::NbShapes () const
186       {  return theShapes->Length();  }
187
188     Handle(TopTools_HSequenceOfShape)  TransferBRep_Reader::Shapes () const
189       {  return theShapes;  }
190
191     const TopoDS_Shape&  TransferBRep_Reader::Shape
192   (const Standard_Integer num) const
193       {  return theShapes->Value(num);  }
194
195     TopoDS_Shape  TransferBRep_Reader::OneShape () const
196 {
197   TopoDS_Shape res;
198   Standard_Integer nb = theShapes->Length();
199   if (nb == 0) return res;
200   else if (nb == 1) return theShapes->Value(1);
201   else {
202     TopoDS_Compound C;
203     BRep_Builder B;
204     B.MakeCompound(C);
205     for (Standard_Integer i = 1; i <= nb; i ++)  B.Add (C,theShapes->Value(i));
206     return C;
207   }
208 }
209
210     TopoDS_Shape  TransferBRep_Reader::ShapeResult
211   (const Handle(Standard_Transient)& ent) const
212       {  return TransferBRep::ShapeResult (theProc,ent);  }
213
214 //   ######    RESULTAT : TRANSIENTS    ######
215
216     Standard_Integer  TransferBRep_Reader::NbTransients () const
217       {  return theTransi->Length();  }
218
219     Handle(TColStd_HSequenceOfTransient)  TransferBRep_Reader::Transients () const
220       {  return theTransi;  }
221
222     Handle(Standard_Transient)  TransferBRep_Reader::Transient
223   (const Standard_Integer num) const
224       {  return theTransi->Value(num);  }
225
226
227 //   ######    CHECKS    ######
228
229     Standard_Boolean  TransferBRep_Reader::CheckStatusResult
230   (const Standard_Boolean withprint) const
231 {
232   Interface_CheckIterator chl;
233   if (!theProc.IsNull()) chl = theProc->CheckList(Standard_False);
234   if (withprint && ! theProc.IsNull()) 
235     chl.Print (theProc->Messenger(), theModel, Standard_False);
236   return chl.IsEmpty(Standard_True);
237 }
238
239     Interface_CheckIterator  TransferBRep_Reader::CheckListResult () const
240 {
241   if (!theProc.IsNull()) return theProc->CheckList(Standard_False);
242   Interface_CheckIterator chbid;  return chbid;
243 }
244
245     Handle(Transfer_TransientProcess)  TransferBRep_Reader::TransientProcess
246   () const
247       {  return theProc;  }
248
249 void TransferBRep_Reader::Destroy() {}
250