1 // Created on: 2000-08-16
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <TDF_Label.hxx>
17 #include <IGESCAFControl_Reader.ixx>
18 #include <TopoDS_Shape.hxx>
19 #include <XCAFDoc_ShapeTool.hxx>
20 #include <XSControl_TransferReader.hxx>
21 #include <XCAFDoc_ColorTool.hxx>
22 #include <IGESData_IGESEntity.hxx>
23 #include <Transfer_Binder.hxx>
24 #include <TransferBRep.hxx>
25 #include <Quantity_Color.hxx>
26 #include <IGESCAFControl.hxx>
27 #include <IGESGraph_Color.hxx>
28 #include <Interface_InterfaceModel.hxx>
29 #include <Transfer_TransientProcess.hxx>
30 #include <XCAFDoc_DocumentTool.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <TopTools_MapOfShape.hxx>
33 #include <TCollection_ExtendedString.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <XCAFDoc_LayerTool.hxx>
36 #include <IGESData_LevelListEntity.hxx>
37 #include <TCollection_HAsciiString.hxx>
38 #include <XCAFDoc_ShapeMapTool.hxx>
39 #include <IGESBasic_SubfigureDef.hxx>
40 #include <TopoDS_Compound.hxx>
41 #include <BRep_Builder.hxx>
43 //=======================================================================
44 //function : IGESCAFControl_Reader
46 //=======================================================================
48 IGESCAFControl_Reader::IGESCAFControl_Reader () :
49 myColorMode( Standard_True ),
50 myNameMode ( Standard_True ),
51 myLayerMode( Standard_True )
56 //=======================================================================
57 //function : IGESCAFControl_Reader
59 //=======================================================================
61 IGESCAFControl_Reader::IGESCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
62 const Standard_Boolean scratch)
65 myColorMode = Standard_True;
66 myNameMode = Standard_True;
67 myLayerMode = Standard_True;
71 //=======================================================================
73 //purpose : basic working method
74 //=======================================================================
75 static void checkColorRange (Standard_Real& theCol)
77 if ( theCol < 0. ) theCol = 0.;
78 if ( theCol > 100. ) theCol = 100.;
81 static inline Standard_Boolean IsComposite (const TopoDS_Shape& theShape)
83 if( theShape.ShapeType() == TopAbs_COMPOUND)
85 if(!theShape.Location().IsIdentity())
87 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
89 for (; anIt.More() ; anIt.Next())
91 if( IsComposite (anIt.Value()))
96 return Standard_False;
99 //=======================================================================
100 //function : AddCompositeShape
101 //purpose : Recursively adds composite shapes (TopoDS_Compounds) into the XDE document.
102 // If the compound does not contain nested compounds then adds it
103 // as no-assembly (i.e. no individual labels for sub-shapes), as this
104 // combination is often encountered in IGES (e.g. Group of Trimmed Surfaces).
105 // If the compound does contain nested compounds then adds it as an
107 // The construction happens bottom-up, i.e. the most deep sub-shapes are added
109 // If theIsTop is False (in a recursive call) then sub-shapes are added without
110 // a location. This is to ensure that no extra label in the XDE document is
111 // created for an instance (as otherwise, XDE will consider it as a free
112 // shape). Correct location and instance will be created when adding a parent
114 // theMap is used to avoid visiting the same compound.
115 //=======================================================================
116 static void AddCompositeShape (const Handle(XCAFDoc_ShapeTool)& theSTool,
117 const TopoDS_Shape& theShape,
118 Standard_Boolean theConsiderLoc,
119 TopTools_MapOfShape& theMap)
121 TopoDS_Shape aShape = theShape;
122 TopLoc_Location aLoc = theShape.Location();
123 if (!theConsiderLoc && !aLoc.IsIdentity())
124 aShape.Location( TopLoc_Location() );
125 if (!theMap.Add (aShape))
128 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
129 Standard_Boolean aHasCompositeSubShape = Standard_False;
130 TopoDS_Compound aSimpleShape;
132 aB.MakeCompound( aSimpleShape);
133 TopoDS_Compound aCompShape;
134 aB.MakeCompound( aCompShape);
135 Standard_Integer nbSimple = 0;
137 for (; anIt.More(); anIt.Next()) {
138 const TopoDS_Shape& aSubShape = anIt.Value();
139 if (IsComposite (aSubShape)) {
140 aHasCompositeSubShape = Standard_True;
141 AddCompositeShape( theSTool, aSubShape,Standard_False ,theMap );
142 aB.Add( aCompShape, aSubShape);
146 aB.Add(aSimpleShape, aSubShape);
150 //case of hybrid shape
151 if( nbSimple && aHasCompositeSubShape)
153 theSTool->AddShape( aSimpleShape, Standard_False, Standard_False );
154 TopoDS_Compound aNewShape;
156 aB.MakeCompound(aNewShape);
157 aB.Add(aNewShape, aSimpleShape);
158 aB.Add(aNewShape,aCompShape);
159 //if (!aLoc.IsIdentity())
160 // aNewShape.Location(aLoc );
161 aNewShape.Orientation(theShape.Orientation());
162 theSTool->AddShape( aNewShape, aHasCompositeSubShape, Standard_False );
165 theSTool->AddShape( aShape, aHasCompositeSubShape, Standard_False );
169 Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
172 Standard_Integer num;// = NbRootsForTransfer();
173 //if ( num <=0 ) return Standard_False;
174 //for ( Standard_Integer i=1; i <= num; i++ ) {
175 // TransferOneRoot ( i );
178 TransferRoots(); // replaces the above
180 if ( num <=0 ) return Standard_False;
182 // and insert them to the document
183 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
184 if(STool.IsNull()) return Standard_False;
186 for(i=1; i<=num; i++) {
187 TopoDS_Shape sh = Shape ( i );
188 // ---- HERE -- to add check [ assembly / hybrid model ]
189 if( !IsComposite (sh))
190 STool->AddShape( sh, Standard_False );
192 TopTools_MapOfShape aMap;
193 AddCompositeShape( STool, sh,Standard_True, aMap );
198 // added by skl 13.10.2003
199 Handle(Interface_InterfaceModel) Model = WS()->Model();
200 //WS()->TransferReader()->SetTransientProcess(TransientProcess());
201 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
202 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
203 Standard_Boolean IsCTool = Standard_True;
204 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
205 if(CTool.IsNull()) IsCTool = Standard_False;
206 Standard_Boolean IsLTool = Standard_True;
207 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool(doc->Main());
208 if(LTool.IsNull()) IsLTool = Standard_False;
210 Standard_Integer nb = Model->NbEntities();
211 for(i=1; i<=nb; i++) {
212 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
213 if ( ent.IsNull() ) continue;
214 Handle(Transfer_Binder) binder = TP->Find ( ent );
215 if ( binder.IsNull() ) continue;
216 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
217 if ( S.IsNull() ) continue;
219 Standard_Boolean IsColor = Standard_False;
221 if( GetColorMode() && IsCTool ) {
223 if(ent->DefColor()==IGESData_DefValue ||
224 ent->DefColor()==IGESData_DefReference) {
226 // decode color and set to document
227 IsColor = Standard_True;
228 if ( ent->DefColor() == IGESData_DefValue ) {
229 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
232 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
233 if ( color.IsNull() ) {
235 cout << "Error: Unrecognized type of color definition" << endl;
237 IsColor = Standard_False;
240 Standard_Real r, g, b;
241 color->RGBIntensity ( r, g, b );
242 checkColorRange ( r );
243 checkColorRange ( g );
244 checkColorRange ( b );
245 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
253 Standard_Boolean IsFound;
255 CTool->AddColor(col);
256 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
259 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
263 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
264 if(STool->SearchUsingMap(it.Value(),L,Standard_False,Standard_True)) {
265 CTool->SetColor(L,col,XCAFDoc_ColorGen);
266 if( GetLayerMode() && IsLTool ) {
268 // set a layers to the document
269 IGESData_DefList aDeflist = ent->DefLevel();
271 case IGESData_DefOne : {
272 TCollection_ExtendedString aLayerName ( ent->Level() );
273 LTool->SetLayer( L, aLayerName );
276 case IGESData_DefSeveral : {
277 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
278 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
279 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
280 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
281 LTool->SetLayer( L, aLayerName );
294 CTool->SetColor(L,col,XCAFDoc_ColorGen);
299 TCollection_AsciiString string = ent->NameValue()->String();
301 string.RightAdjust();
302 TCollection_ExtendedString str(string);
303 TDataStd_Name::Set(L,str);
306 if( GetLayerMode() && IsLTool ) {
308 // set a layers to the document
309 IGESData_DefList aDeflist = ent->DefLevel();
311 case IGESData_DefOne : {
312 TCollection_ExtendedString aLayerName ( ent->Level() );
313 LTool->SetLayer( L, aLayerName );
316 case IGESData_DefSeveral : {
317 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
318 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
319 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
320 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
321 LTool->SetLayer( L, aLayerName );
330 //Checks that current entity is a subfigure
331 Handle(IGESBasic_SubfigureDef) aSubfigure = Handle(IGESBasic_SubfigureDef)::DownCast (ent);
332 if (GetNameMode() && !aSubfigure.IsNull() && STool->Search (S, L, Standard_True, Standard_True))
334 //In this case we attach subfigure name to the label, instead of default "COMPOUND"
335 Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
337 aName->RightAdjust();
338 TCollection_ExtendedString anExtStrName (aName->ToCString());
339 TDataStd_Name::Set (L, anExtStrName);
344 CTool->ReverseChainsOfTreeNodes();
346 // end added by skl 13.10.2003
349 // if ( GetColorMode() )
350 // ReadColors ( doc );
353 // if ( GetNameMode() )
354 // ReadNames ( doc );
357 // if ( GetLayerMode() )
358 // ReadLayers ( doc );
361 return Standard_True;
365 //=======================================================================
368 //=======================================================================
370 Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
371 Handle(TDocStd_Document) &doc)
373 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
374 return Transfer ( doc );
378 //=======================================================================
381 //=======================================================================
383 Standard_Boolean IGESCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
384 Handle(TDocStd_Document) &doc)
386 if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
387 return Transfer ( doc );
391 //=======================================================================
392 //function : ReadColors
394 //=======================================================================
396 Standard_Boolean IGESCAFControl_Reader::ReadColors (Handle(TDocStd_Document)& Doc) const
399 Handle(Interface_InterfaceModel) Model = WS()->Model();
401 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
403 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
404 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
405 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
407 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
408 if ( STool.IsNull() ) return Standard_False;
409 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
410 SMTool->ComputeMaps();
412 Standard_Integer nb = Model->NbEntities();
413 for (Standard_Integer i = 1; i <= nb; i ++) {
414 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
415 if ( ent.IsNull() ) continue;
416 if ( ent->DefColor() != IGESData_DefValue &&
417 ent->DefColor() != IGESData_DefReference ) continue; // no color assigned
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;
425 // decode color and set to document
427 if ( ent->DefColor() == IGESData_DefValue ) {
428 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
431 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
432 if ( color.IsNull() ) {
433 cout << "Error: Unrecognized type of color definition" << endl;
437 Standard_Real r, g, b;
438 color->RGBIntensity ( r, g, b );
439 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
444 if(SMTool->Search(S,L)) {
445 cout<<" find Instance"<<endl;
446 CTool->SetColor(L, col, XCAFDoc_ColorGen);
449 cout<<"L1 is Null"<<endl;
450 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
451 cout<<" add new label1 :"<<L<<endl;
452 CTool->SetColor(L, col, XCAFDoc_ColorGen);
457 cout<<" try to find splitting"<<endl;
458 // may be S is compound of shapes resulting from splitting
459 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
461 if(!SMTool->Search(it.Value(),L)) continue; //break-?
462 cout<<" find splitting"<<endl;
463 CTool->SetColor(L, col, XCAFDoc_ColorGen);
466 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
467 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
468 cout<<" add new label2 :"<<L<<endl;
469 CTool->SetColor(L, col, XCAFDoc_ColorGen);
474 // cout<<"L.Dump():"<<L<<endl;
476 // cout<<"L2 is Null"<<endl;
477 // if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
478 // cout<<" add new label2 :"<<L<<endl;
479 // CTool->SetColor(L, col, XCAFDoc_ColorGen);
484 return Standard_True;
487 //=======================================================================
488 //function : ReadNames
490 //=======================================================================
492 Standard_Boolean IGESCAFControl_Reader::ReadNames (Handle(TDocStd_Document)& /*Doc*/) const
494 Handle(Interface_InterfaceModel) Model = WS()->Model();
496 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
498 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
499 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
501 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
502 if ( STool.IsNull() ) return Standard_False;
503 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
504 SMTool->ComputeMaps();
506 Standard_Integer nb = Model->NbEntities();
507 for (Standard_Integer i = 1; i <= nb; i ++) {
508 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
509 if ( ent.IsNull() || ! ent->HasName() ) continue; //not only Entity Label (f.18) but Name Property also
512 Handle(Transfer_Binder) binder = TP->Find ( ent );
513 if ( binder.IsNull() ) continue;
514 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
515 if ( S.IsNull() ) continue;
518 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
519 if ( ! SMTool->Search ( S, L, Standard_True, Standard_True) ) continue;
521 // set a name to the document
522 TCollection_AsciiString string = ent->NameValue()->String();
524 string.RightAdjust();
525 TCollection_ExtendedString str ( string );
526 TDataStd_Name::Set ( L, str );
529 return Standard_True;
532 //=======================================================================
533 //function : ReadLayers
535 //=======================================================================
537 Standard_Boolean IGESCAFControl_Reader::ReadLayers (Handle(TDocStd_Document)& /*Doc*/) const
539 Handle(Interface_InterfaceModel) Model = WS()->Model();
541 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
543 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
544 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
546 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
547 if ( STool.IsNull() ) return Standard_False;
548 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
549 SMTool->ComputeMaps();
551 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
552 if ( LTool.IsNull() ) return Standard_False;
554 Standard_Integer nb = Model->NbEntities();
555 for (Standard_Integer i = 1; i <= nb; i ++) {
556 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
558 if ( ent.IsNull() ) continue;
561 Handle(Transfer_Binder) binder = TP->Find ( ent );
562 if ( binder.IsNull() ) continue;
563 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
564 if ( S.IsNull() ) continue;
567 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
568 if( !SMTool->Search(S, L, Standard_True, Standard_True) ) continue;
570 // set a layers to the document
571 IGESData_DefList aDeflist = ent->DefLevel();
574 case IGESData_DefOne : {
575 TCollection_ExtendedString aLayerName ( ent->Level() );
576 LTool->SetLayer( L, aLayerName );
578 // cout << "Added layer " << aLayerName << endl;
582 case IGESData_DefSeveral : {
583 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
584 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
585 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
586 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
587 LTool->SetLayer( L, aLayerName );
589 // cout << "Added layer " << aLayerName << endl;
599 return Standard_True;
603 //=======================================================================
604 //function : SetColorMode
606 //=======================================================================
608 void IGESCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
610 myColorMode = colormode;
613 //=======================================================================
614 //function : GetColorMode
616 //=======================================================================
618 Standard_Boolean IGESCAFControl_Reader::GetColorMode () const
623 //=======================================================================
624 //function : SetNameMode
626 //=======================================================================
628 void IGESCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
630 myNameMode = namemode;
633 //=======================================================================
634 //function : GetNameMode
636 //=======================================================================
638 Standard_Boolean IGESCAFControl_Reader::GetNameMode () const
643 //=======================================================================
644 //function : SetLayerMode
646 //=======================================================================
648 void IGESCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
650 myLayerMode = layermode;
653 //=======================================================================
654 //function : GetLayerMode
656 //=======================================================================
658 Standard_Boolean IGESCAFControl_Reader::GetLayerMode () const