Salome HOME
Update copyrights
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Interpolation.cpp
1 // Copyright (C) 2014-2019  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 "BuildPlugin_Interpolation.h"
21
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultConstruction.h>
27
28 #include <Events_InfoMessage.h>
29
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_CurveBuilder.h>
32 #include <GeomAlgoAPI_PointBuilder.h>
33
34 #include <GeomAPI_Edge.h>
35 #include <GeomAPI_Lin.h>
36 #include <GeomAPI_ShapeExplorer.h>
37
38 #include <algorithm>
39
40 //=================================================================================================
41 BuildPlugin_Interpolation::BuildPlugin_Interpolation()
42 {
43 }
44
45 //=================================================================================================
46 void BuildPlugin_Interpolation::initAttributes()
47 {
48   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
49   data()->addAttribute(CLOSED_ID(), ModelAPI_AttributeBoolean::typeId());
50   data()->addAttribute(REORDER_ID(), ModelAPI_AttributeBoolean::typeId());
51   data()->addAttribute(USE_TANGENTS_ID(), ModelAPI_AttributeString::typeId());
52   data()->addAttribute(TANGENT_START_ID(), ModelAPI_AttributeSelection::typeId());
53   data()->addAttribute(TANGENT_END_ID(), ModelAPI_AttributeSelection::typeId());
54 }
55
56 //=================================================================================================
57 static GeomDirPtr selectionToDir(const AttributeSelectionPtr& theSelection)
58 {
59   GeomDirPtr aDir;
60   GeomEdgePtr anEdge;
61
62   GeomShapePtr aShape = theSelection->value();
63   if (!aShape && theSelection->context()) {
64     aShape = theSelection->context()->shape();
65   }
66
67   if (aShape && aShape->isEdge()) {
68     anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
69   }
70
71   if (anEdge && anEdge->isLine()) {
72     aDir = anEdge->line()->direction();
73   }
74
75   return aDir;
76 }
77
78 //=================================================================================================
79 void BuildPlugin_Interpolation::execute()
80 {
81   // Get closed flag value
82   bool isClosed = boolean(CLOSED_ID())->value();
83
84   // Get reorder flag value
85   bool isToReorder = boolean(REORDER_ID())->value();
86
87   // Get use tangents flag value
88   bool isToUseTangents = isClosed? false : (!string(USE_TANGENTS_ID())->value().empty());
89
90   // Get tangent for start and end points
91   GeomDirPtr aDirStart, aDirEnd;
92   if (isToUseTangents) {
93     aDirStart = selectionToDir(selection(TANGENT_START_ID()));
94     aDirEnd = selectionToDir(selection(TANGENT_END_ID()));
95   }
96
97   // Get base objects list.
98   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
99
100   // Collect points.
101   std::list<GeomPointPtr> aPoints;
102   std::set<GeomShapePtr> aContexts;
103   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
104     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
105
106     GeomShapePtr aContextShape = aSelection->context()->shape();
107     aContexts.insert(aContextShape);
108
109                 GeomShapePtr aShape = aSelection->value();
110     if (!aShape.get()) {
111       aShape = aContextShape;
112                 }
113
114     GeomPointPtr aPoint = GeomAlgoAPI_PointBuilder::point(aShape);
115     aPoints.push_back(aPoint);
116         }
117
118   // Create curve from points
119   GeomEdgePtr anEdge =
120     GeomAlgoAPI_CurveBuilder::edge(aPoints, isClosed, isToReorder, aDirStart, aDirEnd);
121   if (!anEdge.get()) {
122     setError("Error: Result curve is empty.");
123     return;
124   }
125
126   // Store result.
127   ResultBodyPtr aResultBody = document()->createBody(data());
128   std::set<GeomShapePtr>::const_iterator aContextIt = aContexts.begin();
129   for (; aContextIt != aContexts.end(); aContextIt++) {
130     aResultBody->storeModified(*aContextIt, anEdge, aContextIt == aContexts.begin());
131   }
132   int aVertexIndex = 1;
133   for (GeomAPI_ShapeExplorer anExp(anEdge, GeomAPI_Shape::VERTEX); anExp.more(); anExp.next()) {
134     std::string aVertexName = "Vertex_" + std::to_string((long long)aVertexIndex);
135     aResultBody->generated(anExp.current(), aVertexName);
136   }
137
138   setResult(aResultBody);
139 }