Salome HOME
e9bc5272d8b1b8cfbbe9ac53b5fbbe1b8c807c06
[modules/geom.git] / src / BREPPlugin / BREPPlugin_IOperations.cxx
1 // Copyright (C) 2014-2023  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 "BREPPlugin_IOperations.hxx"
22 #include "BREPPlugin_ExportDriver.hxx"
23 #include "BREPPlugin_ImportDriver.hxx"
24 #include "BREPPlugin_IExport.hxx"
25 #include "BREPPlugin_IImport.hxx"
26
27 // KERNEL includes
28 #include <Basics_DirUtils.hxx>
29 #include <utilities.h>
30
31 // GEOM includes
32 #include "GEOM_PythonDump.hxx"
33 #include "GEOMImpl_Types.hxx"
34
35 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
36
37 //=============================================================================
38 /*!
39  *  Constructor
40  */
41 //=============================================================================
42 BREPPlugin_IOperations::BREPPlugin_IOperations( GEOM_Engine* theEngine )
43 : GEOMImpl_IBaseIEOperations( theEngine )
44 {
45   MESSAGE( "BREPPlugin_IOperations::BREPPlugin_IOperations" );
46 }
47
48 //=============================================================================
49 /*!
50  *  Destructor
51  */
52 //=============================================================================
53 BREPPlugin_IOperations::~BREPPlugin_IOperations()
54 {
55   MESSAGE( "BREPPlugin_IOperations::~BREPPlugin_IOperations" );
56 }
57
58 //=============================================================================
59 /*!
60  *  ExportBREP
61  *  Export a shape to BREP format
62  *  \param theOriginal The shape to export
63  *  \param theFileName The name of the file to exported
64  *  \param theIsASCII The format of the exported file (ASCII or Binary)
65  *  \param theDeflection The deflection of the shape to exported
66  */
67 //=============================================================================
68 void BREPPlugin_IOperations::ExportBREP( const Handle(GEOM_Object)      theOriginal,
69                                          const TCollection_AsciiString& theFileName )
70 {
71   SetErrorCode(KO);
72   if( theOriginal.IsNull() ) return;
73
74   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
75   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
76
77   //Add a new result object
78   Handle(GEOM_Object) result = GetEngine()->AddObject( GEOM_IMPORT);
79
80   //Add an Export function
81   Handle(GEOM_Function) aFunction = result->AddFunction( BREPPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
82   if( aFunction.IsNull() ) return;
83
84   //Check if the function is set correctly
85   if( aFunction->GetDriverGUID() != BREPPlugin_ExportDriver::GetID() ) return;
86
87   //Set parameters
88   BREPPlugin_IExport aCI( aFunction );
89   aCI.SetOriginal( aRefFunction );
90   aCI.SetFileName( theFileName );
91
92   //Perform the Export
93   try {
94     OCC_CATCH_SIGNALS;
95     if( !GetSolver()->ComputeFunction( aFunction ) ) {
96       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
97       return;
98     }
99   }
100   catch( Standard_Failure& aFail ) {
101     SetErrorCode( aFail.GetMessageString() );
102     return;
103   }
104
105   //Make a Python command
106   std::string convFileName = Kernel_Utils::BackSlashToSlash(theFileName.ToCString());
107   GEOM::TPythonDump(aFunction) << "geompy.ExportBREP(" << theOriginal << ", \""
108     << convFileName.c_str() << "\" )";
109
110   SetErrorCode(OK);
111 }
112
113 //=============================================================================
114 /*!
115  *  ImportBREP
116  *  Import a shape from BREP format
117  *  \param theFileName The name of the file to import
118  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
119  */
120 //=============================================================================
121 Handle(TColStd_HSequenceOfTransient)
122 BREPPlugin_IOperations::ImportBREP( const TCollection_AsciiString& theFileName )
123 {
124   SetErrorCode(KO);
125   if( theFileName.IsEmpty() ) return NULL;
126
127   //Add a new result object
128   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GEOM_IMPORT );
129
130   //Add an Import function
131   Handle(GEOM_Function) aFunction =
132     anImported->AddFunction( BREPPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
133   if (aFunction.IsNull()) return NULL;
134
135   //Check if the function is set correctly
136   if (aFunction->GetDriverGUID() != BREPPlugin_ImportDriver::GetID()) return NULL;
137
138   //Set parameters
139   BREPPlugin_IImport aCI( aFunction );
140   aCI.SetFileName( theFileName );
141
142   //Perform the Import
143   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
144
145   try {
146     OCC_CATCH_SIGNALS;
147     if( !GetSolver()->ComputeFunction( aFunction ) ) {
148       SetErrorCode( "Import driver failed" );
149       return NULL;
150     }
151     aSeq->Append(anImported);
152
153     // Create material groups.
154     // MakeMaterialGroups( anImported, aSeq );
155   }
156   catch( Standard_Failure& aFail ) {
157     SetErrorCode( aFail.GetMessageString() );
158     return NULL;
159   }
160
161   //Make a Python command
162   GEOM::TPythonDump pd (aFunction);
163   std::string convFileName =  Kernel_Utils::BackSlashToSlash( theFileName.ToCString() );
164   pd << aSeq << " = geompy.ImportBREP(\"" << convFileName.c_str() << "\" )";
165   SetErrorCode(OK);
166
167   return aSeq;
168 }