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_ValidatorPipeLocations::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
34 const std::list<std::string>& theArguments,
35 std::string& theError) const
37 static const std::string aCreationMethodID = "creation_method";
38 static const std::string aBaseObjectsID = "base_objects";
39 static const std::string aLocationsID = "locations_objects";
41 if(theFeature->getKind() != "Pipe") {
42 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
46 AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
47 if(!aCreationMethodAttr.get()) {
48 theError = "Error: Could not get \"" + aCreationMethodID + "\" attribute.";
52 if(aCreationMethodAttr->value() != "locations") {
56 AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
57 if(!aBaseObjectsSelectionList.get()) {
58 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
62 AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
63 if(!aLocationsSelectionList.get()) {
64 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
68 if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
69 theError = "Error: Number of locations should be the same as base objects.";
76 //==================================================================================================
77 bool FeaturesPlugin_ValidatorPipeLocations::isNotObligatory(std::string theFeature, std::string theAttribute)
82 //==================================================================================================
83 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
84 const std::list<std::string>& theArguments,
85 std::string& theError) const
87 if(theArguments.empty()) {
88 theError = "Error: Validator parameters is empty.";
92 // Checking attribute.
93 if(!isValidAttribute(theAttribute, theArguments, theError)) {
94 if(theError.empty()) {
95 theError = "Error: Attribute contains unacceptable shape.";
100 std::set<ResultConstructionPtr> aSelectedSketches;
101 std::set<ResultConstructionPtr> aSelectedSketchesFromObjects;
102 GeomAPI_DataMapOfShapeShape aSelectedWiresFromObjects;
103 std::string anAttributeType = theAttribute->attributeType();
104 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
105 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
106 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
107 AttributeSelectionPtr aSelectionAttr = aListAttr->value(anIndex);
108 ResultConstructionPtr aContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelectionAttr->context());
109 if(!aContext.get()) {
110 // It is not a result construction, continue.
114 GeomShapePtr aShape = aSelectionAttr->value();
115 GeomShapePtr aContextShape = aContext->shape();
117 // Whole sketch selected.
118 if(aSelectedSketchesFromObjects.find(aContext) != aSelectedSketchesFromObjects.cend()) {
119 theError = "Error: Object from this sketch is already selected. Sketch is not allowed for selection.";
123 aSelectedSketches.insert(aContext);
125 // Object from sketch selected.
126 if(aSelectedSketches.find(aContext) != aSelectedSketches.cend()) {
127 theError = "Error: Whole sketch with this object is already selected. Don't allow to select this object.";
131 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
132 GeomShapePtr aWire = anExp.current();
133 if(aWire->orientation() != GeomAPI_Shape::FORWARD) {
134 theError = "Error: Wire with wrong orientation selected.";
138 if(aSelectedWiresFromObjects.isBound(aWire)) {
139 theError = "Error: Objects with such wire already selected. Don't allow to select this object.";
143 aSelectedWiresFromObjects.bind(aWire, aWire);
144 aSelectedSketchesFromObjects.insert(aContext);
153 //==================================================================================================
154 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
155 const std::list<std::string>& theArguments,
156 std::string& theError) const
158 if(!theAttribute.get()) {
159 theError = "Error: Empty attribute.";
163 std::string anAttributeType = theAttribute->attributeType();
164 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
165 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
166 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
167 // If at least one attribute is invalid, the result is false.
168 if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
172 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
174 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
175 ResultPtr aContext = anAttr->context();
176 if(!aContext.get()) {
177 theError = "Error: Attribute have empty context.";
181 GeomShapePtr aShape = anAttr->value();
182 GeomShapePtr aContextShape = aContext->shape();
184 aShape = aContextShape;
187 theError = "Error: Empty shape selected";
191 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
192 if(aConstruction.get()) {
193 // Construciotn selected. Check that is is not infinite.
194 if(aConstruction->isInfinite()) {
195 theError = "Error: Infinite constructions is not allowed as base.";
199 if(aShape->isEqual(aContextShape)) {
200 // Whole construction selected. Check that it have faces.
201 if(aConstruction->facesNum() > 0) {
205 // Shape on construction selected. Check that it is a face or wire.
206 if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
214 if(!aShape->isEqual(aContextShape)) {
215 // Local selection on body does not allowed.
216 theError = "Error: Selected shape is in the local selection. Only global selection is allowed.";
220 // Check that object is a shape with allowed type.
221 GeomValidators_ShapeType aShapeTypeValidator;
222 if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
223 theError = "Error: Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, "
224 "whole sketch(if it has at least one face), and whole objects with shape types: ";
225 std::list<std::string>::const_iterator anIt = theArguments.cbegin();
227 for(++anIt; anIt != theArguments.cend(); ++anIt) {
228 theError += ", " + *anIt;
234 theError = "Error: Attribute \"" + anAttributeType + "\" does not supported by this validator.";
241 //==================================================================================================
242 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
243 const std::list<std::string>& theArguments,
244 std::string& theError) const
246 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
247 theError = "Error: The attribute with the " + theAttribute->attributeType() + " type is not processed";
250 if (theArguments.size() != 2) {
251 theError = "Error: Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
254 // first argument is for the base attribute, second - for skipping feature kind
255 std::list<std::string>::const_iterator anIt = theArguments.begin();
256 std::string aBaseAttributeId = *anIt;
257 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
258 AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
259 if (!aBaseAttribute.get()) {
260 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
263 if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
264 // this validator is not necessary anymore
268 std::string aFeatureAttributeKind = *anIt;
269 GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
270 // check whether the selection is on the sketch
271 std::list<std::string> anArguments;
272 anArguments.push_back(aFeatureAttributeKind);
274 bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
275 bool aPlanarFace = false;
276 // check if selection has Face selected
277 GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
279 anArguments.push_back("face");
280 aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
282 bool aValid = !aFeatureKind && aPlanarFace;
286 //==================================================================================================
287 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
288 const std::list<std::string>& theArguments,
289 std::string& theError) const
291 if(theArguments.size() != 2) {
292 theError = "Error: Validator should be used with 2 parameters for extrusion.";
296 std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
298 AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
301 if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
305 AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
306 if(!aSelAttr.get()) {
307 theError = "Error: Could not get selection attribute \"" + *anArgsIt + "\".";
311 GeomShapePtr aShape = aSelAttr->value();
313 ResultPtr aContext = aSelAttr->context();
314 if(!aContext.get()) {
315 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
316 + "\" can not be used with default value. Select direction for extrusion.";
320 aShape = aContext->shape();
324 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
325 + "\" can not be used with default value. Select direction for extrusion.";
332 //==================================================================================================
333 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
338 //==================================================================================================
339 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
340 std::string& theError) const
342 if(!theAttribute.get()) {
346 std::string anAttributeType = theAttribute->attributeType();
347 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
348 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
349 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
350 // If at least one attribute is invalid, the result is false.
351 if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
355 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
357 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
358 ResultPtr aContext = anAttr->context();
359 if(!aContext.get()) {
363 GeomShapePtr aShape = anAttr->value();
364 GeomShapePtr aContextShape = aContext->shape();
366 aShape = aContextShape;
372 if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
373 aShape->shapeType() == GeomAPI_Shape::EDGE ||
374 !aShape->isPlanar()) {
384 //==================================================================================================
385 bool FeaturesPlugin_ValidatorBooleanSelection::isValid(const AttributePtr& theAttribute,
386 const std::list<std::string>& theArguments,
387 std::string& theError) const
389 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
390 if(!anAttrSelectionList.get()) {
391 theError = "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
394 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
395 int anOperationType = aFeature->integer("bool_type")->value();
397 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
398 AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
399 if(!anAttrSelection.get()) {
400 theError = "Error: Empty attribute selection.";
403 ResultPtr aContext = anAttrSelection->context();
404 if(!aContext.get()) {
405 theError = "Error: Empty selection context.";
408 ResultConstructionPtr aResultConstruction =
409 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
410 if(aResultConstruction.get()) {
411 theError = "Error: Result construction not allowed for selection.";
414 std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
416 aShape = aContext->shape();
419 theError = "Error: Empty shape.";
422 int aShapeType = aShape->shapeType();
423 if(anOperationType == 1) {
424 // Fuse operation. Allow to select edges, faces and solids.
425 if(aShapeType != GeomAPI_Shape::EDGE &&
426 aShapeType != GeomAPI_Shape::FACE &&
427 aShapeType != GeomAPI_Shape::SOLID &&
428 aShapeType != GeomAPI_Shape::COMPSOLID &&
429 aShapeType != GeomAPI_Shape::COMPOUND) {
430 theError = "Error: Selected shape has the wrong type.";
434 if(aShapeType != GeomAPI_Shape::SOLID &&
435 aShapeType != GeomAPI_Shape::COMPSOLID &&
436 aShapeType != GeomAPI_Shape::COMPOUND) {
437 theError = "Error: Selected shape has the wrong type.";
446 //==================================================================================================
447 bool FeaturesPlugin_ValidatorPartitionSelection::isValid(const AttributePtr& theAttribute,
448 const std::list<std::string>& theArguments,
449 std::string& theError) const
451 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
452 if(!anAttrSelectionList.get()) {
453 theError = "Error: This validator can only work with selection list in \"Partition\" feature.";
457 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
458 AttributeSelectionPtr aSelectAttr = anAttrSelectionList->value(anIndex);
460 //GeomValidators_BodyShapes aBodyValidator;
461 //if(aBodyValidator.isValid(aSelectAttr, theArguments, theError)) {
465 GeomValidators_FeatureKind aFeatureKindValidator;
466 if(aFeatureKindValidator.isValid(aSelectAttr, theArguments, theError)) {
470 ResultPtr aContext = aSelectAttr->context();
471 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
472 if(aResultConstruction.get()) {
476 theError = "Error: Only body shapes and construction planes are allowed for selection.";
483 //==================================================================================================
484 bool FeaturesPlugin_ValidatorRemoveSubShapesSelection::isValid(const AttributePtr& theAttribute,
485 const std::list<std::string>& theArguments,
486 std::string& theError) const
488 AttributeSelectionListPtr aSubShapesAttrList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
489 if(!aSubShapesAttrList.get()) {
490 theError = "Error: This validator can only work with selection list in \"Remove Sub-Shapes\" feature.";
494 static const std::string aBaseShapeID = "base_shape";
495 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
496 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeID);
498 if(!aShapeAttrSelection.get()) {
499 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
503 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
504 ResultPtr aContext = aShapeAttrSelection->context();
505 if(!aContext.get()) {
506 theError = "Error: Empty context.";
509 if(!aBaseShape.get()) {
510 aBaseShape = aContext->shape();
512 if(!aBaseShape.get()) {
513 theError = "Error: Empty base shape.";
517 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
518 bool isSameFound = false;
519 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
520 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
521 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
522 if(anIt.current()->isEqual(aShapeToAdd)) {
528 theError = "Error: Only sub-shapes of selected shape is allowed for selection.";
536 //==================================================================================================
537 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
538 const std::list<std::string>& theArguments,
539 std::string& theError) const
541 static const std::string aBaseShapeID = "base_shape";
542 static const std::string aSubShapesID = "subshapes";
544 if(theFeature->getKind() != "Remove_SubShapes") {
545 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
549 AttributeSelectionPtr aShapeAttrSelection = theFeature->selection(aBaseShapeID);
550 if(!aShapeAttrSelection.get()) {
551 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
555 AttributeSelectionListPtr aSubShapesAttrList = theFeature->selectionList(aSubShapesID);
556 if(!aSubShapesAttrList.get()) {
557 theError = "Error: Could not get \"" + aSubShapesID + "\" attribute.";
562 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
563 if(!aBaseShape.get()) {
566 GeomShapePtr aResultShape = aBaseShape->emptyCopied();
568 // Copy sub-shapes from list to new shape.
569 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
570 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
571 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
572 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
576 if(!GeomAlgoAPI_ShapeTools::isShapeValid(aResultShape)) {
577 theError = "Error: Resulting shape is not valid.";
584 //==================================================================================================
585 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isNotObligatory(std::string theFeature,
586 std::string theAttribute)