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"
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 <SketchPlugin_Sketch.h>
44 #include <Events_InfoMessage.h>
46 //=================================================================================================
47 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
48 const std::list<std::string>& theArguments,
49 Events_InfoMessage& theError) const
51 // Get base objects list.
52 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
53 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
54 "not support attribute type '%1'\nOnly '%2' is supported.";
55 Events_InfoMessage("BuildPlugin_Validators", aMsg).
56 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
59 AttributeSelectionListPtr aSelectionList =
60 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
61 if(!aSelectionList.get()) {
62 theError = "Could not get selection list.";
65 if(aSelectionList->size() == 0) {
66 theError = "Empty selection list.";
70 // Collect base shapes.
71 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
72 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
73 if(!aSelection.get()) {
74 theError = "Could not get selection.";
77 ResultPtr aContext = aSelection->context();
79 theError = "Attribute have empty context.";
83 GeomShapePtr aShape = aSelection->value();
84 GeomShapePtr aContextShape = aContext->shape();
86 aShape = aContextShape;
89 theError = "Empty shape selected.";
93 // Check that shapes has acceptable type.
94 GeomValidators_ShapeType aValidatorShapeType;
95 if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
99 // Check that it is shape on sketch.
100 ResultConstructionPtr aConstruction =
101 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
102 if(aConstruction.get()) {
103 if(aConstruction->isInfinite()) {
104 theError = "Infinite objects not acceptable.";
113 //=================================================================================================
114 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
115 const std::list<std::string>& theArguments,
116 Events_InfoMessage& theError) const
119 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
120 if(!aSelectionList.get()) {
121 theError = "Empty attribute \"%1\".";
122 theError.arg(theArguments.front());
126 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
127 /// Wire builder: wires and edges selected
128 std::set<GeomAPI_Shape::ShapeType> shapeTypes;
129 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
130 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
131 GeomShapePtr aShape = aSelection->value();
133 shapeTypes.insert(aShape->shapeType());
136 std::set<int> aRemove;
137 if (shapeTypes.find(GeomAPI_Shape::WIRE) != shapeTypes.end())
139 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
140 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
141 GeomShapePtr aShape = aSelection->value();
143 auto aType = aShape->shapeType();
144 if (aType == GeomAPI_Shape::EDGE)
145 aRemove.insert(anIndex);
148 aRemove.insert(anIndex);
152 if (aRemove.size() > 0)
153 aSelectionList->remove(aRemove);
155 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::shapeTypeByStr(theArguments.back());
157 // Collect base shapes.
158 ListOfShape aListOfShapes;
159 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
160 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
161 GeomShapePtr aShape = aSelection->value();
162 ResultPtr aContext = aSelection->context();
163 if (!aShape.get() && aContext.get())
164 aShape = aContext->shape();
166 bool isProper = aShape.get() &&
167 (aShape->shapeType() == GeomAPI_Shape::EDGE || aShape->shapeType() == aShapeType);
170 aListOfShapes.push_back(aShape);
173 FeaturePtr aFeature = aSelection->contextFeature();
174 if (!aFeature.get()) {
175 GeomShapePtr aValue = aSelection->value();
176 // whole sketch is allowed only
177 if (aContext.get() && !aValue.get()) {
178 aFeature = ModelAPI_Feature::feature(aContext);
182 if (!aFeature.get()) {
183 theError = "Error: Incorrect selection.";
187 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
188 theError = "Error: %1 shape is not allowed for selection.";
189 theError.arg(aFeature->getKind());
195 if (aShapeType == GeomAPI_Shape::WIRE) {
197 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
198 if (!aWire.get() && !aListOfShapes.empty()) {
199 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
207 //=================================================================================================
208 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
209 const std::list<std::string>& theArguments,
210 Events_InfoMessage& theError) const
213 if(theArguments.size() != 1) {
214 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
215 "1 parameter (ID of base objects list).";
216 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
219 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
220 if(!aSelectionList.get()) {
221 theError = "Empty attribute \"%1\".";
222 theError.arg(theArguments.front());
226 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
227 /// - Face builder: edges, faces and wires selected
228 /// --> remove edges and wires
229 std::set<GeomAPI_Shape::ShapeType> shapeTypes;
230 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
231 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
232 GeomShapePtr aShape = aSelection->value();
234 shapeTypes.insert(aShape->shapeType());
237 std::set<int> aRemove;
238 if (shapeTypes.find(GeomAPI_Shape::FACE) != shapeTypes.end())
240 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
241 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
242 GeomShapePtr aShape = aSelection->value();
244 auto aType = aShape->shapeType();
245 if (aType == GeomAPI_Shape::WIRE || aType == GeomAPI_Shape::EDGE)
246 aRemove.insert(anIndex);
249 aRemove.insert(anIndex);
252 if (aRemove.size() > 0)
253 aSelectionList->remove(aRemove);
255 bool hasEdgesOrWires = false;
256 bool hasFaces = false;
258 // Collect base shapes.
260 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
261 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
262 GeomShapePtr aShape = aSelection->value();
264 if (!aSelection->context()) {
265 theError = "Objects are not selected.";
268 aShape = aSelection->context()->shape();
270 ResultConstructionPtr aSketchRes =
271 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
273 if (aShape->shapeType() == GeomAPI_Shape::FACE ||
274 (!aSelection->value() && aSketchRes && aSketchRes->facesNum() > 0)) {
275 // skip faces exploding
280 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
281 hasEdgesOrWires = true;
282 GeomShapePtr anEdge = anExp.current();
283 anEdges.push_back(anEdge);
287 if (hasFaces && hasEdgesOrWires) {
288 theError = "Faces and edges/wires should be selected together.";
290 } else if (hasEdgesOrWires && anEdges.empty()) {
291 theError = "Objects are not selected.";
295 // Check that edges does not have intersections.
296 if(anEdges.size() > 1) {
297 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
298 if(!aPaveFiller.isDone()) {
299 theError = "Error while checking if edges intersects.";
302 GeomShapePtr aSectedEdges = aPaveFiller.shape();
304 size_t anEdgesNum = 0;
305 for(GeomAPI_ShapeExplorer
306 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
309 if(anEdgesNum != anEdges.size()) {
310 theError = "Selected objects have intersections.";
315 if (!anEdges.empty()) {
316 // Check that they are planar.
317 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
319 theError = "Selected object(s) should belong to only one plane.";
323 // Check that selected objects have closed contours.
324 GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
325 const ListOfShape& aFaces = aBuilder.faces();
327 theError = "Selected objects do not generate closed contour.";
335 //=================================================================================================
336 bool BuildPlugin_ValidatorBaseForSolids::isValid(
337 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
338 Events_InfoMessage& theError) const
340 // Get base objects list.
341 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
342 if (!aSelectionList.get()) {
343 theError = "Could not get selection list.";
346 if (aSelectionList->size() == 0) {
347 theError = "Empty selection list.";
351 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
352 /// Solid builder: faces and shapes shells or solids seleted
355 std::set<GeomAPI_Shape::ShapeType> shapeTypes;
356 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
357 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
358 GeomShapePtr aShape = aSelection->value();
360 shapeTypes.insert(aShape->shapeType());
363 std::set<int> aRemove;
364 if (shapeTypes.find(GeomAPI_Shape::SHAPE) != shapeTypes.end() ||
365 shapeTypes.find(GeomAPI_Shape::SOLID) != shapeTypes.end() ||
366 shapeTypes.find(GeomAPI_Shape::SHELL) != shapeTypes.end())
368 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
369 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
370 GeomShapePtr aShape = aSelection->value();
372 auto aType = aShape->shapeType();
373 if (aType == GeomAPI_Shape::FACE)
374 aRemove.insert(anIndex);
377 aRemove.insert(anIndex);
381 if (aRemove.size() > 0)
382 aSelectionList->remove(aRemove);
384 // Collect base shapes.
385 ListOfShape anOriginalShapes;
386 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
387 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
388 if (!aSelection->context().get()) {
389 theError = "Invalid selection.";
392 GeomShapePtr aShape = aSelection->value();
394 aShape = aSelection->context()->shape();
395 anOriginalShapes.push_back(aShape);
398 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
399 new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
402 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
407 // set of allowed types of results
408 std::set<GeomAPI_Shape::ShapeType> aResultType;
409 std::string aType = theArguments.back();
410 if (aType == "solid")
411 aResultType.insert(GeomAPI_Shape::SOLID);
412 else if (aType == "compsolid") {
413 aResultType.insert(GeomAPI_Shape::COMPSOLID);
414 aResultType.insert(GeomAPI_Shape::SOLID);
417 GeomShapePtr aCompound = anAlgorithm->shape();
418 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
419 GeomAPI_ShapeIterator anIt(aCompound);
420 GeomShapePtr aFoundSub;
421 for (; anIt.more() && !aFoundSub; anIt.next()) {
422 aFoundSub = anIt.current();
423 if (aResultType.count(aFoundSub->shapeType()) == 0) {
424 theError = "Unable to build a solid";
428 if (anIt.more() || !aFoundSub.get()) {
429 theError = "Unable to build a solid";
432 } else if (aResultType.count(aCompound->shapeType()) == 0) {
433 theError = "Unable to build a solid";
436 // check the internal faces presence
437 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
438 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
439 theError = "Internal faces are not allowed in the resulting solid";
448 //=================================================================================================
449 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
450 const std::list<std::string>& theArguments,
451 Events_InfoMessage& theError) const
453 if(theArguments.size() != 1) {
455 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
456 "1 parameter(Sketch feature id).";
457 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
462 // Get base objects list.
463 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
466 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
467 "%1\"\n Only \"%2\" supported.";
468 Events_InfoMessage("BuildPlugin_Validators", aMsg).
469 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
473 AttributeSelectionListPtr aSelectionList =
474 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
475 if(!aSelectionList.get()) {
476 theError = "Could not get selection list.";
481 const std::string aBaseShapeId = "base_shape";
482 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
483 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
485 if(!aShapeAttrSelection.get()) {
486 theError = "Base shape is empty.";
490 ResultPtr aBaseContext = aShapeAttrSelection->context();
492 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
493 if(!aBaseShape.get()) {
494 theError = "Base shape is empty.";
498 GeomAlgoAPI_ShapeBuilder aBuilder;
499 aBuilder.removeInternal(aBaseShape);
500 aBaseShape = aBuilder.shape();
502 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
503 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
504 switch(aBaseShape->shapeType()) {
505 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
506 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
510 // Check selected shapes.
511 GeomValidators_FeatureKind aFeatureKindValidator;
512 std::list<std::string> anArguments;
513 anArguments.push_back(theArguments.front());
514 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
515 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
516 if(!aSelectionAttrInList.get()) {
517 theError = "Empty attribute in list.";
521 // If context of selection same skip.
522 if(aBaseContext == aSelectionAttrInList->context()) {
526 // Check that it is a selection on Sketch.
527 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
532 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
533 if(!aShapeInList.get()) {
534 aShapeInList = aSelectionAttrInList->context()->shape();
536 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
537 theError = "Selected shape has unacceptable type.";
541 // Check that shape inside wire or face.
542 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
543 theError = "Selected shape is not inside base face.";
552 //=================================================================================================
553 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
554 const std::list<std::string>& /*theArguments*/,
555 Events_InfoMessage& theError) const
557 // Get base objects list.
558 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
561 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
562 "%1\"\n Only \"%2\" supported.";
563 Events_InfoMessage("BuildPlugin_Validators", aMsg).
564 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
568 AttributeSelectionListPtr aSelectionList =
569 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
570 if (!aSelectionList.get()) {
571 theError = "Could not get selection list.";
575 //FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
577 // Check selected shapes.
578 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
579 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
580 if (!aSelectionAttrInList.get()) {
581 theError = "Empty attribute in list.";
585 // Check shape exists.
586 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
587 if (!aShapeInList.get()) {
588 theError = "Object has no shape";
593 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
594 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
595 theError = "Incorrect objects selected";
604 //=================================================================================================
605 bool BuildPlugin_ValidatorBaseForVertex::isValid(const AttributePtr& theAttribute,
606 const std::list<std::string>& /*theArguments*/,
607 Events_InfoMessage& theError) const
609 if (!theAttribute.get()) {
610 theError = "Error: empty selection.";
614 AttributeSelectionListPtr aSelectionList =
615 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
616 if (!aSelectionList.get()) {
617 theError = "Could not get selection list.";
621 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
622 AttributeSelectionPtr aSelectionAttr = aSelectionList->value(anIndex);
623 if (!aSelectionAttr.get()) {
624 theError = "Empty attribute in list.";
629 bool isVertex = false;
630 GeomShapePtr aShape = aSelectionAttr->value();
631 ResultPtr aContext = aSelectionAttr->context();
632 if (!aShape.get() && aContext.get())
633 aShape = aContext->shape();
635 isVertex = (aShape->shapeType() == GeomAPI_Shape::VERTEX);
639 FeaturePtr aFeature = aSelectionAttr->contextFeature();
640 if (!aFeature.get()) {
641 GeomShapePtr aValue = aSelectionAttr->value();
642 // whole sketch is allowed only
643 if (aContext.get() && !aValue.get()) {
644 aFeature = ModelAPI_Feature::feature(aContext);
648 if (!aFeature.get()) {
649 theError = "Error: Incorrect selection.";
653 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
654 theError = "Error: %1 shape is not allowed for selection.";
655 theError.arg(aFeature->getKind());