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
43 if(theArguments.size() != 9) {
44 theError = "Wrong number of validator arguments in xml(expected 9).";
48 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
50 std::string aSelectedMethod;
51 if(theFeature->string(*anIt)) {
52 aSelectedMethod = theFeature->string(*anIt)->value();
55 std::string aCreationMethod = *anIt;
58 ListOfShape aFacesList;
59 if(theFeature->selection(*anIt)) {
60 AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
61 ResultConstructionPtr aConstruction =
62 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
63 if(aConstruction.get()) {
64 int aSketchFacesNum = aConstruction->facesNum();
65 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
66 std::shared_ptr<GeomAPI_Shape> aFace =
67 std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
68 if(aFace->isFace() && aFace->isPlanar()) {
69 aFacesList.push_back(aFace);
73 } else if(theFeature->selectionList(*anIt)) {
74 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
75 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
76 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
77 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
78 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
79 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
80 aFacesList.push_back(aFaceShape);
82 } else { // This may be the whole sketch result selected, check and get faces.
83 ResultPtr aContext = aFaceSel->context();
84 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
85 if(!aContextShape.get()) {
88 ResultConstructionPtr aConstruction =
89 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
90 if(!aConstruction.get()) {
93 int aFacesNum = aConstruction->facesNum();
94 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
95 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
96 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
97 aFacesList.push_back(aFaceShape);
105 double aToSize = 0.0;
106 double aFromSize = 0.0;
108 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
109 aToSize = theFeature->real(*anIt)->value();
112 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
113 aFromSize = theFeature->real(*anIt)->value();
117 if(aSelectedMethod == aCreationMethod) {
118 if(aToSize == -aFromSize) {
119 theError = "ToSize = -FromSize.";
126 std::shared_ptr<GeomAPI_Shape> aToShape;
127 std::shared_ptr<GeomAPI_Shape> aFromShape;
129 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
130 if(anAttrSel && anAttrSel->isInitialized()) {
131 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
132 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
133 aToShape = anAttrSel->context()->shape();
135 if (aToShape->isCompound()) {
136 GeomAPI_ShapeIterator anIt(aToShape);
137 aToShape = anIt.current();
142 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
143 if(anAttrDouble && anAttrDouble->isInitialized()) {
144 aToSize = anAttrDouble->value();
148 anAttrSel = theFeature->selection(*anIt);
149 if(anAttrSel && anAttrSel->isInitialized()) {
150 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
151 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
152 aFromShape = anAttrSel->context()->shape();
154 if (aFromShape->isCompound()) {
155 GeomAPI_ShapeIterator anIt(aFromShape);
156 aFromShape = anIt.current();
161 anAttrDouble = theFeature->real(*anIt);
162 if(anAttrDouble && anAttrDouble->isInitialized()) {
163 aFromSize = anAttrDouble->value();
166 bool isPlanesCoincident = false;
167 if(!aFromShape.get() && !aToShape.get()) {
168 isPlanesCoincident = true;
169 } else if(aFromShape.get() && aToShape.get()) {
170 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
171 if (aFromFace->isNull()) {
172 theError = "From face selection is invalid.";
175 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
177 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
178 if (aToFace->isNull()) {
179 theError = "To face selection is invalid.";
182 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
185 isPlanesCoincident = aFromPln->isCoincident(aToPln);
188 std::shared_ptr<GeomAPI_Face> aFace;
189 if(aFromShape.get()) {
190 aFace.reset(new GeomAPI_Face(aFromShape));
191 if (aFace->isNull()) {
192 theError = "From face selection is invalid.";
196 aFace.reset(new GeomAPI_Face(aToShape));
197 if (aFace->isNull()) {
198 theError = "To face selection is invalid.";
202 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
204 for(ListOfShape::const_iterator
205 anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
206 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
207 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
208 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
209 if(aPln->isCoincident(aSketchPln)) {
210 isPlanesCoincident = true;
217 if(isPlanesCoincident && aFromSize == -aToSize) {
218 theError = "FromSize = -ToSize and bounding planes are coincident.";
225 //=================================================================================================
226 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
228 if(theAttribute == "from_object" || theAttribute == "to_object") {