1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <GeomAlgoAPI_IGESExport.h>
22 #include "GeomAlgoAPI_Tools.h"
24 #include <Basics_OCCTVersion.hxx>
27 #include <IGESControl_Controller.hxx>
28 #include <IGESControl_Writer.hxx>
29 #include <IGESData_IGESModel.hxx>
30 #include <Interface_Static.hxx>
33 #include <XSAlgo_AlgoContainer.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Iterator.hxx>
38 #include <UnitsMethods.hxx>
40 //=============================================================================
44 //=============================================================================
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)
53 //=============================================================================
54 int KindOfBRep (const TopoDS_Shape& theShape)
58 switch (theShape.ShapeType())
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);
70 else if (aKindSub == 1)
72 else if (aKindSub == -1) {
73 return -1; // heterogeneous
76 if (isSimple && isComplex)
77 aKind = -1; // heterogeneous
84 case TopAbs_COMPSOLID:
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)
108 std::cout << "Export IGES into file " << theFileName << std::endl;
111 if (!theShape.get()) {
112 theError = "IGES Export failed: An invalid argument";
116 // theFormatName expected "IGES-5.1", "IGES-5.3"...
117 TCollection_AsciiString aFormatName(theFormatName.c_str());
118 TCollection_AsciiString aVersion = aFormatName.Token("-", 2);
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;
124 // define, whether to write only faces (5.1 IGES format)
125 // or shells and solids also (5.3 IGES format)
127 if( aVersion.IsEqual( "5.3" ) )
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>() );
135 theError = "EXPORT_IGES_HETEROGENEOUS_COMPOUND";
137 } else if( aKind == 2 )
143 // Set "C" numeric locale to save numbers correctly
144 GeomAlgoAPI_Tools::Localizer loc;
147 IGESControl_Controller::Init();
148 IGESControl_Writer ICW( "M", aBrepMode ); // export explicitly in meters
149 Interface_Static::SetCVal( "xstep.cascade.unit", "M" );
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);
158 // 09.03.2010 skl for bug 0020726
159 // change default value "Average" to "Max"
160 Interface_Static::SetCVal( "write.precision.mode", "Max" );
162 // perform shape writing
163 if( ICW.AddShape( theShape->impl<TopoDS_Shape>() ) ) {
165 return ICW.Write(theFileName.c_str()) == Standard_True;