1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "STEPPlugin_IOperations.hxx"
22 #include "STEPPlugin_ExportDriver.hxx"
23 #include "STEPPlugin_ImportDriver.hxx"
24 #include "STEPPlugin_IExport.hxx"
25 #include "STEPPlugin_IImport.hxx"
28 #include <Basics_DirUtils.hxx>
29 #include <utilities.h>
32 #include "GEOM_PythonDump.hxx"
33 #include "GEOMImpl_Types.hxx"
35 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
37 //=============================================================================
41 //=============================================================================
42 STEPPlugin_IOperations::STEPPlugin_IOperations( GEOM_Engine* theEngine )
43 : GEOMImpl_IBaseIEOperations( theEngine )
45 MESSAGE( "STEPPlugin_IOperations::STEPPlugin_IOperations" );
48 //=============================================================================
52 //=============================================================================
53 STEPPlugin_IOperations::~STEPPlugin_IOperations()
55 MESSAGE( "STEPPlugin_IOperations::~STEPPlugin_IOperations" );
58 //=============================================================================
62 //=============================================================================
63 static GEOM::TPythonDump& operator<<
64 (GEOM::TPythonDump &theDump,
65 const STEPPlugin_IOperations::LengthUnit theState)
68 case STEPPlugin_IOperations::LengthUnit_Inch:
69 theDump << "GEOM.LU_INCH";
71 case STEPPlugin_IOperations::LengthUnit_Millimeter:
72 theDump << "GEOM.LU_MILLIMETER";
74 case STEPPlugin_IOperations::LengthUnit_Foot:
75 theDump << "GEOM.LU_FOOT";
77 case STEPPlugin_IOperations::LengthUnit_Mile:
78 theDump << "GEOM.LU_MILE";
80 case STEPPlugin_IOperations::LengthUnit_Meter:
81 theDump << "GEOM.LU_METER";
83 case STEPPlugin_IOperations::LengthUnit_Kilometer:
84 theDump << "GEOM.LU_KILOMETER";
86 case STEPPlugin_IOperations::LengthUnit_Milliinch:
87 theDump << "GEOM.LU_MILLIINCH";
89 case STEPPlugin_IOperations::LengthUnit_Micrometer:
90 theDump << "GEOM.LU_MICROMETER";
92 case STEPPlugin_IOperations::LengthUnit_Centimeter:
93 theDump << "GEOM.LU_CENTIMETER";
95 case STEPPlugin_IOperations::LengthUnit_Microinch:
96 theDump << "GEOM.LU_MICROINCH";
105 //=============================================================================
108 * Export a shape to STEP format
109 * \param theOriginal The shape to export
110 * \param theFileName The name of the file to exported
111 * \param theUnit the length unit
113 //=============================================================================
114 void STEPPlugin_IOperations::ExportSTEP
115 (const Handle(GEOM_Object) theOriginal,
116 const TCollection_AsciiString &theFileName,
117 const LengthUnit theUnit)
120 if( theOriginal.IsNull() ) return;
122 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
123 if( aRefFunction.IsNull() ) return; //There is no function which creates an object to be exported
125 //Add a new result object
126 Handle(GEOM_Object) result = GetEngine()->AddObject( GEOM_IMPORT);
128 //Add an Export function
129 Handle(GEOM_Function) aFunction = result->AddFunction( STEPPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
130 if( aFunction.IsNull() ) return;
132 //Check if the function is set correctly
133 if( aFunction->GetDriverGUID() != STEPPlugin_ExportDriver::GetID() ) return;
136 STEPPlugin_IExport aCI( aFunction );
137 aCI.SetOriginal( aRefFunction );
138 aCI.SetFileName( theFileName );
139 aCI.SetUnit( theUnit );
144 if( !GetSolver()->ComputeFunction( aFunction ) ) {
145 SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
149 catch( Standard_Failure& aFail ) {
150 SetErrorCode( aFail.GetMessageString() );
154 //Make a Python command
155 std::string convFileName = Kernel_Utils::BackSlashToSlash(theFileName.ToCString());
156 GEOM::TPythonDump(aFunction) << "geompy.ExportSTEP(" << theOriginal << ", \""
157 << convFileName.c_str() << "\", " << theUnit << " )";
162 //=============================================================================
165 * Import a shape from STEP format
166 * \param theFileName The name of the file to import
167 * \return List of GEOM_Objects, containing the created shape and propagation groups.
169 //=============================================================================
170 Handle(TColStd_HSequenceOfTransient)
171 STEPPlugin_IOperations::ImportSTEP(const TCollection_AsciiString& theFileName,
172 const bool theIsIgnoreUnits,
173 const bool IsCreateAssemblies)
176 if( theFileName.IsEmpty() ) return NULL;
178 //Add a new result object
179 Handle(GEOM_Object) anImported = GetEngine()->AddObject( GEOM_IMPORT );
181 //Add an Import function
182 Handle(GEOM_Function) aFunction =
183 anImported->AddFunction( STEPPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
184 if (aFunction.IsNull()) return NULL;
186 //Check if the function is set correctly
187 if (aFunction->GetDriverGUID() != STEPPlugin_ImportDriver::GetID()) return NULL;
190 STEPPlugin_IImport aCI( aFunction );
191 aCI.SetFileName( theFileName );
192 aCI.SetIsIgnoreUnits( theIsIgnoreUnits );
193 aCI.SetIsCreateAssemblies( IsCreateAssemblies );
196 Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
200 if( !GetSolver()->ComputeFunction( aFunction ) ) {
201 SetErrorCode( "Import driver failed" );
204 aSeq->Append(anImported);
206 // Create material groups.
207 MakeMaterialGroups( anImported, aSeq );
209 catch( Standard_Failure& aFail ) {
210 SetErrorCode( aFail.GetMessageString() );
214 //Make a Python command
215 GEOM::TPythonDump pd (aFunction);
216 std::string convFileName = Kernel_Utils::BackSlashToSlash( theFileName.ToCString() );
217 pd << aSeq << " = geompy.ImportSTEP(\"" << convFileName.c_str() << "\", ";
218 pd << (theIsIgnoreUnits ? "True" : "False");
219 pd << ", " << (IsCreateAssemblies ? "True" : "False");
226 //=============================================================================
230 //=============================================================================
231 TCollection_AsciiString
232 STEPPlugin_IOperations::ReadValue( const TCollection_AsciiString& theFileName,
233 const TCollection_AsciiString& theParameterName )
237 TCollection_AsciiString aValue, anError;
239 if (theFileName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
241 aValue = STEPPlugin_ImportDriver::GetValue( theFileName, theParameterName, anError );
243 if( aValue.IsEmpty() ) {
244 if( anError.IsEmpty() )
245 anError = theFileName + " doesn't contain requested parameter";
248 if (anError.IsEmpty())
251 SetErrorCode(anError.ToCString());