Salome HOME
1349b8a9674fd277deb593e9e1fa891226fe3d48
[modules/geom.git] / src / IGESExport / IGESExport.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        IGESExport.cxx
24 // Created:     Wed May 19 14:49:45 2004
25 // Author:      Pavel TELKOV
26
27 #include "utilities.h"
28
29 #include <Basics_Utils.hxx>
30
31 #include <IGESControl_Controller.hxx>
32 #include <IGESControl_Writer.hxx>
33 #include <Interface_Static.hxx>
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Iterator.hxx>
37
38 #include <TCollection_AsciiString.hxx>
39
40 #include <Standard_Failure.hxx>
41
42 #ifdef WNT
43  #if defined IGESEXPORT_EXPORTS || defined IGESExport_EXPORTS
44   #if defined WIN32
45    #define IGESEXPORT_EXPORT __declspec( dllexport )
46   #else
47    #define IGESEXPORT_EXPORT
48   #endif
49  #else
50   #if defined WIN32
51    #define IGESEXPORT_EXPORT __declspec( dllimport )
52   #else
53    #define IGESEXPORT_EXPORT
54   #endif
55  #endif
56 #else
57  #define IGESEXPORT_EXPORT
58 #endif
59
60 //=============================================================================
61 /*!
62  *  KindOfBRep
63  *  \return 0 if theShape contains only simple entities (wires, edges and vertices),
64  *          1 if theShape contains only complex entities (shells, solids and compsolids)
65  *          2 if theShape contains only indifferent entities (faces)
66  *         -1 if theShape contains both simple and complex entities (and in this case it
67  *            cannot be saved without any loss neither in BRepMode == 0 nor in BRepMode == 1)
68  */
69 //=============================================================================
70 int KindOfBRep (const TopoDS_Shape& theShape)
71 {
72   int aKind = 2;
73
74   switch (theShape.ShapeType())
75   {
76   case TopAbs_COMPOUND:
77     {
78       bool isSimple = false;
79       bool isComplex = false;
80       TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
81       for (; anIt.More(); anIt.Next()) {
82         TopoDS_Shape aS = anIt.Value();
83         int aKindSub = KindOfBRep(aS);
84         if (aKindSub == 0)
85           isSimple = true;
86         else if (aKindSub == 1)
87           isComplex = true;
88         else if (aKindSub == -1) {
89           return -1; // heterogeneous
90         }
91       }
92       if (isSimple && isComplex)
93         aKind = -1; // heterogeneous
94       else if (isSimple)
95         aKind = 0;
96       else if (isComplex)
97         aKind = 1;
98     }
99     break;
100   case TopAbs_COMPSOLID:
101   case TopAbs_SOLID:
102   case TopAbs_SHELL:
103     aKind = 1;
104     break;
105   case TopAbs_WIRE:
106   case TopAbs_EDGE:
107   case TopAbs_VERTEX:
108     aKind = 0;
109     break;
110   default:
111     aKind = 2;
112   }
113
114   return aKind;
115 }
116
117 //=============================================================================
118 /*!
119  *
120  */
121 //=============================================================================
122
123 extern "C"
124 {
125 IGESEXPORT_EXPORT
126   int Export( const TopoDS_Shape& theShape,
127               const TCollection_AsciiString& theFileName,
128               const TCollection_AsciiString& theFormatName )
129   {
130     bool ok = false;
131
132     // define, whether to write only faces (5.1 IGES format)
133     // or shells and solids also (5.3 IGES format)
134     int aBrepMode = 0;
135     if (theFormatName.IsEqual("IGES_5_3"))
136       aBrepMode = 1;
137
138     MESSAGE("Export IGES into file " << theFileName.ToCString());
139
140     // Mantis issue 0021350: check being exported shape, as some standalone
141     // entities (edges, wires and vertices) cannot be saved in BRepMode
142     if (aBrepMode == 1) {
143       int aKind = KindOfBRep(theShape);
144       if (aKind == -1)
145         Standard_Failure::Raise("EXPORT_IGES_HETEROGENEOUS_COMPOUND");
146       else if (aKind == 2)
147         aBrepMode = 1;
148       else
149         aBrepMode = aKind;
150     }
151
152     // commented for 0021350: Please don't catch exceptions silently and send an
153     // inappropriate error message instead, it is disturbing for the user and for us
154     //try
155     {
156       // Set "C" numeric locale to save numbers correctly
157       Kernel_Utils::Localizer loc;
158
159       // initialize writer
160       IGESControl_Controller::Init();
161       //IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"),
162       //                        Interface_Static::IVal("write.iges.brep.mode"));
163       IGESControl_Writer ICW ("M", aBrepMode); // "write.iges.unit" ->> VSR 15.09.09: export explicitly in meters
164       Interface_Static::SetCVal("xstep.cascade.unit","M");
165
166       // 09.03.2010 skl for bug 0020726
167       // change default value "Average" to "Max"
168       Interface_Static::SetCVal("write.precision.mode","Max");
169
170       // perform shape writing
171       if (ICW.AddShape( theShape )) {
172         ICW.ComputeModel();
173         ok = ICW.Write( theFileName.ToCString() );
174       }
175     }
176     //catch(Standard_Failure)
177     //{
178     //}
179     return ok;
180   }
181 }