0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / IGESCAFControl / IGESCAFControl_Reader.cxx
1 // Created on: 2000-08-16
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
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>
46
47 //=======================================================================
48 //function : checkColorRange
49 //purpose  : 
50 //=======================================================================
51 static void checkColorRange (Standard_Real& theCol)
52 {
53   if ( theCol < 0. ) theCol = 0.;
54   if ( theCol > 100. ) theCol = 100.;
55 }
56
57 static inline Standard_Boolean IsComposite (const TopoDS_Shape& theShape)
58 {
59   if( theShape.ShapeType() == TopAbs_COMPOUND)
60   {
61     if(!theShape.Location().IsIdentity())
62       return Standard_True;
63     TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
64     
65     for (; anIt.More() ; anIt.Next()) 
66     {
67       if( IsComposite (anIt.Value()))
68         return Standard_True;
69     }
70
71   }
72   return Standard_False;
73 }
74
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
82 //           assembly.
83 //           The construction happens bottom-up, i.e. the most deep sub-shapes are added
84 //           first.
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
89 //           compound.
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)
96 {
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)) 
102     return;
103
104   TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
105   Standard_Boolean aHasCompositeSubShape = Standard_False;
106   TopoDS_Compound aSimpleShape;
107   BRep_Builder aB;
108   aB.MakeCompound( aSimpleShape);
109   TopoDS_Compound aCompShape;
110   aB.MakeCompound( aCompShape);
111   Standard_Integer nbSimple = 0;
112
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);
119     }
120     else
121     {
122       aB.Add(aSimpleShape, aSubShape);
123       nbSimple++;
124     }
125   }
126   //case of hybrid shape
127   if( nbSimple && aHasCompositeSubShape)
128   {
129     theSTool->AddShape( aSimpleShape,  Standard_False, Standard_False  );
130
131     TopoDS_Compound aNewShape;
132     aB.MakeCompound(aNewShape);
133     aB.Add(aNewShape, aSimpleShape);
134     aB.Add(aNewShape,aCompShape);
135
136     if (!aLoc.IsIdentity())
137       aNewShape.Location(aLoc );
138     aNewShape.Orientation(theShape.Orientation());
139     theSTool->AddShape( aNewShape,  aHasCompositeSubShape, Standard_False  );
140   }
141   else
142     theSTool->AddShape( aShape,  aHasCompositeSubShape, Standard_False  );
143   return;
144 }
145
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)
152 {
153   // read all shapes
154   Standard_Integer num;// = NbRootsForTransfer();
155   //if ( num <=0 ) return Standard_False;
156   //for ( Standard_Integer i=1; i <= num; i++ ) {
157   //  TransferOneRoot ( i );
158   //}
159   
160   TransferRoots(theProgress); // replaces the above
161   num = NbShapes();
162   if ( num <=0 ) return Standard_False;
163
164   // and insert them to the document
165   Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
166   if(STool.IsNull()) return Standard_False;
167   Standard_Integer i;
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 );
173     else {
174       TopTools_MapOfShape aMap;
175       AddCompositeShape( STool, sh,Standard_True, aMap );
176       
177     }
178   }
179   
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;
190
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;
199
200     Standard_Boolean IsColor = Standard_False;
201     Quantity_Color col;
202     if( GetColorMode() && IsCTool ) {
203       // read colors
204       if(ent->DefColor()==IGESData_DefValue ||
205          ent->DefColor()==IGESData_DefReference) {
206         // color is assigned
207         // decode color and set to document
208         IsColor = Standard_True;
209         if ( ent->DefColor() == IGESData_DefValue ) {
210           col = IGESCAFControl::DecodeColor ( ent->RankColor() );
211         }
212         else {
213           Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
214           if ( color.IsNull() ) {
215 #ifdef OCCT_DEBUG
216             std::cout << "Error: Unrecognized type of color definition" << std::endl;
217 #endif
218             IsColor = Standard_False;
219           }
220           else {
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 );
227           }
228         }
229       }
230     }
231
232     TDF_Label L;
233
234     Standard_Boolean IsFound;
235     if(IsColor) {
236       CTool->AddColor(col);
237       IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
238     }
239     else {
240       IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
241     }
242     if(!IsFound) {
243       if(IsColor) {
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 ) {
248               // read layers
249               // set a layers to the document
250               IGESData_DefList aDeflist = ent->DefLevel();
251               switch (aDeflist) {
252               case IGESData_DefOne : {
253                 TCollection_ExtendedString aLayerName ( ent->Level() );
254                 LTool->SetLayer( L, aLayerName );
255                 break;
256               }
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 );
263                 }
264                 break;
265               }
266                 default : break;
267               }
268             }
269           }
270         }
271       }
272     }
273     else {
274       if(IsColor) {
275         CTool->SetColor(L,col,XCAFDoc_ColorGen);
276       }
277       if(GetNameMode()) {
278         // read names
279         if(ent->HasName()) {
280           TCollection_AsciiString string = ent->NameValue()->String();
281           string.LeftAdjust();
282           string.RightAdjust();
283           TCollection_ExtendedString str(string);
284           TDataStd_Name::Set(L,str);
285         }
286       }
287       if( GetLayerMode() && IsLTool ) {
288         // read layers
289         // set a layers to the document
290         IGESData_DefList aDeflist = ent->DefLevel();
291         switch (aDeflist) {
292         case IGESData_DefOne : {
293           TCollection_ExtendedString aLayerName ( ent->Level() );
294           LTool->SetLayer( L, aLayerName );
295           break;
296         }
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 );
303           }
304           break;
305         }
306           default : break;
307         }
308       }
309     }
310
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))
314     {
315       //In this case we attach subfigure name to the label, instead of default "COMPOUND"
316       Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
317       aName->LeftAdjust();
318       aName->RightAdjust();
319       TCollection_ExtendedString anExtStrName (aName->ToCString());
320       TDataStd_Name::Set (L, anExtStrName);
321     }
322
323   }
324
325   CTool->ReverseChainsOfTreeNodes();
326
327   // end added by skl 13.10.2003
328
329   // Update assembly compounds
330   STool->UpdateAssemblies();
331
332   return Standard_True;
333 }
334   
335
336 //=======================================================================
337 //function : Perform
338 //purpose  : 
339 //=======================================================================
340
341 Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
342                                                  Handle(TDocStd_Document) &doc,
343                                                  const Message_ProgressRange& theProgress)
344 {
345   if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
346   return Transfer ( doc, theProgress );
347 }