Salome HOME
c7575020cc3219ee901ddb2dc1f98994a15120bb
[plugins/canrecplugin.git] / src / CANRECPLUGINEngine / CANRECPluginImpl_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 #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  *  \param theStat is the statistics. A set of integers. For details please
50  *         see CANRECPlugin.idl
51  *  \return New GEOM_Object, containing the created shape.
52  */
53 //=============================================================================
54 Handle(GEOM_Object)
55 CANRECPluginImpl_IOperations::MakeCanonicalRecognition
56                   (Handle(GEOM_Object)                theObject,
57                    bool                               theMergeSurf,
58                    bool                               theMergeCurves,
59                    double                             theTolerance,
60                    Handle_TColStd_HSequenceOfInteger &theStat)
61 {
62   SetErrorCode( KO );
63
64   if (theStat.IsNull()) {
65     theStat = new TColStd_HSequenceOfInteger;
66   } else {
67     theStat->Clear();
68   }
69
70   if ( theObject.IsNull() ) return NULL;
71
72   // Add a new object
73   Handle(GEOM_Object) aShape = GetEngine()->AddObject( GetDocID(), GEOM_CANONICALRECOGNITION );
74
75   // Add a new shape function with parameters
76   Handle(GEOM_Function) aFunction = aShape->AddFunction( CANRECPluginImpl_Driver::GetID(), CANONICALRECOGNITION_OBJ );
77   if ( aFunction.IsNull() ) return NULL;
78
79   // Check if the function is set correctly
80   if ( aFunction->GetDriverGUID() != CANRECPluginImpl_Driver::GetID() ) return NULL;
81
82   Handle(GEOM_Function) aRefFunction = theObject->GetLastFunction();
83
84   if ( aRefFunction.IsNull() ) return NULL;
85
86   CANRECPluginImpl_ICanRec aData( aFunction );
87   aData.SetNeedMergeSurf( theMergeSurf );
88   aData.SetNeedMergeCurves( theMergeCurves );
89   aData.SetTolerance( theTolerance );
90   aData.SetObject( aRefFunction );
91
92   // Compute the resulting value
93   try {
94     OCC_CATCH_SIGNALS;
95     if ( !GetSolver()->ComputeFunction( aFunction ) ) {
96       SetErrorCode( "Canonical Recognition driver failed" );
97       return NULL;
98     }
99   }
100   catch ( Standard_Failure ) {
101     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
102     SetErrorCode( aFail->GetMessageString() );
103     return NULL;
104   }
105
106   Handle(TColStd_HArray1OfInteger) anArray = aData.GetStatistics();
107
108   if (anArray.IsNull() == Standard_False) {
109     Standard_Integer       i   = anArray->Lower();
110     const Standard_Integer iUp = anArray->Upper();
111
112     for (; i <= iUp; ++i) {
113       theStat->Append(anArray->Value(i));
114     }
115   }
116   
117   // Make a Python command
118   GEOM::TPythonDump(aFunction) << aShape << " = " << "geompy.MakeCanonicalRecognition(" << 
119     theObject << ", " << theMergeSurf << ", " << theMergeCurves << ", " << theTolerance <<  ")";
120
121   SetErrorCode( OK );
122
123   return aShape;
124 }
125
126 //=============================================================================
127 /*!
128  *  \brief Get the number of canonical faces in the shape. 
129  *
130  *  \param theObject the input object (solid, compound, compsolid).
131  *
132  *  \return the number of canonical faces in the object.
133  */
134 //=============================================================================
135 Standard_Integer CANRECPluginImpl_IOperations::GetNbCanonicalFaces
136                                     (const Handle(GEOM_Object) &theObject)
137 {
138   SetErrorCode(KO);
139
140   TopoDS_Shape aShape = theObject->GetValue();
141
142   if (aShape.IsNull()) {
143     return -1;
144   }
145
146   const Standard_Integer aResult =
147     CANRECPluginImpl_Driver::GetNbCanonicalFaces(aShape);
148
149   if (aResult >= 0) {
150     SetErrorCode(OK);
151
152     return aResult;
153   }
154
155   return -1;
156 }