Salome HOME
Eliminate some Visual Studio warnings
[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 std::string& theFileName,
82                 const std::string& theFormatName,
83                 const std::shared_ptr<GeomAPI_Shape>& theShape,
84                 std::string& theError)
85 {
86   #ifdef _DEBUG
87   std::cout << "Export IGES into file " << theFileName << std::endl;
88   #endif
89
90   if (!theShape.get()) {
91     theError = "IGES Export failed: An invalid argument";
92     return false;
93   }
94
95   // theFormatName expected "IGES-5.1", "IGES-5.3"...
96   TCollection_AsciiString aFormatName(theFormatName.c_str());
97   TCollection_AsciiString aVersion = aFormatName.Token("-", 2);
98   #ifdef _DEBUG
99   if (!aVersion.IsEqual("5.1") || !aVersion.IsEqual("5.3"))
100     std::cout << "Warning: unrecognized version " << aVersion.ToCString()
101               << ". Default version: 5.1." << std::endl;
102   #endif
103   // define, whether to write only faces (5.1 IGES format)
104   // or shells and solids also (5.3 IGES format)
105   int aBrepMode = 0;
106   if( aVersion.IsEqual( "5.3" ) )
107     aBrepMode = 1;
108
109   // Mantis issue 0021350: check being exported shape, as some stand-alone
110   // entities (edges, wires and vertices) cannot be saved in BRepMode
111   if( aBrepMode == 1 ) {
112     int aKind = KindOfBRep( theShape->impl<TopoDS_Shape>() );
113     if( aKind == -1 ) {
114       theError = "EXPORT_IGES_HETEROGENEOUS_COMPOUND";
115       return false;
116     } else if( aKind == 2 )
117       aBrepMode = 1;
118     else
119       aBrepMode = aKind;
120   }
121
122   // Set "C" numeric locale to save numbers correctly
123   GeomAlgoAPI_Tools::Localizer loc;
124
125   // initialize writer
126   IGESControl_Controller::Init();
127   IGESControl_Writer ICW( "M", aBrepMode ); // export explicitly in meters
128   Interface_Static::SetCVal( "xstep.cascade.unit", "M" );
129
130   // 09.03.2010 skl for bug 0020726
131   // change default value "Average" to "Max"
132   Interface_Static::SetCVal( "write.precision.mode", "Max" );
133
134   // perform shape writing
135   if( ICW.AddShape( theShape->impl<TopoDS_Shape>() ) ) {
136     ICW.ComputeModel();
137     return ICW.Write(theFileName.c_str()) == Standard_True;
138   }
139   return false;
140 }