1 // Created on: 2000-08-16
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
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.
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.
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.
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>
45 //=======================================================================
46 //function : IGESCAFControl_Reader
48 //=======================================================================
50 IGESCAFControl_Reader::IGESCAFControl_Reader () :
51 myColorMode( Standard_True ),
52 myNameMode ( Standard_True ),
53 myLayerMode( Standard_True )
58 //=======================================================================
59 //function : IGESCAFControl_Reader
61 //=======================================================================
63 IGESCAFControl_Reader::IGESCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
64 const Standard_Boolean scratch)
67 myColorMode = Standard_True;
68 myNameMode = Standard_True;
69 myLayerMode = Standard_True;
73 //=======================================================================
75 //purpose : basic working method
76 //=======================================================================
77 static void checkColorRange (Standard_Real& theCol)
79 if ( theCol < 0. ) theCol = 0.;
80 if ( theCol > 100. ) theCol = 100.;
83 Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
86 Standard_Integer num;// = NbRootsForTransfer();
87 //if ( num <=0 ) return Standard_False;
88 //for ( Standard_Integer i=1; i <= num; i++ ) {
89 // TransferOneRoot ( i );
92 TransferRoots(); // replaces the above
94 if ( num <=0 ) return Standard_False;
96 // and insert them to the document
97 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
98 if(STool.IsNull()) return Standard_False;
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 );
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;
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;
127 Standard_Boolean IsColor = Standard_False;
129 if( GetColorMode() && IsCTool ) {
131 if(ent->DefColor()==IGESData_DefValue ||
132 ent->DefColor()==IGESData_DefReference) {
134 // decode color and set to document
135 IsColor = Standard_True;
136 if ( ent->DefColor() == IGESData_DefValue ) {
137 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
140 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
141 if ( color.IsNull() ) {
143 cout << "Error: Unrecognized type of color definition" << endl;
145 IsColor = Standard_False;
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 );
161 Standard_Boolean IsFound;
163 CTool->AddColor(col);
164 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
167 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
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 ) {
176 // set a layers to the document
177 IGESData_DefList aDeflist = ent->DefLevel();
179 case IGESData_DefOne : {
180 TCollection_ExtendedString aLayerName ( ent->Level() );
181 LTool->SetLayer( L, aLayerName );
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 );
202 CTool->SetColor(L,col,XCAFDoc_ColorGen);
207 TCollection_AsciiString string = ent->NameValue()->String();
209 string.RightAdjust();
210 TCollection_ExtendedString str(string);
211 TDataStd_Name::Set(L,str);
214 if( GetLayerMode() && IsLTool ) {
216 // set a layers to the document
217 IGESData_DefList aDeflist = ent->DefLevel();
219 case IGESData_DefOne : {
220 TCollection_ExtendedString aLayerName ( ent->Level() );
221 LTool->SetLayer( L, aLayerName );
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 );
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))
242 //In this case we attach subfigure name to the label, instead of default "COMPOUND"
243 Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
245 aName->RightAdjust();
246 TCollection_ExtendedString anExtStrName (aName->ToCString());
247 TDataStd_Name::Set (L, anExtStrName);
252 CTool->ReverseChainsOfTreeNodes();
254 // end added by skl 13.10.2003
257 // if ( GetColorMode() )
258 // ReadColors ( doc );
261 // if ( GetNameMode() )
262 // ReadNames ( doc );
265 // if ( GetLayerMode() )
266 // ReadLayers ( doc );
269 return Standard_True;
273 //=======================================================================
276 //=======================================================================
278 Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
279 Handle(TDocStd_Document) &doc)
281 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
282 return Transfer ( doc );
286 //=======================================================================
289 //=======================================================================
291 Standard_Boolean IGESCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
292 Handle(TDocStd_Document) &doc)
294 if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
295 return Transfer ( doc );
299 //=======================================================================
300 //function : ReadColors
302 //=======================================================================
304 Standard_Boolean IGESCAFControl_Reader::ReadColors (Handle(TDocStd_Document)& Doc) const
307 Handle(Interface_InterfaceModel) Model = WS()->Model();
309 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
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() );
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();
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
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;
333 // decode color and set to document
335 if ( ent->DefColor() == IGESData_DefValue ) {
336 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
339 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
340 if ( color.IsNull() ) {
341 cout << "Error: Unrecognized type of color definition" << endl;
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 );
352 if(SMTool->Search(S,L)) {
353 cout<<" find Instance"<<endl;
354 CTool->SetColor(L, col, XCAFDoc_ColorGen);
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);
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()) {
369 if(!SMTool->Search(it.Value(),L)) continue; //break-?
370 cout<<" find splitting"<<endl;
371 CTool->SetColor(L, col, XCAFDoc_ColorGen);
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);
382 // cout<<"L.Dump():"<<L<<endl;
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);
392 return Standard_True;
395 //=======================================================================
396 //function : ReadNames
398 //=======================================================================
400 Standard_Boolean IGESCAFControl_Reader::ReadNames (Handle(TDocStd_Document)& /*Doc*/) const
402 Handle(Interface_InterfaceModel) Model = WS()->Model();
404 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
406 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
407 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
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();
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
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;
426 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
427 if ( ! SMTool->Search ( S, L, Standard_True, Standard_True) ) continue;
429 // set a name to the document
430 TCollection_AsciiString string = ent->NameValue()->String();
432 string.RightAdjust();
433 TCollection_ExtendedString str ( string );
434 TDataStd_Name::Set ( L, str );
437 return Standard_True;
440 //=======================================================================
441 //function : ReadLayers
443 //=======================================================================
445 Standard_Boolean IGESCAFControl_Reader::ReadLayers (Handle(TDocStd_Document)& /*Doc*/) const
447 Handle(Interface_InterfaceModel) Model = WS()->Model();
449 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
451 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
452 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
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();
459 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
460 if ( LTool.IsNull() ) return Standard_False;
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) );
466 if ( ent.IsNull() ) continue;
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;
475 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
476 if( !SMTool->Search(S, L, Standard_True, Standard_True) ) continue;
478 // set a layers to the document
479 IGESData_DefList aDeflist = ent->DefLevel();
482 case IGESData_DefOne : {
483 TCollection_ExtendedString aLayerName ( ent->Level() );
484 LTool->SetLayer( L, aLayerName );
486 // cout << "Added layer " << aLayerName << endl;
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 );
497 // cout << "Added layer " << aLayerName << endl;
507 return Standard_True;
511 //=======================================================================
512 //function : SetColorMode
514 //=======================================================================
516 void IGESCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
518 myColorMode = colormode;
521 //=======================================================================
522 //function : GetColorMode
524 //=======================================================================
526 Standard_Boolean IGESCAFControl_Reader::GetColorMode () const
531 //=======================================================================
532 //function : SetNameMode
534 //=======================================================================
536 void IGESCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
538 myNameMode = namemode;
541 //=======================================================================
542 //function : GetNameMode
544 //=======================================================================
546 Standard_Boolean IGESCAFControl_Reader::GetNameMode () const
551 //=======================================================================
552 //function : SetLayerMode
554 //=======================================================================
556 void IGESCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
558 myLayerMode = layermode;
561 //=======================================================================
562 //function : GetLayerMode
564 //=======================================================================
566 Standard_Boolean IGESCAFControl_Reader::GetLayerMode () const