1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 #include "GeomValidators_ShapeType.h"
5 #include "GeomValidators_Tools.h"
7 #include <GeomAPI_Curve.h>
8 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_AttributeRefAttr.h>
13 #include <ModelAPI_AttributeSelectionList.h>
14 #include <ModelAPI_AttributeReference.h>
16 #include <Events_InfoMessage.h>
22 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
24 static EdgeTypes MyShapeTypes;
25 GeomValidators_ShapeType::TypeOfShape
26 GeomValidators_ShapeType::shapeType(const std::string& theType)
28 if (MyShapeTypes.size() == 0) {
29 MyShapeTypes["empty"] = Empty;
30 MyShapeTypes["vertex"] = Vertex;
31 MyShapeTypes["edge"] = Edge;
32 MyShapeTypes["line"] = Line;
33 MyShapeTypes["circle"] = Circle;
34 MyShapeTypes["wire"] = Wire;
35 MyShapeTypes["face"] = Face;
36 MyShapeTypes["plane"] = Plane;
37 MyShapeTypes["shell"] = Shell;
38 MyShapeTypes["solid"] = Solid;
39 MyShapeTypes["compsolid"] = CompSolid;
40 MyShapeTypes["compound"] = Compound;
42 std::string aType = std::string(theType.c_str());
43 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
44 return MyShapeTypes[aType];
46 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
50 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
52 std::string aValue = "";
54 if (MyShapeTypes.size() != 0) {
55 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
56 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
57 for (; anIt != aLast; anIt++) {
58 if (anIt->second == theType)
66 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
67 const std::list<std::string>& theArguments,
68 Events_InfoMessage& theError) const
72 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
73 // returns true if the attribute satisfies at least one of given arguments
74 for (; anIt != aLast; anIt++) {
75 TypeOfShape aShapeType = shapeType(*anIt);
76 // if arguments contain any shape type value, the validator returns true
77 if (aShapeType == AnyShape) {
81 if (isValidAttribute(theAttribute, aShapeType, theError)) {
86 if (!aValid && theError.empty()) {
88 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
89 // returns true if the attribute satisfies at least one of given arguments
90 for (; anIt != aLast; anIt++) {
95 theError = "It does not contain element with acceptable shape type. "
96 "The type should be one of the next: %1";
103 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
104 const TypeOfShape theShapeType,
105 Events_InfoMessage& theError) const
109 std::string anAttributeType = theAttribute->attributeType();
110 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
111 AttributeSelectionPtr anAttr =
112 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
113 GeomShapePtr aShape = anAttr->value();
115 aValid = isValidShape(aShape, theShapeType, theError);
117 aValid = isValidObject(anAttr->context(), theShapeType, theError);
119 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
120 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
121 if (anAttr->isObject()) {
122 aValid = isValidObject(anAttr->object(), theShapeType, theError);
124 else if (theShapeType == Vertex) {
125 AttributePtr aRefAttr = anAttr->attr();
126 if (!aRefAttr.get()){
128 theError = "It has reference to an empty attribute";
131 std::string anAttributeType = aRefAttr->attributeType();
132 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
134 theError = "Shape type is \"%1\", it should be \"%2\"";
135 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
140 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
141 AttributeReferencePtr anAttr =
142 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
143 aValid = isValidObject(anAttr->value(), theShapeType, theError);
145 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
146 AttributeSelectionListPtr aListAttr =
147 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
148 // the Empty value means that the attribute selection list is valid if it is empty
149 if (aListAttr->size() == 0 && theShapeType == Empty) {
152 aValid = false; // the list should have elements if the shape type is not Empty
153 for (int i = 0; i < aListAttr->size(); i++) {
154 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
155 if (!aValid) // if at least one attribute is invalid, the result is false
161 theError = "The attribute with the %1 type is not processed";
162 theError.arg(anAttributeType);
167 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
168 const TypeOfShape theShapeType,
169 Events_InfoMessage& theError) const
172 if (!theObject.get()) {
173 if(theShapeType != Empty) {
175 theError = "The object is empty";
179 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
180 if( theShapeType==Plane )
182 ResultConstructionPtr aResultConstruction =
183 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
184 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
185 const std::string& aKind = aFeature->getKind();
186 return aResult.get() != NULL && aKind == "Plane";
188 if (!aResult.get()) {
190 theError = "The result is empty";
193 aValid = isValidShape(aResult->shape(), theShapeType, theError);
199 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
200 const TypeOfShape theShapeType,
201 Events_InfoMessage& theError) const
205 if (!theShape.get()) {
207 theError = "The shape is empty";
210 switch (theShapeType) {
212 aValid = theShape->isVertex();
215 aValid = theShape->isEdge();
218 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
221 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
224 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
227 aValid = theShape->isFace();
230 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
233 aValid = theShape->isSolid() || theShape->isCompSolid() ||
234 theShape->isCompoundOfSolids();
237 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
240 aValid = theShape->isCompound();