Salome HOME
792703b0e07fea5cf44bf725c0a6f2de1502998d
[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& aFail ) {
101     SetErrorCode( aFail.GetMessageString() );
102     return NULL;
103   }
104
105   Handle(TColStd_HArray1OfInteger) anArray = aData.GetStatistics();
106
107   if (anArray.IsNull() == Standard_False) {
108     Standard_Integer       i   = anArray->Lower();
109     const Standard_Integer iUp = anArray->Upper();
110
111     for (; i <= iUp; ++i) {
112       theStat->Append(anArray->Value(i));
113     }
114   }
115   
116   // Make a Python command
117   GEOM::TPythonDump(aFunction) << aShape << " = " << "geompy.MakeCanonicalRecognition(" << 
118     theObject << ", " << theMergeSurf << ", " << theMergeCurves << ", " << theTolerance <<  ")";
119
120   SetErrorCode( OK );
121
122   return aShape;
123 }
124
125 //=============================================================================
126 /*!
127  *  \brief Get the number of canonical faces in the shape. 
128  *
129  *  \param theObject the input object (solid, compound, compsolid).
130  *
131  *  \return the number of canonical faces in the object.
132  */
133 //=============================================================================
134 Standard_Integer CANRECPluginImpl_IOperations::GetNbCanonicalFaces
135                                     (const Handle(GEOM_Object) &theObject)
136 {
137   SetErrorCode(KO);
138
139   TopoDS_Shape aShape = theObject->GetValue();
140
141   if (aShape.IsNull()) {
142     return -1;
143   }
144
145   const Standard_Integer aResult =
146     CANRECPluginImpl_Driver::GetNbCanonicalFaces(aShape);
147
148   if (aResult >= 0) {
149     SetErrorCode(OK);
150
151     return aResult;
152   }
153
154   return -1;
155 }