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