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.
17 #include <BRep_Builder.hxx>
18 #include <IGESBasic_SubfigureDef.hxx>
19 #include <IGESCAFControl.hxx>
20 #include <IGESCAFControl_Reader.hxx>
21 #include <IGESData_IGESEntity.hxx>
22 #include <IGESData_LevelListEntity.hxx>
23 #include <IGESGraph_Color.hxx>
24 #include <Interface_InterfaceModel.hxx>
25 #include <Quantity_Color.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TCollection_ExtendedString.hxx>
28 #include <TCollection_HAsciiString.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <TDF_Label.hxx>
31 #include <TDocStd_Document.hxx>
32 #include <TopoDS_Compound.hxx>
33 #include <TopoDS_Iterator.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopTools_MapOfShape.hxx>
36 #include <Transfer_Binder.hxx>
37 #include <Transfer_TransientProcess.hxx>
38 #include <TransferBRep.hxx>
39 #include <XCAFDoc_ColorTool.hxx>
40 #include <XCAFDoc_DocumentTool.hxx>
41 #include <XCAFDoc_LayerTool.hxx>
42 #include <XCAFDoc_ShapeMapTool.hxx>
43 #include <XCAFDoc_ShapeTool.hxx>
44 #include <XSControl_TransferReader.hxx>
45 #include <XSControl_WorkSession.hxx>
47 //=======================================================================
48 //function : IGESCAFControl_Reader
50 //=======================================================================
51 IGESCAFControl_Reader::IGESCAFControl_Reader () :
52 myColorMode( Standard_True ),
53 myNameMode ( Standard_True ),
54 myLayerMode( Standard_True )
59 //=======================================================================
60 //function : IGESCAFControl_Reader
62 //=======================================================================
64 IGESCAFControl_Reader::IGESCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
65 const Standard_Boolean scratch)
68 myColorMode = Standard_True;
69 myNameMode = Standard_True;
70 myLayerMode = Standard_True;
74 //=======================================================================
76 //purpose : basic working method
77 //=======================================================================
78 static void checkColorRange (Standard_Real& theCol)
80 if ( theCol < 0. ) theCol = 0.;
81 if ( theCol > 100. ) theCol = 100.;
84 static inline Standard_Boolean IsComposite (const TopoDS_Shape& theShape)
86 if( theShape.ShapeType() == TopAbs_COMPOUND)
88 if(!theShape.Location().IsIdentity())
90 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
92 for (; anIt.More() ; anIt.Next())
94 if( IsComposite (anIt.Value()))
99 return Standard_False;
102 //=======================================================================
103 //function : AddCompositeShape
104 //purpose : Recursively adds composite shapes (TopoDS_Compounds) into the XDE document.
105 // If the compound does not contain nested compounds then adds it
106 // as no-assembly (i.e. no individual labels for sub-shapes), as this
107 // combination is often encountered in IGES (e.g. Group of Trimmed Surfaces).
108 // If the compound does contain nested compounds then adds it as an
110 // The construction happens bottom-up, i.e. the most deep sub-shapes are added
112 // If theIsTop is False (in a recursive call) then sub-shapes are added without
113 // a location. This is to ensure that no extra label in the XDE document is
114 // created for an instance (as otherwise, XDE will consider it as a free
115 // shape). Correct location and instance will be created when adding a parent
117 // theMap is used to avoid visiting the same compound.
118 //=======================================================================
119 static void AddCompositeShape (const Handle(XCAFDoc_ShapeTool)& theSTool,
120 const TopoDS_Shape& theShape,
121 Standard_Boolean theConsiderLoc,
122 TopTools_MapOfShape& theMap)
124 TopoDS_Shape aShape = theShape;
125 TopLoc_Location aLoc = theShape.Location();
126 if (!theConsiderLoc && !aLoc.IsIdentity())
127 aShape.Location( TopLoc_Location() );
128 if (!theMap.Add (aShape))
131 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
132 Standard_Boolean aHasCompositeSubShape = Standard_False;
133 TopoDS_Compound aSimpleShape;
135 aB.MakeCompound( aSimpleShape);
136 TopoDS_Compound aCompShape;
137 aB.MakeCompound( aCompShape);
138 Standard_Integer nbSimple = 0;
140 for (; anIt.More(); anIt.Next()) {
141 const TopoDS_Shape& aSubShape = anIt.Value();
142 if (IsComposite (aSubShape)) {
143 aHasCompositeSubShape = Standard_True;
144 AddCompositeShape( theSTool, aSubShape,Standard_False ,theMap );
145 aB.Add( aCompShape, aSubShape);
149 aB.Add(aSimpleShape, aSubShape);
153 //case of hybrid shape
154 if( nbSimple && aHasCompositeSubShape)
156 theSTool->AddShape( aSimpleShape, Standard_False, Standard_False );
157 TopoDS_Compound aNewShape;
158 aB.MakeCompound(aNewShape);
159 aB.Add(aNewShape, aSimpleShape);
160 aB.Add(aNewShape,aCompShape);
161 //if (!aLoc.IsIdentity())
162 // aNewShape.Location(aLoc );
163 aNewShape.Orientation(theShape.Orientation());
164 theSTool->AddShape( aNewShape, aHasCompositeSubShape, Standard_False );
167 theSTool->AddShape( aShape, aHasCompositeSubShape, Standard_False );
171 Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
174 Standard_Integer num;// = NbRootsForTransfer();
175 //if ( num <=0 ) return Standard_False;
176 //for ( Standard_Integer i=1; i <= num; i++ ) {
177 // TransferOneRoot ( i );
180 TransferRoots(); // replaces the above
182 if ( num <=0 ) return Standard_False;
184 // and insert them to the document
185 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
186 if(STool.IsNull()) return Standard_False;
188 for(i=1; i<=num; i++) {
189 TopoDS_Shape sh = Shape ( i );
190 // ---- HERE -- to add check [ assembly / hybrid model ]
191 if( !IsComposite (sh))
192 STool->AddShape( sh, Standard_False );
194 TopTools_MapOfShape aMap;
195 AddCompositeShape( STool, sh,Standard_True, aMap );
200 // added by skl 13.10.2003
201 Handle(Interface_InterfaceModel) Model = WS()->Model();
202 //WS()->TransferReader()->SetTransientProcess(TransientProcess());
203 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
204 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
205 Standard_Boolean IsCTool = Standard_True;
206 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
207 if(CTool.IsNull()) IsCTool = Standard_False;
208 Standard_Boolean IsLTool = Standard_True;
209 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool(doc->Main());
210 if(LTool.IsNull()) IsLTool = Standard_False;
212 Standard_Integer nb = Model->NbEntities();
213 for(i=1; i<=nb; i++) {
214 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
215 if ( ent.IsNull() ) continue;
216 Handle(Transfer_Binder) binder = TP->Find ( ent );
217 if ( binder.IsNull() ) continue;
218 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
219 if ( S.IsNull() ) continue;
221 Standard_Boolean IsColor = Standard_False;
223 if( GetColorMode() && IsCTool ) {
225 if(ent->DefColor()==IGESData_DefValue ||
226 ent->DefColor()==IGESData_DefReference) {
228 // decode color and set to document
229 IsColor = Standard_True;
230 if ( ent->DefColor() == IGESData_DefValue ) {
231 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
234 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
235 if ( color.IsNull() ) {
237 cout << "Error: Unrecognized type of color definition" << endl;
239 IsColor = Standard_False;
242 Standard_Real r, g, b;
243 color->RGBIntensity ( r, g, b );
244 checkColorRange ( r );
245 checkColorRange ( g );
246 checkColorRange ( b );
247 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
255 Standard_Boolean IsFound;
257 CTool->AddColor(col);
258 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
261 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
265 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
266 if(STool->SearchUsingMap(it.Value(),L,Standard_False,Standard_True)) {
267 CTool->SetColor(L,col,XCAFDoc_ColorGen);
268 if( GetLayerMode() && IsLTool ) {
270 // set a layers to the document
271 IGESData_DefList aDeflist = ent->DefLevel();
273 case IGESData_DefOne : {
274 TCollection_ExtendedString aLayerName ( ent->Level() );
275 LTool->SetLayer( L, aLayerName );
278 case IGESData_DefSeveral : {
279 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
280 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
281 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
282 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
283 LTool->SetLayer( L, aLayerName );
296 CTool->SetColor(L,col,XCAFDoc_ColorGen);
301 TCollection_AsciiString string = ent->NameValue()->String();
303 string.RightAdjust();
304 TCollection_ExtendedString str(string);
305 TDataStd_Name::Set(L,str);
308 if( GetLayerMode() && IsLTool ) {
310 // set a layers to the document
311 IGESData_DefList aDeflist = ent->DefLevel();
313 case IGESData_DefOne : {
314 TCollection_ExtendedString aLayerName ( ent->Level() );
315 LTool->SetLayer( L, aLayerName );
318 case IGESData_DefSeveral : {
319 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
320 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
321 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
322 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
323 LTool->SetLayer( L, aLayerName );
332 //Checks that current entity is a subfigure
333 Handle(IGESBasic_SubfigureDef) aSubfigure = Handle(IGESBasic_SubfigureDef)::DownCast (ent);
334 if (GetNameMode() && !aSubfigure.IsNull() && STool->Search (S, L, Standard_True, Standard_True))
336 //In this case we attach subfigure name to the label, instead of default "COMPOUND"
337 Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
339 aName->RightAdjust();
340 TCollection_ExtendedString anExtStrName (aName->ToCString());
341 TDataStd_Name::Set (L, anExtStrName);
346 CTool->ReverseChainsOfTreeNodes();
348 // end added by skl 13.10.2003
351 // if ( GetColorMode() )
352 // ReadColors ( doc );
355 // if ( GetNameMode() )
356 // ReadNames ( doc );
359 // if ( GetLayerMode() )
360 // ReadLayers ( doc );
363 return Standard_True;
367 //=======================================================================
370 //=======================================================================
372 Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
373 Handle(TDocStd_Document) &doc)
375 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
376 return Transfer ( doc );
380 //=======================================================================
383 //=======================================================================
385 Standard_Boolean IGESCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
386 Handle(TDocStd_Document) &doc)
388 if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
389 return Transfer ( doc );
393 //=======================================================================
394 //function : ReadColors
396 //=======================================================================
398 Standard_Boolean IGESCAFControl_Reader::ReadColors (Handle(TDocStd_Document)& Doc) const
401 Handle(Interface_InterfaceModel) Model = WS()->Model();
403 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
405 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
406 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
407 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
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() ) continue;
418 if ( ent->DefColor() != IGESData_DefValue &&
419 ent->DefColor() != IGESData_DefReference ) continue; // no color assigned
422 Handle(Transfer_Binder) binder = TP->Find ( ent );
423 if ( binder.IsNull() ) continue;
424 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
425 if ( S.IsNull() ) continue;
427 // decode color and set to document
429 if ( ent->DefColor() == IGESData_DefValue ) {
430 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
433 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
434 if ( color.IsNull() ) {
435 cout << "Error: Unrecognized type of color definition" << endl;
439 Standard_Real r, g, b;
440 color->RGBIntensity ( r, g, b );
441 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
446 if(SMTool->Search(S,L)) {
447 cout<<" find Instance"<<endl;
448 CTool->SetColor(L, col, XCAFDoc_ColorGen);
451 cout<<"L1 is Null"<<endl;
452 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
453 cout<<" add new label1 :"<<L<<endl;
454 CTool->SetColor(L, col, XCAFDoc_ColorGen);
459 cout<<" try to find splitting"<<endl;
460 // may be S is compound of shapes resulting from splitting
461 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
463 if(!SMTool->Search(it.Value(),L)) continue; //break-?
464 cout<<" find splitting"<<endl;
465 CTool->SetColor(L, col, XCAFDoc_ColorGen);
468 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
469 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
470 cout<<" add new label2 :"<<L<<endl;
471 CTool->SetColor(L, col, XCAFDoc_ColorGen);
476 // cout<<"L.Dump():"<<L<<endl;
478 // cout<<"L2 is Null"<<endl;
479 // if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
480 // cout<<" add new label2 :"<<L<<endl;
481 // CTool->SetColor(L, col, XCAFDoc_ColorGen);
486 return Standard_True;
489 //=======================================================================
490 //function : ReadNames
492 //=======================================================================
494 Standard_Boolean IGESCAFControl_Reader::ReadNames (Handle(TDocStd_Document)& /*Doc*/) const
496 Handle(Interface_InterfaceModel) Model = WS()->Model();
498 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
500 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
501 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
503 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
504 if ( STool.IsNull() ) return Standard_False;
505 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
506 SMTool->ComputeMaps();
508 Standard_Integer nb = Model->NbEntities();
509 for (Standard_Integer i = 1; i <= nb; i ++) {
510 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
511 if ( ent.IsNull() || ! ent->HasName() ) continue; //not only Entity Label (f.18) but Name Property also
514 Handle(Transfer_Binder) binder = TP->Find ( ent );
515 if ( binder.IsNull() ) continue;
516 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
517 if ( S.IsNull() ) continue;
520 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
521 if ( ! SMTool->Search ( S, L, Standard_True, Standard_True) ) continue;
523 // set a name to the document
524 TCollection_AsciiString string = ent->NameValue()->String();
526 string.RightAdjust();
527 TCollection_ExtendedString str ( string );
528 TDataStd_Name::Set ( L, str );
531 return Standard_True;
534 //=======================================================================
535 //function : ReadLayers
537 //=======================================================================
539 Standard_Boolean IGESCAFControl_Reader::ReadLayers (Handle(TDocStd_Document)& /*Doc*/) const
541 Handle(Interface_InterfaceModel) Model = WS()->Model();
543 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
545 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
546 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
548 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
549 if ( STool.IsNull() ) return Standard_False;
550 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
551 SMTool->ComputeMaps();
553 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
554 if ( LTool.IsNull() ) return Standard_False;
556 Standard_Integer nb = Model->NbEntities();
557 for (Standard_Integer i = 1; i <= nb; i ++) {
558 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
560 if ( ent.IsNull() ) continue;
563 Handle(Transfer_Binder) binder = TP->Find ( ent );
564 if ( binder.IsNull() ) continue;
565 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
566 if ( S.IsNull() ) continue;
569 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
570 if( !SMTool->Search(S, L, Standard_True, Standard_True) ) continue;
572 // set a layers to the document
573 IGESData_DefList aDeflist = ent->DefLevel();
576 case IGESData_DefOne : {
577 TCollection_ExtendedString aLayerName ( ent->Level() );
578 LTool->SetLayer( L, aLayerName );
580 // cout << "Added layer " << aLayerName << endl;
584 case IGESData_DefSeveral : {
585 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
586 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
587 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
588 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
589 LTool->SetLayer( L, aLayerName );
591 // cout << "Added layer " << aLayerName << endl;
601 return Standard_True;
605 //=======================================================================
606 //function : SetColorMode
608 //=======================================================================
610 void IGESCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
612 myColorMode = colormode;
615 //=======================================================================
616 //function : GetColorMode
618 //=======================================================================
620 Standard_Boolean IGESCAFControl_Reader::GetColorMode () const
625 //=======================================================================
626 //function : SetNameMode
628 //=======================================================================
630 void IGESCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
632 myNameMode = namemode;
635 //=======================================================================
636 //function : GetNameMode
638 //=======================================================================
640 Standard_Boolean IGESCAFControl_Reader::GetNameMode () const
645 //=======================================================================
646 //function : SetLayerMode
648 //=======================================================================
650 void IGESCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
652 myLayerMode = layermode;
655 //=======================================================================
656 //function : GetLayerMode
658 //=======================================================================
660 Standard_Boolean IGESCAFControl_Reader::GetLayerMode () const