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_Vertex.h"
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>
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>
36 #include <GeomAPI_ShapeExplorer.h>
38 //=================================================================================================
39 BuildPlugin_Vertex::BuildPlugin_Vertex()
43 //=================================================================================================
44 void BuildPlugin_Vertex::initAttributes()
46 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
48 data()->addAttribute(INTERSECT_ID(), ModelAPI_AttributeBoolean::typeId());
49 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), INTERSECT_ID());
52 void BuildPlugin_Vertex::buildVertices(const ListOfShape& theShapes, bool isIntersect)
55 std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo;
57 aPartitionAlgo.reset(new GeomAlgoAPI_Partition(theShapes, ListOfShape()));
60 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), anError)) {
65 aResult = aPartitionAlgo->shape();
68 aResult = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
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);
80 std::shared_ptr<GeomAlgoAPI_Copy> aCopy(new GeomAlgoAPI_Copy(aVertex));
81 aVertex.reset(new GeomAPI_Vertex(aCopy->shape()));
83 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
85 aMakeShapeList->appendAlgo(aPartitionAlgo);
86 aMakeShapeList->appendAlgo(aCopy);
89 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
90 aResultBody->storeModified(theShapes, aVertex, aMakeShapeList);
91 setResult(aResultBody, aResultIndex);
95 removeResults(aResultIndex);
98 static void collectEdgesAndVertices(AttributeSelectionPtr theSelection, ListOfShape& thePrimitives)
100 FeaturePtr aFeature = theSelection->contextFeature();
101 ResultPtr aContext = theSelection->context();
102 GeomShapePtr aShape = theSelection->value();
104 thePrimitives.push_back(aShape);
106 if (aContext && !aFeature)
107 aFeature = ModelAPI_Feature::feature(aContext);
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());
117 CompositeFeaturePtr aComposite =
118 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
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();
127 for (anIt = aSubResults.begin(); anIt != aSubResults.cend(); ++anIt) {
128 GeomShapePtr aSubResShape = (*anIt)->shape();
129 if (aSubResShape->isVertex())
130 thePrimitives.push_back(aSubResShape);
136 void BuildPlugin_Vertex::execute()
138 // Get base objects list.
139 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
140 if (!aSelectionList.get()) {
141 setError("Error: Could not get selection list.");
144 if (aSelectionList->size() == 0) {
145 setError("Error: Empty selection list.");
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();
155 // Iterate arguments and collect shapes
157 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
158 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
159 collectEdgesAndVertices(aSelection, aShapes);
162 buildVertices(aShapes, isIntersect);