Salome HOME
f9b0adb19a10415e448761a9405c126a15a33bd3
[modules/geom.git] / src / IGESPlugin / IGESPlugin_ExportDriver.cxx
1 // Copyright (C) 2014-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "IGESPlugin_ExportDriver.hxx"
22 #include "IGESPlugin_IExport.hxx"
23
24 // KERNEL includes
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
27
28 // GEOM includes
29 #include "GEOM_Function.hxx"
30
31 // OOCT includes
32 #include <IGESControl_Controller.hxx>
33 #include <IGESControl_Writer.hxx>
34 #include <Interface_Static.hxx>
35
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Iterator.hxx>
38
39 //=============================================================================
40 /*!
41  *  KindOfBRep
42  *  \return 0 if theShape contains only simple entities (wires, edges and vertices),
43  *          1 if theShape contains only complex entities (shells, solids and compsolids)
44  *          2 if theShape contains only indifferent entities (faces)
45  *         -1 if theShape contains both simple and complex entities (and in this case it
46  *            cannot be saved without any loss neither in BRepMode == 0 nor in BRepMode == 1)
47  */
48 //=============================================================================
49 int KindOfBRep (const TopoDS_Shape& theShape)
50 {
51   int aKind = 2;
52
53   switch (theShape.ShapeType())
54   {
55   case TopAbs_COMPOUND:
56     {
57       bool isSimple = false;
58       bool isComplex = false;
59       TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
60       for (; anIt.More(); anIt.Next()) {
61         TopoDS_Shape aS = anIt.Value();
62         int aKindSub = KindOfBRep(aS);
63         if (aKindSub == 0)
64           isSimple = true;
65         else if (aKindSub == 1)
66           isComplex = true;
67         else if (aKindSub == -1) {
68           return -1; // heterogeneous
69         }
70       }
71       if (isSimple && isComplex)
72         aKind = -1; // heterogeneous
73       else if (isSimple)
74         aKind = 0;
75       else if (isComplex)
76         aKind = 1;
77     }
78     break;
79   case TopAbs_COMPSOLID:
80   case TopAbs_SOLID:
81   case TopAbs_SHELL:
82     aKind = 1;
83     break;
84   case TopAbs_WIRE:
85   case TopAbs_EDGE:
86   case TopAbs_VERTEX:
87     aKind = 0;
88     break;
89   default:
90     aKind = 2;
91   }
92
93   return aKind;
94 }
95
96 //=======================================================================
97 //function : GetID
98 //purpose  :
99 //=======================================================================
100 const Standard_GUID& IGESPlugin_ExportDriver::GetID()
101 {
102   static Standard_GUID aGUID("8fa4111a-24cb-4819-8f03-b0e2890d2a2a");
103   return aGUID;
104 }
105
106 //=======================================================================
107 //function : IGESPlugin_ExportDriver
108 //purpose  :
109 //=======================================================================
110 IGESPlugin_ExportDriver::IGESPlugin_ExportDriver()
111 {
112 }
113
114 //=======================================================================
115 //function : Execute
116 //purpose  :
117 //=======================================================================
118 Standard_Integer IGESPlugin_ExportDriver::Execute( TFunction_Logbook& log ) const
119 {
120   if (Label().IsNull()) return 0;
121   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
122
123   IGESPlugin_IExport aData (aFunction);
124
125   // retrieve the being exported shape
126   TopoDS_Shape aShape;
127   Handle(GEOM_Function) aRefFunction = aData.GetOriginal();
128   if( aRefFunction.IsNull() ) return 0;
129   aShape = aRefFunction->GetValue();
130   if( aShape.IsNull() ) return 0;
131   // set the result of function to be used by next operations
132   aFunction->SetValue( aShape );
133
134   TCollection_AsciiString aFileName = aData.GetFileName();
135   TCollection_AsciiString aVersion = aData.GetVersion();
136   // define, whether to write only faces (5.1 IGES format)
137   // or shells and solids also (5.3 IGES format)
138   int aBrepMode = 0;
139   if( aVersion.IsEqual( "5.3" ) )
140     aBrepMode = 1;
141
142   MESSAGE("Export IGES into file " << aFileName.ToCString());
143
144   // Mantis issue 0021350: check being exported shape, as some standalone
145   // entities (edges, wires and vertices) cannot be saved in BRepMode
146   if( aBrepMode == 1 ) {
147     int aKind = KindOfBRep( aShape );
148     if( aKind == -1 )
149       Standard_Failure::Raise( "EXPORT_IGES_HETEROGENEOUS_COMPOUND" );
150     else if( aKind == 2 )
151       aBrepMode = 1;
152     else
153       aBrepMode = aKind;
154   }
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( "M", aBrepMode ); // export explicitly in meters
162   Interface_Static::SetCVal( "xstep.cascade.unit", "M" );
163
164   // 09.03.2010 skl for bug 0020726
165   // change default value "Average" to "Max"
166   Interface_Static::SetCVal( "write.precision.mode", "Max" );
167
168   // perform shape writing
169   if( ICW.AddShape( aShape ) ) {
170     ICW.ComputeModel();
171     return ICW.Write( aFileName.ToCString() );
172   }
173   return 0;
174 }
175
176 //=======================================================================
177 //function : MustExecute
178 //purpose  :
179 //=======================================================================
180 Standard_Boolean IGESPlugin_ExportDriver::MustExecute( const TFunction_Logbook& ) const
181 {
182   return Standard_True;
183 }
184
185 //================================================================================
186 /*!
187  * \brief Returns a name of creation operation and names and values of creation parameters
188  */
189 //================================================================================
190 bool IGESPlugin_ExportDriver::
191 GetCreationInformation( std::string&             theOperationName,
192                         std::vector<GEOM_Param>& theParams )
193 {
194   return false;
195 }
196
197 IMPLEMENT_STANDARD_HANDLE( IGESPlugin_ExportDriver,GEOM_BaseDriver );
198 IMPLEMENT_STANDARD_RTTIEXT( IGESPlugin_ExportDriver,GEOM_BaseDriver );