1 // Copyright (C) 2014-2015 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 <utilities.h>
31 #include "GEOM_PythonDump.hxx"
32 #include "GEOMImpl_Types.hxx"
34 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
36 //=============================================================================
40 //=============================================================================
41 STEPPlugin_IOperations::STEPPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
42 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
44 MESSAGE( "STEPPlugin_IOperations::STEPPlugin_IOperations" );
47 //=============================================================================
51 //=============================================================================
52 STEPPlugin_IOperations::~STEPPlugin_IOperations()
54 MESSAGE( "STEPPlugin_IOperations::~STEPPlugin_IOperations" );
57 //=============================================================================
61 //=============================================================================
62 static GEOM::TPythonDump& operator<<
63 (GEOM::TPythonDump &theDump,
64 const STEPPlugin_IOperations::LengthUnit theState)
67 case STEPPlugin_IOperations::LengthUnit_Inch:
68 theDump << "GEOM.LU_INCH";
70 case STEPPlugin_IOperations::LengthUnit_Millimeter:
71 theDump << "GEOM.LU_MILLIMETER";
73 case STEPPlugin_IOperations::LengthUnit_Foot:
74 theDump << "GEOM.LU_FOOT";
76 case STEPPlugin_IOperations::LengthUnit_Mile:
77 theDump << "GEOM.LU_MILE";
79 case STEPPlugin_IOperations::LengthUnit_Meter:
80 theDump << "GEOM.LU_METER";
82 case STEPPlugin_IOperations::LengthUnit_Kilometer:
83 theDump << "GEOM.LU_KILOMETER";
85 case STEPPlugin_IOperations::LengthUnit_Milliinch:
86 theDump << "GEOM.LU_MILLIINCH";
88 case STEPPlugin_IOperations::LengthUnit_Micrometer:
89 theDump << "GEOM.LU_MICROMETER";
91 case STEPPlugin_IOperations::LengthUnit_Centimeter:
92 theDump << "GEOM.LU_CENTIMETER";
94 case STEPPlugin_IOperations::LengthUnit_Microinch:
95 theDump << "GEOM.LU_MICROINCH";
104 //=============================================================================
107 * Export a shape to STEP format
108 * \param theOriginal The shape to export
109 * \param theFileName The name of the file to exported
110 * \param theUnit the length unit
112 //=============================================================================
113 void STEPPlugin_IOperations::ExportSTEP
114 (const Handle(GEOM_Object) theOriginal,
115 const TCollection_AsciiString &theFileName,
116 const LengthUnit theUnit)
119 if( theOriginal.IsNull() ) return;
121 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
122 if( aRefFunction.IsNull() ) return; //There is no function which creates an object to be exported
124 //Add a new result object
125 Handle(GEOM_Object) result = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT);
127 //Add an Export function
128 Handle(GEOM_Function) aFunction = result->AddFunction( STEPPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
129 if( aFunction.IsNull() ) return;
131 //Check if the function is set correctly
132 if( aFunction->GetDriverGUID() != STEPPlugin_ExportDriver::GetID() ) return;
135 STEPPlugin_IExport aCI( aFunction );
136 aCI.SetOriginal( aRefFunction );
137 aCI.SetFileName( theFileName );
138 aCI.SetUnit( theUnit );
143 if( !GetSolver()->ComputeFunction( aFunction ) ) {
144 SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
148 catch( Standard_Failure ) {
149 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
150 SetErrorCode( aFail->GetMessageString() );
154 //Make a Python command
155 GEOM::TPythonDump(aFunction) << "geompy.ExportSTEP(" << theOriginal << ", \""
156 << theFileName.ToCString() << "\", " << theUnit << " )";
161 //=============================================================================
164 * Import a shape from STEP format
165 * \param theFileName The name of the file to import
166 * \return List of GEOM_Objects, containing the created shape and propagation groups.
168 //=============================================================================
169 Handle(TColStd_HSequenceOfTransient)
170 STEPPlugin_IOperations::ImportSTEP(const TCollection_AsciiString& theFileName,
171 const bool theIsIgnoreUnits,
172 const bool IsCreateAssemblies)
175 if( theFileName.IsEmpty() ) return NULL;
177 //Add a new result object
178 Handle(GEOM_Object) anImported = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT );
180 //Add an Import function
181 Handle(GEOM_Function) aFunction =
182 anImported->AddFunction( STEPPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
183 if (aFunction.IsNull()) return NULL;
185 //Check if the function is set correctly
186 if (aFunction->GetDriverGUID() != STEPPlugin_ImportDriver::GetID()) return NULL;
189 STEPPlugin_IImport aCI( aFunction );
190 aCI.SetFileName( theFileName );
191 aCI.SetIsIgnoreUnits( theIsIgnoreUnits );
192 aCI.SetIsCreateAssemblies( IsCreateAssemblies );
195 Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
199 if( !GetSolver()->ComputeFunction( aFunction ) ) {
200 SetErrorCode( "Import driver failed" );
203 aSeq->Append(anImported);
205 // Greate material groups.
206 MakeMaterialGroups( anImported, aSeq );
208 catch( Standard_Failure ) {
209 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
210 SetErrorCode( aFail->GetMessageString() );
214 //Make a Python command
215 GEOM::TPythonDump pd (aFunction);
216 pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", ";
217 pd << (theIsIgnoreUnits ? "True" : "False");
218 pd << ", " << (IsCreateAssemblies ? "True" : "False");
225 //=============================================================================
229 //=============================================================================
230 TCollection_AsciiString
231 STEPPlugin_IOperations::ReadValue( const TCollection_AsciiString& theFileName,
232 const TCollection_AsciiString& theParameterName )
236 TCollection_AsciiString aValue, anError;
238 if (theFileName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
240 aValue = STEPPlugin_ImportDriver::GetValue( theFileName, theParameterName, anError );
242 if( aValue.IsEmpty() ) {
243 if( anError.IsEmpty() )
244 anError = theFileName + " doesn't contain requested parameter";
247 if (anError.IsEmpty())
250 SetErrorCode(anError.ToCString());