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 <Events_Error.h>
20 #include <GeomValidators_BodyShapes.h>
21 #include <GeomValidators_FeatureKind.h>
22 #include <GeomValidators_ShapeType.h>
24 #include <GeomAPI_DataMapOfShapeShape.h>
25 #include <GeomAPI_PlanarEdges.h>
26 #include <GeomAPI_ShapeExplorer.h>
27 #include <GeomAPI_ShapeIterator.h>
29 #include <GeomAlgoAPI_ShapeBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_WireBuilder.h>
33 //==================================================================================================
34 bool FeaturesPlugin_ValidatorPipePath::isValid(const AttributePtr& theAttribute,
35 const std::list<std::string>& theArguments,
36 std::string& theError) const
38 AttributeSelectionPtr aPathAttrSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
39 if(!aPathAttrSelection.get()) {
40 theError = "Error: This validator can only work with path selector in \"Pipe\" feature.";
44 GeomShapePtr aPathShape = aPathAttrSelection->value();
45 ResultPtr aContext = aPathAttrSelection->context();
47 theError = "Error: Empty context.";
50 GeomShapePtr aContextShape = aContext->shape();
51 if(aPathShape.get() && aPathShape->shapeType() == GeomAPI_Shape::WIRE && !aPathShape->isEqual(aContextShape)) {
52 theError = "Error: Local selection of wires not allowed.";
59 //==================================================================================================
60 bool FeaturesPlugin_ValidatorPipeLocations::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
61 const std::list<std::string>& theArguments,
62 std::string& theError) const
64 static const std::string aCreationMethodID = "creation_method";
65 static const std::string aBaseObjectsID = "base_objects";
66 static const std::string aLocationsID = "locations_objects";
68 if(theFeature->getKind() != "Pipe") {
69 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
73 AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
74 if(!aCreationMethodAttr.get()) {
75 theError = "Error: Could not get \"" + aCreationMethodID + "\" attribute.";
79 if(aCreationMethodAttr->value() != "locations") {
83 AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
84 if(!aBaseObjectsSelectionList.get()) {
85 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
89 AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
90 if(!aLocationsSelectionList.get()) {
91 theError = "Error: Could not get \"" + aBaseObjectsID + "\" attribute.";
95 if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
96 theError = "Error: Number of locations should be the same as base objects.";
103 //==================================================================================================
104 bool FeaturesPlugin_ValidatorPipeLocations::isNotObligatory(std::string theFeature, std::string theAttribute)
109 //==================================================================================================
110 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
111 const std::list<std::string>& theArguments,
112 std::string& theError) const
114 if(theArguments.empty()) {
115 theError = "Error: Validator parameters is empty.";
119 // Checking attribute.
120 if(!isValidAttribute(theAttribute, theArguments, theError)) {
121 if(theError.empty()) {
122 theError = "Error: Attribute contains unacceptable shape.";
127 std::set<ResultConstructionPtr> aSelectedSketches;
128 std::set<ResultConstructionPtr> aSelectedSketchesFromObjects;
129 GeomAPI_DataMapOfShapeShape aSelectedWiresFromObjects;
130 std::string anAttributeType = theAttribute->attributeType();
131 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
132 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
133 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
134 AttributeSelectionPtr aSelectionAttr = aListAttr->value(anIndex);
135 ResultPtr aContext = aSelectionAttr->context();
136 if(!aContext.get()) {
137 theError = "Error: Empty context.";
141 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
142 if(!aResultConstruction.get()) {
143 // It is not a result construction. If shape is compound check that it contains only faces and edges.
144 GeomShapePtr aShape = aSelectionAttr->value();
146 aShape = aContext->shape();
149 if(aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
150 for(GeomAPI_ShapeIterator anIt(aShape); anIt.more(); anIt.next()) {
151 GeomShapePtr aSubShape = anIt.current();
152 if(aSubShape->shapeType() != GeomAPI_Shape::EDGE
153 && aSubShape->shapeType() != GeomAPI_Shape::FACE) {
154 theError = "Error: Compound should contain only faces and edges.";
163 GeomShapePtr aShape = aSelectionAttr->value();
164 GeomShapePtr aContextShape = aResultConstruction->shape();
166 // Whole sketch selected.
167 if(aSelectedSketchesFromObjects.find(aResultConstruction) != aSelectedSketchesFromObjects.cend()) {
168 theError = "Error: Object from this sketch is already selected. Sketch is not allowed for selection.";
172 aSelectedSketches.insert(aResultConstruction);
174 // Object from sketch selected.
175 if(aSelectedSketches.find(aResultConstruction) != aSelectedSketches.cend()) {
176 theError = "Error: Whole sketch with this object is already selected. Don't allow to select this object.";
180 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
181 GeomShapePtr aWire = anExp.current();
182 if(aWire->orientation() != GeomAPI_Shape::FORWARD) {
183 theError = "Error: Wire with wrong orientation selected.";
187 if(aSelectedWiresFromObjects.isBound(aWire)) {
188 theError = "Error: Objects with such wire already selected. Don't allow to select this object.";
192 aSelectedWiresFromObjects.bind(aWire, aWire);
193 aSelectedSketchesFromObjects.insert(aResultConstruction);
202 //==================================================================================================
203 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
204 const std::list<std::string>& theArguments,
205 std::string& theError) const
207 if(!theAttribute.get()) {
208 theError = "Error: Empty attribute.";
212 std::string anAttributeType = theAttribute->attributeType();
213 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
214 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
215 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
216 // If at least one attribute is invalid, the result is false.
217 if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
221 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
223 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
224 ResultPtr aContext = anAttr->context();
225 if(!aContext.get()) {
226 theError = "Error: Attribute have empty context.";
230 GeomShapePtr aShape = anAttr->value();
231 GeomShapePtr aContextShape = aContext->shape();
233 aShape = aContextShape;
236 theError = "Error: Empty shape selected";
240 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
241 if(aConstruction.get()) {
242 // Construciotn selected. Check that is is not infinite.
243 if(aConstruction->isInfinite()) {
244 theError = "Error: Infinite constructions is not allowed as base.";
248 if(aShape->isEqual(aContextShape)) {
249 // Whole construction selected. Check that it have faces.
250 if(aConstruction->facesNum() > 0) {
254 // Shape on construction selected. Check that it is a face or wire.
255 if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
263 if(!aShape->isEqual(aContextShape)) {
264 // Local selection on body does not allowed.
265 theError = "Error: Selected shape is in the local selection. Only global selection is allowed.";
269 // Check that object is a shape with allowed type.
270 GeomValidators_ShapeType aShapeTypeValidator;
271 if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
272 theError = "Error: Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, "
273 "whole sketch(if it has at least one face), and whole objects with shape types: ";
274 std::list<std::string>::const_iterator anIt = theArguments.cbegin();
276 for(++anIt; anIt != theArguments.cend(); ++anIt) {
277 theError += ", " + *anIt;
283 theError = "Error: Attribute \"" + anAttributeType + "\" does not supported by this validator.";
290 //==================================================================================================
291 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
292 const std::list<std::string>& theArguments,
293 std::string& theError) const
295 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
296 theError = "Error: The attribute with the " + theAttribute->attributeType() + " type is not processed";
299 if (theArguments.size() != 2) {
300 theError = "Error: Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
303 // first argument is for the base attribute, second - for skipping feature kind
304 std::list<std::string>::const_iterator anIt = theArguments.begin();
305 std::string aBaseAttributeId = *anIt;
306 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
307 AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
308 if (!aBaseAttribute.get()) {
309 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
312 if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
313 // this validator is not necessary anymore
317 std::string aFeatureAttributeKind = *anIt;
318 GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
319 // check whether the selection is on the sketch
320 std::list<std::string> anArguments;
321 anArguments.push_back(aFeatureAttributeKind);
323 bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
324 bool aPlanarFace = false;
325 // check if selection has Face selected
326 GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
328 anArguments.push_back("face");
329 aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
331 bool aValid = !aFeatureKind && aPlanarFace;
335 //==================================================================================================
336 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
337 const std::list<std::string>& theArguments,
338 std::string& theError) const
340 if(theArguments.size() != 2) {
341 theError = "Error: Validator should be used with 2 parameters for extrusion.";
345 std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
347 AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
350 if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
354 AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
355 if(!aSelAttr.get()) {
356 theError = "Error: Could not get selection attribute \"" + *anArgsIt + "\".";
360 GeomShapePtr aShape = aSelAttr->value();
362 ResultPtr aContext = aSelAttr->context();
363 if(!aContext.get()) {
364 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
365 + "\" can not be used with default value. Select direction for extrusion.";
369 aShape = aContext->shape();
373 theError = "Error: Base objects list contains vertex or edge, so attribute \"" + *anArgsIt
374 + "\" can not be used with default value. Select direction for extrusion.";
381 //==================================================================================================
382 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
387 //==================================================================================================
388 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
389 std::string& theError) const
391 if(!theAttribute.get()) {
395 std::string anAttributeType = theAttribute->attributeType();
396 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
397 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
398 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
399 // If at least one attribute is invalid, the result is false.
400 if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
404 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
406 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
407 ResultPtr aContext = anAttr->context();
408 if(!aContext.get()) {
412 GeomShapePtr aShape = anAttr->value();
413 GeomShapePtr aContextShape = aContext->shape();
415 aShape = aContextShape;
421 if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
422 aShape->shapeType() == GeomAPI_Shape::EDGE ||
423 !aShape->isPlanar()) {
433 //==================================================================================================
434 bool FeaturesPlugin_ValidatorBooleanSelection::isValid(const AttributePtr& theAttribute,
435 const std::list<std::string>& theArguments,
436 std::string& theError) const
438 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
439 if(!anAttrSelectionList.get()) {
440 theError = "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
443 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
444 int anOperationType = aFeature->integer("bool_type")->value();
446 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
447 AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
448 if(!anAttrSelection.get()) {
449 theError = "Error: Empty attribute selection.";
452 ResultPtr aContext = anAttrSelection->context();
453 if(!aContext.get()) {
454 theError = "Error: Empty selection context.";
457 ResultConstructionPtr aResultConstruction =
458 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
459 if(aResultConstruction.get()) {
460 theError = "Error: Result construction not allowed for selection.";
463 std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
465 aShape = aContext->shape();
468 theError = "Error: Empty shape.";
471 int aShapeType = aShape->shapeType();
472 if(anOperationType == 1) {
473 // Fuse operation. Allow to select edges, faces and solids.
474 if(aShapeType != GeomAPI_Shape::EDGE &&
475 aShapeType != GeomAPI_Shape::FACE &&
476 aShapeType != GeomAPI_Shape::SOLID &&
477 aShapeType != GeomAPI_Shape::COMPSOLID &&
478 aShapeType != GeomAPI_Shape::COMPOUND) {
479 theError = "Error: Selected shape has the wrong type.";
483 if(aShapeType != GeomAPI_Shape::SOLID &&
484 aShapeType != GeomAPI_Shape::COMPSOLID &&
485 aShapeType != GeomAPI_Shape::COMPOUND) {
486 theError = "Error: Selected shape has the wrong type.";
495 //==================================================================================================
496 bool FeaturesPlugin_ValidatorPartitionSelection::isValid(const AttributePtr& theAttribute,
497 const std::list<std::string>& theArguments,
498 std::string& theError) const
500 AttributeSelectionListPtr anAttrSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
501 if(!anAttrSelectionList.get()) {
502 theError = "Error: This validator can only work with selection list in \"Partition\" feature.";
506 for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
507 AttributeSelectionPtr aSelectAttr = anAttrSelectionList->value(anIndex);
509 //GeomValidators_BodyShapes aBodyValidator;
510 //if(aBodyValidator.isValid(aSelectAttr, theArguments, theError)) {
514 GeomValidators_FeatureKind aFeatureKindValidator;
515 if(aFeatureKindValidator.isValid(aSelectAttr, theArguments, theError)) {
519 ResultPtr aContext = aSelectAttr->context();
520 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
521 if(aResultConstruction.get()) {
522 theError = "Error: Only body shapes and construction planes are allowed for selection.";
526 ResultCompSolidPtr aResultCompsolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
527 if(aResultCompsolid.get()) {
531 theError = "Error: Only body shapes and construction planes are allowed for selection.";
539 //==================================================================================================
540 bool FeaturesPlugin_ValidatorRemoveSubShapesSelection::isValid(const AttributePtr& theAttribute,
541 const std::list<std::string>& theArguments,
542 std::string& theError) const
544 AttributeSelectionListPtr aSubShapesAttrList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
545 if(!aSubShapesAttrList.get()) {
546 theError = "Error: This validator can only work with selection list in \"Remove Sub-Shapes\" feature.";
550 static const std::string aBaseShapeID = "base_shape";
551 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
552 AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeID);
554 if(!aShapeAttrSelection.get()) {
555 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
559 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
560 ResultPtr aContext = aShapeAttrSelection->context();
561 if(!aContext.get()) {
562 theError = "Error: Empty context.";
565 if(!aBaseShape.get()) {
566 aBaseShape = aContext->shape();
568 if(!aBaseShape.get()) {
569 theError = "Error: Empty base shape.";
573 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
574 bool isSameFound = false;
575 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
576 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
577 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
578 if(anIt.current()->isEqual(aShapeToAdd)) {
584 theError = "Error: Only sub-shapes of selected shape is allowed for selection.";
592 //==================================================================================================
593 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
594 const std::list<std::string>& theArguments,
595 std::string& theError) const
597 static const std::string aBaseShapeID = "base_shape";
598 static const std::string aSubShapesID = "subshapes";
600 if(theFeature->getKind() != "Remove_SubShapes") {
601 theError = "Error: Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
605 AttributeSelectionPtr aShapeAttrSelection = theFeature->selection(aBaseShapeID);
606 if(!aShapeAttrSelection.get()) {
607 theError = "Error: Could not get \"" + aBaseShapeID + "\" attribute.";
611 AttributeSelectionListPtr aSubShapesAttrList = theFeature->selectionList(aSubShapesID);
612 if(!aSubShapesAttrList.get()) {
613 theError = "Error: Could not get \"" + aSubShapesID + "\" attribute.";
618 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
619 if(!aBaseShape.get()) {
622 GeomShapePtr aResultShape = aBaseShape->emptyCopied();
624 // Copy sub-shapes from list to new shape.
625 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
626 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
627 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
628 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
632 if(!GeomAlgoAPI_ShapeTools::isShapeValid(aResultShape)) {
633 theError = "Error: Resulting shape is not valid.";
640 //==================================================================================================
641 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isNotObligatory(std::string theFeature,
642 std::string theAttribute)