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