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_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_AttributeReference.h>
15 #include <Events_Error.h>
17 #include <GeomValidators_FeatureKind.h>
18 #include <GeomValidators_ShapeType.h>
20 #include <GeomAPI_PlanarEdges.h>
21 #include <GeomAlgoAPI_WireBuilder.h>
23 //=================================================================================================
24 bool FeaturesPlugin_ValidatorPipeLocations::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
25 const std::list<std::string>& theArguments,
26 std::string& theError) const
28 static const std::string aCreationMethodID = "creation_method";
29 static const std::string aBaseObjectsID = "base_objects";
30 static const std::string aLocationsID = "locations_objects";
32 if(theFeature->getKind() != "Pipe") {
33 theError = "Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
37 AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
38 if(!aCreationMethodAttr.get()) {
39 theError = "Could not get \"" + aCreationMethodID + "\" attribute.";
43 if(aCreationMethodAttr->value() != "locations") {
47 AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
48 if(!aBaseObjectsSelectionList.get()) {
49 theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
53 AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
54 if(!aLocationsSelectionList.get()) {
55 theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
59 if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
60 theError = "Number of locations should be the same as base objects.";
67 //=================================================================================================
68 bool FeaturesPlugin_ValidatorPipeLocations::isNotObligatory(std::string theFeature, std::string theAttribute)
73 //=================================================================================================
74 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
75 const std::list<std::string>& theArguments,
76 std::string& theError) const
78 if(theArguments.empty()) {
79 theError = "Validator parameters is empty.";
83 // Checking attribute.
84 if(!isValidAttribute(theAttribute, theArguments, theError)) {
85 if(theError.empty()) {
86 theError = "Attribute contains unacceptable shape.";
94 //=================================================================================================
95 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
96 const std::list<std::string>& theArguments,
97 std::string& theError) const
99 if(!theAttribute.get()) {
100 theError = "Empty attribute.";
104 std::string anAttributeType = theAttribute->attributeType();
105 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
106 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
107 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
108 // If at least one attribute is invalid, the result is false.
109 if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
113 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
115 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
116 ResultPtr aContext = anAttr->context();
117 if(!aContext.get()) {
118 theError = "Attribute have empty context.";
122 GeomShapePtr aShape = anAttr->value();
123 GeomShapePtr aContextShape = aContext->shape();
125 aShape = aContextShape;
128 theError = "Empty shape selected";
132 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
133 if(aConstruction.get()) {
134 // Construciotn selected. Check that is is not infinite.
135 if(aConstruction->isInfinite()) {
136 theError = "Infinite constructions is not allowed as base.";
140 if(aShape->isEqual(aContextShape)) {
141 // Whole construction selected. Check that it have faces.
142 if(aConstruction->facesNum() > 0) {
146 // Shape on construction selected. Check that it is a face or wire.
147 if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
155 if(!aShape->isEqual(aContextShape)) {
156 // Local selection on body does not allowed.
157 theError = "Selected shape is in the local selection. Only global selection is allowed.";
161 // Check that object is a shape with allowed type.
162 GeomValidators_ShapeType aShapeTypeValidator;
163 if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
164 theError = "Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, \
165 whole sketch(if it has at least one face), and whole objects with shape types: ";
166 std::list<std::string>::const_iterator anIt = theArguments.cbegin();
168 for(++anIt; anIt != theArguments.cend(); ++anIt) {
169 theError += ", " + *anIt;
175 theError = "Following attribute does not supported: " + anAttributeType + ".";
182 //=================================================================================================
183 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
184 const std::list<std::string>& theArguments,
185 std::string& theError) const
187 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
188 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
191 if (theArguments.size() != 2) {
192 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
195 // first argument is for the base attribute, second - for skipping feature kind
196 std::list<std::string>::const_iterator anIt = theArguments.begin();
197 std::string aBaseAttributeId = *anIt;
198 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
199 AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
200 if (!aBaseAttribute.get()) {
201 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
204 if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
205 // this validator is not necessary anymore
209 std::string aFeatureAttributeKind = *anIt;
210 GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
211 // check whether the selection is on the sketch
212 std::list<std::string> anArguments;
213 anArguments.push_back(aFeatureAttributeKind);
215 bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
216 bool aPlanarFace = false;
217 // check if selection has Face selected
218 GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
220 anArguments.push_back("face");
221 aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
223 bool aValid = !aFeatureKind && aPlanarFace;
227 //=================================================================================================
228 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
229 const std::list<std::string>& theArguments,
230 std::string& theError) const
232 if(theArguments.size() != 2) {
233 theError = "Validator should be used with 2 parameters for extrusion.";
237 std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
239 AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
242 if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
246 AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
247 if(!aSelAttr.get()) {
248 theError = "Could not get selection attribute \"" + *anArgsIt + "\".";
252 GeomShapePtr aShape = aSelAttr->value();
254 ResultPtr aContext = aSelAttr->context();
255 if(!aContext.get()) {
256 theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
260 aShape = aContext->shape();
264 theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
271 //=================================================================================================
272 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
277 //=================================================================================================
278 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
279 std::string& theError) const
281 if(!theAttribute.get()) {
285 std::string anAttributeType = theAttribute->attributeType();
286 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
287 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
288 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
289 // If at least one attribute is invalid, the result is false.
290 if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
294 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
296 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
297 ResultPtr aContext = anAttr->context();
298 if(!aContext.get()) {
302 GeomShapePtr aShape = anAttr->value();
303 GeomShapePtr aContextShape = aContext->shape();
305 aShape = aContextShape;
311 if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
312 aShape->shapeType() == GeomAPI_Shape::EDGE ||
313 !aShape->isPlanar()) {
323 //=================================================================================================
324 bool FeaturesPlugin_ValidatorBaseForWire::isValid(const AttributePtr& theAttribute,
325 const std::list<std::string>& theArguments,
326 std::string& theError) const
328 // Get base objects list.
329 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
330 Events_Error::send("Validator does not support attribute type \"" + theAttribute->attributeType()
331 + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
334 AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
335 if(!aSelectionList.get()) {
336 theError = "Could not get selection list.";
339 if(aSelectionList->size() == 0) {
340 theError = "Empty selection list.";
344 // Collect base shapes.
345 ListOfShape aListOfShapes;
346 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
347 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
348 if(!aSelection.get()) {
349 theError = "Could not get selection.";
352 ResultPtr aContext = aSelection->context();
353 if(!aContext.get()) {
354 theError = "Attribute have empty context.";
358 GeomShapePtr aShape = aSelection->value();
359 GeomShapePtr aContextShape = aContext->shape();
361 aShape = aContextShape;
364 theError = "Empty shape selected.";
368 // Check that shape has acceptable type.
369 if(aShape->shapeType() != GeomAPI_Shape::EDGE && aShape->shapeType() != GeomAPI_Shape::WIRE) {
370 theError = "Selected shape has wrong type. Only edges and wires acceptable.";
374 // Check that it is edge on sketch.
375 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
376 if(aConstruction.get()) {
377 if(aConstruction->isInfinite()) {
378 theError = "Inifinte objects not acceptable.";
382 std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
384 // It is not an edge on the sketch.
385 // Check that it is not local selection.
386 if(!aShape->isEqual(aContextShape)) {
387 // Local selection on body does not allowed.
388 theError = "Selected shape is in the local selection. Only global selection is allowed.";
394 aListOfShapes.push_back(aShape);
398 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
400 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";