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