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 : checkColorRange
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 );
131 TopoDS_Compound aNewShape;
132 aB.MakeCompound(aNewShape);
133 aB.Add(aNewShape, aSimpleShape);
134 aB.Add(aNewShape,aCompShape);
136 if (!aLoc.IsIdentity())
137 aNewShape.Location(aLoc );
138 aNewShape.Orientation(theShape.Orientation());
139 theSTool->AddShape( aNewShape, aHasCompositeSubShape, Standard_False );
142 theSTool->AddShape( aShape, aHasCompositeSubShape, Standard_False );
146 //=======================================================================
147 //function : Transfer
148 //purpose : basic working method
149 //=======================================================================
150 Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc,
151 const Message_ProgressRange& theProgress)
154 Standard_Integer num;// = NbRootsForTransfer();
155 //if ( num <=0 ) return Standard_False;
156 //for ( Standard_Integer i=1; i <= num; i++ ) {
157 // TransferOneRoot ( i );
160 TransferRoots(theProgress); // replaces the above
162 if ( num <=0 ) return Standard_False;
164 // and insert them to the document
165 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
166 if(STool.IsNull()) return Standard_False;
168 for(i=1; i<=num; i++) {
169 TopoDS_Shape sh = Shape ( i );
170 // ---- HERE -- to add check [ assembly / hybrid model ]
171 if( !IsComposite (sh))
172 STool->AddShape( sh, Standard_False );
174 TopTools_MapOfShape aMap;
175 AddCompositeShape( STool, sh,Standard_True, aMap );
180 // added by skl 13.10.2003
181 const Handle(Interface_InterfaceModel) &Model = WS()->Model();
182 const Handle(XSControl_TransferReader) &TR = WS()->TransferReader();
183 const Handle(Transfer_TransientProcess) &TP = TR->TransientProcess();
184 Standard_Boolean IsCTool = Standard_True;
185 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
186 if(CTool.IsNull()) IsCTool = Standard_False;
187 Standard_Boolean IsLTool = Standard_True;
188 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool(doc->Main());
189 if(LTool.IsNull()) IsLTool = Standard_False;
191 Standard_Integer nb = Model->NbEntities();
192 for(i=1; i<=nb; i++) {
193 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
194 if ( ent.IsNull() ) continue;
195 Handle(Transfer_Binder) binder = TP->Find ( ent );
196 if ( binder.IsNull() ) continue;
197 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
198 if ( S.IsNull() ) continue;
200 Standard_Boolean IsColor = Standard_False;
202 if( GetColorMode() && IsCTool ) {
204 if(ent->DefColor()==IGESData_DefValue ||
205 ent->DefColor()==IGESData_DefReference) {
207 // decode color and set to document
208 IsColor = Standard_True;
209 if ( ent->DefColor() == IGESData_DefValue ) {
210 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
213 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
214 if ( color.IsNull() ) {
216 std::cout << "Error: Unrecognized type of color definition" << std::endl;
218 IsColor = Standard_False;
221 Standard_Real r, g, b;
222 color->RGBIntensity ( r, g, b );
223 checkColorRange ( r );
224 checkColorRange ( g );
225 checkColorRange ( b );
226 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_sRGB );
234 Standard_Boolean IsFound;
236 CTool->AddColor(col);
237 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
240 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
244 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
245 if(STool->SearchUsingMap(it.Value(),L,Standard_False,Standard_True)) {
246 CTool->SetColor(L,col,XCAFDoc_ColorGen);
247 if( GetLayerMode() && IsLTool ) {
249 // set a layers to the document
250 IGESData_DefList aDeflist = ent->DefLevel();
252 case IGESData_DefOne : {
253 TCollection_ExtendedString aLayerName ( ent->Level() );
254 LTool->SetLayer( L, aLayerName );
257 case IGESData_DefSeveral : {
258 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
259 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
260 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
261 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
262 LTool->SetLayer( L, aLayerName );
275 CTool->SetColor(L,col,XCAFDoc_ColorGen);
280 TCollection_AsciiString string = ent->NameValue()->String();
282 string.RightAdjust();
283 TCollection_ExtendedString str(string);
284 TDataStd_Name::Set(L,str);
287 if( GetLayerMode() && IsLTool ) {
289 // set a layers to the document
290 IGESData_DefList aDeflist = ent->DefLevel();
292 case IGESData_DefOne : {
293 TCollection_ExtendedString aLayerName ( ent->Level() );
294 LTool->SetLayer( L, aLayerName );
297 case IGESData_DefSeveral : {
298 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
299 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
300 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
301 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
302 LTool->SetLayer( L, aLayerName );
311 //Checks that current entity is a subfigure
312 Handle(IGESBasic_SubfigureDef) aSubfigure = Handle(IGESBasic_SubfigureDef)::DownCast (ent);
313 if (GetNameMode() && !aSubfigure.IsNull() && STool->Search (S, L, Standard_True, Standard_True))
315 //In this case we attach subfigure name to the label, instead of default "COMPOUND"
316 Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
318 aName->RightAdjust();
319 TCollection_ExtendedString anExtStrName (aName->ToCString());
320 TDataStd_Name::Set (L, anExtStrName);
325 CTool->ReverseChainsOfTreeNodes();
327 // end added by skl 13.10.2003
329 // Update assembly compounds
330 STool->UpdateAssemblies();
332 return Standard_True;
336 //=======================================================================
339 //=======================================================================
341 Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
342 Handle(TDocStd_Document) &doc,
343 const Message_ProgressRange& theProgress)
345 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
346 return Transfer ( doc, theProgress );