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_ResultConstruction.h>
17 #include <Events_Error.h>
19 #include <GeomValidators_BodyShapes.h>
20 #include <GeomValidators_FeatureKind.h>
21 #include <GeomValidators_ShapeType.h>
23 #include <GeomAPI_DataMapOfShapeShape.h>
24 #include <GeomAPI_PlanarEdges.h>
25 #include <GeomAPI_ShapeExplorer.h>
26 #include <GeomAPI_ShapeIterator.h>
28 #include <GeomAlgoAPI_ShapeBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30 #include <GeomAlgoAPI_WireBuilder.h>
32 //==================================================================================================
33 bool FeaturesPlugin_ValidatorPipePath::isValid(const AttributePtr& theAttribute,
34 const std::list<std::string>& theArguments,
35 std::string& theError) const
37 AttributeSelectionPtr aPathAttrSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
38 if(!aPathAttrSelection.get()) {
39 theError = "Error: This validator can only work with path selector in \"Pipe\" feature.";
43 GeomShapePtr aPathShape = aPathAttrSelection->value();
44 ResultPtr aContext = aPathAttrSelection->context();
46 theError = "Error: Empty context.";
49 GeomShapePtr aContextShape = aContext->shape();
50 if(aPathShape.get() && aPathShape->shapeType() == GeomAPI_Shape::WIRE && !aPathShape->isEqual(aContextShape)) {
51 theError = "Error: Local selection of wires not allowed.";
58 //==================================================================================================
59 bool FeaturesPlugin_ValidatorPipeLocations::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
60 const std::list<std::string>& theArguments,
61 std::string& theError) const
63 static const std::string aCreationMethodID = "creation_method";
64 static const std::string aBaseObjectsID = "base_objects";
65 static const std::string aLocationsID = "locations_objects";
67 if(theFeature->getKind() != "Pipe") {
68 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
72 AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
73 if(!aCreationMethodAttr.get()) {
74 theError = "Error: Could not get \"" + aCreationMethodID + "\" attribute.";
78 if(aCreationMethodAttr->value() != "locations") {
82 AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
83 if(!aBaseObjectsSelectionList.get()) {
84 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
88 AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
89 if(!aLocationsSelectionList.get()) {
90 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
94 if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
95 theError = "Error: Number of locations should be the same as base objects.";
102 //==================================================================================================
103 bool FeaturesPlugin_ValidatorPipeLocations::isNotObligatory(std::string theFeature, std::string theAttribute)
108 //==================================================================================================
109 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
110 const std::list<std::string>& theArguments,
111 std::string& theError) const
113 if(theArguments.empty()) {
114 theError = "Error: Validator parameters is empty.";
118 // Checking attribute.
119 if(!isValidAttribute(theAttribute, theArguments, theError)) {
120 if(theError.empty()) {
121 theError = "Error: Attribute contains unacceptable shape.";
126 std::set<ResultConstructionPtr> aSelectedSketches;
127 std::set<ResultConstructionPtr> aSelectedSketchesFromObjects;
128 GeomAPI_DataMapOfShapeShape aSelectedWiresFromObjects;
129 std::string anAttributeType = theAttribute->attributeType();
130 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
131 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
132 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
133 AttributeSelectionPtr aSelectionAttr = aListAttr->value(anIndex);
134 ResultConstructionPtr aContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelectionAttr->context());
135 if(!aContext.get()) {
136 // It is not a result construction, continue.
140 GeomShapePtr aShape = aSelectionAttr->value();
141 GeomShapePtr aContextShape = aContext->shape();
143 // Whole sketch selected.
144 if(aSelectedSketchesFromObjects.find(aContext) != aSelectedSketchesFromObjects.cend()) {
145 theError = "Error: Object from this sketch is already selected. Sketch is not allowed for selection.";
149 aSelectedSketches.insert(aContext);
151 // Object from sketch selected.
152 if(aSelectedSketches.find(aContext) != aSelectedSketches.cend()) {
153 theError = "Error: Whole sketch with this object is already selected. Don't allow to select this object.";
157 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
158 GeomShapePtr aWire = anExp.current();
159 if(aWire->orientation() != GeomAPI_Shape::FORWARD) {
160 theError = "Error: Wire with wrong orientation selected.";
164 if(aSelectedWiresFromObjects.isBound(aWire)) {
165 theError = "Error: Objects with such wire already selected. Don't allow to select this object.";
169 aSelectedWiresFromObjects.bind(aWire, aWire);
170 aSelectedSketchesFromObjects.insert(aContext);
179 //==================================================================================================
180 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
181 const std::list<std::string>& theArguments,
182 std::string& theError) const
184 if(!theAttribute.get()) {
185 theError = "Error: Empty attribute.";
189 std::string anAttributeType = theAttribute->attributeType();
190 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
191 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
192 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
193 // If at least one attribute is invalid, the result is false.
194 if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
198 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
200 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
201 ResultPtr aContext = anAttr->context();
202 if(!aContext.get()) {
203 theError = "Error: Attribute have empty context.";
207 GeomShapePtr aShape = anAttr->value();
208 GeomShapePtr aContextShape = aContext->shape();
210 aShape = aContextShape;
213 theError = "Error: Empty shape selected";
217 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
218 if(aConstruction.get()) {
219 // Construciotn selected. Check that is is not infinite.
220 if(aConstruction->isInfinite()) {
221 theError = "Error: Infinite constructions is not allowed as base.";
225 if(aShape->isEqual(aContextShape)) {
226 // Whole construction selected. Check that it have faces.
227 if(aConstruction->facesNum() > 0) {
231 // Shape on construction selected. Check that it is a face or wire.
232 if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
240 if(!aShape->isEqual(aContextShape)) {
241 // Local selection on body does not allowed.
242 theError = "Error: Selected shape is in the local selection. Only global selection is allowed.";
246 // Check that object is a shape with allowed type.
247 GeomValidators_ShapeType aShapeTypeValidator;
248 if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
249 theError = "Error: Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, "
250 "whole sketch(if it has at least one face), and whole objects with shape types: ";
251 std::list<std::string>::const_iterator anIt = theArguments.cbegin();
253 for(++anIt; anIt != theArguments.cend(); ++anIt) {
254 theError += ", " + *anIt;
260 theError = "Error: Attribute \"" + anAttributeType + "\" does not supported by this validator.";
267 //==================================================================================================
268 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
269 const std::list<std::string>& theArguments,
270 std::string& theError) const
272 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
273 theError = "Error: The attribute with the " + theAttribute->attributeType() + " type is not processed";
276 if (theArguments.size() != 2) {
277 theError = "Error: Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
280 // first argument is for the base attribute, second - for skipping feature kind
281 std::list<std::string>::const_iterator anIt = theArguments.begin();
282 std::string aBaseAttributeId = *anIt;
283 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
284 AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
285 if (!aBaseAttribute.get()) {
286 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
289 if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
290 // this validator is not necessary anymore
294 std::string aFeatureAttributeKind = *anIt;
295 GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
296 // check whether the selection is on the sketch
297 std::list<std::string> anArguments;
298 anArguments.push_back(aFeatureAttributeKind);
300 bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
301 bool aPlanarFace = false;
302 // check if selection has Face selected
303 GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
305 anArguments.push_back("face");
306 aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
308 bool aValid = !aFeatureKind && aPlanarFace;
312 //==================================================================================================
313 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
314 const std::list<std::string>& theArguments,
315 std::string& theError) const
317 if(theArguments.size() != 2) {
318 theError = "Error: Validator should be used with 2 parameters for extrusion.";
322 std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
324 AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
327 if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
331 AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
332 if(!aSelAttr.get()) {
333 theError = "Error: Could not get selection attribute \"" + *anArgsIt + "\".";
337 GeomShapePtr aShape = aSelAttr->value();
339 ResultPtr aContext = aSelAttr->context();
340 if(!aContext.get()) {
341 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
342 + "\" can not be used with default value. Select direction for extrusion.";
346 aShape = aContext->shape();
350 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
351 + "\" can not be used with default value. Select direction for extrusion.";
358 //==================================================================================================
359 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
364 //==================================================================================================
365 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
366 std::string& theError) const
368 if(!theAttribute.get()) {
372 std::string anAttributeType = theAttribute->attributeType();
373 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
374 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
375 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
376 // If at least one attribute is invalid, the result is false.
377 if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
381 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
383 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
384 ResultPtr aContext = anAttr->context();
385 if(!aContext.get()) {
389 GeomShapePtr aShape = anAttr->value();
390 GeomShapePtr aContextShape = aContext->shape();
392 aShape = aContextShape;
398 if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
399 aShape->shapeType() == GeomAPI_Shape::EDGE ||
400 !aShape->isPlanar()) {
410 //==================================================================================================
411 bool FeaturesPlugin_ValidatorBooleanSelection::isValid(const AttributePtr& theAttribute,
412 const std::list<std::string>& theArguments,
413 std::string& theError) const
415 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
416 if(!anAttrSelectionList.get()) {
417 theError = "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
420 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
421 int anOperationType = aFeature->integer("bool_type")->value();
423 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
424 AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
425 if(!anAttrSelection.get()) {
426 theError = "Error: Empty attribute selection.";
429 ResultPtr aContext = anAttrSelection->context();
430 if(!aContext.get()) {
431 theError = "Error: Empty selection context.";
434 ResultConstructionPtr aResultConstruction =
435 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
436 if(aResultConstruction.get()) {
437 theError = "Error: Result construction not allowed for selection.";
440 std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
442 aShape = aContext->shape();
445 theError = "Error: Empty shape.";
448 int aShapeType = aShape->shapeType();
449 if(anOperationType == 1) {
450 // Fuse operation. Allow to select edges, faces and solids.
451 if(aShapeType != GeomAPI_Shape::EDGE &&
452 aShapeType != GeomAPI_Shape::FACE &&
453 aShapeType != GeomAPI_Shape::SOLID &&
454 aShapeType != GeomAPI_Shape::COMPSOLID &&
455 aShapeType != GeomAPI_Shape::COMPOUND) {
456 theError = "Error: Selected shape has the wrong type.";
460 if(aShapeType != GeomAPI_Shape::SOLID &&
461 aShapeType != GeomAPI_Shape::COMPSOLID &&
462 aShapeType != GeomAPI_Shape::COMPOUND) {
463 theError = "Error: Selected shape has the wrong type.";
472 //==================================================================================================
473 bool FeaturesPlugin_ValidatorPartitionSelection::isValid(const AttributePtr& theAttribute,
474 const std::list<std::string>& theArguments,
475 std::string& theError) const
477 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
478 if(!anAttrSelectionList.get()) {
479 theError = "Error: This validator can only work with selection list in \"Partition\" feature.";
483 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
484 AttributeSelectionPtr aSelectAttr = anAttrSelectionList->value(anIndex);
486 //GeomValidators_BodyShapes aBodyValidator;
487 //if(aBodyValidator.isValid(aSelectAttr, theArguments, theError)) {
491 GeomValidators_FeatureKind aFeatureKindValidator;
492 if(aFeatureKindValidator.isValid(aSelectAttr, theArguments, theError)) {
496 ResultPtr aContext = aSelectAttr->context();
497 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
498 if(aResultConstruction.get()) {
502 theError = "Error: Only body shapes and construction planes are allowed for selection.";
509 //==================================================================================================
510 bool FeaturesPlugin_ValidatorRemoveSubShapesSelection::isValid(const AttributePtr& theAttribute,
511 const std::list<std::string>& theArguments,
512 std::string& theError) const
514 AttributeSelectionListPtr aSubShapesAttrList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
515 if(!aSubShapesAttrList.get()) {
516 theError = "Error: This validator can only work with selection list in \"Remove Sub-Shapes\" feature.";
520 static const std::string aBaseShapeID = "base_shape";
521 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
522 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeID);
524 if(!aShapeAttrSelection.get()) {
525 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
529 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
530 ResultPtr aContext = aShapeAttrSelection->context();
531 if(!aContext.get()) {
532 theError = "Error: Empty context.";
535 if(!aBaseShape.get()) {
536 aBaseShape = aContext->shape();
538 if(!aBaseShape.get()) {
539 theError = "Error: Empty base shape.";
543 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
544 bool isSameFound = false;
545 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
546 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
547 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
548 if(anIt.current()->isEqual(aShapeToAdd)) {
554 theError = "Error: Only sub-shapes of selected shape is allowed for selection.";
562 //==================================================================================================
563 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
564 const std::list<std::string>& theArguments,
565 std::string& theError) const
567 static const std::string aBaseShapeID = "base_shape";
568 static const std::string aSubShapesID = "subshapes";
570 if(theFeature->getKind() != "Remove_SubShapes") {
571 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
575 AttributeSelectionPtr aShapeAttrSelection = theFeature->selection(aBaseShapeID);
576 if(!aShapeAttrSelection.get()) {
577 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
581 AttributeSelectionListPtr aSubShapesAttrList = theFeature->selectionList(aSubShapesID);
582 if(!aSubShapesAttrList.get()) {
583 theError = "Error: Could not get \"" + aSubShapesID + "\" attribute.";
588 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
589 if(!aBaseShape.get()) {
592 GeomShapePtr aResultShape = aBaseShape->emptyCopied();
594 // Copy sub-shapes from list to new shape.
595 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
596 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
597 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
598 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
602 if(!GeomAlgoAPI_ShapeTools::isShapeValid(aResultShape)) {
603 theError = "Error: Resulting shape is not valid.";
610 //==================================================================================================
611 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isNotObligatory(std::string theFeature,
612 std::string theAttribute)