Salome HOME
93fb05b025dc428c319d64a5869d15143dad8bbc
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_IGESExport.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    GEOMALGOAPI_IGESExport.cpp
4 // Created: Dec 24, 2014
5 // Author:  Sergey BELASH
6
7 #include <GeomAlgoAPI_IGESExport.h>
8
9 #include "GeomAlgoAPI_Tools.h"
10
11 // OOCT includes
12 #include <IGESControl_Controller.hxx>
13 #include <IGESControl_Writer.hxx>
14 #include <Interface_Static.hxx>
15
16 #include <TopoDS_Shape.hxx>
17 #include <TopoDS_Iterator.hxx>
18
19 //=============================================================================
20 /*!
21  *
22  */
23 //=============================================================================
24 /*!
25  *  KindOfBRep
26  *  \return 0 if theShape contains only simple entities (wires, edges and vertices),
27  *          1 if theShape contains only complex entities (shells, solids and compsolids)
28  *          2 if theShape contains only indifferent entities (faces)
29  *         -1 if theShape contains both simple and complex entities (and in this case it
30  *            cannot be saved without any loss neither in BRepMode == 0 nor in BRepMode == 1)
31  */
32 //=============================================================================
33 int KindOfBRep (const TopoDS_Shape& theShape)
34 {
35   int aKind = 2;
36
37   switch (theShape.ShapeType())
38   {
39   case TopAbs_COMPOUND:
40     {
41       bool isSimple = false;
42       bool isComplex = false;
43       TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
44       for (; anIt.More(); anIt.Next()) {
45         TopoDS_Shape aS = anIt.Value();
46         int aKindSub = KindOfBRep(aS);
47         if (aKindSub == 0)
48           isSimple = true;
49         else if (aKindSub == 1)
50           isComplex = true;
51         else if (aKindSub == -1) {
52           return -1; // heterogeneous
53         }
54       }
55       if (isSimple && isComplex)
56         aKind = -1; // heterogeneous
57       else if (isSimple)
58         aKind = 0;
59       else if (isComplex)
60         aKind = 1;
61     }
62     break;
63   case TopAbs_COMPSOLID:
64   case TopAbs_SOLID:
65   case TopAbs_SHELL:
66     aKind = 1;
67     break;
68   case TopAbs_WIRE:
69   case TopAbs_EDGE:
70   case TopAbs_VERTEX:
71     aKind = 0;
72     break;
73   default:
74     aKind = 2;
75   }
76
77   return aKind;
78 }
79
80 //=============================================================================
81 bool IGESExport(const TCollection_AsciiString& theFileName,
82                 const TCollection_AsciiString& theFormatName,
83                 const TopoDS_Shape& theShape,
84                 TCollection_AsciiString& theError)
85 {
86   // theFormatName expected "IGES-5.1", "IGES-5.3"...
87   TCollection_AsciiString aVersion = theFormatName.Token("-", 2);
88   #ifdef _DEBUG
89   if (!aVersion.IsEqual("5.1") || !aVersion.IsEqual("5.3"))
90     std::cout << "Warning: unrecognized version " << aVersion.ToCString()
91               << ". Default version: 5.1." << std::endl;
92   #endif
93   // define, whether to write only faces (5.1 IGES format)
94   // or shells and solids also (5.3 IGES format)
95   int aBrepMode = 0;
96   if( aVersion.IsEqual( "5.3" ) )
97     aBrepMode = 1;
98
99   #ifdef _DEBUG
100   std::cout << "Export IGES into file " << theFileName.ToCString() << std::endl;
101   #endif
102
103   // Mantis issue 0021350: check being exported shape, as some stand-alone
104   // entities (edges, wires and vertices) cannot be saved in BRepMode
105   if( aBrepMode == 1 ) {
106     int aKind = KindOfBRep( theShape );
107     if( aKind == -1 ) {
108       theError = "EXPORT_IGES_HETEROGENEOUS_COMPOUND";
109       return false;
110     } else if( aKind == 2 )
111       aBrepMode = 1;
112     else
113       aBrepMode = aKind;
114   }
115
116   // Set "C" numeric locale to save numbers correctly
117   GeomAlgoAPI_Tools::Localizer loc;
118
119   // initialize writer
120   IGESControl_Controller::Init();
121   IGESControl_Writer ICW( "M", aBrepMode ); // export explicitly in meters
122   Interface_Static::SetCVal( "xstep.cascade.unit", "M" );
123
124   // 09.03.2010 skl for bug 0020726
125   // change default value "Average" to "Max"
126   Interface_Static::SetCVal( "write.precision.mode", "Max" );
127
128   // perform shape writing
129   if( ICW.AddShape( theShape ) ) {
130     ICW.ComputeModel();
131     return ICW.Write( theFileName.ToCString() );
132   }
133   return false;
134 }