0027349: XtControl_Reader is not thread-safe
[occt.git] / src / IGESCAFControl / IGESCAFControl_Reader.cxx
CommitLineData
b311480e 1// Created on: 2000-08-16
2// Created by: Andrey BETENEV
973c2be1 3// Copyright (c) 2000-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
42cf5bc1 16
17#include <BRep_Builder.hxx>
18#include <IGESBasic_SubfigureDef.hxx>
7fd59977 19#include <IGESCAFControl.hxx>
42cf5bc1 20#include <IGESCAFControl_Reader.hxx>
21#include <IGESData_IGESEntity.hxx>
22#include <IGESData_LevelListEntity.hxx>
7fd59977 23#include <IGESGraph_Color.hxx>
24#include <Interface_InterfaceModel.hxx>
42cf5bc1 25#include <Quantity_Color.hxx>
26#include <TCollection_AsciiString.hxx>
7fd59977 27#include <TCollection_ExtendedString.hxx>
42cf5bc1 28#include <TCollection_HAsciiString.hxx>
7fd59977 29#include <TDataStd_Name.hxx>
42cf5bc1 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>
7fd59977 41#include <XCAFDoc_LayerTool.hxx>
7fd59977 42#include <XCAFDoc_ShapeMapTool.hxx>
42cf5bc1 43#include <XCAFDoc_ShapeTool.hxx>
44#include <XSControl_TransferReader.hxx>
45#include <XSControl_WorkSession.hxx>
7fd59977 46
7fd59977 47//=======================================================================
48//function : Transfer
49//purpose : basic working method
50//=======================================================================
1332f047
A
51static void checkColorRange (Standard_Real& theCol)
52{
53 if ( theCol < 0. ) theCol = 0.;
54 if ( theCol > 100. ) theCol = 100.;
55}
7fd59977 56
d4a2c515 57static 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//=======================================================================
92static 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 TopoDS_Compound aNewShape;
d4a2c515 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 );
138 }
139 else
140 theSTool->AddShape( aShape, aHasCompositeSubShape, Standard_False );
141 return;
142}
143
7fd59977 144Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
145{
146 // read all shapes
147 Standard_Integer num;// = NbRootsForTransfer();
148 //if ( num <=0 ) return Standard_False;
149 //for ( Standard_Integer i=1; i <= num; i++ ) {
150 // TransferOneRoot ( i );
151 //}
152
153 TransferRoots(); // replaces the above
154 num = NbShapes();
155 if ( num <=0 ) return Standard_False;
156
157 // and insert them to the document
158 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
159 if(STool.IsNull()) return Standard_False;
160 Standard_Integer i;
161 for(i=1; i<=num; i++) {
162 TopoDS_Shape sh = Shape ( i );
163 // ---- HERE -- to add check [ assembly / hybrid model ]
d4a2c515 164 if( !IsComposite (sh))
165 STool->AddShape( sh, Standard_False );
166 else {
167 TopTools_MapOfShape aMap;
168 AddCompositeShape( STool, sh,Standard_True, aMap );
169
170 }
7fd59977 171 }
172
173 // added by skl 13.10.2003
7f56eba8 174 const Handle(Interface_InterfaceModel) &Model = WS()->Model();
175 const Handle(XSControl_TransferReader) &TR = WS()->TransferReader();
176 const Handle(Transfer_TransientProcess) &TP = TR->TransientProcess();
7fd59977 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;
183
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;
192
193 Standard_Boolean IsColor = Standard_False;
194 Quantity_Color col;
195 if( GetColorMode() && IsCTool ) {
196 // read colors
197 if(ent->DefColor()==IGESData_DefValue ||
198 ent->DefColor()==IGESData_DefReference) {
199 // color is assigned
200 // decode color and set to document
201 IsColor = Standard_True;
202 if ( ent->DefColor() == IGESData_DefValue ) {
203 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
204 }
205 else {
206 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
207 if ( color.IsNull() ) {
0797d9d3 208#ifdef OCCT_DEBUG
7fd59977 209 cout << "Error: Unrecognized type of color definition" << endl;
210#endif
211 IsColor = Standard_False;
212 }
213 else {
214 Standard_Real r, g, b;
215 color->RGBIntensity ( r, g, b );
1332f047
A
216 checkColorRange ( r );
217 checkColorRange ( g );
218 checkColorRange ( b );
7fd59977 219 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
220 }
221 }
222 }
223 }
224
225 TDF_Label L;
226
227 Standard_Boolean IsFound;
228 if(IsColor) {
229 CTool->AddColor(col);
230 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
231 }
232 else {
233 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
234 }
235 if(!IsFound) {
236 if(IsColor) {
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 ) {
241 // read layers
242 // set a layers to the document
243 IGESData_DefList aDeflist = ent->DefLevel();
244 switch (aDeflist) {
245 case IGESData_DefOne : {
246 TCollection_ExtendedString aLayerName ( ent->Level() );
247 LTool->SetLayer( L, aLayerName );
248 break;
249 }
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 );
256 }
257 break;
258 }
259 default : break;
260 }
261 }
262 }
263 }
264 }
265 }
266 else {
267 if(IsColor) {
268 CTool->SetColor(L,col,XCAFDoc_ColorGen);
269 }
270 if(GetNameMode()) {
271 // read names
272 if(ent->HasName()) {
273 TCollection_AsciiString string = ent->NameValue()->String();
274 string.LeftAdjust();
275 string.RightAdjust();
276 TCollection_ExtendedString str(string);
277 TDataStd_Name::Set(L,str);
278 }
279 }
280 if( GetLayerMode() && IsLTool ) {
281 // read layers
282 // set a layers to the document
283 IGESData_DefList aDeflist = ent->DefLevel();
284 switch (aDeflist) {
285 case IGESData_DefOne : {
286 TCollection_ExtendedString aLayerName ( ent->Level() );
287 LTool->SetLayer( L, aLayerName );
288 break;
289 }
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 );
296 }
297 break;
298 }
299 default : break;
300 }
301 }
302 }
275e812f 303
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))
307 {
308 //In this case we attach subfigure name to the label, instead of default "COMPOUND"
309 Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
310 aName->LeftAdjust();
311 aName->RightAdjust();
312 TCollection_ExtendedString anExtStrName (aName->ToCString());
313 TDataStd_Name::Set (L, anExtStrName);
314 }
315
7fd59977 316 }
317
318 CTool->ReverseChainsOfTreeNodes();
319
320 // end added by skl 13.10.2003
321
7fd59977 322 return Standard_True;
323}
324
325
326//=======================================================================
327//function : Perform
328//purpose :
329//=======================================================================
330
331Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
332 Handle(TDocStd_Document) &doc)
333{
334 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
335 return Transfer ( doc );
336}