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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include <GeomValidators_ZeroOffset.h>
22 #include <Events_InfoMessage.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_ResultConstruction.h>
30 #include <GeomAPI_Dir.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_Shape.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_Pnt.h>
36 //=================================================================================================
37 bool GeomValidators_ZeroOffset::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
38 const std::list<std::string>& theArguments,
39 Events_InfoMessage& theError) const
41 if(theArguments.size() != 9) {
42 theError = "Wrong number of validator arguments in xml(expected 9).";
46 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
48 std::string aSelectedMethod;
49 if(theFeature->string(*anIt)) {
50 aSelectedMethod = theFeature->string(*anIt)->value();
53 std::string aCreationMethod = *anIt;
56 ListOfShape aFacesList;
57 if(theFeature->selection(*anIt)) {
58 AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
59 ResultConstructionPtr aConstruction =
60 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
61 if(aConstruction.get()) {
62 int aSketchFacesNum = aConstruction->facesNum();
63 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
64 std::shared_ptr<GeomAPI_Shape> aFace =
65 std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
66 if(aFace->isFace() && aFace->isPlanar()) {
67 aFacesList.push_back(aFace);
71 } else if(theFeature->selectionList(*anIt)) {
72 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
73 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
74 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
75 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
76 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
77 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
78 aFacesList.push_back(aFaceShape);
80 } else { // This may be the whole sketch result selected, check and get faces.
81 ResultPtr aContext = aFaceSel->context();
82 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
83 if(!aContextShape.get()) {
86 ResultConstructionPtr aConstruction =
87 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
88 if(!aConstruction.get()) {
91 int aFacesNum = aConstruction->facesNum();
92 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
93 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
94 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
95 aFacesList.push_back(aFaceShape);
103 double aToSize = 0.0;
104 double aFromSize = 0.0;
106 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
107 aToSize = theFeature->real(*anIt)->value();
110 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
111 aFromSize = theFeature->real(*anIt)->value();
115 if(aSelectedMethod == aCreationMethod) {
116 if(aToSize == -aFromSize) {
117 theError = "ToSize = -FromSize.";
124 std::shared_ptr<GeomAPI_Shape> aToShape;
125 std::shared_ptr<GeomAPI_Shape> aFromShape;
127 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
128 if(anAttrSel && anAttrSel->isInitialized()) {
129 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
130 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
131 aToShape = anAttrSel->context()->shape();
136 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
137 if(anAttrDouble && anAttrDouble->isInitialized()) {
138 aToSize = anAttrDouble->value();
142 anAttrSel = theFeature->selection(*anIt);
143 if(anAttrSel && anAttrSel->isInitialized()) {
144 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
145 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
146 aFromShape = anAttrSel->context()->shape();
151 anAttrDouble = theFeature->real(*anIt);
152 if(anAttrDouble && anAttrDouble->isInitialized()) {
153 aFromSize = anAttrDouble->value();
156 bool isPlanesCoincident = false;
157 if(!aFromShape.get() && !aToShape.get()) {
158 isPlanesCoincident = true;
159 } else if(aFromShape.get() && aToShape.get()) {
160 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
161 if (aFromFace->isNull()) {
162 theError = "From face selection is invalid.";
165 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
167 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
168 if (aToFace->isNull()) {
169 theError = "To face selection is invalid.";
172 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
175 isPlanesCoincident = aFromPln->isCoincident(aToPln);
178 std::shared_ptr<GeomAPI_Face> aFace;
179 if(aFromShape.get()) {
180 aFace.reset(new GeomAPI_Face(aFromShape));
181 if (aFace->isNull()) {
182 theError = "From face selection is invalid.";
186 aFace.reset(new GeomAPI_Face(aToShape));
187 if (aFace->isNull()) {
188 theError = "To face selection is invalid.";
192 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
194 for(ListOfShape::const_iterator
195 anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
196 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
197 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
198 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
199 if(aPln->isCoincident(aSketchPln)) {
200 isPlanesCoincident = true;
207 if(isPlanesCoincident && aFromSize == -aToSize) {
208 theError = "FromSize = -ToSize and bounding planes are coincident.";
215 //=================================================================================================
216 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
218 if(theAttribute == "from_object" || theAttribute == "to_object") {