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_Validators.h"
21 #include "BuildPlugin_Solid.h"
22 #include "BuildPlugin_Face.h"
23 #include "BuildPlugin_Wire.h"
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_ResultConstruction.h>
29 #include <GeomAPI_PlanarEdges.h>
30 #include <GeomAPI_Pln.h>
31 #include <GeomAPI_ShapeExplorer.h>
32 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAlgoAPI_CompoundBuilder.h>
35 #include <GeomAlgoAPI_PaveFiller.h>
36 #include <GeomAlgoAPI_ShapeBuilder.h>
37 #include <GeomAlgoAPI_ShapeTools.h>
38 #include <GeomAlgoAPI_SketchBuilder.h>
39 #include <GeomAlgoAPI_WireBuilder.h>
40 #include <GeomAlgoAPI_MakeVolume.h>
41 #include <GeomAlgoAPI_Tools.h>
43 #include <GeomValidators_FeatureKind.h>
44 #include <GeomValidators_ShapeType.h>
46 #include <BuildPlugin_Interpolation.h>
48 #include <SketchPlugin_Sketch.h>
50 #include <Events_InfoMessage.h>
52 //=================================================================================================
53 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
54 const std::list<std::string>& theArguments,
55 Events_InfoMessage& theError) const
57 // Get base objects list.
58 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
59 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
60 "not support attribute type '%1'\nOnly '%2' is supported.";
61 Events_InfoMessage("BuildPlugin_Validators", aMsg).
62 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
65 AttributeSelectionListPtr aSelectionList =
66 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
67 if(!aSelectionList.get()) {
68 theError = "Could not get selection list.";
71 if(aSelectionList->size() == 0) {
72 theError = "Empty selection list.";
76 // Collect base shapes.
77 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
78 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
79 if(!aSelection.get()) {
80 theError = "Could not get selection.";
83 ResultPtr aContext = aSelection->context();
85 theError = "Attribute have empty context.";
89 GeomShapePtr aShape = aSelection->value();
90 GeomShapePtr aContextShape = aContext->shape();
92 aShape = aContextShape;
95 theError = "Empty shape selected.";
99 // Check that shapes has acceptable type.
100 GeomValidators_ShapeType aValidatorShapeType;
101 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
105 // Check that it is shape on sketch.
106 ResultConstructionPtr aConstruction =
107 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
108 if(aConstruction.get()) {
109 if(aConstruction->isInfinite()) {
110 theError = "Infinite objects not acceptable.";
119 //=================================================================================================
120 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
121 const std::list<std::string>& theArguments,
122 Events_InfoMessage& theError) const
125 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
126 if(!aSelectionList.get()) {
127 theError = "Empty attribute \"%1\".";
128 theError.arg(theArguments.front());
132 if (theFeature->getKind() == BuildPlugin_Wire::ID()) {
133 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
134 /// Wire builder: wires and edges selected
135 std::set<int> aRemove;
136 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
137 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
138 GeomShapePtr aShape = aSelection->value();
140 GeomAPI_Shape::ShapeType aType = aShape->shapeType();
141 if (aType == GeomAPI_Shape::WIRE) {
143 GeomAPI_ShapeExplorer anEdgeExp(aShape, GeomAPI_Shape::EDGE);
144 for (; anEdgeExp.more(); anEdgeExp.next()) {
145 GeomShapePtr aEdge = anEdgeExp.current();
146 for (int i = 0; i < aSelectionList->size(); ++i) {
147 AttributeSelectionPtr aSel = aSelectionList->value(i);
148 GeomShapePtr aShp = aSel->value();
150 if (aShp->shapeType() == GeomAPI_Shape::EDGE) {
151 if (aShp->isEqual(aEdge) || aShp->isSameGeometry(aEdge))
156 aRemove.insert(anIndex);
163 if (aRemove.size() > 0)
164 aSelectionList->remove(aRemove);
167 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::shapeTypeByStr(theArguments.back());
168 // Collect base shapes.
169 ListOfShape aListOfShapes;
170 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
171 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
172 GeomShapePtr aShape = aSelection->value();
173 ResultPtr aContext = aSelection->context();
174 if (!aShape.get() && aContext.get())
175 aShape = aContext->shape();
177 bool isProper = aShape.get() &&
178 (aShape->shapeType() == GeomAPI_Shape::EDGE || aShape->shapeType() == aShapeType);
181 aListOfShapes.push_back(aShape);
184 FeaturePtr aFeature = aSelection->contextFeature();
185 if (!aFeature.get()) {
186 GeomShapePtr aValue = aSelection->value();
187 // whole sketch is allowed only
188 if (aContext.get() && !aValue.get()) {
189 aFeature = ModelAPI_Feature::feature(aContext);
193 if (!aFeature.get()) {
194 theError = "Error: Incorrect selection.";
198 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
199 theError = "Error: %1 shape is not allowed for selection.";
200 theError.arg(aFeature->getKind());
206 if (aShapeType == GeomAPI_Shape::WIRE) {
208 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
209 if (!aWire.get() && !aListOfShapes.empty()) {
210 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
218 //=================================================================================================
219 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
220 const std::list<std::string>& theArguments,
221 Events_InfoMessage& theError) const
224 if(theArguments.size() != 1) {
225 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
226 "1 parameter (ID of base objects list).";
227 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
230 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
231 if(!aSelectionList.get()) {
232 theError = "Empty attribute \"%1\".";
233 theError.arg(theArguments.front());
237 if (theFeature->getKind() == BuildPlugin_Face::ID()) {
238 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
239 /// - Face builder: edges, faces and wires selected
240 /// --> remove edges and wires
241 std::set<int> aRemove;
242 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
243 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
244 GeomShapePtr aShape = aSelection->value();
246 GeomAPI_Shape::ShapeType aType = aShape->shapeType();
247 if (aType == GeomAPI_Shape::FACE) {
249 GeomAPI_ShapeExplorer anWireExp(aShape, GeomAPI_Shape::WIRE);
250 for (; anWireExp.more(); anWireExp.next()) {
251 GeomShapePtr aWire = anWireExp.current();
252 for (int i = 0; i < aSelectionList->size(); ++i) {
253 AttributeSelectionPtr aSel = aSelectionList->value(i);
254 GeomShapePtr aShp = aSel->value();
256 if (aShp->shapeType() == GeomAPI_Shape::WIRE) {
257 if (aShp->isEqual(aWire) || aShp->isSameGeometry(aWire))
262 aRemove.insert(anIndex);
268 GeomAPI_ShapeExplorer anEdgeExp(aShape, GeomAPI_Shape::EDGE);
269 for (; anEdgeExp.more(); anEdgeExp.next()) {
270 GeomShapePtr aEdge = anEdgeExp.current();
271 for (int i = 0; i < aSelectionList->size(); ++i) {
272 AttributeSelectionPtr aSel = aSelectionList->value(i);
273 GeomShapePtr aShp = aSel->value();
275 if (aShp->shapeType() == GeomAPI_Shape::EDGE) {
276 if (aShp->isEqual(aEdge) || aShp->isSameGeometry(aEdge))
281 aRemove.insert(anIndex);
288 if (aRemove.size() > 0)
289 aSelectionList->remove(aRemove);
291 bool hasEdgesOrWires = false;
292 bool hasFaces = false;
294 // Collect base shapes.
296 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
297 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
298 GeomShapePtr aShape = aSelection->value();
300 if (!aSelection->context()) {
301 theError = "Objects are not selected.";
304 aShape = aSelection->context()->shape();
306 ResultConstructionPtr aSketchRes =
307 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
309 if (aShape->shapeType() == GeomAPI_Shape::FACE ||
310 (!aSelection->value() && aSketchRes && aSketchRes->facesNum() > 0)) {
311 // skip faces exploding
316 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
317 hasEdgesOrWires = true;
318 GeomShapePtr anEdge = anExp.current();
319 anEdges.push_back(anEdge);
323 if (hasFaces && hasEdgesOrWires) {
324 theError = "Faces and edges/wires should be selected together.";
326 } else if (hasEdgesOrWires && anEdges.empty()) {
327 theError = "Objects are not selected.";
331 // Check that edges does not have intersections.
332 if(anEdges.size() > 1) {
333 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
334 if(!aPaveFiller.isDone()) {
335 theError = "Error while checking if edges intersects.";
338 GeomShapePtr aSectedEdges = aPaveFiller.shape();
340 size_t anEdgesNum = 0;
341 for(GeomAPI_ShapeExplorer
342 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
345 if(anEdgesNum != anEdges.size()) {
346 theError = "Selected objects have intersections.";
351 if (!anEdges.empty()) {
352 // Check that they are planar.
353 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
355 theError = "Selected object(s) should belong to only one plane.";
359 // Check that selected objects have closed contours.
360 GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
361 const ListOfShape& aFaces = aBuilder.faces();
363 theError = "Selected objects do not generate closed contour.";
371 //=================================================================================================
372 bool BuildPlugin_ValidatorBaseForSolids::isValid(
373 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
374 Events_InfoMessage& theError) const
376 // Get base objects list.
377 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
378 if (!aSelectionList.get()) {
379 theError = "Could not get selection list.";
382 if (aSelectionList->size() == 0) {
383 theError = "Empty selection list.";
387 if (theFeature->getKind() == BuildPlugin_Solid::ID()) {
388 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
389 /// Solid builder: faces and shapes shells or solids seleted
392 std::set<int> aRemove;
393 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
394 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
395 GeomShapePtr aShape = aSelection->value();
397 GeomAPI_Shape::ShapeType aType = aShape->shapeType();
398 if ((aType == GeomAPI_Shape::SHAPE) ||
399 (aType == GeomAPI_Shape::SOLID) ||
400 (aType == GeomAPI_Shape::SHELL)) {
402 GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::FACE);
403 for (; anExp.more(); anExp.next()) {
404 GeomShapePtr aFace = anExp.current();
405 for (int i = 0; i < aSelectionList->size(); ++i) {
406 AttributeSelectionPtr aSel = aSelectionList->value(i);
407 GeomShapePtr aShp = aSel->value();
409 if (aShp->shapeType() == GeomAPI_Shape::FACE) {
410 if (aShp->isEqual(aFace))
415 aRemove.insert(anIndex);
422 if (aRemove.size() > 0)
423 aSelectionList->remove(aRemove);
426 // Collect base shapes.
427 ListOfShape anOriginalShapes;
428 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
429 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
430 if (!aSelection->context().get()) {
431 theError = "Invalid selection.";
434 GeomShapePtr aShape = aSelection->value();
436 aShape = aSelection->context()->shape();
437 anOriginalShapes.push_back(aShape);
440 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
441 new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
444 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
449 // set of allowed types of results
450 std::set<GeomAPI_Shape::ShapeType> aResultType;
451 std::string aType = theArguments.back();
452 if (aType == "solid")
453 aResultType.insert(GeomAPI_Shape::SOLID);
454 else if (aType == "compsolid") {
455 aResultType.insert(GeomAPI_Shape::COMPSOLID);
456 aResultType.insert(GeomAPI_Shape::SOLID);
459 GeomShapePtr aCompound = anAlgorithm->shape();
460 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
461 GeomAPI_ShapeIterator anIt(aCompound);
462 GeomShapePtr aFoundSub;
463 for (; anIt.more() && !aFoundSub; anIt.next()) {
464 aFoundSub = anIt.current();
465 if (aResultType.count(aFoundSub->shapeType()) == 0) {
466 theError = "Unable to build a solid";
470 if (anIt.more() || !aFoundSub.get()) {
471 theError = "Unable to build a solid";
474 } else if (aResultType.count(aCompound->shapeType()) == 0) {
475 theError = "Unable to build a solid";
478 // check the internal faces presence
479 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
480 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
481 theError = "Internal faces are not allowed in the resulting solid";
490 //=================================================================================================
491 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
492 const std::list<std::string>& theArguments,
493 Events_InfoMessage& theError) const
495 if(theArguments.size() != 1) {
497 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
498 "1 parameter(Sketch feature id).";
499 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
504 // Get base objects list.
505 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
508 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
509 "%1\"\n Only \"%2\" supported.";
510 Events_InfoMessage("BuildPlugin_Validators", aMsg).
511 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
515 AttributeSelectionListPtr aSelectionList =
516 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
517 if(!aSelectionList.get()) {
518 theError = "Could not get selection list.";
523 const std::string aBaseShapeId = "base_shape";
524 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
525 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
527 if(!aShapeAttrSelection.get()) {
528 theError = "Base shape is empty.";
532 ResultPtr aBaseContext = aShapeAttrSelection->context();
534 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
535 if(!aBaseShape.get()) {
536 theError = "Base shape is empty.";
540 GeomAlgoAPI_ShapeBuilder aBuilder;
541 aBuilder.removeInternal(aBaseShape);
542 aBaseShape = aBuilder.shape();
544 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
545 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
546 switch(aBaseShape->shapeType()) {
547 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
548 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
552 // Check selected shapes.
553 GeomValidators_FeatureKind aFeatureKindValidator;
554 std::list<std::string> anArguments;
555 anArguments.push_back(theArguments.front());
556 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
557 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
558 if(!aSelectionAttrInList.get()) {
559 theError = "Empty attribute in list.";
563 // If context of selection same skip.
564 if(aBaseContext == aSelectionAttrInList->context()) {
568 // Check that it is a selection on Sketch.
569 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
574 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
575 if(!aShapeInList.get()) {
576 aShapeInList = aSelectionAttrInList->context()->shape();
578 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
579 theError = "Selected shape has unacceptable type.";
583 // Check that shape inside wire or face.
584 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
585 theError = "Selected shape is not inside base face.";
594 //=================================================================================================
595 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
596 const std::list<std::string>& /*theArguments*/,
597 Events_InfoMessage& theError) const
599 // Get base objects list.
600 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
603 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
604 "%1\"\n Only \"%2\" supported.";
605 Events_InfoMessage("BuildPlugin_Validators", aMsg).
606 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
610 AttributeSelectionListPtr aSelectionList =
611 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
612 if (!aSelectionList.get()) {
613 theError = "Could not get selection list.";
617 //FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
619 // Check selected shapes.
620 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
621 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
622 if (!aSelectionAttrInList.get()) {
623 theError = "Empty attribute in list.";
627 // Check shape exists.
628 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
629 if (!aShapeInList.get()) {
630 theError = "Object has no shape";
635 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
636 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
637 theError = "Incorrect objects selected";
646 //=================================================================================================
647 bool BuildPlugin_ValidatorBaseForVertex::isValid(const AttributePtr& theAttribute,
648 const std::list<std::string>& /*theArguments*/,
649 Events_InfoMessage& theError) const
651 if (!theAttribute.get()) {
652 theError = "Error: empty selection.";
656 AttributeSelectionListPtr aSelectionList =
657 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
658 if (!aSelectionList.get()) {
659 theError = "Could not get selection list.";
663 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
664 AttributeSelectionPtr aSelectionAttr = aSelectionList->value(anIndex);
665 if (!aSelectionAttr.get()) {
666 theError = "Empty attribute in list.";
671 bool isVertex = false;
672 GeomShapePtr aShape = aSelectionAttr->value();
673 ResultPtr aContext = aSelectionAttr->context();
674 if (!aShape.get() && aContext.get())
675 aShape = aContext->shape();
677 isVertex = (aShape->shapeType() == GeomAPI_Shape::VERTEX);
681 FeaturePtr aFeature = aSelectionAttr->contextFeature();
682 if (!aFeature.get()) {
683 GeomShapePtr aValue = aSelectionAttr->value();
684 // whole sketch is allowed only
685 if (aContext.get() && !aValue.get()) {
686 aFeature = ModelAPI_Feature::feature(aContext);
690 if (!aFeature.get()) {
691 theError = "Error: Incorrect selection.";
695 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
696 theError = "Error: %1 shape is not allowed for selection.";
697 theError.arg(aFeature->getKind());
706 //=================================================================================================
707 bool BuildPlugin_ValidatorExpressionInterpolation::isValid(const AttributePtr& theAttribute,
708 const std::list<std::string>& /*theArguments*/,
709 Events_InfoMessage& theError) const
711 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
713 AttributeStringPtr aStrAttr =
714 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
715 if (!aStrAttr->isInitialized()) {
716 theError = "Attribute \"%1\" is not initialized.";
717 theError.arg(aStrAttr->id());
720 bool isEmptyExpr = aStrAttr->value().empty();
722 theError = "Expression is empty.";
726 theError = aFeature->string(BuildPlugin_Interpolation::EXPRESSION_ERROR_ID())->value();
727 return theError.empty();