Salome HOME
Initial repository creation
[plugins/canrecplugin.git] / src / CANRECPLUGINEngine / CANRECPluginImpl_Driver.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 #include "CANRECPluginImpl_Driver.hxx"
20 #include "CANRECPluginImpl_Types.hxx"
21 #include "CANRECPluginImpl_ICanRec.hxx"
22
23 #include <GEOM_Function.hxx>
24
25 #include <BRepOffsetAPI_Sewing.hxx>
26 #include <Standard_Version.hxx>
27 #include <TFunction_Logbook.hxx>
28 #include <ShapeConvert_CanonicAPI.hxx>
29 #include <ShapeConvert_UnionFaces.hxx>
30 #include <ShapeConvert_UnionEdges.hxx>
31
32 #ifdef CANREC_HASLICENSE
33 #include "CANRECPluginImpl_license.h"
34 #include <OCCLicense_Activate.hxx>
35 #include <Standard_LicenseError.hxx>
36 #endif // CANREC_HASLICENSE
37
38 const Standard_GUID& CANRECPluginImpl_Driver::GetID()
39 {
40   static Standard_GUID aGUID("7e1492bb-b4cd-4a40-ad8f-102902b0047e");
41   return aGUID;
42 }
43
44 CANRECPluginImpl_Driver::CANRECPluginImpl_Driver()
45 {
46 }
47
48 CANRECPluginImpl_Driver::~CANRECPluginImpl_Driver()
49 {
50 }
51
52 Standard_Boolean CANRECPluginImpl_Driver::MustExecute( const TFunction_Logbook& ) const
53 {
54   return Standard_True;
55 }
56
57 void CANRECPluginImpl_Driver::Validate( TFunction_Logbook& ) const
58 {
59
60
61 Standard_Integer CANRECPluginImpl_Driver::Execute( TFunction_Logbook& log ) const
62 {
63   if ( Label().IsNull() ) return 0;
64   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
65
66   CANRECPluginImpl_ICanRec aData( aFunction );
67
68   // Getting data
69   bool isNeedMergeSurf = aData.GetNeedMergeSurf();
70   bool isNeedMergeCurves = aData.GetNeedMergeCurves();
71   double aTolerance = aData.GetTolerance();
72   Handle(GEOM_Function) anObject = aData.GetObject();
73   TopoDS_Shape aShape = anObject->GetValue();
74
75 #ifdef CANREC_HASLICENSE
76   try {
77     OCCLicense_Activate( "CANREC-"OCC_VERSION_STRING, CANREC_LICENSE );
78   }
79   catch (Standard_LicenseError) {
80     return 0;
81   }
82 #endif // CANREC_HASLICENSE
83
84   ShapeConvert_CanonicAPI aRecognizer;
85   aRecognizer.Tolerance() = aTolerance;
86   aRecognizer.SurfaceMode() = isNeedMergeSurf;
87   aRecognizer.CurveMode() = isNeedMergeCurves;
88
89   // 1. Recognizing
90   aRecognizer.SetShape( aShape );
91   aRecognizer.UnifyMode() = false;
92   aRecognizer.Perform();
93   TopoDS_Shape aResultingShape = aRecognizer.Shape();
94     
95   // 2. Sewing
96   BRepOffsetAPI_Sewing aSewing( aTolerance );
97   aSewing.Add( aResultingShape );
98   aSewing.Perform();
99   aResultingShape = aSewing.SewedShape();
100   
101   // 3.1. Union Surfaces
102   if ( isNeedMergeSurf ) {
103     ShapeConvert_UnionFaces aFaceUnifier;
104     aFaceUnifier.GetTolerance() = aTolerance;
105     aResultingShape = aFaceUnifier.Perform( aResultingShape );
106   }
107   
108   // 3.2. Union Curves 
109   if ( isNeedMergeCurves ) {
110     ShapeConvert_UnionEdges anEdgeUnifier;
111     aResultingShape = anEdgeUnifier.Perform( aResultingShape, aTolerance );
112   }
113   
114   if ( aResultingShape.IsNull() ) return 0;
115
116   aFunction->SetValue( aResultingShape );
117
118   log.SetTouched( Label() );
119   
120   return 1;
121 }
122
123 bool CANRECPluginImpl_Driver::
124 GetCreationInformation( std::string&             theOperationName,
125                         std::vector<GEOM_Param>& theParams )
126 {
127   if ( Label().IsNull() ) return 0;
128   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
129
130   CANRECPluginImpl_ICanRec aCI( aFunction );
131
132   theOperationName = "CANONICALRECOGNITION";
133   AddParam( theParams, PLUGIN_NAME, "CANRECPlugin" );
134   AddParam( theParams, "Tolerance", aCI.GetTolerance() );
135   AddParam( theParams, "Merge Surfaces", aCI.GetNeedMergeSurf() );
136   AddParam( theParams, "Merge Curves", aCI.GetNeedMergeCurves() );
137   
138   return true;
139 }
140
141 IMPLEMENT_STANDARD_HANDLE( CANRECPluginImpl_Driver, GEOM_BaseDriver );
142 IMPLEMENT_STANDARD_RTTIEXT( CANRECPluginImpl_Driver, GEOM_BaseDriver );