]> SALOME platform Git repositories - modules/shaper.git/blob - src/BuildPlugin/BuildPlugin_Polyline.cpp
Salome HOME
Issue #2559: Add Polyline feature to Build plugin for 3D polyline creation.
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Polyline.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_Polyline.h"
22
23 #include <ModelAPI_AttributeBoolean.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 <GeomAPI_ShapeExplorer.h>
31 #include <GeomAlgoAPI_WireBuilder.h>
32
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_EdgeBuilder.h>
35 #include <GeomAlgoAPI_PointBuilder.h>
36 #include <GeomAlgoAPI_WireBuilder.h>
37
38 #include <algorithm>
39
40 //=================================================================================================
41 BuildPlugin_Polyline::BuildPlugin_Polyline()
42 {
43 }
44
45 //=================================================================================================
46 void BuildPlugin_Polyline::initAttributes()
47 {
48   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
49   data()->addAttribute(CLOSED_ID(), ModelAPI_AttributeBoolean::typeId());
50 }
51
52 //=================================================================================================
53 void BuildPlugin_Polyline::execute()
54 {
55   // Get closed flag value
56   bool isClosed = boolean(CLOSED_ID())->value();
57
58   // Get base objects list.
59   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
60
61   // Collect points.
62   ListOfShape aPoints;
63   std::set<GeomShapePtr> aContexts;
64   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
65     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
66                 GeomShapePtr aShape = aSelection->value();
67
68     ResultPtr aContext = aSelection->context();
69     aContexts.insert(aContext->shape());
70
71     if (!aShape.get()) {
72       aShape = aContext->shape();
73                 }
74
75     aPoints.push_back(aShape);
76         }
77
78   // Prepare a list of edges
79   ListOfShape anEdges;
80
81   ListOfShape::const_iterator aPointsIt = aPoints.begin();
82   for (; aPointsIt != std::prev(aPoints.end()); ++aPointsIt) {
83     std::shared_ptr<GeomAPI_Pnt> aPnt = GeomAlgoAPI_PointBuilder::point(*aPointsIt);
84     std::shared_ptr<GeomAPI_Pnt> aPntNext = GeomAlgoAPI_PointBuilder::point(*std::next(aPointsIt));
85     anEdges.push_back(GeomAlgoAPI_EdgeBuilder::line(aPnt, aPntNext));
86   }
87
88   if (isClosed) {
89     std::shared_ptr<GeomAPI_Pnt> aLastPnt = GeomAlgoAPI_PointBuilder::point(aPoints.back());
90     std::shared_ptr<GeomAPI_Pnt> aFirstPnt = GeomAlgoAPI_PointBuilder::point(aPoints.front());
91     anEdges.push_back(GeomAlgoAPI_EdgeBuilder::line(aLastPnt, aFirstPnt));
92   }
93
94   // Create wire from edges
95   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(anEdges);
96         if (!aWire.get()) {
97                 setError("Error: Result polyline is empty.");
98                 return;
99         }
100
101   // Check the wire.
102   if (GeomAlgoAPI_WireBuilder::isSelfIntersected(aWire)) {
103     setError("Error: Result polyline has self-intersections.");
104     return;
105   }
106
107   // Store result.
108   ResultBodyPtr aResultBody = document()->createBody(data());
109   std::set<GeomShapePtr>::const_iterator aContextIt = aContexts.begin();
110   for (; aContextIt != aContexts.end(); aContextIt++) {
111     aResultBody->storeModified(*aContextIt, aWire, aContextIt == aContexts.begin() ? 0 : -2);
112   }
113
114   aPointsIt = aPoints.cbegin();
115   GeomAPI_ShapeExplorer anExp(aWire, GeomAPI_Shape::EDGE);
116   for (; anExp.more() && aPointsIt != aPoints.cend(); anExp.next(), ++aPointsIt) {
117     GeomShapePtr aPoint = *aPointsIt;
118     GeomShapePtr anEdge = anExp.current();
119     aResultBody->generated(aPoint, anEdge, "Edge", 1);
120   }
121
122   if (!isClosed) {
123     ListOfShape aResPoints;
124     anExp.init(aWire, GeomAPI_Shape::VERTEX);
125     for (; anExp.more(); anExp.next()) {
126       aResPoints.push_back(anExp.current());
127     }
128
129     aResultBody->generated(aResPoints.front(), "FirstVertex", 2);
130     aResultBody->generated(aResPoints.back(), "LastVertex", 3);
131   }
132
133   setResult(aResultBody);
134 }