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_ShapeIterator.h>
35 #include <GeomAPI_Pln.h>
36 #include <GeomAPI_Pnt.h>
38 //=================================================================================================
39 bool GeomValidators_ZeroOffset::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
40 const std::list<std::string>& theArguments,
41 Events_InfoMessage& theError) const
44 if(theArguments.size() != 9) {
45 theError = "Wrong number of validator arguments in xml(expected 9).";
50 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
52 std::string aSelectedMethod;
53 if(theFeature->string(*anIt)) {
54 aSelectedMethod = theFeature->string(*anIt)->value();
57 std::string aCreationMethod = *anIt;
60 ListOfShape aFacesList;
61 if(theFeature->selection(*anIt)) {
62 AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
63 ResultConstructionPtr aConstruction =
64 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
65 if(aConstruction.get()) {
66 int aSketchFacesNum = aConstruction->facesNum();
67 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
68 std::shared_ptr<GeomAPI_Shape> aFace =
69 std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
70 if(aFace->isFace() && aFace->isPlanar()) {
71 aFacesList.push_back(aFace);
75 } else if(theFeature->selectionList(*anIt)) {
76 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
77 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
78 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
79 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
80 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
81 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
82 aFacesList.push_back(aFaceShape);
84 } else { // This may be the whole sketch result selected, check and get faces.
85 ResultPtr aContext = aFaceSel->context();
86 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
87 if(!aContextShape.get()) {
90 ResultConstructionPtr aConstruction =
91 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
92 if(!aConstruction.get()) {
95 int aFacesNum = aConstruction->facesNum();
96 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
97 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
98 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
99 aFacesList.push_back(aFaceShape);
107 double aToSize = 0.0;
108 double aFromSize = 0.0;
110 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
111 aToSize = theFeature->real(*anIt)->value();
114 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
115 aFromSize = theFeature->real(*anIt)->value();
119 if(aSelectedMethod == aCreationMethod) {
120 if(aToSize == -aFromSize) {
121 theError = "ToSize = -FromSize.";
128 std::shared_ptr<GeomAPI_Shape> aToShape;
129 std::shared_ptr<GeomAPI_Shape> aFromShape;
131 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
132 if(anAttrSel && anAttrSel->isInitialized()) {
133 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
134 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
135 aToShape = anAttrSel->context()->shape();
137 if (aToShape->isCompound()) {
138 GeomAPI_ShapeIterator anIt(aToShape);
139 aToShape = anIt.current();
144 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
145 if(anAttrDouble && anAttrDouble->isInitialized()) {
146 aToSize = anAttrDouble->value();
150 anAttrSel = theFeature->selection(*anIt);
151 if(anAttrSel && anAttrSel->isInitialized()) {
152 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
153 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
154 aFromShape = anAttrSel->context()->shape();
156 if (aFromShape->isCompound()) {
157 GeomAPI_ShapeIterator anIt(aFromShape);
158 aFromShape = anIt.current();
163 anAttrDouble = theFeature->real(*anIt);
164 if(anAttrDouble && anAttrDouble->isInitialized()) {
165 aFromSize = anAttrDouble->value();
168 bool isPlanesCoincident = false;
169 if(!aFromShape.get() && !aToShape.get()) {
170 isPlanesCoincident = true;
171 } else if(aFromShape.get() && aToShape.get()) {
172 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
173 if (aFromFace->isNull()) {
174 theError = "From face selection is invalid.";
177 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
179 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
180 if (aToFace->isNull()) {
181 theError = "To face selection is invalid.";
184 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
187 isPlanesCoincident = aFromPln->isCoincident(aToPln);
190 std::shared_ptr<GeomAPI_Face> aFace;
191 if(aFromShape.get()) {
192 aFace.reset(new GeomAPI_Face(aFromShape));
193 if (aFace->isNull()) {
194 theError = "From face selection is invalid.";
198 aFace.reset(new GeomAPI_Face(aToShape));
199 if (aFace->isNull()) {
200 theError = "To face selection is invalid.";
204 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
206 for(ListOfShape::const_iterator
207 anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
208 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
209 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
210 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
211 if(aPln->isCoincident(aSketchPln)) {
212 isPlanesCoincident = true;
219 if(isPlanesCoincident && aFromSize == -aToSize) {
220 theError = "FromSize = -ToSize and bounding planes are coincident.";
227 //=================================================================================================
228 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
230 if(theAttribute == "from_object" || theAttribute == "to_object") {