Salome HOME
Implementation of DXFPLUGIN as a GEOM plugin (added files)
[plugins/dxfplugin.git] / src / DXFPlugin_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 "DXFPlugin_IOperations.hxx"
22 #include "DXFPlugin_ExportDriver.hxx"
23 #include "DXFPlugin_ImportDriver.hxx"
24 #include "DXFPlugin_IExport.hxx"
25 #include "DXFPlugin_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 DXFPlugin_IOperations::DXFPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
42 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
43 {
44   MESSAGE( "DXFPlugin_IOperations::DXFPlugin_IOperations" );
45 }
46
47 //=============================================================================
48 /*!
49  *  Destructor
50  */
51 //=============================================================================
52 DXFPlugin_IOperations::~DXFPlugin_IOperations()
53 {
54   MESSAGE( "DXFPlugin_IOperations::~DXFPlugin_IOperations" );
55 }
56
57 //=============================================================================
58 /*!
59  *  ExportDXF
60  *  Export a shape to DXF 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 DXFPlugin_IOperations::ExportDXF( 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( DXFPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
81   if( aFunction.IsNull() ) return;
82
83   //Check if the function is set correctly
84   if( aFunction->GetDriverGUID() != DXFPlugin_ExportDriver::GetID() ) return;
85
86   //Set parameters
87   DXFPlugin_IExport aCI( aFunction );
88   aCI.SetOriginal( aRefFunction );
89   aCI.SetFileName( theFileName );
90
91   //Perform the Export
92   try {
93 #if OCC_VERSION_LARGE > 0x06010000
94     OCC_CATCH_SIGNALS;
95 #endif
96     if( !GetSolver()->ComputeFunction( aFunction ) ) {
97       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
98       return;
99     }
100   }
101   catch( Standard_Failure ) {
102     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
103     SetErrorCode( aFail->GetMessageString() );
104     return;
105   }
106
107   //Make a Python command
108   GEOM::TPythonDump(aFunction) << "geompy.ExportDXF(" << theOriginal << ", \""
109     << theFileName.ToCString() << "\" )";
110
111   SetErrorCode(OK);
112 }
113
114 //=============================================================================
115 /*!
116  *  ImportDXF
117  *  Import a shape from DXF format
118  *  \param theFileName The name of the file to import
119  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
120  */
121 //=============================================================================
122 Handle(TColStd_HSequenceOfTransient)
123 DXFPlugin_IOperations::ImportDXF( const TCollection_AsciiString& theFileName )
124 {
125   SetErrorCode(KO);
126   if( theFileName.IsEmpty() ) return NULL;
127
128   //Add a new result object
129   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT );
130
131   //Add an Import function
132   Handle(GEOM_Function) aFunction =
133     anImported->AddFunction( DXFPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
134   if (aFunction.IsNull()) return NULL;
135
136   //Check if the function is set correctly
137   if (aFunction->GetDriverGUID() != DXFPlugin_ImportDriver::GetID()) return NULL;
138
139   //Set parameters
140   DXFPlugin_IImport aCI( aFunction );
141   aCI.SetFileName( theFileName );
142
143   //Perform the Import
144   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
145
146   try {
147 #if OCC_VERSION_LARGE > 0x06010000
148     OCC_CATCH_SIGNALS;
149 #endif
150     if( !GetSolver()->ComputeFunction( aFunction ) ) {
151       SetErrorCode( "Import driver failed" );
152       return NULL;
153     }
154     aSeq->Append(anImported);
155
156     // Greate material groups.
157     //MakeMaterialGroups( anImported, aSeq );
158   }
159   catch( Standard_Failure ) {
160     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
161     SetErrorCode( aFail->GetMessageString() );
162     return NULL;
163   }
164
165   //Make a Python command
166   GEOM::TPythonDump pd (aFunction);
167   pd << aSeq << " = geompy.ImportDXF(\"" << theFileName.ToCString() << "\" )";
168   SetErrorCode(OK);
169
170   return aSeq;
171 }