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 =
47 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
48 if(aConstruction.get()) {
49 int aSketchFacesNum = aConstruction->facesNum();
50 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
51 std::shared_ptr<GeomAPI_Shape> aFace =
52 std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
53 if(aFace->isFace() && aFace->isPlanar()) {
54 aFacesList.push_back(aFace);
58 } else if(theFeature->selectionList(*anIt)) {
59 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
60 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
61 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
62 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
63 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
64 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
65 aFacesList.push_back(aFaceShape);
67 } else { // This may be the whole sketch result selected, check and get faces.
68 ResultPtr aContext = aFaceSel->context();
69 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
70 if(!aContextShape.get()) {
73 ResultConstructionPtr aConstruction =
74 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
75 if(!aConstruction.get()) {
78 int aFacesNum = aConstruction->facesNum();
79 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
80 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
81 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
82 aFacesList.push_back(aFaceShape);
91 double aFromSize = 0.0;
93 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
94 aToSize = theFeature->real(*anIt)->value();
97 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
98 aFromSize = theFeature->real(*anIt)->value();
102 if(aSelectedMethod == aCreationMethod) {
103 if(aToSize == -aFromSize) {
104 theError = "ToSize = -FromSize.";
111 std::shared_ptr<GeomAPI_Shape> aToShape;
112 std::shared_ptr<GeomAPI_Shape> aFromShape;
114 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
115 if(anAttrSel && anAttrSel->isInitialized()) {
116 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
117 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
118 aToShape = anAttrSel->context()->shape();
123 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
124 if(anAttrDouble && anAttrDouble->isInitialized()) {
125 aToSize = anAttrDouble->value();
129 anAttrSel = theFeature->selection(*anIt);
130 if(anAttrSel && anAttrSel->isInitialized()) {
131 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
132 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
133 aFromShape = anAttrSel->context()->shape();
138 anAttrDouble = theFeature->real(*anIt);
139 if(anAttrDouble && anAttrDouble->isInitialized()) {
140 aFromSize = anAttrDouble->value();
143 bool isPlanesCoincident = false;
144 if(!aFromShape.get() && !aToShape.get()) {
145 isPlanesCoincident = true;
146 } else if(aFromShape.get() && aToShape.get()) {
147 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
148 if (aFromFace->isNull()) {
149 theError = "From face selection is invalid.";
152 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
154 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
155 if (aToFace->isNull()) {
156 theError = "To face selection is invalid.";
159 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
162 isPlanesCoincident = aFromPln->isCoincident(aToPln);
165 std::shared_ptr<GeomAPI_Face> aFace;
166 if(aFromShape.get()) {
167 aFace.reset(new GeomAPI_Face(aFromShape));
168 if (aFace->isNull()) {
169 theError = "From face selection is invalid.";
173 aFace.reset(new GeomAPI_Face(aToShape));
174 if (aFace->isNull()) {
175 theError = "To face selection is invalid.";
179 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
181 for(ListOfShape::const_iterator
182 anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
183 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
184 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
185 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
186 if(aPln->isCoincident(aSketchPln)) {
187 isPlanesCoincident = true;
194 if(isPlanesCoincident && aFromSize == -aToSize) {
195 theError = "FromSize = -ToSize and bounding planes are coincident.";
202 //=================================================================================================
203 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
205 if(theAttribute == "from_object" || theAttribute == "to_object") {