1 // Copyright (C) 2014-2021 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<GeomAPI_Shape::ShapeType> shapeTypes;
136 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
137 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
138 GeomShapePtr aShape = aSelection->value();
140 shapeTypes.insert(aShape->shapeType());
143 std::set<int> aRemove;
144 if (shapeTypes.find(GeomAPI_Shape::WIRE) != shapeTypes.end())
146 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
147 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
148 GeomShapePtr aShape = aSelection->value();
150 auto aType = aShape->shapeType();
151 if (aType == GeomAPI_Shape::EDGE)
152 aRemove.insert(anIndex);
155 aRemove.insert(anIndex);
159 if (aRemove.size() > 0)
160 aSelectionList->remove(aRemove);
163 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::shapeTypeByStr(theArguments.back());
164 // Collect base shapes.
165 ListOfShape aListOfShapes;
166 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
167 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
168 GeomShapePtr aShape = aSelection->value();
169 ResultPtr aContext = aSelection->context();
170 if (!aShape.get() && aContext.get())
171 aShape = aContext->shape();
173 bool isProper = aShape.get() &&
174 (aShape->shapeType() == GeomAPI_Shape::EDGE || aShape->shapeType() == aShapeType);
177 aListOfShapes.push_back(aShape);
180 FeaturePtr aFeature = aSelection->contextFeature();
181 if (!aFeature.get()) {
182 GeomShapePtr aValue = aSelection->value();
183 // whole sketch is allowed only
184 if (aContext.get() && !aValue.get()) {
185 aFeature = ModelAPI_Feature::feature(aContext);
189 if (!aFeature.get()) {
190 theError = "Error: Incorrect selection.";
194 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
195 theError = "Error: %1 shape is not allowed for selection.";
196 theError.arg(aFeature->getKind());
202 if (aShapeType == GeomAPI_Shape::WIRE) {
204 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
205 if (!aWire.get() && !aListOfShapes.empty()) {
206 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
214 //=================================================================================================
215 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
216 const std::list<std::string>& theArguments,
217 Events_InfoMessage& theError) const
220 if(theArguments.size() != 1) {
221 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
222 "1 parameter (ID of base objects list).";
223 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
226 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
227 if(!aSelectionList.get()) {
228 theError = "Empty attribute \"%1\".";
229 theError.arg(theArguments.front());
233 if (theFeature->getKind() == BuildPlugin_Face::ID()) {
234 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
235 /// - Face builder: edges, faces and wires selected
236 /// --> remove edges and wires
237 std::set<GeomAPI_Shape::ShapeType> shapeTypes;
238 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
239 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
240 GeomShapePtr aShape = aSelection->value();
242 shapeTypes.insert(aShape->shapeType());
245 std::set<int> aRemove;
246 if (shapeTypes.find(GeomAPI_Shape::FACE) != shapeTypes.end())
248 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
249 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
250 GeomShapePtr aShape = aSelection->value();
252 auto aType = aShape->shapeType();
253 if (aType == GeomAPI_Shape::WIRE || aType == GeomAPI_Shape::EDGE)
254 aRemove.insert(anIndex);
257 aRemove.insert(anIndex);
260 if (aRemove.size() > 0)
261 aSelectionList->remove(aRemove);
263 bool hasEdgesOrWires = false;
264 bool hasFaces = false;
266 // Collect base shapes.
268 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
269 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
270 GeomShapePtr aShape = aSelection->value();
272 if (!aSelection->context()) {
273 theError = "Objects are not selected.";
276 aShape = aSelection->context()->shape();
278 ResultConstructionPtr aSketchRes =
279 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
281 if (aShape->shapeType() == GeomAPI_Shape::FACE ||
282 (!aSelection->value() && aSketchRes && aSketchRes->facesNum() > 0)) {
283 // skip faces exploding
288 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
289 hasEdgesOrWires = true;
290 GeomShapePtr anEdge = anExp.current();
291 anEdges.push_back(anEdge);
295 if (hasFaces && hasEdgesOrWires) {
296 theError = "Faces and edges/wires should be selected together.";
298 } else if (hasEdgesOrWires && anEdges.empty()) {
299 theError = "Objects are not selected.";
303 // Check that edges does not have intersections.
304 if(anEdges.size() > 1) {
305 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
306 if(!aPaveFiller.isDone()) {
307 theError = "Error while checking if edges intersects.";
310 GeomShapePtr aSectedEdges = aPaveFiller.shape();
312 size_t anEdgesNum = 0;
313 for(GeomAPI_ShapeExplorer
314 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
317 if(anEdgesNum != anEdges.size()) {
318 theError = "Selected objects have intersections.";
323 if (!anEdges.empty()) {
324 // Check that they are planar.
325 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
327 theError = "Selected object(s) should belong to only one plane.";
331 // Check that selected objects have closed contours.
332 GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
333 const ListOfShape& aFaces = aBuilder.faces();
335 theError = "Selected objects do not generate closed contour.";
343 //=================================================================================================
344 bool BuildPlugin_ValidatorBaseForSolids::isValid(
345 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
346 Events_InfoMessage& theError) const
348 // Get base objects list.
349 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
350 if (!aSelectionList.get()) {
351 theError = "Could not get selection list.";
354 if (aSelectionList->size() == 0) {
355 theError = "Empty selection list.";
359 if (theFeature->getKind() == BuildPlugin_Solid::ID()) {
360 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
361 /// Solid builder: faces and shapes shells or solids seleted
364 std::set<GeomAPI_Shape::ShapeType> shapeTypes;
365 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
366 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
367 GeomShapePtr aShape = aSelection->value();
369 shapeTypes.insert(aShape->shapeType());
372 std::set<int> aRemove;
373 if (shapeTypes.find(GeomAPI_Shape::SHAPE) != shapeTypes.end() ||
374 shapeTypes.find(GeomAPI_Shape::SOLID) != shapeTypes.end() ||
375 shapeTypes.find(GeomAPI_Shape::SHELL) != shapeTypes.end())
377 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
378 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
379 GeomShapePtr aShape = aSelection->value();
381 auto aType = aShape->shapeType();
382 if (aType == GeomAPI_Shape::FACE)
383 aRemove.insert(anIndex);
386 aRemove.insert(anIndex);
390 if (aRemove.size() > 0)
391 aSelectionList->remove(aRemove);
394 // Collect base shapes.
395 ListOfShape anOriginalShapes;
396 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
397 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
398 if (!aSelection->context().get()) {
399 theError = "Invalid selection.";
402 GeomShapePtr aShape = aSelection->value();
404 aShape = aSelection->context()->shape();
405 anOriginalShapes.push_back(aShape);
408 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
409 new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
412 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
417 // set of allowed types of results
418 std::set<GeomAPI_Shape::ShapeType> aResultType;
419 std::string aType = theArguments.back();
420 if (aType == "solid")
421 aResultType.insert(GeomAPI_Shape::SOLID);
422 else if (aType == "compsolid") {
423 aResultType.insert(GeomAPI_Shape::COMPSOLID);
424 aResultType.insert(GeomAPI_Shape::SOLID);
427 GeomShapePtr aCompound = anAlgorithm->shape();
428 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
429 GeomAPI_ShapeIterator anIt(aCompound);
430 GeomShapePtr aFoundSub;
431 for (; anIt.more() && !aFoundSub; anIt.next()) {
432 aFoundSub = anIt.current();
433 if (aResultType.count(aFoundSub->shapeType()) == 0) {
434 theError = "Unable to build a solid";
438 if (anIt.more() || !aFoundSub.get()) {
439 theError = "Unable to build a solid";
442 } else if (aResultType.count(aCompound->shapeType()) == 0) {
443 theError = "Unable to build a solid";
446 // check the internal faces presence
447 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
448 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
449 theError = "Internal faces are not allowed in the resulting solid";
458 //=================================================================================================
459 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
460 const std::list<std::string>& theArguments,
461 Events_InfoMessage& theError) const
463 if(theArguments.size() != 1) {
465 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
466 "1 parameter(Sketch feature id).";
467 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
472 // Get base objects list.
473 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
476 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
477 "%1\"\n Only \"%2\" supported.";
478 Events_InfoMessage("BuildPlugin_Validators", aMsg).
479 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
483 AttributeSelectionListPtr aSelectionList =
484 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
485 if(!aSelectionList.get()) {
486 theError = "Could not get selection list.";
491 const std::string aBaseShapeId = "base_shape";
492 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
493 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
495 if(!aShapeAttrSelection.get()) {
496 theError = "Base shape is empty.";
500 ResultPtr aBaseContext = aShapeAttrSelection->context();
502 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
503 if(!aBaseShape.get()) {
504 theError = "Base shape is empty.";
508 GeomAlgoAPI_ShapeBuilder aBuilder;
509 aBuilder.removeInternal(aBaseShape);
510 aBaseShape = aBuilder.shape();
512 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
513 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
514 switch(aBaseShape->shapeType()) {
515 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
516 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
520 // Check selected shapes.
521 GeomValidators_FeatureKind aFeatureKindValidator;
522 std::list<std::string> anArguments;
523 anArguments.push_back(theArguments.front());
524 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
525 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
526 if(!aSelectionAttrInList.get()) {
527 theError = "Empty attribute in list.";
531 // If context of selection same skip.
532 if(aBaseContext == aSelectionAttrInList->context()) {
536 // Check that it is a selection on Sketch.
537 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
542 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
543 if(!aShapeInList.get()) {
544 aShapeInList = aSelectionAttrInList->context()->shape();
546 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
547 theError = "Selected shape has unacceptable type.";
551 // Check that shape inside wire or face.
552 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
553 theError = "Selected shape is not inside base face.";
562 //=================================================================================================
563 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
564 const std::list<std::string>& /*theArguments*/,
565 Events_InfoMessage& theError) const
567 // Get base objects list.
568 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
571 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
572 "%1\"\n Only \"%2\" supported.";
573 Events_InfoMessage("BuildPlugin_Validators", aMsg).
574 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
578 AttributeSelectionListPtr aSelectionList =
579 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
580 if (!aSelectionList.get()) {
581 theError = "Could not get selection list.";
585 //FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
587 // Check selected shapes.
588 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
589 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
590 if (!aSelectionAttrInList.get()) {
591 theError = "Empty attribute in list.";
595 // Check shape exists.
596 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
597 if (!aShapeInList.get()) {
598 theError = "Object has no shape";
603 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
604 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
605 theError = "Incorrect objects selected";
614 //=================================================================================================
615 bool BuildPlugin_ValidatorBaseForVertex::isValid(const AttributePtr& theAttribute,
616 const std::list<std::string>& /*theArguments*/,
617 Events_InfoMessage& theError) const
619 if (!theAttribute.get()) {
620 theError = "Error: empty selection.";
624 AttributeSelectionListPtr aSelectionList =
625 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
626 if (!aSelectionList.get()) {
627 theError = "Could not get selection list.";
631 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
632 AttributeSelectionPtr aSelectionAttr = aSelectionList->value(anIndex);
633 if (!aSelectionAttr.get()) {
634 theError = "Empty attribute in list.";
639 bool isVertex = false;
640 GeomShapePtr aShape = aSelectionAttr->value();
641 ResultPtr aContext = aSelectionAttr->context();
642 if (!aShape.get() && aContext.get())
643 aShape = aContext->shape();
645 isVertex = (aShape->shapeType() == GeomAPI_Shape::VERTEX);
649 FeaturePtr aFeature = aSelectionAttr->contextFeature();
650 if (!aFeature.get()) {
651 GeomShapePtr aValue = aSelectionAttr->value();
652 // whole sketch is allowed only
653 if (aContext.get() && !aValue.get()) {
654 aFeature = ModelAPI_Feature::feature(aContext);
658 if (!aFeature.get()) {
659 theError = "Error: Incorrect selection.";
663 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
664 theError = "Error: %1 shape is not allowed for selection.";
665 theError.arg(aFeature->getKind());
674 //=================================================================================================
675 bool BuildPlugin_ValidatorExpressionInterpolation::isValid(const AttributePtr& theAttribute,
676 const std::list<std::string>& /*theArguments*/,
677 Events_InfoMessage& theError) const
679 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
681 AttributeStringPtr aStrAttr =
682 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
683 if (!aStrAttr->isInitialized()) {
684 theError = "Attribute \"%1\" is not initialized.";
685 theError.arg(aStrAttr->id());
688 bool isEmptyExpr = aStrAttr->value().empty();
690 theError = "Expression is empty.";
694 theError = aFeature->string(BuildPlugin_Interpolation::EXPRESSION_ERROR_ID())->value();
695 return theError.empty();