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