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