1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "BuildPlugin_Interpolation.h"
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>
28 #include <Events_InfoMessage.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_CurveBuilder.h>
32 #include <GeomAlgoAPI_PointBuilder.h>
34 #include <GeomAPI_Edge.h>
35 #include <GeomAPI_Lin.h>
36 #include <GeomAPI_ShapeExplorer.h>
40 //=================================================================================================
41 BuildPlugin_Interpolation::BuildPlugin_Interpolation()
45 //=================================================================================================
46 void BuildPlugin_Interpolation::initAttributes()
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());
56 //=================================================================================================
57 static GeomDirPtr selectionToDir(const AttributeSelectionPtr& theSelection)
62 GeomShapePtr aShape = theSelection->value();
63 if (!aShape && theSelection->context()) {
64 aShape = theSelection->context()->shape();
67 if (aShape && aShape->isEdge()) {
68 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
71 if (anEdge && anEdge->isLine()) {
72 aDir = anEdge->line()->direction();
78 //=================================================================================================
79 void BuildPlugin_Interpolation::execute()
81 // Get closed flag value
82 bool isClosed = boolean(CLOSED_ID())->value();
84 // Get reorder flag value
85 bool isToReorder = boolean(REORDER_ID())->value();
87 // Get use tangents flag value
88 bool isToUseTangents = isClosed? false : (!string(USE_TANGENTS_ID())->value().empty());
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()));
97 // Get base objects list.
98 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
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);
106 GeomShapePtr aContextShape = aSelection->context()->shape();
107 aContexts.insert(aContextShape);
109 GeomShapePtr aShape = aSelection->value();
111 aShape = aContextShape;
114 GeomPointPtr aPoint = GeomAlgoAPI_PointBuilder::point(aShape);
115 aPoints.push_back(aPoint);
118 // Create curve from points
120 GeomAlgoAPI_CurveBuilder::edge(aPoints, isClosed, isToReorder, aDirStart, aDirEnd);
122 setError("Error: Result curve is empty.");
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());
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);
138 setResult(aResultBody);