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"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_ResultConstruction.h>
26 #include <GeomAPI_PlanarEdges.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomAPI_ShapeIterator.h>
31 #include <GeomAlgoAPI_CompoundBuilder.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeBuilder.h>
34 #include <GeomAlgoAPI_ShapeTools.h>
35 #include <GeomAlgoAPI_SketchBuilder.h>
36 #include <GeomAlgoAPI_WireBuilder.h>
37 #include <GeomAlgoAPI_MakeVolume.h>
38 #include <GeomAlgoAPI_Tools.h>
40 #include <GeomValidators_FeatureKind.h>
41 #include <GeomValidators_ShapeType.h>
43 #include <BuildPlugin_Interpolation.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 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::shapeTypeByStr(theArguments.back());
131 // Collect base shapes.
132 ListOfShape aListOfShapes;
133 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
134 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
135 GeomShapePtr aShape = aSelection->value();
136 ResultPtr aContext = aSelection->context();
137 if (!aShape.get() && aContext.get())
138 aShape = aContext->shape();
140 bool isProper = aShape.get() &&
141 (aShape->shapeType() == GeomAPI_Shape::EDGE || aShape->shapeType() == aShapeType);
144 aListOfShapes.push_back(aShape);
147 FeaturePtr aFeature = aSelection->contextFeature();
148 if (!aFeature.get()) {
149 GeomShapePtr aValue = aSelection->value();
150 // whole sketch is allowed only
151 if (aContext.get() && !aValue.get()) {
152 aFeature = ModelAPI_Feature::feature(aContext);
156 if (!aFeature.get()) {
157 theError = "Error: Incorrect selection.";
161 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
162 theError = "Error: %1 shape is not allowed for selection.";
163 theError.arg(aFeature->getKind());
169 if (aShapeType == GeomAPI_Shape::WIRE) {
171 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
172 if (!aWire.get() && !aListOfShapes.empty()) {
173 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
181 //=================================================================================================
182 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
183 const std::list<std::string>& theArguments,
184 Events_InfoMessage& theError) const
187 if(theArguments.size() != 1) {
188 std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
189 "1 parameter (ID of base objects list).";
190 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
193 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
194 if(!aSelectionList.get()) {
195 theError = "Empty attribute \"%1\".";
196 theError.arg(theArguments.front());
200 bool hasEdgesOrWires = false;
201 bool hasFaces = false;
203 // Collect base shapes.
205 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
206 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
207 GeomShapePtr aShape = aSelection->value();
209 if (!aSelection->context()) {
210 theError = "Objects are not selected.";
213 aShape = aSelection->context()->shape();
215 ResultConstructionPtr aSketchRes =
216 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
218 if (aShape->shapeType() == GeomAPI_Shape::FACE ||
219 (!aSelection->value() && aSketchRes && aSketchRes->facesNum() > 0)) {
220 // skip faces exploding
225 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
226 hasEdgesOrWires = true;
227 GeomShapePtr anEdge = anExp.current();
228 anEdges.push_back(anEdge);
232 if (hasFaces && hasEdgesOrWires) {
233 theError = "Faces and edges/wires should be selected together.";
235 } else if (hasEdgesOrWires && anEdges.empty()) {
236 theError = "Objects are not selected.";
240 // Check that edges does not have intersections.
241 if(anEdges.size() > 1) {
242 GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
243 if(!aPaveFiller.isDone()) {
244 theError = "Error while checking if edges intersects.";
247 GeomShapePtr aSectedEdges = aPaveFiller.shape();
249 size_t anEdgesNum = 0;
250 for(GeomAPI_ShapeExplorer
251 anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
254 if(anEdgesNum != anEdges.size()) {
255 theError = "Selected objects have intersections.";
260 if (!anEdges.empty()) {
261 // Check that they are planar.
262 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
264 theError = "Selected object(s) should belong to only one plane.";
268 // Check that selected objects have closed contours.
269 GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
270 const ListOfShape& aFaces = aBuilder.faces();
272 theError = "Selected objects do not generate closed contour.";
280 //=================================================================================================
281 bool BuildPlugin_ValidatorBaseForSolids::isValid(
282 const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
283 Events_InfoMessage& theError) const
285 // Get base objects list.
286 AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
287 if (!aSelectionList.get()) {
288 theError = "Could not get selection list.";
291 if (aSelectionList->size() == 0) {
292 theError = "Empty selection list.";
296 // Collect base shapes.
297 ListOfShape anOriginalShapes;
298 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
299 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
300 if (!aSelection->context().get()) {
301 theError = "Invalid selection.";
304 GeomShapePtr aShape = aSelection->value();
306 aShape = aSelection->context()->shape();
307 anOriginalShapes.push_back(aShape);
310 std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
311 new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
314 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
319 // set of allowed types of results
320 std::set<GeomAPI_Shape::ShapeType> aResultType;
321 std::string aType = theArguments.back();
322 if (aType == "solid")
323 aResultType.insert(GeomAPI_Shape::SOLID);
324 else if (aType == "compsolid") {
325 aResultType.insert(GeomAPI_Shape::COMPSOLID);
326 aResultType.insert(GeomAPI_Shape::SOLID);
329 GeomShapePtr aCompound = anAlgorithm->shape();
330 if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
331 GeomAPI_ShapeIterator anIt(aCompound);
332 GeomShapePtr aFoundSub;
333 for (; anIt.more() && !aFoundSub; anIt.next()) {
334 aFoundSub = anIt.current();
335 if (aResultType.count(aFoundSub->shapeType()) == 0) {
336 theError = "Unable to build a solid";
340 if (anIt.more() || !aFoundSub.get()) {
341 theError = "Unable to build a solid";
344 } else if (aResultType.count(aCompound->shapeType()) == 0) {
345 theError = "Unable to build a solid";
348 // check the internal faces presence
349 for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
350 if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
351 theError = "Internal faces are not allowed in the resulting solid";
360 //=================================================================================================
361 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
362 const std::list<std::string>& theArguments,
363 Events_InfoMessage& theError) const
365 if(theArguments.size() != 1) {
367 std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
368 "1 parameter(Sketch feature id).";
369 Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
374 // Get base objects list.
375 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
378 "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
379 "%1\"\n Only \"%2\" supported.";
380 Events_InfoMessage("BuildPlugin_Validators", aMsg).
381 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
385 AttributeSelectionListPtr aSelectionList =
386 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
387 if(!aSelectionList.get()) {
388 theError = "Could not get selection list.";
393 const std::string aBaseShapeId = "base_shape";
394 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
395 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
397 if(!aShapeAttrSelection.get()) {
398 theError = "Base shape is empty.";
402 ResultPtr aBaseContext = aShapeAttrSelection->context();
404 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
405 if(!aBaseShape.get()) {
406 theError = "Base shape is empty.";
410 GeomAlgoAPI_ShapeBuilder aBuilder;
411 aBuilder.removeInternal(aBaseShape);
412 aBaseShape = aBuilder.shape();
414 // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
415 std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
416 switch(aBaseShape->shapeType()) {
417 case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
418 case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
422 // Check selected shapes.
423 GeomValidators_FeatureKind aFeatureKindValidator;
424 std::list<std::string> anArguments;
425 anArguments.push_back(theArguments.front());
426 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
427 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
428 if(!aSelectionAttrInList.get()) {
429 theError = "Empty attribute in list.";
433 // If context of selection same skip.
434 if(aBaseContext == aSelectionAttrInList->context()) {
438 // Check that it is a selection on Sketch.
439 if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
444 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
445 if(!aShapeInList.get()) {
446 aShapeInList = aSelectionAttrInList->context()->shape();
448 if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
449 theError = "Selected shape has unacceptable type.";
453 // Check that shape inside wire or face.
454 if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
455 theError = "Selected shape is not inside base face.";
464 //=================================================================================================
465 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
466 const std::list<std::string>& /*theArguments*/,
467 Events_InfoMessage& theError) const
469 // Get base objects list.
470 if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
473 "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
474 "%1\"\n Only \"%2\" supported.";
475 Events_InfoMessage("BuildPlugin_Validators", aMsg).
476 arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
480 AttributeSelectionListPtr aSelectionList =
481 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
482 if (!aSelectionList.get()) {
483 theError = "Could not get selection list.";
487 //FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
489 // Check selected shapes.
490 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
491 AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
492 if (!aSelectionAttrInList.get()) {
493 theError = "Empty attribute in list.";
497 // Check shape exists.
498 GeomShapePtr aShapeInList = aSelectionAttrInList->value();
499 if (!aShapeInList.get()) {
500 theError = "Object has no shape";
505 GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
506 if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
507 theError = "Incorrect objects selected";
516 //=================================================================================================
517 bool BuildPlugin_ValidatorBaseForVertex::isValid(const AttributePtr& theAttribute,
518 const std::list<std::string>& /*theArguments*/,
519 Events_InfoMessage& theError) const
521 if (!theAttribute.get()) {
522 theError = "Error: empty selection.";
526 AttributeSelectionListPtr aSelectionList =
527 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
528 if (!aSelectionList.get()) {
529 theError = "Could not get selection list.";
533 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
534 AttributeSelectionPtr aSelectionAttr = aSelectionList->value(anIndex);
535 if (!aSelectionAttr.get()) {
536 theError = "Empty attribute in list.";
541 bool isVertex = false;
542 GeomShapePtr aShape = aSelectionAttr->value();
543 ResultPtr aContext = aSelectionAttr->context();
544 if (!aShape.get() && aContext.get())
545 aShape = aContext->shape();
547 isVertex = (aShape->shapeType() == GeomAPI_Shape::VERTEX);
551 FeaturePtr aFeature = aSelectionAttr->contextFeature();
552 if (!aFeature.get()) {
553 GeomShapePtr aValue = aSelectionAttr->value();
554 // whole sketch is allowed only
555 if (aContext.get() && !aValue.get()) {
556 aFeature = ModelAPI_Feature::feature(aContext);
560 if (!aFeature.get()) {
561 theError = "Error: Incorrect selection.";
565 if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
566 theError = "Error: %1 shape is not allowed for selection.";
567 theError.arg(aFeature->getKind());
576 //=================================================================================================
577 bool BuildPlugin_ValidatorExpressionInterpolation::isValid(const AttributePtr& theAttribute,
578 const std::list<std::string>& /*theArguments*/,
579 Events_InfoMessage& theError) const
581 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
583 AttributeStringPtr aStrAttr =
584 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
585 if (!aStrAttr->isInitialized()) {
586 theError = "Attribute \"%1\" is not initialized.";
587 theError.arg(aStrAttr->id());
590 bool isEmptyExpr = aStrAttr->value().empty();
592 theError = "Expression is empty.";
596 theError = aFeature->string(BuildPlugin_Interpolation::EXPRESSION_ERROR_ID())->value();
597 return theError.empty();