Salome HOME
12beb50235cdd12f1ca7902a76b94d896f566111
[modules/geom.git] / src / STLPlugin / STLPlugin_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 "STLPlugin_IOperations.hxx"
22 #include "STLPlugin_ExportDriver.hxx"
23 #include "STLPlugin_ImportDriver.hxx"
24 #include "STLPlugin_IExport.hxx"
25 #include "STLPlugin_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 STLPlugin_IOperations::STLPlugin_IOperations( GEOM_Engine* theEngine )
43 : GEOMImpl_IBaseIEOperations( theEngine )
44 {
45   MESSAGE( "STLPlugin_IOperations::STLPlugin_IOperations" );
46 }
47
48 //=============================================================================
49 /*!
50  *  Destructor
51  */
52 //=============================================================================
53 STLPlugin_IOperations::~STLPlugin_IOperations()
54 {
55   MESSAGE( "STLPlugin_IOperations::~STLPlugin_IOperations" );
56 }
57
58 //=============================================================================
59 /*!
60  *  ExportSTL
61  *  Export a shape to STL 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  *  \param theIsRelative The mode for writing the file. If True (default value),
67  *         the deflection is calculated relatively to the size of the shape;
68  *         if False, the user defined deflection is used.
69  */
70 //=============================================================================
71 void STLPlugin_IOperations::ExportSTL( const Handle(GEOM_Object)      theOriginal,
72                                        const TCollection_AsciiString& theFileName,
73                                        const bool                     theIsASCII,
74                                        const double                   theDeflection,
75                                        const bool                     theIsRelative )
76 {
77   SetErrorCode(KO);
78   if( theOriginal.IsNull() ) return;
79
80   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
81   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
82
83   //Add a new result object
84   Handle(GEOM_Object) result = GetEngine()->AddObject( GEOM_IMPORT);
85
86   //Add an Export function
87   Handle(GEOM_Function) aFunction = result->AddFunction( STLPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
88   if( aFunction.IsNull() ) return;
89
90   //Check if the function is set correctly
91   if( aFunction->GetDriverGUID() != STLPlugin_ExportDriver::GetID() ) return;
92
93   //Set parameters
94   STLPlugin_IExport aCI( aFunction );
95   aCI.SetOriginal( aRefFunction );
96   aCI.SetFileName( theFileName );
97   aCI.SetIsASCII( theIsASCII );
98   aCI.SetIsRelative( theIsRelative );
99   aCI.SetDeflection( theDeflection );
100
101   //Perform the Export
102   try {
103     OCC_CATCH_SIGNALS;
104     if( !GetSolver()->ComputeFunction( aFunction ) ) {
105       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
106       return;
107     }
108   }
109   catch( Standard_Failure& aFail ) {
110     SetErrorCode( aFail.GetMessageString() );
111     return;
112   }
113
114   //Make a Python command
115   std::string convFileName = Kernel_Utils::BackSlashToSlash(theFileName.ToCString());
116   GEOM::TPythonDump(aFunction) << "geompy.ExportSTL(" << theOriginal << ", \""
117     << convFileName.c_str() << "\", " << theIsASCII << ", " << theDeflection << ", "
118     << theIsRelative << ")";
119
120   SetErrorCode(OK);
121 }
122
123 //=============================================================================
124 /*!
125  *  ImportSTL
126  *  Import a shape from STL format
127  *  \param theFileName The name of the file to import
128  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
129  */
130 //=============================================================================
131 Handle(TColStd_HSequenceOfTransient)
132 STLPlugin_IOperations::ImportSTL( const TCollection_AsciiString& theFileName )
133 {
134   SetErrorCode(KO);
135   if( theFileName.IsEmpty() ) return NULL;
136
137   //Add a new result object
138   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GEOM_IMPORT );
139
140   //Add an Import function
141   Handle(GEOM_Function) aFunction =
142     anImported->AddFunction( STLPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
143   if (aFunction.IsNull()) return NULL;
144
145   //Check if the function is set correctly
146   if (aFunction->GetDriverGUID() != STLPlugin_ImportDriver::GetID()) return NULL;
147
148   //Set parameters
149   STLPlugin_IImport aCI( aFunction );
150   aCI.SetFileName( theFileName );
151
152   //Perform the Import
153   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
154
155   try {
156     OCC_CATCH_SIGNALS;
157     if( !GetSolver()->ComputeFunction( aFunction ) ) {
158       SetErrorCode( "Import driver failed" );
159       return NULL;
160     }
161     aSeq->Append(anImported);
162
163     // Create material groups.
164     // MakeMaterialGroups( anImported, aSeq );
165   }
166   catch( Standard_Failure& aFail ) {
167     SetErrorCode( aFail.GetMessageString() );
168     return NULL;
169   }
170
171   //Make a Python command
172   GEOM::TPythonDump pd (aFunction);
173   std::string convFileName = Kernel_Utils::BackSlashToSlash(theFileName.ToCString());
174   pd << aSeq << " = geompy.ImportSTL(\"" << convFileName.c_str() << "\" )";
175   SetErrorCode(OK);
176
177   return aSeq;
178 }