Salome HOME
c03917287c5764aea68914cda22c361d5b35b96f
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Chamfer.cpp
1 // Copyright (C) 2017-2023  CEA/DEN, EDF R&D
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 "FeaturesPlugin_Chamfer.h"
21 ////#include "FeaturesPlugin_Tools.h"
22 ////
23 ////#include <ModelAPI_Data.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 ////#include <ModelAPI_ResultBody.h>
28 ////#include <ModelAPI_Session.h>
29 ////#include <ModelAPI_Tools.h>
30 ////#include <ModelAPI_Validator.h>
31 ////
32 ////#include <GeomAlgoAPI_CompoundBuilder.h>
33 #include <GeomAlgoAPI_Chamfer.h>
34 #include <GeomAlgoAPI_MakeShape.h>
35 #include <GeomAlgoAPI_Tools.h>
36 ////
37 ////#include <GeomAPI_DataMapOfShapeMapOfShapes.h>
38 #include <GeomAPI_ShapeExplorer.h>
39 ////#include <GeomAPI_ShapeIterator.h>
40 ////
41 ////// Obtain all sub-shapes from the shape and append them to the list
42 ////static void collectSubs(const GeomShapePtr& theShape,
43 ////                              ListOfShape& theSubs,
44 ////                        const GeomAPI_Shape::ShapeType theShapeType)
45 ////{
46 ////  GeomAPI_ShapeExplorer anExp(theShape, theShapeType);
47 ////  for (; anExp.more(); anExp.next()) {
48 ////    GeomShapePtr aShape = anExp.current();
49 ////    // Store all shapes with FORWARD orientation to avoid duplication of shared edges/vertices
50 ////    aShape->setOrientation(GeomAPI_Shape::FORWARD);
51 ////    theSubs.push_back(aShape);
52 ////  }
53 ////}
54
55 // Extract edges from the list
56 static void extractEdgesAndFaces(const ListOfShape& theShapes,
57                                  ListOfShape& theEdges,
58                                  std::map<GeomShapePtr, GeomShapePtr>& theMapEdgeFace)
59 {
60   for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
61     if ((*anIt)->isEdge())
62       theEdges.push_back(*anIt);
63     else {
64       for (GeomAPI_ShapeExplorer anExp(*anIt, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
65         GeomShapePtr aCurrent = anExp.current();
66         theEdges.push_back(aCurrent);
67         theMapEdgeFace[aCurrent] = *anIt;
68       }
69     }
70 }
71
72
73 FeaturesPlugin_Chamfer::FeaturesPlugin_Chamfer()
74 {
75 }
76
77 void FeaturesPlugin_Chamfer::initAttributes()
78 {
79   data()->addAttribute(FeaturesPlugin_Chamfer::CREATION_METHOD(),
80                        ModelAPI_AttributeString::typeId());
81   AttributePtr aSelectionList = data()->addAttribute(FeaturesPlugin_Chamfer::OBJECT_LIST_ID(),
82                        ModelAPI_AttributeSelectionList::typeId());
83   data()->addAttribute(FeaturesPlugin_Chamfer::D1_ID(), ModelAPI_AttributeDouble::typeId());
84   data()->addAttribute(FeaturesPlugin_Chamfer::D2_ID(), ModelAPI_AttributeDouble::typeId());
85   data()->addAttribute(FeaturesPlugin_Chamfer::D_ID(), ModelAPI_AttributeDouble::typeId());
86   data()->addAttribute(FeaturesPlugin_Chamfer::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
87
88   initVersion(aSelectionList);
89 }
90
91 AttributePtr FeaturesPlugin_Chamfer::objectsAttribute()
92 {
93   return attribute(OBJECT_LIST_ID());
94 }
95
96 const std::string& FeaturesPlugin_Chamfer::modifiedShapePrefix() const
97 {
98   static const std::string& THE_PREFIX("Chamfer");
99   return THE_PREFIX;
100 }
101
102 GeomMakeShapePtr FeaturesPlugin_Chamfer::performOperation(const GeomShapePtr& theSolid,
103                                                           const ListOfShape& theEdges)
104 {
105   AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
106   if (!aCreationMethod)
107     return GeomMakeShapePtr();
108
109   bool isDistDist = aCreationMethod->value() == CREATION_METHOD_DISTANCE_DISTANCE();
110   double aD1 = 0.0, aD2 = 0.0, aD = 0.0, anAngle = 0.0;
111   if (isDistDist) {
112     aD1 = real(FeaturesPlugin_Chamfer::D1_ID())->value();
113     aD2 = real(FeaturesPlugin_Chamfer::D2_ID())->value();
114   }
115   else {
116     aD = real(FeaturesPlugin_Chamfer::D_ID())->value();
117     anAngle = real(FeaturesPlugin_Chamfer::ANGLE_ID())->value();
118   }
119
120   // Perform chamfer operation
121   std::shared_ptr<GeomAlgoAPI_Chamfer> aChamferBuilder;
122   std::string anError;
123
124   ListOfShape aChamferEdges;
125   std::map<GeomShapePtr, GeomShapePtr> aMapEdgeFace;
126   extractEdgesAndFaces(theEdges, aChamferEdges, aMapEdgeFace);
127
128   if (isDistDist) {
129     aChamferBuilder.reset(new GeomAlgoAPI_Chamfer(
130         theSolid, aChamferEdges, aMapEdgeFace, true, aD1, aD2));
131   }
132   else {
133     aChamferBuilder.reset(new GeomAlgoAPI_Chamfer(
134         theSolid, aChamferEdges, aMapEdgeFace, false, aD, anAngle));
135   }
136
137   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aChamferBuilder, getKind(), anError)) {
138     setError(anError);
139     return GeomMakeShapePtr();
140   }
141   return aChamferBuilder;
142 }