Salome HOME
Initial repository creation
[plugins/canrecplugin.git] / src / CANRECPLUGINEngine / CANRECPluginImpl_IOperations.cxx
1 // Copyright (C) 2014-2015  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 #include "CANRECPluginImpl_IOperations.hxx"
21 #include "CANRECPluginImpl_Driver.hxx"
22 #include "CANRECPluginImpl_ICanRec.hxx"
23 #include "CANRECPluginImpl_Types.hxx"
24
25 #include <GEOM_PythonDump.hxx>
26
27 #include <utilities.h>
28
29 #include <Standard_ErrorHandler.hxx>
30
31 CANRECPluginImpl_IOperations::CANRECPluginImpl_IOperations( GEOM_Engine* theEngine, int theDocID)
32   : GEOM_IOperations( theEngine, theDocID )
33 {
34   MESSAGE( "CANRECPluginImpl_IOperations::CANRECPluginImpl_IOperations()" );
35 }
36
37 CANRECPluginImpl_IOperations::~CANRECPluginImpl_IOperations()
38 {
39   MESSAGE( "CANRECPluginImpl_IOperations::~CANRECPluginImpl_IOperations()" );
40 }
41
42 //=============================================================================
43 /*!
44  *  Create a canonical recognition for the object
45  *  \param theMergeSurf set merging surfaces
46  *  \param theMergeCurves set merging curves
47  *  \param theTolerance set tolerance
48  *  \param theObject is initial object
49  *  \return New GEOM_Object, containing the created shape.
50  */
51 //=============================================================================
52 Handle(GEOM_Object)
53 CANRECPluginImpl_IOperations::MakeCanonicalRecognition ( Handle(GEOM_Object) theObject,
54                                                          bool theMergeSurf,
55                                                          bool theMergeCurves,
56                                                          double theTolerance )
57 {
58   SetErrorCode( KO );
59
60   if ( theObject.IsNull() ) return NULL;
61
62   // Add a new object
63   Handle(GEOM_Object) aShape = GetEngine()->AddObject( GetDocID(), GEOM_CANONICALRECOGNITION );
64
65   // Add a new shape function with parameters
66   Handle(GEOM_Function) aFunction = aShape->AddFunction( CANRECPluginImpl_Driver::GetID(), CANONICALRECOGNITION_OBJ );
67   if ( aFunction.IsNull() ) return NULL;
68
69   // Check if the function is set correctly
70   if ( aFunction->GetDriverGUID() != CANRECPluginImpl_Driver::GetID() ) return NULL;
71
72   Handle(GEOM_Function) aRefFunction = theObject->GetLastFunction();
73
74   if ( aRefFunction.IsNull() ) return NULL;
75
76   CANRECPluginImpl_ICanRec aData( aFunction );
77   aData.SetNeedMergeSurf( theMergeSurf );
78   aData.SetNeedMergeCurves( theMergeCurves );
79   aData.SetTolerance( theTolerance );
80   aData.SetObject( aRefFunction );
81
82   // Compute the resulting value
83   try {
84     OCC_CATCH_SIGNALS;
85     if ( !GetSolver()->ComputeFunction( aFunction ) ) {
86       SetErrorCode( "Canonical Recognition driver failed" );
87       return NULL;
88     }
89   }
90   catch ( Standard_Failure ) {
91     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
92     SetErrorCode( aFail->GetMessageString() );
93     return NULL;
94   }
95   
96   // Make a Python command
97   GEOM::TPythonDump(aFunction) << aShape << " = " << "geompy.MakeCanonicalRecognition(" << 
98     theObject << ", " << theMergeSurf << ", " << theMergeCurves << ", " << theTolerance <<  ")";
99
100   SetErrorCode( OK );
101
102   return aShape;
103 }