1 // Copyright (C) 2014-2019 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_Validators.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultConstruction.h>
25 #include <GeomAPI_PlanarEdges.h>
26 #include <GeomAPI_Pln.h>
27 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAPI_ShapeIterator.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeBuilder.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_SketchBuilder.h>
35 #include <GeomAlgoAPI_WireBuilder.h>
36 #include <GeomAlgoAPI_MakeVolume.h>
37 #include <GeomAlgoAPI_Tools.h>
39 #include <GeomValidators_FeatureKind.h>
40 #include <GeomValidators_ShapeType.h>
42 #include <Events_InfoMessage.h>
44 //=================================================================================================
45 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
46 const std::list<std::string>& theArguments,
47 Events_InfoMessage& theError) const
49 // Get base objects list.
50 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
51 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
52 "not support attribute type '%1'\nOnly '%2' is supported.";
53 Events_InfoMessage("BuildPlugin_Validators", aMsg).
54 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
57 AttributeSelectionListPtr aSelectionList =
58 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
59 if(!aSelectionList.get()) {
60 theError = "Could not get selection list.";
63 if(aSelectionList->size() == 0) {
64 theError = "Empty selection list.";
68 // Collect base shapes.
69 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
70 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
71 if(!aSelection.get()) {
72 theError = "Could not get selection.";
75 ResultPtr aContext = aSelection->context();
77 theError = "Attribute have empty context.";
81 GeomShapePtr aShape = aSelection->value();
82 GeomShapePtr aContextShape = aContext->shape();
84 aShape = aContextShape;
87 theError = "Empty shape selected.";
91 // Check that shapes has acceptable type.
92 GeomValidators_ShapeType aValidatorShapeType;
93 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
97 // Check that it is shape on sketch.
98 ResultConstructionPtr aConstruction =
99 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
100 if(aConstruction.get()) {
101 if(aConstruction->isInfinite()) {
102 theError = "Infinite objects not acceptable.";
106 std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
107 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
109 if(aShape->isEqual(aContextShape)) {
110 // It is whole sketch.
122 //=================================================================================================
123 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
124 const std::list<std::string>& theArguments,
125 Events_InfoMessage& theError) const
128 if(theArguments.size() != 1) {
129 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
130 "with 1 parameter (ID of base objects list).";
131 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
134 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
135 if(!aSelectionList.get()) {
136 theError = "Empty attribute \"%1\".";
137 theError.arg(theArguments.front());
142 // Collect base shapes.
143 ListOfShape aListOfShapes;
144 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
145 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
146 GeomShapePtr aShape = aSelection->value();
148 if (aSelection->context().get())
149 aShape = aSelection->context()->shape();
152 aListOfShapes.push_back(aShape);
156 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
158 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
165 //=================================================================================================
166 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
167 const std::list<std::string>& theArguments,
168 Events_InfoMessage& theError) const
171 if(theArguments.size() != 1) {
172 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
173 "1 parameter (ID of base objects list).";
174 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
177 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
178 if(!aSelectionList.get()) {
179 theError = "Empty attribute \"%1\".";
180 theError.arg(theArguments.front());
184 bool hasEdgesOrWires = false;
185 bool hasFaces = false;
187 // Collect base shapes.
189 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
190 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
191 GeomShapePtr aShape = aSelection->value();
193 if (!aSelection->context()) {
194 theError = "Objects are not selected.";
197 aShape = aSelection->context()->shape();
199 if (aShape->shapeType() == GeomAPI_Shape::FACE) {
200 // skip faces exploding
205 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
206 hasEdgesOrWires = true;
207 GeomShapePtr anEdge = anExp.current();
208 anEdges.push_back(anEdge);
212 if (hasFaces && hasEdgesOrWires) {
213 theError = "Faces and edges/wires should be selected together.";
215 } else if (hasEdgesOrWires && anEdges.empty()) {
216 theError = "Objects are not selected.";
220 // Check that edges does not have intersections.
221 if(anEdges.size() > 1) {
222 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
223 if(!aPaveFiller.isDone()) {
224 theError = "Error while checking if edges intersects.";
227 GeomShapePtr aSectedEdges = aPaveFiller.shape();
230 for(GeomAPI_ShapeExplorer
231 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
234 if(anEdgesNum != anEdges.size()) {
235 theError = "Selected objects have intersections.";
240 if (!anEdges.empty()) {
241 // Check that they are planar.
242 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
244 theError = "Selected object(s) should belong to only one plane.";
248 // Check that selected objects have closed contours.
249 GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
250 const ListOfShape& aFaces = aBuilder.faces();
252 theError = "Selected objects do not generate closed contour.";
260 //=================================================================================================
261 bool BuildPlugin_ValidatorBaseForSolids::isValid(
262 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
263 Events_InfoMessage& theError) const
265 // Get base objects list.
266 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
267 if (!aSelectionList.get()) {
268 theError = "Could not get selection list.";
271 if (aSelectionList->size() == 0) {
272 theError = "Empty selection list.";
276 // Collect base shapes.
277 ListOfShape anOriginalShapes;
278 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
279 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
280 if (!aSelection->context().get()) {
281 theError = "Invalid selection.";
284 GeomShapePtr aShape = aSelection->value();
286 aShape = aSelection->context()->shape();
287 anOriginalShapes.push_back(aShape);
290 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
291 new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
294 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
299 // set of allowed types of results
300 std::set<GeomAPI_Shape::ShapeType> aResultType;
301 std::string aType = theArguments.back();
302 if (aType == "solid")
303 aResultType.insert(GeomAPI_Shape::SOLID);
304 else if (aType == "compsolid") {
305 aResultType.insert(GeomAPI_Shape::COMPSOLID);
306 aResultType.insert(GeomAPI_Shape::SOLID);
309 GeomShapePtr aCompound = anAlgorithm->shape();
310 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
311 GeomAPI_ShapeIterator anIt(aCompound);
312 GeomShapePtr aFoundSub;
313 for (; anIt.more() && !aFoundSub; anIt.next()) {
314 aFoundSub = anIt.current();
315 if (aResultType.count(aFoundSub->shapeType()) == 0) {
316 theError = "Unable to build a solid";
320 if (anIt.more() || !aFoundSub.get()) {
321 theError = "Unable to build a solid";
324 } else if (aResultType.count(aCompound->shapeType()) == 0) {
325 theError = "Unable to build a solid";
328 // check the internal faces presence
329 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
330 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
331 theError = "Internal faces are not allowed in the resulting solid";
340 //=================================================================================================
341 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
342 const std::list<std::string>& theArguments,
343 Events_InfoMessage& theError) const
345 if(theArguments.size() != 1) {
347 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
348 "1 parameter(Sketch feature id).";
349 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
354 // Get base objects list.
355 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
358 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
359 "%1\"\n Only \"%2\" supported.";
360 Events_InfoMessage("BuildPlugin_Validators", aMsg).
361 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
365 AttributeSelectionListPtr aSelectionList =
366 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
367 if(!aSelectionList.get()) {
368 theError = "Could not get selection list.";
373 const std::string aBaseShapeId = "base_shape";
374 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
375 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
377 if(!aShapeAttrSelection.get()) {
378 theError = "Base shape is empty.";
382 ResultPtr aBaseContext = aShapeAttrSelection->context();
384 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
385 if(!aBaseShape.get()) {
386 theError = "Base shape is empty.";
390 GeomAlgoAPI_ShapeBuilder aBuilder;
391 aBuilder.removeInternal(aBaseShape);
392 aBaseShape = aBuilder.shape();
394 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
395 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
396 switch(aBaseShape->shapeType()) {
397 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
398 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
402 // Check selected shapes.
403 GeomValidators_FeatureKind aFeatureKindValidator;
404 std::list<std::string> anArguments;
405 anArguments.push_back(theArguments.front());
406 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
407 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
408 if(!aSelectionAttrInList.get()) {
409 theError = "Empty attribute in list.";
413 // If context of selection same skip.
414 if(aBaseContext == aSelectionAttrInList->context()) {
418 // Check that it is a selection on Sketch.
419 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
424 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
425 if(!aShapeInList.get()) {
426 aShapeInList = aSelectionAttrInList->context()->shape();
428 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
429 theError = "Selected shape has unacceptable type.";
433 // Check that shape inside wire or face.
434 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
435 theError = "Selected shape is not inside base face.";
444 //=================================================================================================
445 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
446 const std::list<std::string>& theArguments,
447 Events_InfoMessage& theError) const
449 // Get base objects list.
450 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
453 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
454 "%1\"\n Only \"%2\" supported.";
455 Events_InfoMessage("BuildPlugin_Validators", aMsg).
456 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
460 AttributeSelectionListPtr aSelectionList =
461 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
462 if (!aSelectionList.get()) {
463 theError = "Could not get selection list.";
467 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
469 // Check selected shapes.
470 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
471 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
472 if (!aSelectionAttrInList.get()) {
473 theError = "Empty attribute in list.";
477 // Check shape exists.
478 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
479 if (!aShapeInList.get()) {
480 theError = "Object has no shape";
485 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
486 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
487 theError = "Incorrect objects selected";