0015697: External Contribution on OCC DE Improvement
[occt.git] / src / IGESCAFControl / IGESCAFControl_Reader.cxx
CommitLineData
7fd59977 1// File: IGESCAFControl_Reader.cxx
2// Created: Wed Aug 16 18:01:56 2000
3// Author: Andrey BETENEV
4// <abv@doomox.nnov.matra-dtv.fr>
5
6#include <TDF_Label.hxx>
7#include <IGESCAFControl_Reader.ixx>
8#include <TopoDS_Shape.hxx>
9#include <XCAFDoc_ShapeTool.hxx>
10#include <XSControl_TransferReader.hxx>
11#include <XCAFDoc_ColorTool.hxx>
12#include <IGESData_IGESEntity.hxx>
13#include <Transfer_Binder.hxx>
14#include <TransferBRep.hxx>
15#include <Quantity_Color.hxx>
16#include <IGESCAFControl.hxx>
17#include <IGESGraph_Color.hxx>
18#include <Interface_InterfaceModel.hxx>
19#include <Transfer_TransientProcess.hxx>
20#include <XCAFDoc_DocumentTool.hxx>
21#include <TopoDS_Iterator.hxx>
22#include <TCollection_ExtendedString.hxx>
23#include <TDataStd_Name.hxx>
24#include <XCAFDoc_LayerTool.hxx>
25#include <IGESData_LevelListEntity.hxx>
26#include <TCollection_HAsciiString.hxx>
27#include <XCAFDoc_ShapeMapTool.hxx>
28
29//=======================================================================
30//function : IGESCAFControl_Reader
31//purpose :
32//=======================================================================
33
34IGESCAFControl_Reader::IGESCAFControl_Reader () :
35 myColorMode( Standard_True ),
36 myNameMode ( Standard_True ),
37 myLayerMode( Standard_True )
38{
39}
40
41
42//=======================================================================
43//function : IGESCAFControl_Reader
44//purpose :
45//=======================================================================
46
47IGESCAFControl_Reader::IGESCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
48 const Standard_Boolean scratch)
49{
50 SetWS (WS,scratch);
51 myColorMode = Standard_True;
52 myNameMode = Standard_True;
53 myLayerMode = Standard_True;
54}
55
56
57//=======================================================================
58//function : Transfer
59//purpose : basic working method
60//=======================================================================
1332f047
A
61static void checkColorRange (Standard_Real& theCol)
62{
63 if ( theCol < 0. ) theCol = 0.;
64 if ( theCol > 100. ) theCol = 100.;
65}
7fd59977 66
67Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
68{
69 // read all shapes
70 Standard_Integer num;// = NbRootsForTransfer();
71 //if ( num <=0 ) return Standard_False;
72 //for ( Standard_Integer i=1; i <= num; i++ ) {
73 // TransferOneRoot ( i );
74 //}
75
76 TransferRoots(); // replaces the above
77 num = NbShapes();
78 if ( num <=0 ) return Standard_False;
79
80 // and insert them to the document
81 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
82 if(STool.IsNull()) return Standard_False;
83 Standard_Integer i;
84 for(i=1; i<=num; i++) {
85 TopoDS_Shape sh = Shape ( i );
86 // ---- HERE -- to add check [ assembly / hybrid model ]
87 STool->AddShape ( sh, Standard_False );
88 }
89
90 // added by skl 13.10.2003
91 Handle(Interface_InterfaceModel) Model = WS()->Model();
92 //WS()->TransferReader()->SetTransientProcess(TransientProcess());
93 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
94 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
95 Standard_Boolean IsCTool = Standard_True;
96 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
97 if(CTool.IsNull()) IsCTool = Standard_False;
98 Standard_Boolean IsLTool = Standard_True;
99 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool(doc->Main());
100 if(LTool.IsNull()) IsLTool = Standard_False;
101
102 Standard_Integer nb = Model->NbEntities();
103 for(i=1; i<=nb; i++) {
104 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
105 if ( ent.IsNull() ) continue;
106 Handle(Transfer_Binder) binder = TP->Find ( ent );
107 if ( binder.IsNull() ) continue;
108 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
109 if ( S.IsNull() ) continue;
110
111 Standard_Boolean IsColor = Standard_False;
112 Quantity_Color col;
113 if( GetColorMode() && IsCTool ) {
114 // read colors
115 if(ent->DefColor()==IGESData_DefValue ||
116 ent->DefColor()==IGESData_DefReference) {
117 // color is assigned
118 // decode color and set to document
119 IsColor = Standard_True;
120 if ( ent->DefColor() == IGESData_DefValue ) {
121 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
122 }
123 else {
124 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
125 if ( color.IsNull() ) {
126#ifdef DEB
127 cout << "Error: Unrecognized type of color definition" << endl;
128#endif
129 IsColor = Standard_False;
130 }
131 else {
132 Standard_Real r, g, b;
133 color->RGBIntensity ( r, g, b );
1332f047
A
134 checkColorRange ( r );
135 checkColorRange ( g );
136 checkColorRange ( b );
7fd59977 137 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
138 }
139 }
140 }
141 }
142
143 TDF_Label L;
144
145 Standard_Boolean IsFound;
146 if(IsColor) {
147 CTool->AddColor(col);
148 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
149 }
150 else {
151 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
152 }
153 if(!IsFound) {
154 if(IsColor) {
155 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
156 if(STool->SearchUsingMap(it.Value(),L,Standard_False,Standard_True)) {
157 CTool->SetColor(L,col,XCAFDoc_ColorGen);
158 if( GetLayerMode() && IsLTool ) {
159 // read layers
160 // set a layers to the document
161 IGESData_DefList aDeflist = ent->DefLevel();
162 switch (aDeflist) {
163 case IGESData_DefOne : {
164 TCollection_ExtendedString aLayerName ( ent->Level() );
165 LTool->SetLayer( L, aLayerName );
166 break;
167 }
168 case IGESData_DefSeveral : {
169 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
170 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
171 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
172 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
173 LTool->SetLayer( L, aLayerName );
174 }
175 break;
176 }
177 default : break;
178 }
179 }
180 }
181 }
182 }
183 }
184 else {
185 if(IsColor) {
186 CTool->SetColor(L,col,XCAFDoc_ColorGen);
187 }
188 if(GetNameMode()) {
189 // read names
190 if(ent->HasName()) {
191 TCollection_AsciiString string = ent->NameValue()->String();
192 string.LeftAdjust();
193 string.RightAdjust();
194 TCollection_ExtendedString str(string);
195 TDataStd_Name::Set(L,str);
196 }
197 }
198 if( GetLayerMode() && IsLTool ) {
199 // read layers
200 // set a layers to the document
201 IGESData_DefList aDeflist = ent->DefLevel();
202 switch (aDeflist) {
203 case IGESData_DefOne : {
204 TCollection_ExtendedString aLayerName ( ent->Level() );
205 LTool->SetLayer( L, aLayerName );
206 break;
207 }
208 case IGESData_DefSeveral : {
209 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
210 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
211 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
212 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
213 LTool->SetLayer( L, aLayerName );
214 }
215 break;
216 }
217 default : break;
218 }
219 }
220 }
221 }
222
223 CTool->ReverseChainsOfTreeNodes();
224
225 // end added by skl 13.10.2003
226
227 // read colors
228// if ( GetColorMode() )
229// ReadColors ( doc );
230
231 // read names
232// if ( GetNameMode() )
233// ReadNames ( doc );
234
235 // read layers
236// if ( GetLayerMode() )
237// ReadLayers ( doc );
238
239
240 return Standard_True;
241}
242
243
244//=======================================================================
245//function : Perform
246//purpose :
247//=======================================================================
248
249Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
250 Handle(TDocStd_Document) &doc)
251{
252 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
253 return Transfer ( doc );
254}
255
256
257//=======================================================================
258//function : Perform
259//purpose :
260//=======================================================================
261
262Standard_Boolean IGESCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
263 Handle(TDocStd_Document) &doc)
264{
265 if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
266 return Transfer ( doc );
267}
268
269
270//=======================================================================
271//function : ReadColors
272//purpose :
273//=======================================================================
274
275Standard_Boolean IGESCAFControl_Reader::ReadColors (Handle(TDocStd_Document)& Doc) const
276{
277
278 Handle(Interface_InterfaceModel) Model = WS()->Model();
279
280 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
281
282 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
283 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
284 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
285/*
286 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
287 if ( STool.IsNull() ) return Standard_False;
288 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
289 SMTool->ComputeMaps();
290
291 Standard_Integer nb = Model->NbEntities();
292 for (Standard_Integer i = 1; i <= nb; i ++) {
293 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
294 if ( ent.IsNull() ) continue;
295 if ( ent->DefColor() != IGESData_DefValue &&
296 ent->DefColor() != IGESData_DefReference ) continue; // no color assigned
297
298 // find tartet shape
299 Handle(Transfer_Binder) binder = TP->Find ( ent );
300 if ( binder.IsNull() ) continue;
301 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
302 if ( S.IsNull() ) continue;
303
304 // decode color and set to document
305 Quantity_Color col;
306 if ( ent->DefColor() == IGESData_DefValue ) {
307 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
308 }
309 else {
310 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
311 if ( color.IsNull() ) {
312 cout << "Error: Unrecognized type of color definition" << endl;
313 continue;
314 }
315
316 Standard_Real r, g, b;
317 color->RGBIntensity ( r, g, b );
318 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
319 }
320
321 TDF_Label L;
322 cout<<"i="<<i<<endl;
323 if(SMTool->Search(S,L)) {
324 cout<<" find Instance"<<endl;
325 CTool->SetColor(L, col, XCAFDoc_ColorGen);
326 }
327 if(L.IsNull()) {
328 cout<<"L1 is Null"<<endl;
329 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
330 cout<<" add new label1 :"<<L<<endl;
331 CTool->SetColor(L, col, XCAFDoc_ColorGen);
332 }
333 }
334 if(L.IsNull()) {
335 //else {
336 cout<<" try to find splitting"<<endl;
337 // may be S is compound of shapes resulting from splitting
338 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
339 //TDF_Label L1;
340 if(!SMTool->Search(it.Value(),L)) continue; //break-?
341 cout<<" find splitting"<<endl;
342 CTool->SetColor(L, col, XCAFDoc_ColorGen);
343 }
344 if(L.IsNull()) {
345 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
346 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
347 cout<<" add new label2 :"<<L<<endl;
348 CTool->SetColor(L, col, XCAFDoc_ColorGen);
349 }
350 }
351 }
352 }
353// cout<<"L.Dump():"<<L<<endl;
354// if(L.IsNull()) {
355// cout<<"L2 is Null"<<endl;
356// if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
357// cout<<" add new label2 :"<<L<<endl;
358// CTool->SetColor(L, col, XCAFDoc_ColorGen);
359// }
360// }
361 }
362*/
363 return Standard_True;
364}
365
366//=======================================================================
367//function : ReadNames
368//purpose :
369//=======================================================================
370
371Standard_Boolean IGESCAFControl_Reader::ReadNames (Handle(TDocStd_Document)& Doc) const
372{
373 Handle(Interface_InterfaceModel) Model = WS()->Model();
374
375 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
376
377 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
378 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
379/*
380 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
381 if ( STool.IsNull() ) return Standard_False;
382 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
383 SMTool->ComputeMaps();
384
385 Standard_Integer nb = Model->NbEntities();
386 for (Standard_Integer i = 1; i <= nb; i ++) {
387 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
388 if ( ent.IsNull() || ! ent->HasName() ) continue; //not only Entity Label (f.18) but Name Property also
389
390 // find target shape
391 Handle(Transfer_Binder) binder = TP->Find ( ent );
392 if ( binder.IsNull() ) continue;
393 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
394 if ( S.IsNull() ) continue;
395
396 TDF_Label L;
397 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
398 if ( ! SMTool->Search ( S, L, Standard_True, Standard_True) ) continue;
399
400 // set a name to the document
401 TCollection_AsciiString string = ent->NameValue()->String();
402 string.LeftAdjust();
403 string.RightAdjust();
404 TCollection_ExtendedString str ( string );
405 TDataStd_Name::Set ( L, str );
406 }
407*/
408 return Standard_True;
409}
410
411//=======================================================================
412//function : ReadLayers
413//purpose :
414//=======================================================================
415
416Standard_Boolean IGESCAFControl_Reader::ReadLayers (Handle(TDocStd_Document)& Doc) const
417{
418 Handle(Interface_InterfaceModel) Model = WS()->Model();
419
420 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
421
422 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
423 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
424/*
425 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
426 if ( STool.IsNull() ) return Standard_False;
427 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
428 SMTool->ComputeMaps();
429
430 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
431 if ( LTool.IsNull() ) return Standard_False;
432
433 Standard_Integer nb = Model->NbEntities();
434 for (Standard_Integer i = 1; i <= nb; i ++) {
435 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
436
437 if ( ent.IsNull() ) continue;
438
439 // find target shape
440 Handle(Transfer_Binder) binder = TP->Find ( ent );
441 if ( binder.IsNull() ) continue;
442 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
443 if ( S.IsNull() ) continue;
444
445 TDF_Label L;
446 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
447 if( !SMTool->Search(S, L, Standard_True, Standard_True) ) continue;
448
449 // set a layers to the document
450 IGESData_DefList aDeflist = ent->DefLevel();
451
452 switch (aDeflist) {
453 case IGESData_DefOne : {
454 TCollection_ExtendedString aLayerName ( ent->Level() );
455 LTool->SetLayer( L, aLayerName );
456#ifdef DEB
457// cout << "Added layer " << aLayerName << endl;
458#endif
459 break;
460 }
461 case IGESData_DefSeveral : {
462 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
463 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
464 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
465 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
466 LTool->SetLayer( L, aLayerName );
467#ifdef DEB
468// cout << "Added layer " << aLayerName << endl;
469#endif
470 }
471 break;
472 }
473 default : break;
474 }
475
476 }
477*/
478 return Standard_True;
479}
480
481
482//=======================================================================
483//function : SetColorMode
484//purpose :
485//=======================================================================
486
487void IGESCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
488{
489 myColorMode = colormode;
490}
491
492//=======================================================================
493//function : GetColorMode
494//purpose :
495//=======================================================================
496
497Standard_Boolean IGESCAFControl_Reader::GetColorMode () const
498{
499 return myColorMode;
500}
501
502//=======================================================================
503//function : SetNameMode
504//purpose :
505//=======================================================================
506
507void IGESCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
508{
509 myNameMode = namemode;
510}
511
512//=======================================================================
513//function : GetNameMode
514//purpose :
515//=======================================================================
516
517Standard_Boolean IGESCAFControl_Reader::GetNameMode () const
518{
519 return myNameMode;
520}
521
522//=======================================================================
523//function : SetLayerMode
524//purpose :
525//=======================================================================
526
527void IGESCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
528{
529 myLayerMode = layermode;
530}
531
532//=======================================================================
533//function : GetLayerMode
534//purpose :
535//=======================================================================
536
537Standard_Boolean IGESCAFControl_Reader::GetLayerMode () const
538{
539 return myLayerMode;
540}
541