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