1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomValidators_ZeroOffset.cpp
4 // Created: 13 May 2015
5 // Author: Dmitry Bobylev
7 #include <GeomValidators_ZeroOffset.h>
9 #include <Events_InfoMessage.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeSelectionList.h>
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_ResultConstruction.h>
17 #include <GeomAPI_Dir.h>
18 #include <GeomAPI_Face.h>
19 #include <GeomAPI_Shape.h>
20 #include <GeomAPI_Pln.h>
21 #include <GeomAPI_Pnt.h>
23 //=================================================================================================
24 bool GeomValidators_ZeroOffset::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
25 const std::list<std::string>& theArguments,
26 Events_InfoMessage& theError) const
28 if(theArguments.size() != 9) {
29 theError = "Wrong number of validator arguments in xml(expected 9).";
33 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
35 std::string aSelectedMethod;
36 if(theFeature->string(*anIt)) {
37 aSelectedMethod = theFeature->string(*anIt)->value();
40 std::string aCreationMethod = *anIt;
43 ListOfShape aFacesList;
44 if(theFeature->selection(*anIt)) {
45 AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
46 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
47 if(aConstruction.get()) {
48 int aSketchFacesNum = aConstruction->facesNum();
49 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
50 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
51 if(aFace->isFace() && aFace->isPlanar()) {
52 aFacesList.push_back(aFace);
56 } else if(theFeature->selectionList(*anIt)) {
57 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
58 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
59 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
60 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
61 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
62 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
63 aFacesList.push_back(aFaceShape);
65 } else { // This may be the whole sketch result selected, check and get faces.
66 ResultPtr aContext = aFaceSel->context();
67 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
68 if(!aContextShape.get()) {
71 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
72 if(!aConstruction.get()) {
75 int aFacesNum = aConstruction->facesNum();
76 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
77 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
78 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
79 aFacesList.push_back(aFaceShape);
88 double aFromSize = 0.0;
90 if(theFeature->real(*anIt)) {
91 aToSize = theFeature->real(*anIt)->value();
94 if(theFeature->real(*anIt)) {
95 aFromSize = theFeature->real(*anIt)->value();
99 if(aSelectedMethod == aCreationMethod) {
100 if(aToSize == -aFromSize) {
101 theError = "ToSize = -FromSize.";
108 std::shared_ptr<GeomAPI_Shape> aToShape;
109 std::shared_ptr<GeomAPI_Shape> aFromShape;
111 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
113 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
114 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
115 aToShape = anAttrSel->context()->shape();
120 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
122 aToSize = anAttrDouble->value();
126 anAttrSel = theFeature->selection(*anIt);
128 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
129 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
130 aFromShape = anAttrSel->context()->shape();
135 anAttrDouble = theFeature->real(*anIt);
137 aFromSize = anAttrDouble->value();
140 bool isPlanesCoincident = false;
141 if(!aFromShape.get() && !aToShape.get()) {
142 isPlanesCoincident = true;
143 } else if(aFromShape.get() && aToShape.get()) {
144 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
145 if (aFromFace->isNull()) {
146 theError = "From face selection is invalid.";
149 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
151 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
152 if (aToFace->isNull()) {
153 theError = "To face selection is invalid.";
156 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
159 isPlanesCoincident = aFromPln->isCoincident(aToPln);
162 std::shared_ptr<GeomAPI_Face> aFace;
163 if(aFromShape.get()) {
164 aFace.reset(new GeomAPI_Face(aFromShape));
165 if (aFace->isNull()) {
166 theError = "From face selection is invalid.";
170 aFace.reset(new GeomAPI_Face(aToShape));
171 if (aFace->isNull()) {
172 theError = "To face selection is invalid.";
176 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
178 for(ListOfShape::const_iterator anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
179 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
180 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
181 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
182 if(aPln->isCoincident(aSketchPln)) {
183 isPlanesCoincident = true;
190 if(isPlanesCoincident && aFromSize == -aToSize) {
191 theError = "FromSize = -ToSize and bounding planes are coincident.";
198 //=================================================================================================
199 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
201 if(theAttribute == "from_object" || theAttribute == "to_object") {