Salome HOME
Switch to standard C++ exception mechanism
[plugins/acisplugin.git] / src / ACISPlugin_IOperations.cxx
1 // Copyright (C) 2014-2016  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 "ACISPlugin_IOperations.hxx"
22 #include "ACISPlugin_ExportDriver.hxx"
23 #include "ACISPlugin_ImportDriver.hxx"
24 #include "ACISPlugin_IExport.hxx"
25 #include "ACISPlugin_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 ACISPlugin_IOperations::ACISPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
42 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
43 {
44   MESSAGE( "ACISPlugin_IOperations::ACISPlugin_IOperations" );
45 }
46
47 //=============================================================================
48 /*!
49  *  Destructor
50  */
51 //=============================================================================
52 ACISPlugin_IOperations::~ACISPlugin_IOperations()
53 {
54   MESSAGE( "ACISPlugin_IOperations::~ACISPlugin_IOperations" );
55 }
56
57 //=============================================================================
58 /*!
59  *  ExportACIS
60  *  Export a shape to ACIS format
61  *  \param theOriginal The shape to export
62  *  \param theFileName The name of the file to exported
63  */
64 //=============================================================================
65 void ACISPlugin_IOperations::ExportACIS( const Handle(GEOM_Object)      theOriginal,
66                                          const TCollection_AsciiString& theFileName )
67 {
68   SetErrorCode(KO);
69   if( theOriginal.IsNull() ) return;
70
71   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
72   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
73
74   //Add a new result object
75   Handle(GEOM_Object) result = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT);
76
77   //Add an Export function
78   Handle(GEOM_Function) aFunction = result->AddFunction( ACISPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
79   if( aFunction.IsNull() ) return;
80
81   //Check if the function is set correctly
82   if( aFunction->GetDriverGUID() != ACISPlugin_ExportDriver::GetID() ) return;
83
84   //Set parameters
85   ACISPlugin_IExport aCI( aFunction );
86   aCI.SetOriginal( aRefFunction );
87   aCI.SetFileName( theFileName );
88
89   //Perform the Export
90   try {
91     OCC_CATCH_SIGNALS;
92     if( !GetSolver()->ComputeFunction( aFunction ) ) {
93       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
94       return;
95     }
96   }
97   catch( Standard_Failure& aFail ) {
98     SetErrorCode( aFail.GetMessageString() );
99     return;
100   }
101
102   //Make a Python command
103   GEOM::TPythonDump(aFunction) << "geompy.ExportACIS(" << theOriginal << ", \""
104     << theFileName.ToCString() << "\" )";
105
106   SetErrorCode(OK);
107 }
108
109 //=============================================================================
110 /*!
111  *  ImportACIS
112  *  Import a shape from ACIS format
113  *  \param theFileName The name of the file to import
114  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
115  */
116 //=============================================================================
117 Handle(TColStd_HSequenceOfTransient)
118 ACISPlugin_IOperations::ImportACIS( const TCollection_AsciiString& theFileName )
119 {
120   SetErrorCode(KO);
121   if( theFileName.IsEmpty() ) return NULL;
122
123   //Add a new result object
124   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT );
125
126   //Add an Import function
127   Handle(GEOM_Function) aFunction =
128     anImported->AddFunction( ACISPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
129   if (aFunction.IsNull()) return NULL;
130
131   //Check if the function is set correctly
132   if (aFunction->GetDriverGUID() != ACISPlugin_ImportDriver::GetID()) return NULL;
133
134   //Set parameters
135   ACISPlugin_IImport aCI( aFunction );
136   aCI.SetFileName( theFileName );
137
138   //Perform the Import
139   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
140
141   try {
142     OCC_CATCH_SIGNALS;
143     if( !GetSolver()->ComputeFunction( aFunction ) ) {
144       SetErrorCode( "Import driver failed" );
145       return NULL;
146     }
147     aSeq->Append(anImported);
148
149     // Greate material groups.
150     //MakeMaterialGroups( anImported, aSeq );
151   }
152   catch( Standard_Failure& aFail ) {
153     SetErrorCode( aFail.GetMessageString() );
154     return NULL;
155   }
156
157   //Make a Python command
158   GEOM::TPythonDump pd (aFunction);
159   pd << aSeq << " = geompy.ImportACIS(\"" << theFileName.ToCString() << "\" )";
160   SetErrorCode(OK);
161
162   return aSeq;
163 }