1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <GeomValidators_ZeroOffset.h>
23 #include <Events_InfoMessage.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_ResultConstruction.h>
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_Shape.h>
34 #include <GeomAPI_Pln.h>
35 #include <GeomAPI_Pnt.h>
37 //=================================================================================================
38 bool GeomValidators_ZeroOffset::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
39 const std::list<std::string>& theArguments,
40 Events_InfoMessage& theError) const
42 if(theArguments.size() != 9) {
43 theError = "Wrong number of validator arguments in xml(expected 9).";
47 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
49 std::string aSelectedMethod;
50 if(theFeature->string(*anIt)) {
51 aSelectedMethod = theFeature->string(*anIt)->value();
54 std::string aCreationMethod = *anIt;
57 ListOfShape aFacesList;
58 if(theFeature->selection(*anIt)) {
59 AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
60 ResultConstructionPtr aConstruction =
61 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
62 if(aConstruction.get()) {
63 int aSketchFacesNum = aConstruction->facesNum();
64 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
65 std::shared_ptr<GeomAPI_Shape> aFace =
66 std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
67 if(aFace->isFace() && aFace->isPlanar()) {
68 aFacesList.push_back(aFace);
72 } else if(theFeature->selectionList(*anIt)) {
73 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
74 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
75 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
76 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
77 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
78 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
79 aFacesList.push_back(aFaceShape);
81 } else { // This may be the whole sketch result selected, check and get faces.
82 ResultPtr aContext = aFaceSel->context();
83 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
84 if(!aContextShape.get()) {
87 ResultConstructionPtr aConstruction =
88 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
89 if(!aConstruction.get()) {
92 int aFacesNum = aConstruction->facesNum();
93 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
94 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
95 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
96 aFacesList.push_back(aFaceShape);
104 double aToSize = 0.0;
105 double aFromSize = 0.0;
107 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
108 aToSize = theFeature->real(*anIt)->value();
111 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
112 aFromSize = theFeature->real(*anIt)->value();
116 if(aSelectedMethod == aCreationMethod) {
117 if(aToSize == -aFromSize) {
118 theError = "ToSize = -FromSize.";
125 std::shared_ptr<GeomAPI_Shape> aToShape;
126 std::shared_ptr<GeomAPI_Shape> aFromShape;
128 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
129 if(anAttrSel && anAttrSel->isInitialized()) {
130 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
131 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
132 aToShape = anAttrSel->context()->shape();
137 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
138 if(anAttrDouble && anAttrDouble->isInitialized()) {
139 aToSize = anAttrDouble->value();
143 anAttrSel = theFeature->selection(*anIt);
144 if(anAttrSel && anAttrSel->isInitialized()) {
145 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
146 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
147 aFromShape = anAttrSel->context()->shape();
152 anAttrDouble = theFeature->real(*anIt);
153 if(anAttrDouble && anAttrDouble->isInitialized()) {
154 aFromSize = anAttrDouble->value();
157 bool isPlanesCoincident = false;
158 if(!aFromShape.get() && !aToShape.get()) {
159 isPlanesCoincident = true;
160 } else if(aFromShape.get() && aToShape.get()) {
161 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
162 if (aFromFace->isNull()) {
163 theError = "From face selection is invalid.";
166 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
168 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
169 if (aToFace->isNull()) {
170 theError = "To face selection is invalid.";
173 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
176 isPlanesCoincident = aFromPln->isCoincident(aToPln);
179 std::shared_ptr<GeomAPI_Face> aFace;
180 if(aFromShape.get()) {
181 aFace.reset(new GeomAPI_Face(aFromShape));
182 if (aFace->isNull()) {
183 theError = "From face selection is invalid.";
187 aFace.reset(new GeomAPI_Face(aToShape));
188 if (aFace->isNull()) {
189 theError = "To face selection is invalid.";
193 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
195 for(ListOfShape::const_iterator
196 anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
197 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
198 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
199 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
200 if(aPln->isCoincident(aSketchPln)) {
201 isPlanesCoincident = true;
208 if(isPlanesCoincident && aFromSize == -aToSize) {
209 theError = "FromSize = -ToSize and bounding planes are coincident.";
216 //=================================================================================================
217 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
219 if(theAttribute == "from_object" || theAttribute == "to_object") {