1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Validators.cpp
4 // Created: 22 March 2016
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_Validators.h"
9 #include <ModelAPI_Attribute.h>
10 #include <ModelAPI_AttributeInteger.h>
11 #include <ModelAPI_AttributeSelectionList.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_AttributeReference.h>
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_ResultCompSolid.h>
16 #include <ModelAPI_ResultConstruction.h>
18 #include <GeomValidators_BodyShapes.h>
19 #include <GeomValidators_FeatureKind.h>
20 #include <GeomValidators_ShapeType.h>
22 #include <GeomAPI_DataMapOfShapeShape.h>
23 #include <GeomAPI_PlanarEdges.h>
24 #include <GeomAPI_ShapeExplorer.h>
25 #include <GeomAPI_ShapeIterator.h>
27 #include <GeomAlgoAPI_ShapeBuilder.h>
28 #include <GeomAlgoAPI_ShapeTools.h>
29 #include <GeomAlgoAPI_WireBuilder.h>
31 //==================================================================================================
32 bool FeaturesPlugin_ValidatorPipePath::isValid(const AttributePtr& theAttribute,
33 const std::list<std::string>& theArguments,
34 std::string& theError) const
36 AttributeSelectionPtr aPathAttrSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
37 if(!aPathAttrSelection.get()) {
38 theError = "Error: This validator can only work with path selector in \"Pipe\" feature.";
42 GeomShapePtr aPathShape = aPathAttrSelection->value();
43 ResultPtr aContext = aPathAttrSelection->context();
45 theError = "Error: Empty context.";
48 GeomShapePtr aContextShape = aContext->shape();
49 if(aPathShape.get() && aPathShape->shapeType() == GeomAPI_Shape::WIRE && !aPathShape->isEqual(aContextShape)) {
50 theError = "Error: Local selection of wires not allowed.";
57 //==================================================================================================
58 bool FeaturesPlugin_ValidatorPipeLocations::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
59 const std::list<std::string>& theArguments,
60 std::string& theError) const
62 static const std::string aCreationMethodID = "creation_method";
63 static const std::string aBaseObjectsID = "base_objects";
64 static const std::string aLocationsID = "locations_objects";
66 if(theFeature->getKind() != "Pipe") {
67 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
71 AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
72 if(!aCreationMethodAttr.get()) {
73 theError = "Error: Could not get \"" + aCreationMethodID + "\" attribute.";
77 if(aCreationMethodAttr->value() != "locations") {
81 AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
82 if(!aBaseObjectsSelectionList.get()) {
83 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
87 AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
88 if(!aLocationsSelectionList.get()) {
89 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
93 if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
94 theError = "Error: Number of locations should be the same as base objects.";
101 //==================================================================================================
102 bool FeaturesPlugin_ValidatorPipeLocations::isNotObligatory(std::string theFeature, std::string theAttribute)
107 //==================================================================================================
108 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
109 const std::list<std::string>& theArguments,
110 std::string& theError) const
112 if(theArguments.empty()) {
113 theError = "Error: Validator parameters is empty.";
117 // Checking attribute.
118 if(!isValidAttribute(theAttribute, theArguments, theError)) {
119 if(theError.empty()) {
120 theError = "Error: Attribute contains unacceptable shape.";
125 std::set<ResultConstructionPtr> aSelectedSketches;
126 std::set<ResultConstructionPtr> aSelectedSketchesFromObjects;
127 GeomAPI_DataMapOfShapeShape aSelectedWiresFromObjects;
128 std::string anAttributeType = theAttribute->attributeType();
129 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
130 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
131 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
132 AttributeSelectionPtr aSelectionAttr = aListAttr->value(anIndex);
133 ResultPtr aContext = aSelectionAttr->context();
134 if(!aContext.get()) {
135 theError = "Error: Empty context.";
139 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
140 if(!aResultConstruction.get()) {
141 // It is not a result construction. If shape is compound check that it contains only faces and edges.
142 GeomShapePtr aShape = aSelectionAttr->value();
144 aShape = aContext->shape();
147 if(aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
148 for(GeomAPI_ShapeIterator anIt(aShape); anIt.more(); anIt.next()) {
149 GeomShapePtr aSubShape = anIt.current();
150 if(aSubShape->shapeType() != GeomAPI_Shape::EDGE
151 && aSubShape->shapeType() != GeomAPI_Shape::FACE) {
152 theError = "Error: Compound should contain only faces and edges.";
161 GeomShapePtr aShape = aSelectionAttr->value();
162 GeomShapePtr aContextShape = aResultConstruction->shape();
164 // Whole sketch selected.
165 if(aSelectedSketchesFromObjects.find(aResultConstruction) != aSelectedSketchesFromObjects.cend()) {
166 theError = "Error: Object from this sketch is already selected. Sketch is not allowed for selection.";
170 aSelectedSketches.insert(aResultConstruction);
172 // Object from sketch selected.
173 if(aSelectedSketches.find(aResultConstruction) != aSelectedSketches.cend()) {
174 theError = "Error: Whole sketch with this object is already selected. Don't allow to select this object.";
178 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
179 GeomShapePtr aWire = anExp.current();
180 if(aWire->orientation() != GeomAPI_Shape::FORWARD) {
181 theError = "Error: Wire with wrong orientation selected.";
185 if(aSelectedWiresFromObjects.isBound(aWire)) {
186 theError = "Error: Objects with such wire already selected. Don't allow to select this object.";
190 aSelectedWiresFromObjects.bind(aWire, aWire);
191 aSelectedSketchesFromObjects.insert(aResultConstruction);
200 //==================================================================================================
201 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
202 const std::list<std::string>& theArguments,
203 std::string& theError) const
205 if(!theAttribute.get()) {
206 theError = "Error: Empty attribute.";
210 std::string anAttributeType = theAttribute->attributeType();
211 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
212 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
213 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
214 // If at least one attribute is invalid, the result is false.
215 if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
219 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
221 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
222 ResultPtr aContext = anAttr->context();
223 if(!aContext.get()) {
224 theError = "Error: Attribute have empty context.";
228 GeomShapePtr aShape = anAttr->value();
229 GeomShapePtr aContextShape = aContext->shape();
231 aShape = aContextShape;
234 theError = "Error: Empty shape selected";
238 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
239 if(aConstruction.get()) {
240 // Construciotn selected. Check that is is not infinite.
241 if(aConstruction->isInfinite()) {
242 theError = "Error: Infinite constructions is not allowed as base.";
246 if(aShape->isEqual(aContextShape)) {
247 // Whole construction selected. Check that it have faces.
248 if(aConstruction->facesNum() > 0) {
252 // Shape on construction selected. Check that it is a face or wire.
253 if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
261 if(!aShape->isEqual(aContextShape)) {
262 // Local selection on body does not allowed.
263 theError = "Error: Selected shape is in the local selection. Only global selection is allowed.";
267 // Check that object is a shape with allowed type.
268 GeomValidators_ShapeType aShapeTypeValidator;
269 if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
270 theError = "Error: Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, "
271 "whole sketch(if it has at least one face), and whole objects with shape types: ";
272 std::list<std::string>::const_iterator anIt = theArguments.cbegin();
274 for(++anIt; anIt != theArguments.cend(); ++anIt) {
275 theError += ", " + *anIt;
281 theError = "Error: Attribute \"" + anAttributeType + "\" does not supported by this validator.";
288 //==================================================================================================
289 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
290 const std::list<std::string>& theArguments,
291 std::string& theError) const
293 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
294 theError = "Error: The attribute with the " + theAttribute->attributeType() + " type is not processed";
297 if (theArguments.size() != 2) {
298 theError = "Error: Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
301 // first argument is for the base attribute, second - for skipping feature kind
302 std::list<std::string>::const_iterator anIt = theArguments.begin();
303 std::string aBaseAttributeId = *anIt;
304 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
305 AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
306 if (!aBaseAttribute.get()) {
307 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
310 if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
311 // this validator is not necessary anymore
315 std::string aFeatureAttributeKind = *anIt;
316 GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
317 // check whether the selection is on the sketch
318 std::list<std::string> anArguments;
319 anArguments.push_back(aFeatureAttributeKind);
321 bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
322 bool aPlanarFace = false;
323 // check if selection has Face selected
324 GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
326 anArguments.push_back("face");
327 aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
329 bool aValid = !aFeatureKind && aPlanarFace;
333 //==================================================================================================
334 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
335 const std::list<std::string>& theArguments,
336 std::string& theError) const
338 if(theArguments.size() != 2) {
339 theError = "Error: Validator should be used with 2 parameters for extrusion.";
343 std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
345 AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
348 if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
352 AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
353 if(!aSelAttr.get()) {
354 theError = "Error: Could not get selection attribute \"" + *anArgsIt + "\".";
358 GeomShapePtr aShape = aSelAttr->value();
360 ResultPtr aContext = aSelAttr->context();
361 if(!aContext.get()) {
362 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
363 + "\" can not be used with default value. Select direction for extrusion.";
367 aShape = aContext->shape();
371 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
372 + "\" can not be used with default value. Select direction for extrusion.";
379 //==================================================================================================
380 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
385 //==================================================================================================
386 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
387 std::string& theError) const
389 if(!theAttribute.get()) {
393 std::string anAttributeType = theAttribute->attributeType();
394 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
395 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
396 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
397 // If at least one attribute is invalid, the result is false.
398 if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
402 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
404 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
405 ResultPtr aContext = anAttr->context();
406 if(!aContext.get()) {
410 GeomShapePtr aShape = anAttr->value();
411 GeomShapePtr aContextShape = aContext->shape();
413 aShape = aContextShape;
419 if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
420 aShape->shapeType() == GeomAPI_Shape::EDGE ||
421 !aShape->isPlanar()) {
431 //==================================================================================================
432 bool FeaturesPlugin_ValidatorBooleanSelection::isValid(const AttributePtr& theAttribute,
433 const std::list<std::string>& theArguments,
434 std::string& theError) const
436 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
437 if(!anAttrSelectionList.get()) {
438 theError = "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
441 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
442 int anOperationType = aFeature->integer("bool_type")->value();
444 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
445 AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
446 if(!anAttrSelection.get()) {
447 theError = "Error: Empty attribute selection.";
450 ResultPtr aContext = anAttrSelection->context();
451 if(!aContext.get()) {
452 theError = "Error: Empty selection context.";
455 ResultConstructionPtr aResultConstruction =
456 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
457 if(aResultConstruction.get()) {
458 theError = "Error: Result construction not allowed for selection.";
461 std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
463 aShape = aContext->shape();
466 theError = "Error: Empty shape.";
469 int aShapeType = aShape->shapeType();
470 if(anOperationType == 1) {
471 // Fuse operation. Allow to select edges, faces and solids.
472 if(aShapeType != GeomAPI_Shape::EDGE &&
473 aShapeType != GeomAPI_Shape::FACE &&
474 aShapeType != GeomAPI_Shape::SOLID &&
475 aShapeType != GeomAPI_Shape::COMPSOLID &&
476 aShapeType != GeomAPI_Shape::COMPOUND) {
477 theError = "Error: Selected shape has the wrong type.";
481 if(aShapeType != GeomAPI_Shape::SOLID &&
482 aShapeType != GeomAPI_Shape::COMPSOLID &&
483 aShapeType != GeomAPI_Shape::COMPOUND) {
484 theError = "Error: Selected shape has the wrong type.";
493 //==================================================================================================
494 bool FeaturesPlugin_ValidatorPartitionSelection::isValid(const AttributePtr& theAttribute,
495 const std::list<std::string>& theArguments,
496 std::string& theError) const
498 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
499 if(!anAttrSelectionList.get()) {
500 theError = "Error: This validator can only work with selection list in \"Partition\" feature.";
504 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
505 AttributeSelectionPtr aSelectAttr = anAttrSelectionList->value(anIndex);
507 //GeomValidators_BodyShapes aBodyValidator;
508 //if(aBodyValidator.isValid(aSelectAttr, theArguments, theError)) {
512 GeomValidators_FeatureKind aFeatureKindValidator;
513 if(aFeatureKindValidator.isValid(aSelectAttr, theArguments, theError)) {
517 ResultPtr aContext = aSelectAttr->context();
518 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
519 if(aResultConstruction.get()) {
520 theError = "Error: Only body shapes and construction planes are allowed for selection.";
524 ResultCompSolidPtr aResultCompsolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
525 if(aResultCompsolid.get()) {
529 theError = "Error: Only body shapes and construction planes are allowed for selection.";
537 //==================================================================================================
538 bool FeaturesPlugin_ValidatorRemoveSubShapesSelection::isValid(const AttributePtr& theAttribute,
539 const std::list<std::string>& theArguments,
540 std::string& theError) const
542 AttributeSelectionListPtr aSubShapesAttrList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
543 if(!aSubShapesAttrList.get()) {
544 theError = "Error: This validator can only work with selection list in \"Remove Sub-Shapes\" feature.";
548 static const std::string aBaseShapeID = "base_shape";
549 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
550 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeID);
552 if(!aShapeAttrSelection.get()) {
553 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
557 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
558 ResultPtr aContext = aShapeAttrSelection->context();
559 if(!aContext.get()) {
560 theError = "Error: Empty context.";
563 if(!aBaseShape.get()) {
564 aBaseShape = aContext->shape();
566 if(!aBaseShape.get()) {
567 theError = "Error: Empty base shape.";
571 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
572 bool isSameFound = false;
573 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
574 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
575 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
576 if(anIt.current()->isEqual(aShapeToAdd)) {
582 theError = "Error: Only sub-shapes of selected shape is allowed for selection.";
590 //==================================================================================================
591 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
592 const std::list<std::string>& theArguments,
593 std::string& theError) const
595 static const std::string aBaseShapeID = "base_shape";
596 static const std::string aSubShapesID = "subshapes";
598 if(theFeature->getKind() != "Remove_SubShapes") {
599 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
603 AttributeSelectionPtr aShapeAttrSelection = theFeature->selection(aBaseShapeID);
604 if(!aShapeAttrSelection.get()) {
605 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
609 AttributeSelectionListPtr aSubShapesAttrList = theFeature->selectionList(aSubShapesID);
610 if(!aSubShapesAttrList.get()) {
611 theError = "Error: Could not get \"" + aSubShapesID + "\" attribute.";
616 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
617 if(!aBaseShape.get()) {
620 GeomShapePtr aResultShape = aBaseShape->emptyCopied();
622 // Copy sub-shapes from list to new shape.
623 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
624 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
625 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
626 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
630 if(!GeomAlgoAPI_ShapeTools::isShapeValid(aResultShape)) {
631 theError = "Error: Resulting shape is not valid.";
638 //==================================================================================================
639 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isNotObligatory(std::string theFeature,
640 std::string theAttribute)