Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_IGESExport.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAlgoAPI_IGESExport.h>
22
23 #include "GeomAlgoAPI_Tools.h"
24
25 // OOCT includes
26 #include <IGESControl_Controller.hxx>
27 #include <IGESControl_Writer.hxx>
28 #include <Interface_Static.hxx>
29
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Iterator.hxx>
32
33 //=============================================================================
34 /*!
35  *
36  */
37 //=============================================================================
38 /*!
39  *  KindOfBRep
40  *  \return 0 if theShape contains only simple entities (wires, edges and vertices),
41  *          1 if theShape contains only complex entities (shells, solids and compsolids)
42  *          2 if theShape contains only indifferent entities (faces)
43  *         -1 if theShape contains both simple and complex entities (and in this case it
44  *            cannot be saved without any loss neither in BRepMode == 0 nor in BRepMode == 1)
45  */
46 //=============================================================================
47 int KindOfBRep (const TopoDS_Shape& theShape)
48 {
49   int aKind = 2;
50
51   switch (theShape.ShapeType())
52   {
53   case TopAbs_COMPOUND:
54     {
55       bool isSimple = false;
56       bool isComplex = false;
57       TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
58       for (; anIt.More(); anIt.Next()) {
59         TopoDS_Shape aS = anIt.Value();
60         int aKindSub = KindOfBRep(aS);
61         if (aKindSub == 0)
62           isSimple = true;
63         else if (aKindSub == 1)
64           isComplex = true;
65         else if (aKindSub == -1) {
66           return -1; // heterogeneous
67         }
68       }
69       if (isSimple && isComplex)
70         aKind = -1; // heterogeneous
71       else if (isSimple)
72         aKind = 0;
73       else if (isComplex)
74         aKind = 1;
75     }
76     break;
77   case TopAbs_COMPSOLID:
78   case TopAbs_SOLID:
79   case TopAbs_SHELL:
80     aKind = 1;
81     break;
82   case TopAbs_WIRE:
83   case TopAbs_EDGE:
84   case TopAbs_VERTEX:
85     aKind = 0;
86     break;
87   default:
88     aKind = 2;
89   }
90
91   return aKind;
92 }
93
94 //=============================================================================
95 bool IGESExport(const std::string& theFileName,
96                 const std::string& theFormatName,
97                 const std::shared_ptr<GeomAPI_Shape>& theShape,
98                 std::string& theError)
99 {
100   #ifdef _DEBUG
101   std::cout << "Export IGES into file " << theFileName << std::endl;
102   #endif
103
104   if (!theShape.get()) {
105     theError = "IGES Export failed: An invalid argument";
106     return false;
107   }
108
109   // theFormatName expected "IGES-5.1", "IGES-5.3"...
110   TCollection_AsciiString aFormatName(theFormatName.c_str());
111   TCollection_AsciiString aVersion = aFormatName.Token("-", 2);
112   #ifdef _DEBUG
113   if (!aVersion.IsEqual("5.1") || !aVersion.IsEqual("5.3"))
114     std::cout << "Warning: unrecognized version " << aVersion.ToCString()
115               << ". Default version: 5.1." << std::endl;
116   #endif
117   // define, whether to write only faces (5.1 IGES format)
118   // or shells and solids also (5.3 IGES format)
119   int aBrepMode = 0;
120   if( aVersion.IsEqual( "5.3" ) )
121     aBrepMode = 1;
122
123   // Mantis issue 0021350: check being exported shape, as some stand-alone
124   // entities (edges, wires and vertices) cannot be saved in BRepMode
125   if( aBrepMode == 1 ) {
126     int aKind = KindOfBRep( theShape->impl<TopoDS_Shape>() );
127     if( aKind == -1 ) {
128       theError = "EXPORT_IGES_HETEROGENEOUS_COMPOUND";
129       return false;
130     } else if( aKind == 2 )
131       aBrepMode = 1;
132     else
133       aBrepMode = aKind;
134   }
135
136   // Set "C" numeric locale to save numbers correctly
137   GeomAlgoAPI_Tools::Localizer loc;
138
139   // initialize writer
140   IGESControl_Controller::Init();
141   IGESControl_Writer ICW( "M", aBrepMode ); // export explicitly in meters
142   Interface_Static::SetCVal( "xstep.cascade.unit", "M" );
143
144   // 09.03.2010 skl for bug 0020726
145   // change default value "Average" to "Max"
146   Interface_Static::SetCVal( "write.precision.mode", "Max" );
147
148   // perform shape writing
149   if( ICW.AddShape( theShape->impl<TopoDS_Shape>() ) ) {
150     ICW.ComputeModel();
151     return ICW.Write(theFileName.c_str()) == Standard_True;
152   }
153   return false;
154 }