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_ResultConstruction.h>
28 #include <GeomAPI_PlanarEdges.h>
29 #include <GeomAPI_Pln.h>
30 #include <GeomAPI_ShapeExplorer.h>
31 #include <GeomAPI_ShapeIterator.h>
33 #include <GeomAlgoAPI_CompoundBuilder.h>
34 #include <GeomAlgoAPI_PaveFiller.h>
35 #include <GeomAlgoAPI_ShapeBuilder.h>
36 #include <GeomAlgoAPI_ShapeTools.h>
37 #include <GeomAlgoAPI_SketchBuilder.h>
38 #include <GeomAlgoAPI_WireBuilder.h>
39 #include <GeomAlgoAPI_MakeVolume.h>
40 #include <GeomAlgoAPI_Tools.h>
42 #include <GeomValidators_FeatureKind.h>
43 #include <GeomValidators_ShapeType.h>
45 #include <SketchPlugin_Sketch.h>
47 #include <Events_InfoMessage.h>
49 //=================================================================================================
50 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
51 const std::list<std::string>& theArguments,
52 Events_InfoMessage& theError) const
54 // Get base objects list.
55 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
56 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
57 "not support attribute type '%1'\nOnly '%2' is supported.";
58 Events_InfoMessage("BuildPlugin_Validators", aMsg).
59 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
62 AttributeSelectionListPtr aSelectionList =
63 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
64 if(!aSelectionList.get()) {
65 theError = "Could not get selection list.";
68 if(aSelectionList->size() == 0) {
69 theError = "Empty selection list.";
73 // Collect base shapes.
74 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
75 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
76 if(!aSelection.get()) {
77 theError = "Could not get selection.";
80 ResultPtr aContext = aSelection->context();
82 theError = "Attribute have empty context.";
86 GeomShapePtr aShape = aSelection->value();
87 GeomShapePtr aContextShape = aContext->shape();
89 aShape = aContextShape;
92 theError = "Empty shape selected.";
96 // Check that shapes has acceptable type.
97 GeomValidators_ShapeType aValidatorShapeType;
98 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
102 // Check that it is shape on sketch.
103 ResultConstructionPtr aConstruction =
104 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
105 if(aConstruction.get()) {
106 if(aConstruction->isInfinite()) {
107 theError = "Infinite objects not acceptable.";
116 //=================================================================================================
117 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
118 const std::list<std::string>& theArguments,
119 Events_InfoMessage& theError) const
122 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
123 if(!aSelectionList.get()) {
124 theError = "Empty attribute \"%1\".";
125 theError.arg(theArguments.front());
129 if (theFeature->getKind() == BuildPlugin_Wire::ID()) {
130 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
131 /// Wire builder: wires and edges selected
132 std::set<int> aRemove;
133 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
134 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
135 GeomShapePtr aShape = aSelection->value();
137 GeomAPI_Shape::ShapeType aType = aShape->shapeType();
138 if (aType == GeomAPI_Shape::WIRE) {
140 GeomAPI_ShapeExplorer anEdgeExp(aShape, GeomAPI_Shape::EDGE);
141 for (; anEdgeExp.more(); anEdgeExp.next()) {
142 GeomShapePtr aEdge = anEdgeExp.current();
143 for (int i = 0; i < aSelectionList->size(); ++i) {
144 AttributeSelectionPtr aSel = aSelectionList->value(i);
145 GeomShapePtr aShp = aSel->value();
147 if (aShp->shapeType() == GeomAPI_Shape::EDGE) {
148 if (aShp->isEqual(aEdge) || aShp->isSameGeometry(aEdge))
153 aRemove.insert(anIndex);
160 if (aRemove.size() > 0)
161 aSelectionList->remove(aRemove);
164 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::shapeTypeByStr(theArguments.back());
165 // Collect base shapes.
166 ListOfShape aListOfShapes;
167 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
168 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
169 GeomShapePtr aShape = aSelection->value();
170 ResultPtr aContext = aSelection->context();
171 if (!aShape.get() && aContext.get())
172 aShape = aContext->shape();
174 bool isProper = aShape.get() &&
175 (aShape->shapeType() == GeomAPI_Shape::EDGE || aShape->shapeType() == aShapeType);
178 aListOfShapes.push_back(aShape);
181 FeaturePtr aFeature = aSelection->contextFeature();
182 if (!aFeature.get()) {
183 GeomShapePtr aValue = aSelection->value();
184 // whole sketch is allowed only
185 if (aContext.get() && !aValue.get()) {
186 aFeature = ModelAPI_Feature::feature(aContext);
190 if (!aFeature.get()) {
191 theError = "Error: Incorrect selection.";
195 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
196 theError = "Error: %1 shape is not allowed for selection.";
197 theError.arg(aFeature->getKind());
203 if (aShapeType == GeomAPI_Shape::WIRE) {
205 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
206 if (!aWire.get() && !aListOfShapes.empty()) {
207 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
215 //=================================================================================================
216 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
217 const std::list<std::string>& theArguments,
218 Events_InfoMessage& theError) const
221 if(theArguments.size() != 1) {
222 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
223 "1 parameter (ID of base objects list).";
224 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
227 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
228 if(!aSelectionList.get()) {
229 theError = "Empty attribute \"%1\".";
230 theError.arg(theArguments.front());
234 if (theFeature->getKind() == BuildPlugin_Face::ID()) {
235 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
236 /// - Face builder: edges, faces and wires selected
237 /// --> remove edges and wires
238 std::set<int> aRemove;
239 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
240 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
241 GeomShapePtr aShape = aSelection->value();
243 GeomAPI_Shape::ShapeType aType = aShape->shapeType();
244 if (aType == GeomAPI_Shape::FACE) {
246 GeomAPI_ShapeExplorer anWireExp(aShape, GeomAPI_Shape::WIRE);
247 for (; anWireExp.more(); anWireExp.next()) {
248 GeomShapePtr aWire = anWireExp.current();
249 for (int i = 0; i < aSelectionList->size(); ++i) {
250 AttributeSelectionPtr aSel = aSelectionList->value(i);
251 GeomShapePtr aShp = aSel->value();
253 if (aShp->shapeType() == GeomAPI_Shape::WIRE) {
254 if (aShp->isEqual(aWire) || aShp->isSameGeometry(aWire))
259 aRemove.insert(anIndex);
265 GeomAPI_ShapeExplorer anEdgeExp(aShape, GeomAPI_Shape::EDGE);
266 for (; anEdgeExp.more(); anEdgeExp.next()) {
267 GeomShapePtr aEdge = anEdgeExp.current();
268 for (int i = 0; i < aSelectionList->size(); ++i) {
269 AttributeSelectionPtr aSel = aSelectionList->value(i);
270 GeomShapePtr aShp = aSel->value();
272 if (aShp->shapeType() == GeomAPI_Shape::EDGE) {
273 if (aShp->isEqual(aEdge) || aShp->isSameGeometry(aEdge))
278 aRemove.insert(anIndex);
285 if (aRemove.size() > 0)
286 aSelectionList->remove(aRemove);
288 bool hasEdgesOrWires = false;
289 bool hasFaces = false;
291 // Collect base shapes.
293 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
294 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
295 GeomShapePtr aShape = aSelection->value();
297 if (!aSelection->context()) {
298 theError = "Objects are not selected.";
301 aShape = aSelection->context()->shape();
303 ResultConstructionPtr aSketchRes =
304 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
306 if (aShape->shapeType() == GeomAPI_Shape::FACE ||
307 (!aSelection->value() && aSketchRes && aSketchRes->facesNum() > 0)) {
308 // skip faces exploding
313 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
314 hasEdgesOrWires = true;
315 GeomShapePtr anEdge = anExp.current();
316 anEdges.push_back(anEdge);
320 if (hasFaces && hasEdgesOrWires) {
321 theError = "Faces and edges/wires should be selected together.";
323 } else if (hasEdgesOrWires && anEdges.empty()) {
324 theError = "Objects are not selected.";
328 // Check that edges does not have intersections.
329 if(anEdges.size() > 1) {
330 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
331 if(!aPaveFiller.isDone()) {
332 theError = "Error while checking if edges intersects.";
335 GeomShapePtr aSectedEdges = aPaveFiller.shape();
337 size_t anEdgesNum = 0;
338 for(GeomAPI_ShapeExplorer
339 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
342 if(anEdgesNum != anEdges.size()) {
343 theError = "Selected objects have intersections.";
348 if (!anEdges.empty()) {
349 // Check that they are planar.
350 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
352 theError = "Selected object(s) should belong to only one plane.";
356 // Check that selected objects have closed contours.
357 GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
358 const ListOfShape& aFaces = aBuilder.faces();
360 theError = "Selected objects do not generate closed contour.";
368 //=================================================================================================
369 bool BuildPlugin_ValidatorBaseForSolids::isValid(
370 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
371 Events_InfoMessage& theError) const
373 // Get base objects list.
374 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
375 if (!aSelectionList.get()) {
376 theError = "Could not get selection list.";
379 if (aSelectionList->size() == 0) {
380 theError = "Empty selection list.";
384 if (theFeature->getKind() == BuildPlugin_Solid::ID()) {
385 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
386 /// Solid builder: faces and shapes shells or solids seleted
389 std::set<int> aRemove;
390 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
391 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
392 GeomShapePtr aShape = aSelection->value();
394 GeomAPI_Shape::ShapeType aType = aShape->shapeType();
395 if ((aType == GeomAPI_Shape::SHAPE) ||
396 (aType == GeomAPI_Shape::SOLID) ||
397 (aType == GeomAPI_Shape::SHELL)) {
399 GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::FACE);
400 for (; anExp.more(); anExp.next()) {
401 GeomShapePtr aFace = anExp.current();
402 for (int i = 0; i < aSelectionList->size(); ++i) {
403 AttributeSelectionPtr aSel = aSelectionList->value(i);
404 GeomShapePtr aShp = aSel->value();
406 if (aShp->shapeType() == GeomAPI_Shape::FACE) {
407 if (aShp->isEqual(aFace))
412 aRemove.insert(anIndex);
419 if (aRemove.size() > 0)
420 aSelectionList->remove(aRemove);
423 // Collect base shapes.
424 ListOfShape anOriginalShapes;
425 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
426 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
427 if (!aSelection->context().get()) {
428 theError = "Invalid selection.";
431 GeomShapePtr aShape = aSelection->value();
433 aShape = aSelection->context()->shape();
434 anOriginalShapes.push_back(aShape);
437 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
438 new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
441 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
446 // set of allowed types of results
447 std::set<GeomAPI_Shape::ShapeType> aResultType;
448 std::string aType = theArguments.back();
449 if (aType == "solid")
450 aResultType.insert(GeomAPI_Shape::SOLID);
451 else if (aType == "compsolid") {
452 aResultType.insert(GeomAPI_Shape::COMPSOLID);
453 aResultType.insert(GeomAPI_Shape::SOLID);
456 GeomShapePtr aCompound = anAlgorithm->shape();
457 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
458 GeomAPI_ShapeIterator anIt(aCompound);
459 GeomShapePtr aFoundSub;
460 for (; anIt.more() && !aFoundSub; anIt.next()) {
461 aFoundSub = anIt.current();
462 if (aResultType.count(aFoundSub->shapeType()) == 0) {
463 theError = "Unable to build a solid";
467 if (anIt.more() || !aFoundSub.get()) {
468 theError = "Unable to build a solid";
471 } else if (aResultType.count(aCompound->shapeType()) == 0) {
472 theError = "Unable to build a solid";
475 // check the internal faces presence
476 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
477 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
478 theError = "Internal faces are not allowed in the resulting solid";
487 //=================================================================================================
488 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
489 const std::list<std::string>& theArguments,
490 Events_InfoMessage& theError) const
492 if(theArguments.size() != 1) {
494 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
495 "1 parameter(Sketch feature id).";
496 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
501 // Get base objects list.
502 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
505 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
506 "%1\"\n Only \"%2\" supported.";
507 Events_InfoMessage("BuildPlugin_Validators", aMsg).
508 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
512 AttributeSelectionListPtr aSelectionList =
513 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
514 if(!aSelectionList.get()) {
515 theError = "Could not get selection list.";
520 const std::string aBaseShapeId = "base_shape";
521 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
522 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
524 if(!aShapeAttrSelection.get()) {
525 theError = "Base shape is empty.";
529 ResultPtr aBaseContext = aShapeAttrSelection->context();
531 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
532 if(!aBaseShape.get()) {
533 theError = "Base shape is empty.";
537 GeomAlgoAPI_ShapeBuilder aBuilder;
538 aBuilder.removeInternal(aBaseShape);
539 aBaseShape = aBuilder.shape();
541 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
542 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
543 switch(aBaseShape->shapeType()) {
544 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
545 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
549 // Check selected shapes.
550 GeomValidators_FeatureKind aFeatureKindValidator;
551 std::list<std::string> anArguments;
552 anArguments.push_back(theArguments.front());
553 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
554 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
555 if(!aSelectionAttrInList.get()) {
556 theError = "Empty attribute in list.";
560 // If context of selection same skip.
561 if(aBaseContext == aSelectionAttrInList->context()) {
565 // Check that it is a selection on Sketch.
566 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
571 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
572 if(!aShapeInList.get()) {
573 aShapeInList = aSelectionAttrInList->context()->shape();
575 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
576 theError = "Selected shape has unacceptable type.";
580 // Check that shape inside wire or face.
581 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
582 theError = "Selected shape is not inside base face.";
591 //=================================================================================================
592 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
593 const std::list<std::string>& /*theArguments*/,
594 Events_InfoMessage& theError) const
596 // Get base objects list.
597 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
600 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
601 "%1\"\n Only \"%2\" supported.";
602 Events_InfoMessage("BuildPlugin_Validators", aMsg).
603 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
607 AttributeSelectionListPtr aSelectionList =
608 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
609 if (!aSelectionList.get()) {
610 theError = "Could not get selection list.";
614 //FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
616 // Check selected shapes.
617 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
618 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
619 if (!aSelectionAttrInList.get()) {
620 theError = "Empty attribute in list.";
624 // Check shape exists.
625 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
626 if (!aShapeInList.get()) {
627 theError = "Object has no shape";
632 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
633 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
634 theError = "Incorrect objects selected";
643 //=================================================================================================
644 bool BuildPlugin_ValidatorBaseForVertex::isValid(const AttributePtr& theAttribute,
645 const std::list<std::string>& /*theArguments*/,
646 Events_InfoMessage& theError) const
648 if (!theAttribute.get()) {
649 theError = "Error: empty selection.";
653 AttributeSelectionListPtr aSelectionList =
654 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
655 if (!aSelectionList.get()) {
656 theError = "Could not get selection list.";
660 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
661 AttributeSelectionPtr aSelectionAttr = aSelectionList->value(anIndex);
662 if (!aSelectionAttr.get()) {
663 theError = "Empty attribute in list.";
668 bool isVertex = false;
669 GeomShapePtr aShape = aSelectionAttr->value();
670 ResultPtr aContext = aSelectionAttr->context();
671 if (!aShape.get() && aContext.get())
672 aShape = aContext->shape();
674 isVertex = (aShape->shapeType() == GeomAPI_Shape::VERTEX);
678 FeaturePtr aFeature = aSelectionAttr->contextFeature();
679 if (!aFeature.get()) {
680 GeomShapePtr aValue = aSelectionAttr->value();
681 // whole sketch is allowed only
682 if (aContext.get() && !aValue.get()) {
683 aFeature = ModelAPI_Feature::feature(aContext);
687 if (!aFeature.get()) {
688 theError = "Error: Incorrect selection.";
692 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
693 theError = "Error: %1 shape is not allowed for selection.";
694 theError.arg(aFeature->getKind());
703 //=================================================================================================
704 bool BuildPlugin_ValidatorExpressionInterpolation::isValid(const AttributePtr& theAttribute,
705 const std::list<std::string>& /*theArguments*/,
706 Events_InfoMessage& theError) const
708 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
710 AttributeStringPtr aStrAttr =
711 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
712 if (!aStrAttr->isInitialized()) {
713 theError = "Attribute \"%1\" is not initialized.";
714 theError.arg(aStrAttr->id());
717 bool isEmptyExpr = aStrAttr->value().empty();
719 theError = "Expression is empty.";
723 theError = aFeature->string(BuildPlugin_Interpolation::EXPRESSION_ERROR_ID())->value();
724 return theError.empty();