1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "BuildPlugin_Interpolation.h"
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>
29 #include <Events_InfoMessage.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_CurveBuilder.h>
33 #include <GeomAlgoAPI_PointBuilder.h>
35 #include <GeomAPI_Edge.h>
36 #include <GeomAPI_Lin.h>
37 #include <GeomAPI_ShapeExplorer.h>
41 //=================================================================================================
42 BuildPlugin_Interpolation::BuildPlugin_Interpolation()
46 //=================================================================================================
47 void BuildPlugin_Interpolation::initAttributes()
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());
57 //=================================================================================================
58 static GeomDirPtr selectionToDir(const AttributeSelectionPtr& theSelection)
63 GeomShapePtr aShape = theSelection->value();
64 if (!aShape && theSelection->context()) {
65 aShape = theSelection->context()->shape();
68 if (aShape && aShape->isEdge()) {
69 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
72 if (anEdge && anEdge->isLine()) {
73 aDir = anEdge->line()->direction();
79 //=================================================================================================
80 void BuildPlugin_Interpolation::execute()
82 // Get closed flag value
83 bool isClosed = boolean(CLOSED_ID())->value();
85 // Get reorder flag value
86 bool isToReorder = boolean(REORDER_ID())->value();
88 // Get use tangents flag value
89 bool isToUseTangents = isClosed? false : (!string(USE_TANGENTS_ID())->value().empty());
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()));
98 // Get base objects list.
99 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
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);
107 GeomShapePtr aContextShape = aSelection->context()->shape();
108 aContexts.insert(aContextShape);
110 GeomShapePtr aShape = aSelection->value();
112 aShape = aContextShape;
115 GeomPointPtr aPoint = GeomAlgoAPI_PointBuilder::point(aShape);
116 aPoints.push_back(aPoint);
119 // Create curve from points
121 GeomAlgoAPI_CurveBuilder::edge(aPoints, isClosed, isToReorder, aDirStart, aDirEnd);
123 setError("Error: Result curve is empty.");
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());
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);
139 setResult(aResultBody);