Salome HOME
5cf8d10df311627439ff4b13fc296d511e5e1f43
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Vertex.cpp
1 // Copyright (C) 2014-2021  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_Vertex.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_CompositeFeature.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Session.h>
28
29 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <GeomAlgoAPI_Copy.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_Tools.h>
33 #include <GeomAlgoAPI_Partition.h>
34 #include <GeomAlgoAPI_ShapeTools.h>
35
36 #include <GeomAPI_ShapeExplorer.h>
37
38 //=================================================================================================
39 BuildPlugin_Vertex::BuildPlugin_Vertex()
40 {
41 }
42
43 //=================================================================================================
44 void BuildPlugin_Vertex::initAttributes()
45 {
46   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
47
48   data()->addAttribute(INTERSECT_ID(), ModelAPI_AttributeBoolean::typeId());
49   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), INTERSECT_ID());
50 }
51
52 void BuildPlugin_Vertex::buildVertices(const ListOfShape& theShapes, bool isIntersect)
53 {
54   GeomShapePtr aResult;
55   std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo;
56   if (isIntersect) {
57     aPartitionAlgo.reset(new GeomAlgoAPI_Partition(theShapes, ListOfShape()));
58
59     std::string anError;
60     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), anError)) {
61       setError(anError);
62       return;
63     }
64
65     aResult = aPartitionAlgo->shape();
66   }
67   else
68     aResult = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
69
70   int aResultIndex = 0;
71
72   // Explode on vertices
73   std::set<GeomVertexPtr, GeomAPI_Vertex::GeometricComparator> aProcessed;
74   for (GeomAPI_ShapeExplorer anExp(aResult, GeomAPI_Shape::VERTEX); anExp.more(); anExp.next()) {
75     GeomVertexPtr aVertex(new GeomAPI_Vertex(anExp.current()));
76     if (aProcessed.find(aVertex) != aProcessed.end())
77       continue; // vertex is already processed
78     aProcessed.insert(aVertex);
79
80     std::shared_ptr<GeomAlgoAPI_Copy> aCopy(new GeomAlgoAPI_Copy(aVertex));
81     aVertex.reset(new GeomAPI_Vertex(aCopy->shape()));
82
83     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
84     if (aPartitionAlgo)
85       aMakeShapeList->appendAlgo(aPartitionAlgo);
86     aMakeShapeList->appendAlgo(aCopy);
87
88     // Store result.
89     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
90     aResultBody->storeModified(theShapes, aVertex, aMakeShapeList);
91     setResult(aResultBody, aResultIndex);
92     ++aResultIndex;
93   }
94
95   removeResults(aResultIndex);
96 }
97
98 static void collectEdgesAndVertices(AttributeSelectionPtr theSelection, ListOfShape& thePrimitives)
99 {
100   FeaturePtr aFeature = theSelection->contextFeature();
101   ResultPtr aContext = theSelection->context();
102   GeomShapePtr aShape = theSelection->value();
103   if (aShape)
104     thePrimitives.push_back(aShape);
105   else {
106     if (aContext && !aFeature)
107       aFeature = ModelAPI_Feature::feature(aContext);
108     if (!aFeature)
109       return;
110
111     // process results of the feature
112     const std::list<ResultPtr>& aResults = aFeature->results();
113     std::list<ResultPtr>::const_iterator anIt = aResults.begin();
114     for (; anIt != aResults.end(); ++anIt)
115       thePrimitives.push_back((*anIt)->shape());
116
117     CompositeFeaturePtr aComposite =
118         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
119     if (!aComposite)
120       return;
121
122     // add construction points (centers of circles, etc.)
123     for (int i = 0, nbSubs = aComposite->numberOfSubs(); i < nbSubs; ++i) {
124       FeaturePtr aSubFeature = aComposite->subFeature(i);
125       const std::list<ResultPtr>& aSubResults = aSubFeature->results();
126       // find all points
127       for (anIt = aSubResults.begin(); anIt != aSubResults.cend(); ++anIt) {
128         GeomShapePtr aSubResShape = (*anIt)->shape();
129         if (aSubResShape->isVertex())
130           thePrimitives.push_back(aSubResShape);
131       }
132     }
133   }
134 }
135
136 void BuildPlugin_Vertex::execute()
137 {
138   // Get base objects list.
139   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
140   if (!aSelectionList.get()) {
141     setError("Error: Could not get selection list.");
142     return;
143   }
144   if (aSelectionList->size() == 0) {
145     setError("Error: Empty selection list.");
146     return;
147   }
148
149   // Get "Compute intersections" flag value
150   bool isIntersect = false;
151   if (boolean(INTERSECT_ID()).get() && boolean(INTERSECT_ID())->isInitialized()) {
152     isIntersect = boolean(INTERSECT_ID())->value();
153   }
154
155   // Iterate arguments and collect shapes
156   ListOfShape aShapes;
157   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
158     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
159     collectEdgesAndVertices(aSelection, aShapes);
160   }
161
162   buildVertices(aShapes, isIntersect);
163 }