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) {
153 if(!aShape->isEqual(aContextShape)) {
154 // Local selection on body does not allowed.
155 theError = "Selected shape is in the local selection. Only global selection is allowed.";
159 // Check that object is a shape with allowed type.
160 GeomValidators_ShapeType aShapeTypeValidator;
161 if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
162 theError = "Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, \
163 whole sketch(if it has at least one face), and whole objects with shape types: ";
164 std::list<std::string>::const_iterator anIt = theArguments.cbegin();
166 for(++anIt; anIt != theArguments.cend(); ++anIt) {
167 theError += ", " + *anIt;
173 theError = "Following attribute does not supported: " + anAttributeType + ".";
180 //=================================================================================================
181 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
182 const std::list<std::string>& theArguments,
183 std::string& theError) const
185 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
186 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
189 if (theArguments.size() != 2) {
190 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
193 // first argument is for the base attribute, second - for skipping feature kind
194 std::list<std::string>::const_iterator anIt = theArguments.begin();
195 std::string aBaseAttributeId = *anIt;
196 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
197 AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
198 if (!aBaseAttribute.get()) {
199 theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
202 if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
203 // this validator is not necessary anymore
207 std::string aFeatureAttributeKind = *anIt;
208 GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
209 // check whether the selection is on the sketch
210 std::list<std::string> anArguments;
211 anArguments.push_back(aFeatureAttributeKind);
213 bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
214 bool aPlanarFace = false;
215 // check if selection has Face selected
216 GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
218 anArguments.push_back("face");
219 aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
221 bool aValid = !aFeatureKind && aPlanarFace;
225 //=================================================================================================
226 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
227 const std::list<std::string>& theArguments,
228 std::string& theError) const
230 if(theArguments.size() != 2) {
231 theError = "Validator should be used with 2 parameters for extrusion.";
235 std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
237 AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
240 if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
244 AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
245 if(!aSelAttr.get()) {
246 theError = "Could not get selection attribute \"" + *anArgsIt + "\".";
250 GeomShapePtr aShape = aSelAttr->value();
252 ResultPtr aContext = aSelAttr->context();
253 if(!aContext.get()) {
254 theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
258 aShape = aContext->shape();
262 theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
269 //=================================================================================================
270 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
275 //=================================================================================================
276 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
277 std::string& theError) const
279 if(!theAttribute.get()) {
283 std::string anAttributeType = theAttribute->attributeType();
284 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
285 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
286 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
287 // If at least one attribute is invalid, the result is false.
288 if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
292 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
294 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
295 ResultPtr aContext = anAttr->context();
296 if(!aContext.get()) {
300 GeomShapePtr aShape = anAttr->value();
301 GeomShapePtr aContextShape = aContext->shape();
303 aShape = aContextShape;
309 if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
310 aShape->shapeType() == GeomAPI_Shape::EDGE ||
311 !aShape->isPlanar()) {
321 //=================================================================================================
322 bool FeaturesPlugin_ValidatorBaseForWire::isValid(const AttributePtr& theAttribute,
323 const std::list<std::string>& theArguments,
324 std::string& theError) const
326 // Get base objects list.
327 if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
328 Events_Error::send("Validator does not support attribute type \"" + theAttribute->attributeType()
329 + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
332 AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
333 if(!aSelectionList.get()) {
334 theError = "Could not get selection list.";
337 if(aSelectionList->size() == 0) {
338 theError = "Empty selection list.";
342 // Collect base shapes.
343 ListOfShape aListOfShapes;
344 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
345 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
346 if(!aSelection.get()) {
347 theError = "Could not get selection.";
350 ResultPtr aContext = aSelection->context();
351 if(!aContext.get()) {
352 theError = "Attribute have empty context.";
356 GeomShapePtr aShape = aSelection->value();
357 GeomShapePtr aContextShape = aContext->shape();
359 aShape = aContextShape;
362 theError = "Empty shape selected.";
366 // Check that shape has acceptable type.
367 if(aShape->shapeType() != GeomAPI_Shape::EDGE && aShape->shapeType() != GeomAPI_Shape::WIRE) {
368 theError = "Selected shape has wrong type. Only edges and wires acceptable.";
372 // Check that it is edge on sketch.
373 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
374 if(aConstruction.get()) {
375 if(aConstruction->isInfinite()) {
376 theError = "Inifinte objects not acceptable.";
380 std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
382 // It is not an edge on the sketch.
383 // Check that it is not local selection.
384 if(!aShape->isEqual(aContextShape)) {
385 // Local selection on body does not allowed.
386 theError = "Selected shape is in the local selection. Only global selection is allowed.";
392 aListOfShapes.push_back(aShape);
396 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
398 theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";