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++) {
94 theError = "It does not contain element with acceptable shape type. \
95 The type should be one of the next: %1";
102 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
103 const TypeOfShape theShapeType,
104 Events_InfoMessage& theError) const
108 std::string anAttributeType = theAttribute->attributeType();
109 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
110 AttributeSelectionPtr anAttr =
111 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
112 GeomShapePtr aShape = anAttr->value();
114 aValid = isValidShape(aShape, theShapeType, theError);
116 aValid = isValidObject(anAttr->context(), theShapeType, theError);
118 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
119 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
120 if (anAttr->isObject()) {
121 aValid = isValidObject(anAttr->object(), theShapeType, theError);
123 else if (theShapeType == Vertex) {
124 AttributePtr aRefAttr = anAttr->attr();
125 if (!aRefAttr.get()){
127 theError = "It has reference to an empty attribute";
130 std::string anAttributeType = aRefAttr->attributeType();
131 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
133 theError = "Shape type is \"%1\", it should be \"%2\"";
134 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
139 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
140 AttributeReferencePtr anAttr =
141 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
142 aValid = isValidObject(anAttr->value(), theShapeType, theError);
144 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
145 AttributeSelectionListPtr aListAttr =
146 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
147 // the Empty value means that the attribute selection list is valid if it is empty
148 if (aListAttr->size() == 0 && theShapeType == Empty) {
151 aValid = false; // the list should have elements if the shape type is not Empty
152 for (int i = 0; i < aListAttr->size(); i++) {
153 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
154 if (!aValid) // if at least one attribute is invalid, the result is false
160 theError = "The attribute with the %1 type is not processed";
161 theError.arg(anAttributeType);
166 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
167 const TypeOfShape theShapeType,
168 Events_InfoMessage& theError) const
171 if (!theObject.get()) {
172 if(theShapeType != Empty) {
174 theError = "The object is empty";
178 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
179 if( theShapeType==Plane )
181 ResultConstructionPtr aResultConstruction =
182 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
183 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
184 const std::string& aKind = aFeature->getKind();
185 return aResult.get() != NULL && aKind == "Plane";
187 if (!aResult.get()) {
189 theError = "The result is empty";
192 aValid = isValidShape(aResult->shape(), theShapeType, theError);
198 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
199 const TypeOfShape theShapeType,
200 Events_InfoMessage& theError) const
204 if (!theShape.get()) {
206 theError = "The shape is empty";
209 switch (theShapeType) {
211 aValid = theShape->isVertex();
214 aValid = theShape->isEdge();
217 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
220 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
223 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
226 aValid = theShape->isFace();
229 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
232 aValid = theShape->isSolid() || theShape->isCompSolid() ||
233 theShape->isCompoundOfSolids();
236 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
239 aValid = theShape->isCompound();