Salome HOME
'fr' and 'ja' translations for 0022617: [CEA 1060] In OCC view, add "Show vertices...
[modules/geom.git] / src / IGESExport / IGESExport.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 WIN32
43   #if defined IGESEXPORT_EXPORTS || defined IGESExport_EXPORTS
44     #define IGESEXPORT_EXPORT __declspec( dllexport )
45   #else
46     #define IGESEXPORT_EXPORT __declspec( dllimport )
47   #endif
48 #else
49   #define IGESEXPORT_EXPORT
50 #endif
51
52 //=============================================================================
53 /*!
54  *  KindOfBRep
55  *  \return 0 if theShape contains only simple entities (wires, edges and vertices),
56  *          1 if theShape contains only complex entities (shells, solids and compsolids)
57  *          2 if theShape contains only indifferent entities (faces)
58  *         -1 if theShape contains both simple and complex entities (and in this case it
59  *            cannot be saved without any loss neither in BRepMode == 0 nor in BRepMode == 1)
60  */
61 //=============================================================================
62 int KindOfBRep (const TopoDS_Shape& theShape)
63 {
64   int aKind = 2;
65
66   switch (theShape.ShapeType())
67   {
68   case TopAbs_COMPOUND:
69     {
70       bool isSimple = false;
71       bool isComplex = false;
72       TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
73       for (; anIt.More(); anIt.Next()) {
74         TopoDS_Shape aS = anIt.Value();
75         int aKindSub = KindOfBRep(aS);
76         if (aKindSub == 0)
77           isSimple = true;
78         else if (aKindSub == 1)
79           isComplex = true;
80         else if (aKindSub == -1) {
81           return -1; // heterogeneous
82         }
83       }
84       if (isSimple && isComplex)
85         aKind = -1; // heterogeneous
86       else if (isSimple)
87         aKind = 0;
88       else if (isComplex)
89         aKind = 1;
90     }
91     break;
92   case TopAbs_COMPSOLID:
93   case TopAbs_SOLID:
94   case TopAbs_SHELL:
95     aKind = 1;
96     break;
97   case TopAbs_WIRE:
98   case TopAbs_EDGE:
99   case TopAbs_VERTEX:
100     aKind = 0;
101     break;
102   default:
103     aKind = 2;
104   }
105
106   return aKind;
107 }
108
109 //=============================================================================
110 /*!
111  *
112  */
113 //=============================================================================
114
115 extern "C"
116 {
117 IGESEXPORT_EXPORT
118   int Export( const TopoDS_Shape& theShape,
119               const TCollection_AsciiString& theFileName,
120               const TCollection_AsciiString& theFormatName )
121   {
122     bool ok = false;
123
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 (theFormatName.IsEqual("IGES_5_3"))
128       aBrepMode = 1;
129
130     MESSAGE("Export IGES into file " << theFileName.ToCString());
131
132     // Mantis issue 0021350: check being exported shape, as some standalone
133     // entities (edges, wires and vertices) cannot be saved in BRepMode
134     if (aBrepMode == 1) {
135       int aKind = KindOfBRep(theShape);
136       if (aKind == -1)
137         Standard_Failure::Raise("EXPORT_IGES_HETEROGENEOUS_COMPOUND");
138       else if (aKind == 2)
139         aBrepMode = 1;
140       else
141         aBrepMode = aKind;
142     }
143
144     // commented for 0021350: Please don't catch exceptions silently and send an
145     // inappropriate error message instead, it is disturbing for the user and for us
146     //try
147     {
148       // Set "C" numeric locale to save numbers correctly
149       Kernel_Utils::Localizer loc;
150
151       // initialize writer
152       IGESControl_Controller::Init();
153       //IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"),
154       //                        Interface_Static::IVal("write.iges.brep.mode"));
155       IGESControl_Writer ICW ("M", aBrepMode); // "write.iges.unit" ->> VSR 15.09.09: export explicitly in meters
156       Interface_Static::SetCVal("xstep.cascade.unit","M");
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 )) {
164         ICW.ComputeModel();
165         ok = ICW.Write( theFileName.ToCString() );
166       }
167     }
168     //catch(Standard_Failure)
169     //{
170     //}
171     return ok;
172   }
173 }