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_Error.h>
22 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
24 static EdgeTypes MyShapeTypes;
25 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
27 if (MyShapeTypes.size() == 0) {
28 MyShapeTypes["empty"] = Empty;
29 MyShapeTypes["vertex"] = Vertex;
30 MyShapeTypes["edge"] = Edge;
31 MyShapeTypes["line"] = Line;
32 MyShapeTypes["circle"] = Circle;
33 MyShapeTypes["wire"] = Wire;
34 MyShapeTypes["face"] = Face;
35 MyShapeTypes["solid"] = Solid;
36 MyShapeTypes["plane"] = Plane;
38 std::string aType = std::string(theType.c_str());
39 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
40 return MyShapeTypes[aType];
42 Events_Error::send("Shape type defined in XML is not implemented!");
46 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
48 std::string aValue = "";
50 if (MyShapeTypes.size() != 0) {
51 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator anIt = MyShapeTypes.begin(),
52 aLast = MyShapeTypes.end();
53 for (; anIt != aLast; anIt++) {
54 if (anIt->second == theType)
62 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
63 const std::list<std::string>& theArguments,
64 std::string& theError) const
68 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
69 // returns true if the attribute satisfies at least one of given arguments
70 for (; anIt != aLast; anIt++) {
71 TypeOfShape aShapeType = shapeType(*anIt);
72 // if arguments contain any shape type value, the validator returns true
73 if (aShapeType == AnyShape) {
77 if (isValidAttribute(theAttribute, aShapeType, theError)) {
82 if (!aValid && theError.empty()) {
84 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
85 // returns true if the attribute satisfies at least one of given arguments
86 for (; anIt != aLast; anIt++) {
90 theError = "It does not contain element with acceptable shape type. The type should be one of the next: "
97 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
98 const TypeOfShape theShapeType,
99 std::string& theError) const
103 std::string anAttributeType = theAttribute->attributeType();
104 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
105 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
106 GeomShapePtr aShape = anAttr->value();
108 aValid = isValidShape(aShape, theShapeType, theError);
110 aValid = isValidObject(anAttr->context(), theShapeType, theError);
112 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
113 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
114 if (anAttr->isObject()) {
115 aValid = isValidObject(anAttr->object(), theShapeType, theError);
117 else if (theShapeType == Vertex) {
118 AttributePtr aRefAttr = anAttr->attr();
119 if (!aRefAttr.get()){
121 theError = "It has reference to an empty attribute";
124 std::string anAttributeType = aRefAttr->attributeType();
125 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
127 theError = "Shape type is \"" + anAttributeType +
128 "\", it should be \"" + getShapeTypeDescription(theShapeType) + "\"";
132 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
133 AttributeReferencePtr anAttr =
134 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
135 aValid = isValidObject(anAttr->value(), theShapeType, theError);
137 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
138 AttributeSelectionListPtr aListAttr =
139 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
140 // the Empty value means that the attribute selection list is valid if it is empty
141 if (aListAttr->size() == 0 && theShapeType == Empty) {
144 aValid = false; // the list should have elements if the shape type is not Empty
145 for (int i = 0; i < aListAttr->size(); i++) {
146 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
147 if (!aValid) // if at least one attribute is invalid, the result is false
153 theError = "The attribute with the " + anAttributeType + " type is not processed";
158 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
159 const TypeOfShape theShapeType,
160 std::string& theError) const
163 if (!theObject.get()) {
164 if(theShapeType != Empty) {
166 theError = "The object is empty";
170 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
171 if( theShapeType==Plane )
173 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
174 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
175 const std::string& aKind = aFeature->getKind();
176 return aResult.get() != NULL && aKind == "Plane";
178 if (!aResult.get()) {
180 theError = "The result is empty";
183 aValid = isValidShape(aResult->shape(), theShapeType, theError);
189 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
190 const TypeOfShape theShapeType,
191 std::string& theError) const
195 if (!theShape.get()) {
197 theError = "The shape is empty";
200 switch (theShapeType) {
202 aValid = theShape->isVertex();
205 aValid = theShape->isEdge();
208 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
211 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
214 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
217 aValid = theShape->isFace();
220 aValid = theShape->isSolid() || theShape->isCompSolid() ||
221 theShape->isCompoundOfSolids();
224 aValid = theShape->isCompound();