1 // Created on: 2000-08-16
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <TDF_Label.hxx>
22 #include <IGESCAFControl_Reader.ixx>
23 #include <TopoDS_Shape.hxx>
24 #include <XCAFDoc_ShapeTool.hxx>
25 #include <XSControl_TransferReader.hxx>
26 #include <XCAFDoc_ColorTool.hxx>
27 #include <IGESData_IGESEntity.hxx>
28 #include <Transfer_Binder.hxx>
29 #include <TransferBRep.hxx>
30 #include <Quantity_Color.hxx>
31 #include <IGESCAFControl.hxx>
32 #include <IGESGraph_Color.hxx>
33 #include <Interface_InterfaceModel.hxx>
34 #include <Transfer_TransientProcess.hxx>
35 #include <XCAFDoc_DocumentTool.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopTools_MapOfShape.hxx>
38 #include <TCollection_ExtendedString.hxx>
39 #include <TDataStd_Name.hxx>
40 #include <XCAFDoc_LayerTool.hxx>
41 #include <IGESData_LevelListEntity.hxx>
42 #include <TCollection_HAsciiString.hxx>
43 #include <XCAFDoc_ShapeMapTool.hxx>
44 #include <IGESBasic_SubfigureDef.hxx>
45 #include <TopoDS_Compound.hxx>
46 #include <BRep_Builder.hxx>
48 //=======================================================================
49 //function : IGESCAFControl_Reader
51 //=======================================================================
53 IGESCAFControl_Reader::IGESCAFControl_Reader () :
54 myColorMode( Standard_True ),
55 myNameMode ( Standard_True ),
56 myLayerMode( Standard_True )
61 //=======================================================================
62 //function : IGESCAFControl_Reader
64 //=======================================================================
66 IGESCAFControl_Reader::IGESCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
67 const Standard_Boolean scratch)
70 myColorMode = Standard_True;
71 myNameMode = Standard_True;
72 myLayerMode = Standard_True;
76 //=======================================================================
78 //purpose : basic working method
79 //=======================================================================
80 static void checkColorRange (Standard_Real& theCol)
82 if ( theCol < 0. ) theCol = 0.;
83 if ( theCol > 100. ) theCol = 100.;
86 static inline Standard_Boolean IsComposite (const TopoDS_Shape& theShape)
88 if( theShape.ShapeType() == TopAbs_COMPOUND)
90 if(!theShape.Location().IsIdentity())
92 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
94 for (; anIt.More() ; anIt.Next())
96 if( IsComposite (anIt.Value()))
101 return Standard_False;
104 //=======================================================================
105 //function : AddCompositeShape
106 //purpose : Recursively adds composite shapes (TopoDS_Compounds) into the XDE document.
107 // If the compound does not contain nested compounds then adds it
108 // as no-assembly (i.e. no individual labels for sub-shapes), as this
109 // combination is often encountered in IGES (e.g. Group of Trimmed Surfaces).
110 // If the compound does contain nested compounds then adds it as an
112 // The construction happens bottom-up, i.e. the most deep sub-shapes are added
114 // If theIsTop is False (in a recursive call) then sub-shapes are added without
115 // a location. This is to ensure that no extra label in the XDE document is
116 // created for an instance (as otherwise, XDE will consider it as a free
117 // shape). Correct location and instance will be created when adding a parent
119 // theMap is used to avoid visiting the same compound.
120 //=======================================================================
121 static void AddCompositeShape (const Handle(XCAFDoc_ShapeTool)& theSTool,
122 const TopoDS_Shape& theShape,
123 Standard_Boolean theConsiderLoc,
124 TopTools_MapOfShape& theMap)
126 TopoDS_Shape aShape = theShape;
127 TopLoc_Location aLoc = theShape.Location();
128 if (!theConsiderLoc && !aLoc.IsIdentity())
129 aShape.Location( TopLoc_Location() );
130 if (!theMap.Add (aShape))
133 TopoDS_Iterator anIt( theShape, Standard_False, Standard_False );
134 Standard_Boolean aHasCompositeSubShape = Standard_False;
135 TopoDS_Compound aSimpleShape;
137 aB.MakeCompound( aSimpleShape);
138 TopoDS_Compound aCompShape;
139 aB.MakeCompound( aCompShape);
140 Standard_Integer nbSimple = 0;
142 for (; anIt.More(); anIt.Next()) {
143 const TopoDS_Shape& aSubShape = anIt.Value();
144 if (IsComposite (aSubShape)) {
145 aHasCompositeSubShape = Standard_True;
146 AddCompositeShape( theSTool, aSubShape,Standard_False ,theMap );
147 aB.Add( aCompShape, aSubShape);
151 aB.Add(aSimpleShape, aSubShape);
155 //case of hybrid shape
156 if( nbSimple && aHasCompositeSubShape)
158 theSTool->AddShape( aSimpleShape, Standard_False, Standard_False );
159 TopoDS_Compound aNewShape;
161 aB.MakeCompound(aNewShape);
162 aB.Add(aNewShape, aSimpleShape);
163 aB.Add(aNewShape,aCompShape);
164 //if (!aLoc.IsIdentity())
165 // aNewShape.Location(aLoc );
166 aNewShape.Orientation(theShape.Orientation());
167 theSTool->AddShape( aNewShape, aHasCompositeSubShape, Standard_False );
170 theSTool->AddShape( aShape, aHasCompositeSubShape, Standard_False );
174 Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
177 Standard_Integer num;// = NbRootsForTransfer();
178 //if ( num <=0 ) return Standard_False;
179 //for ( Standard_Integer i=1; i <= num; i++ ) {
180 // TransferOneRoot ( i );
183 TransferRoots(); // replaces the above
185 if ( num <=0 ) return Standard_False;
187 // and insert them to the document
188 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
189 if(STool.IsNull()) return Standard_False;
191 for(i=1; i<=num; i++) {
192 TopoDS_Shape sh = Shape ( i );
193 // ---- HERE -- to add check [ assembly / hybrid model ]
194 if( !IsComposite (sh))
195 STool->AddShape( sh, Standard_False );
197 TopTools_MapOfShape aMap;
198 AddCompositeShape( STool, sh,Standard_True, aMap );
203 // added by skl 13.10.2003
204 Handle(Interface_InterfaceModel) Model = WS()->Model();
205 //WS()->TransferReader()->SetTransientProcess(TransientProcess());
206 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
207 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
208 Standard_Boolean IsCTool = Standard_True;
209 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
210 if(CTool.IsNull()) IsCTool = Standard_False;
211 Standard_Boolean IsLTool = Standard_True;
212 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool(doc->Main());
213 if(LTool.IsNull()) IsLTool = Standard_False;
215 Standard_Integer nb = Model->NbEntities();
216 for(i=1; i<=nb; i++) {
217 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
218 if ( ent.IsNull() ) continue;
219 Handle(Transfer_Binder) binder = TP->Find ( ent );
220 if ( binder.IsNull() ) continue;
221 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
222 if ( S.IsNull() ) continue;
224 Standard_Boolean IsColor = Standard_False;
226 if( GetColorMode() && IsCTool ) {
228 if(ent->DefColor()==IGESData_DefValue ||
229 ent->DefColor()==IGESData_DefReference) {
231 // decode color and set to document
232 IsColor = Standard_True;
233 if ( ent->DefColor() == IGESData_DefValue ) {
234 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
237 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
238 if ( color.IsNull() ) {
240 cout << "Error: Unrecognized type of color definition" << endl;
242 IsColor = Standard_False;
245 Standard_Real r, g, b;
246 color->RGBIntensity ( r, g, b );
247 checkColorRange ( r );
248 checkColorRange ( g );
249 checkColorRange ( b );
250 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
258 Standard_Boolean IsFound;
260 CTool->AddColor(col);
261 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
264 IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
268 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
269 if(STool->SearchUsingMap(it.Value(),L,Standard_False,Standard_True)) {
270 CTool->SetColor(L,col,XCAFDoc_ColorGen);
271 if( GetLayerMode() && IsLTool ) {
273 // set a layers to the document
274 IGESData_DefList aDeflist = ent->DefLevel();
276 case IGESData_DefOne : {
277 TCollection_ExtendedString aLayerName ( ent->Level() );
278 LTool->SetLayer( L, aLayerName );
281 case IGESData_DefSeveral : {
282 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
283 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
284 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
285 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
286 LTool->SetLayer( L, aLayerName );
299 CTool->SetColor(L,col,XCAFDoc_ColorGen);
304 TCollection_AsciiString string = ent->NameValue()->String();
306 string.RightAdjust();
307 TCollection_ExtendedString str(string);
308 TDataStd_Name::Set(L,str);
311 if( GetLayerMode() && IsLTool ) {
313 // set a layers to the document
314 IGESData_DefList aDeflist = ent->DefLevel();
316 case IGESData_DefOne : {
317 TCollection_ExtendedString aLayerName ( ent->Level() );
318 LTool->SetLayer( L, aLayerName );
321 case IGESData_DefSeveral : {
322 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
323 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
324 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
325 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
326 LTool->SetLayer( L, aLayerName );
335 //Checks that current entity is a subfigure
336 Handle(IGESBasic_SubfigureDef) aSubfigure = Handle(IGESBasic_SubfigureDef)::DownCast (ent);
337 if (GetNameMode() && !aSubfigure.IsNull() && STool->Search (S, L, Standard_True, Standard_True))
339 //In this case we attach subfigure name to the label, instead of default "COMPOUND"
340 Handle(TCollection_HAsciiString) aName = aSubfigure->Name();
342 aName->RightAdjust();
343 TCollection_ExtendedString anExtStrName (aName->ToCString());
344 TDataStd_Name::Set (L, anExtStrName);
349 CTool->ReverseChainsOfTreeNodes();
351 // end added by skl 13.10.2003
354 // if ( GetColorMode() )
355 // ReadColors ( doc );
358 // if ( GetNameMode() )
359 // ReadNames ( doc );
362 // if ( GetLayerMode() )
363 // ReadLayers ( doc );
366 return Standard_True;
370 //=======================================================================
373 //=======================================================================
375 Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
376 Handle(TDocStd_Document) &doc)
378 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
379 return Transfer ( doc );
383 //=======================================================================
386 //=======================================================================
388 Standard_Boolean IGESCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
389 Handle(TDocStd_Document) &doc)
391 if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
392 return Transfer ( doc );
396 //=======================================================================
397 //function : ReadColors
399 //=======================================================================
401 Standard_Boolean IGESCAFControl_Reader::ReadColors (Handle(TDocStd_Document)& Doc) const
404 Handle(Interface_InterfaceModel) Model = WS()->Model();
406 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
408 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
409 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
410 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
412 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
413 if ( STool.IsNull() ) return Standard_False;
414 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
415 SMTool->ComputeMaps();
417 Standard_Integer nb = Model->NbEntities();
418 for (Standard_Integer i = 1; i <= nb; i ++) {
419 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
420 if ( ent.IsNull() ) continue;
421 if ( ent->DefColor() != IGESData_DefValue &&
422 ent->DefColor() != IGESData_DefReference ) continue; // no color assigned
425 Handle(Transfer_Binder) binder = TP->Find ( ent );
426 if ( binder.IsNull() ) continue;
427 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
428 if ( S.IsNull() ) continue;
430 // decode color and set to document
432 if ( ent->DefColor() == IGESData_DefValue ) {
433 col = IGESCAFControl::DecodeColor ( ent->RankColor() );
436 Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
437 if ( color.IsNull() ) {
438 cout << "Error: Unrecognized type of color definition" << endl;
442 Standard_Real r, g, b;
443 color->RGBIntensity ( r, g, b );
444 col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
449 if(SMTool->Search(S,L)) {
450 cout<<" find Instance"<<endl;
451 CTool->SetColor(L, col, XCAFDoc_ColorGen);
454 cout<<"L1 is Null"<<endl;
455 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
456 cout<<" add new label1 :"<<L<<endl;
457 CTool->SetColor(L, col, XCAFDoc_ColorGen);
462 cout<<" try to find splitting"<<endl;
463 // may be S is compound of shapes resulting from splitting
464 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
466 if(!SMTool->Search(it.Value(),L)) continue; //break-?
467 cout<<" find splitting"<<endl;
468 CTool->SetColor(L, col, XCAFDoc_ColorGen);
471 for (TopoDS_Iterator it(S); it.More(); it.Next()) {
472 if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
473 cout<<" add new label2 :"<<L<<endl;
474 CTool->SetColor(L, col, XCAFDoc_ColorGen);
479 // cout<<"L.Dump():"<<L<<endl;
481 // cout<<"L2 is Null"<<endl;
482 // if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
483 // cout<<" add new label2 :"<<L<<endl;
484 // CTool->SetColor(L, col, XCAFDoc_ColorGen);
489 return Standard_True;
492 //=======================================================================
493 //function : ReadNames
495 //=======================================================================
497 Standard_Boolean IGESCAFControl_Reader::ReadNames (Handle(TDocStd_Document)& /*Doc*/) const
499 Handle(Interface_InterfaceModel) Model = WS()->Model();
501 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
503 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
504 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
506 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
507 if ( STool.IsNull() ) return Standard_False;
508 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
509 SMTool->ComputeMaps();
511 Standard_Integer nb = Model->NbEntities();
512 for (Standard_Integer i = 1; i <= nb; i ++) {
513 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
514 if ( ent.IsNull() || ! ent->HasName() ) continue; //not only Entity Label (f.18) but Name Property also
517 Handle(Transfer_Binder) binder = TP->Find ( ent );
518 if ( binder.IsNull() ) continue;
519 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
520 if ( S.IsNull() ) continue;
523 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
524 if ( ! SMTool->Search ( S, L, Standard_True, Standard_True) ) continue;
526 // set a name to the document
527 TCollection_AsciiString string = ent->NameValue()->String();
529 string.RightAdjust();
530 TCollection_ExtendedString str ( string );
531 TDataStd_Name::Set ( L, str );
534 return Standard_True;
537 //=======================================================================
538 //function : ReadLayers
540 //=======================================================================
542 Standard_Boolean IGESCAFControl_Reader::ReadLayers (Handle(TDocStd_Document)& /*Doc*/) const
544 Handle(Interface_InterfaceModel) Model = WS()->Model();
546 //WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
548 Handle(XSControl_TransferReader) TR = WS()->TransferReader();
549 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
551 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
552 if ( STool.IsNull() ) return Standard_False;
553 Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
554 SMTool->ComputeMaps();
556 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
557 if ( LTool.IsNull() ) return Standard_False;
559 Standard_Integer nb = Model->NbEntities();
560 for (Standard_Integer i = 1; i <= nb; i ++) {
561 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
563 if ( ent.IsNull() ) continue;
566 Handle(Transfer_Binder) binder = TP->Find ( ent );
567 if ( binder.IsNull() ) continue;
568 TopoDS_Shape S = TransferBRep::ShapeResult (binder);
569 if ( S.IsNull() ) continue;
572 //if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
573 if( !SMTool->Search(S, L, Standard_True, Standard_True) ) continue;
575 // set a layers to the document
576 IGESData_DefList aDeflist = ent->DefLevel();
579 case IGESData_DefOne : {
580 TCollection_ExtendedString aLayerName ( ent->Level() );
581 LTool->SetLayer( L, aLayerName );
583 // cout << "Added layer " << aLayerName << endl;
587 case IGESData_DefSeveral : {
588 Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
589 Standard_Integer layerNb = aLevelList->NbLevelNumbers();
590 for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
591 TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
592 LTool->SetLayer( L, aLayerName );
594 // cout << "Added layer " << aLayerName << endl;
604 return Standard_True;
608 //=======================================================================
609 //function : SetColorMode
611 //=======================================================================
613 void IGESCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
615 myColorMode = colormode;
618 //=======================================================================
619 //function : GetColorMode
621 //=======================================================================
623 Standard_Boolean IGESCAFControl_Reader::GetColorMode () const
628 //=======================================================================
629 //function : SetNameMode
631 //=======================================================================
633 void IGESCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
635 myNameMode = namemode;
638 //=======================================================================
639 //function : GetNameMode
641 //=======================================================================
643 Standard_Boolean IGESCAFControl_Reader::GetNameMode () const
648 //=======================================================================
649 //function : SetLayerMode
651 //=======================================================================
653 void IGESCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
655 myLayerMode = layermode;
658 //=======================================================================
659 //function : GetLayerMode
661 //=======================================================================
663 Standard_Boolean IGESCAFControl_Reader::GetLayerMode () const