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