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 //=======================================================================
49 //purpose : basic working method
50 //=======================================================================
51 static void checkColorRange (Standard_Real& theCol)
53 if ( theCol < 0. ) theCol = 0.;
54 if ( theCol > 100. ) theCol = 100.;
57 static inline Standard_Boolean IsComposite (const TopoDS_Shape& theShape)
59 if( theShape.ShapeType() == TopAbs_COMPOUND)
61 if(!theShape.Location().IsIdentity())
63 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
65 for (; anIt.More() ; anIt.Next())
67 if( IsComposite (anIt.Value()))
72 return Standard_False;
75 //=======================================================================
76 //function : AddCompositeShape
77 //purpose : Recursively adds composite shapes (TopoDS_Compounds) into the XDE document.
78 // If the compound does not contain nested compounds then adds it
79 // as no-assembly (i.e. no individual labels for sub-shapes), as this
80 // combination is often encountered in IGES (e.g. Group of Trimmed Surfaces).
81 // If the compound does contain nested compounds then adds it as an
83 // The construction happens bottom-up, i.e. the most deep sub-shapes are added
85 // If theIsTop is False (in a recursive call) then sub-shapes are added without
86 // a location. This is to ensure that no extra label in the XDE document is
87 // created for an instance (as otherwise, XDE will consider it as a free
88 // shape). Correct location and instance will be created when adding a parent
90 // theMap is used to avoid visiting the same compound.
91 //=======================================================================
92 static void AddCompositeShape (const Handle(XCAFDoc_ShapeTool)& theSTool,
93 const TopoDS_Shape& theShape,
94 Standard_Boolean theConsiderLoc,
95 TopTools_MapOfShape& theMap)
97 TopoDS_Shape aShape = theShape;
98 TopLoc_Location aLoc = theShape.Location();
99 if (!theConsiderLoc && !aLoc.IsIdentity())
100 aShape.Location( TopLoc_Location() );
101 if (!theMap.Add (aShape))
104 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
105 Standard_Boolean aHasCompositeSubShape = Standard_False;
106 TopoDS_Compound aSimpleShape;
108 aB.MakeCompound( aSimpleShape);
109 TopoDS_Compound aCompShape;
110 aB.MakeCompound( aCompShape);
111 Standard_Integer nbSimple = 0;
113 for (; anIt.More(); anIt.Next()) {
114 const TopoDS_Shape& aSubShape = anIt.Value();
115 if (IsComposite (aSubShape)) {
116 aHasCompositeSubShape = Standard_True;
117 AddCompositeShape( theSTool, aSubShape,Standard_False ,theMap );
118 aB.Add( aCompShape, aSubShape);
122 aB.Add(aSimpleShape, aSubShape);
126 //case of hybrid shape
127 if( nbSimple && aHasCompositeSubShape)
129 theSTool->AddShape( aSimpleShape, Standard_False, Standard_False );
130 TopoDS_Compound aNewShape;
131 aB.MakeCompound(aNewShape);
132 aB.Add(aNewShape, aSimpleShape);
133 aB.Add(aNewShape,aCompShape);
134 //if (!aLoc.IsIdentity())
135 // aNewShape.Location(aLoc );
136 aNewShape.Orientation(theShape.Orientation());
137 theSTool->AddShape( aNewShape, aHasCompositeSubShape, Standard_False );
140 theSTool->AddShape( aShape, aHasCompositeSubShape, Standard_False );
144 Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
147 Standard_Integer num;// = NbRootsForTransfer();
148 //if ( num <=0 ) return Standard_False;
149 //for ( Standard_Integer i=1; i <= num; i++ ) {
150 // TransferOneRoot ( i );
153 TransferRoots(); // replaces the above
155 if ( num <=0 ) return Standard_False;
157 // and insert them to the document
158 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
159 if(STool.IsNull()) return Standard_False;
161 for(i=1; i<=num; i++) {
162 TopoDS_Shape sh = Shape ( i );
163 // ---- HERE -- to add check [ assembly / hybrid model ]
164 if( !IsComposite (sh))
165 STool->AddShape( sh, Standard_False );
167 TopTools_MapOfShape aMap;
168 AddCompositeShape( STool, sh,Standard_True, aMap );
173 // added by skl 13.10.2003
174 const Handle(Interface_InterfaceModel) &Model = WS()->Model();
175 const Handle(XSControl_TransferReader) &TR = WS()->TransferReader();
176 const Handle(Transfer_TransientProcess) &TP = TR->TransientProcess();
177 Standard_Boolean IsCTool = Standard_True;
178 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
179 if(CTool.IsNull()) IsCTool = Standard_False;
180 Standard_Boolean IsLTool = Standard_True;
181 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool(doc->Main());
182 if(LTool.IsNull()) IsLTool = Standard_False;
184 Standard_Integer nb = Model->NbEntities();
185 for(i=1; i<=nb; i++) {
186 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
187 if ( ent.IsNull() ) continue;
188 Handle(Transfer_Binder) binder = TP->Find ( ent );
189 if ( binder.IsNull() ) continue;
190 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
191 if ( S.IsNull() ) continue;
193 Standard_Boolean IsColor = Standard_False;
195 if( GetColorMode() && IsCTool ) {
197 if(ent->DefColor()==IGESData_DefValue ||
198 ent->DefColor()==IGESData_DefReference) {
200 // decode color and set to document
201 IsColor = Standard_True;
202 if ( ent->DefColor() == IGESData_DefValue ) {
203 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
206 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
207 if ( color.IsNull() ) {
209 cout << "Error: Unrecognized type of color definition" << endl;
211 IsColor = Standard_False;
214 Standard_Real r, g, b;
215 color->RGBIntensity ( r, g, b );
216 checkColorRange ( r );
217 checkColorRange ( g );
218 checkColorRange ( b );
219 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
227 Standard_Boolean IsFound;
229 CTool->AddColor(col);
230 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
233 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
237 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
238 if(STool->SearchUsingMap(it.Value(),L,Standard_False,Standard_True)) {
239 CTool->SetColor(L,col,XCAFDoc_ColorGen);
240 if( GetLayerMode() && IsLTool ) {
242 // set a layers to the document
243 IGESData_DefList aDeflist = ent->DefLevel();
245 case IGESData_DefOne : {
246 TCollection_ExtendedString aLayerName ( ent->Level() );
247 LTool->SetLayer( L, aLayerName );
250 case IGESData_DefSeveral : {
251 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
252 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
253 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
254 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
255 LTool->SetLayer( L, aLayerName );
268 CTool->SetColor(L,col,XCAFDoc_ColorGen);
273 TCollection_AsciiString string = ent->NameValue()->String();
275 string.RightAdjust();
276 TCollection_ExtendedString str(string);
277 TDataStd_Name::Set(L,str);
280 if( GetLayerMode() && IsLTool ) {
282 // set a layers to the document
283 IGESData_DefList aDeflist = ent->DefLevel();
285 case IGESData_DefOne : {
286 TCollection_ExtendedString aLayerName ( ent->Level() );
287 LTool->SetLayer( L, aLayerName );
290 case IGESData_DefSeveral : {
291 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
292 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
293 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
294 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
295 LTool->SetLayer( L, aLayerName );
304 //Checks that current entity is a subfigure
305 Handle(IGESBasic_SubfigureDef) aSubfigure = Handle(IGESBasic_SubfigureDef)::DownCast (ent);
306 if (GetNameMode() && !aSubfigure.IsNull() && STool->Search (S, L, Standard_True, Standard_True))
308 //In this case we attach subfigure name to the label, instead of default "COMPOUND"
309 Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
311 aName->RightAdjust();
312 TCollection_ExtendedString anExtStrName (aName->ToCString());
313 TDataStd_Name::Set (L, anExtStrName);
318 CTool->ReverseChainsOfTreeNodes();
320 // end added by skl 13.10.2003
322 return Standard_True;
326 //=======================================================================
329 //=======================================================================
331 Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
332 Handle(TDocStd_Document) &doc)
334 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
335 return Transfer ( doc );