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