Salome HOME
e2fa9fad29d83346dd59fc73450d2db9103148fc
[modules/geom.git] / src / STEPPlugin / STEPPlugin_IOperations.cxx
1 // Copyright (C) 2014  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 "STEPPlugin_IOperations.hxx"
22 #include "STEPPlugin_ExportDriver.hxx"
23 #include "STEPPlugin_ImportDriver.hxx"
24 #include "STEPPlugin_IExport.hxx"
25 #include "STEPPlugin_IImport.hxx"
26
27 // KERNEL includes
28 #include <utilities.h>
29
30 // GEOM includes
31 #include "GEOM_PythonDump.hxx"
32 #include "GEOMImpl_Types.hxx"
33
34 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
35
36 //=============================================================================
37 /*!
38  *  Constructor
39  */
40 //=============================================================================
41 STEPPlugin_IOperations::STEPPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
42 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
43 {
44   MESSAGE( "STEPPlugin_IOperations::STEPPlugin_IOperations" );
45 }
46
47 //=============================================================================
48 /*!
49  *  Destructor
50  */
51 //=============================================================================
52 STEPPlugin_IOperations::~STEPPlugin_IOperations()
53 {
54   MESSAGE( "STEPPlugin_IOperations::~STEPPlugin_IOperations" );
55 }
56
57 //=============================================================================
58 /*!
59  *  ExportSTEP
60  *  Export a shape to STEP format
61  *  \param theOriginal The shape to export
62  *  \param theFileName The name of the file to exported
63  *  \param theIsASCII The format of the exported file (ASCII or Binary)
64  *  \param theDeflection The deflection of the shape to exported
65  */
66 //=============================================================================
67 void STEPPlugin_IOperations::ExportSTEP( const Handle(GEOM_Object)      theOriginal,
68                                          const TCollection_AsciiString& theFileName )
69 {
70   SetErrorCode(KO);
71   if( theOriginal.IsNull() ) return;
72
73   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
74   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
75
76   //Add a new result object
77   Handle(GEOM_Object) result = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT);
78
79   //Add an Export function
80   Handle(GEOM_Function) aFunction = result->AddFunction( STEPPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
81   if( aFunction.IsNull() ) return;
82
83   //Check if the function is set correctly
84   if( aFunction->GetDriverGUID() != STEPPlugin_ExportDriver::GetID() ) return;
85
86   //Set parameters
87   STEPPlugin_IExport aCI( aFunction );
88   aCI.SetOriginal( aRefFunction );
89   aCI.SetFileName( theFileName );
90
91   //Perform the Export
92   try {
93     OCC_CATCH_SIGNALS;
94     if( !GetSolver()->ComputeFunction( aFunction ) ) {
95       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
96       return;
97     }
98   }
99   catch( Standard_Failure ) {
100     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
101     SetErrorCode( aFail->GetMessageString() );
102     return;
103   }
104
105   //Make a Python command
106   GEOM::TPythonDump(aFunction) << "geompy.ExportSTEP(" << theOriginal << ", \""
107     << theFileName.ToCString() << "\" )";
108
109   SetErrorCode(OK);
110 }
111
112 //=============================================================================
113 /*!
114  *  ImportSTEP
115  *  Import a shape from STEP format
116  *  \param theFileName The name of the file to import
117  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
118  */
119 //=============================================================================
120 Handle(TColStd_HSequenceOfTransient)
121 STEPPlugin_IOperations::ImportSTEP( const TCollection_AsciiString& theFileName,
122                                     const bool theIsIgnoreUnits )
123 {
124   SetErrorCode(KO);
125   if( theFileName.IsEmpty() ) return NULL;
126
127   //Add a new result object
128   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT );
129
130   //Add an Import function
131   Handle(GEOM_Function) aFunction =
132     anImported->AddFunction( STEPPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
133   if (aFunction.IsNull()) return NULL;
134
135   //Check if the function is set correctly
136   if (aFunction->GetDriverGUID() != STEPPlugin_ImportDriver::GetID()) return NULL;
137
138   //Set parameters
139   STEPPlugin_IImport aCI( aFunction );
140   aCI.SetFileName( theFileName );
141   aCI.SetIsIgnoreUnits( theIsIgnoreUnits );
142
143   //Perform the Import
144   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
145
146   try {
147     OCC_CATCH_SIGNALS;
148     if( !GetSolver()->ComputeFunction( aFunction ) ) {
149       SetErrorCode( "Import driver failed" );
150       return NULL;
151     }
152     aSeq->Append(anImported);
153
154     // Greate material groups.
155     MakeMaterialGroups( anImported, aSeq );
156   }
157   catch( Standard_Failure ) {
158     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
159     SetErrorCode( aFail->GetMessageString() );
160     return NULL;
161   }
162
163   //Make a Python command
164   GEOM::TPythonDump pd (aFunction);
165   if( theIsIgnoreUnits )
166     pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
167   else
168     pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
169   SetErrorCode(OK);
170
171   return aSeq;
172 }
173
174 //=============================================================================
175 /*!
176  *  ReadValue
177  */
178 //=============================================================================
179 TCollection_AsciiString
180 STEPPlugin_IOperations::ReadValue( const TCollection_AsciiString& theFileName,
181                                    const TCollection_AsciiString& theParameterName )
182 {
183   SetErrorCode(KO);
184
185   TCollection_AsciiString aValue, anError;
186
187   if (theFileName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
188
189   aValue = STEPPlugin_ImportDriver::GetValue( theFileName, theParameterName, anError );
190
191   if( aValue.IsEmpty() ) {
192     if( anError.IsEmpty() )
193       anError = theFileName + " doesn't contain requested parameter";
194     return aValue;
195   }
196   if (anError.IsEmpty())
197     SetErrorCode(OK);
198   else
199     SetErrorCode(anError.ToCString());
200
201   return aValue;
202 }