1 // Copyright (C) 2014-2019 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
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_ShapeIterator.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
43 if(theArguments.size() != 9) {
44 theError = "Wrong number of validator arguments in xml (expected 9).";
49 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
51 std::string aSelectedMethod;
52 if(theFeature->string(*anIt)) {
53 aSelectedMethod = theFeature->string(*anIt)->value();
56 std::string aCreationMethod = *anIt;
59 ListOfShape aFacesList;
60 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);
76 } else if(theFeature->selectionList(*anIt)) {
77 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
78 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
79 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
80 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
81 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
82 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
83 aFacesList.push_back(aFaceShape);
85 } else { // This may be the whole sketch result selected, check and get faces.
86 ResultPtr aContext = aFaceSel->context();
87 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
88 if(!aContextShape.get()) {
91 ResultConstructionPtr aConstruction =
92 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
93 if(!aConstruction.get()) {
96 int aFacesNum = aConstruction->facesNum();
97 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
98 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
99 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
100 aFacesList.push_back(aFaceShape);
108 double aToSize = 0.0;
109 double aFromSize = 0.0;
111 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
112 aToSize = theFeature->real(*anIt)->value();
115 if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
116 aFromSize = theFeature->real(*anIt)->value();
120 if(aSelectedMethod == aCreationMethod) {
121 if(aToSize == -aFromSize) {
122 theError = "ToSize = -FromSize.";
129 std::shared_ptr<GeomAPI_Shape> aToShape;
130 std::shared_ptr<GeomAPI_Shape> aFromShape;
132 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
133 if(anAttrSel && anAttrSel->isInitialized()) {
134 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
135 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
136 aToShape = anAttrSel->context()->shape();
138 if (aToShape->isCompound()) {
139 GeomAPI_ShapeIterator anIt(aToShape);
140 aToShape = anIt.current();
145 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
146 if(anAttrDouble && anAttrDouble->isInitialized()) {
147 aToSize = anAttrDouble->value();
151 anAttrSel = theFeature->selection(*anIt);
152 if(anAttrSel && anAttrSel->isInitialized()) {
153 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
154 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
155 aFromShape = anAttrSel->context()->shape();
157 if (aFromShape->isCompound()) {
158 GeomAPI_ShapeIterator anIt(aFromShape);
159 aFromShape = anIt.current();
164 anAttrDouble = theFeature->real(*anIt);
165 if(anAttrDouble && anAttrDouble->isInitialized()) {
166 aFromSize = anAttrDouble->value();
169 bool isPlanesCoincident = false;
170 if(!aFromShape.get() && !aToShape.get()) {
171 isPlanesCoincident = true;
172 } else if(aFromShape.get() && aToShape.get()) {
173 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
174 if (aFromFace->isNull()) {
175 theError = "From face selection is invalid.";
178 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
180 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
181 if (aToFace->isNull()) {
182 theError = "To face selection is invalid.";
185 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
188 isPlanesCoincident = aFromPln->isCoincident(aToPln);
191 std::shared_ptr<GeomAPI_Face> aFace;
192 if(aFromShape.get()) {
193 aFace.reset(new GeomAPI_Face(aFromShape));
194 if (aFace->isNull()) {
195 theError = "From face selection is invalid.";
199 aFace.reset(new GeomAPI_Face(aToShape));
200 if (aFace->isNull()) {
201 theError = "To face selection is invalid.";
205 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
207 for(ListOfShape::const_iterator
208 anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
209 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
210 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
211 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
212 if(aPln->isCoincident(aSketchPln)) {
213 isPlanesCoincident = true;
220 if(isPlanesCoincident && aFromSize == -aToSize) {
221 theError = "FromSize = -ToSize and bounding planes are coincident.";
228 //=================================================================================================
230 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
232 if(theAttribute == "from_object" || theAttribute == "to_object") {