1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomValidators_ZeroOffset.cpp
4 // Created: 13 May 2015
5 // Author: Dmitry Bobylev
7 #include <GeomValidators_ZeroOffset.h>
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelection.h>
11 #include <ModelAPI_AttributeSelectionList.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_ResultConstruction.h>
15 #include <GeomAPI_Dir.h>
16 #include <GeomAPI_Face.h>
17 #include <GeomAPI_Shape.h>
18 #include <GeomAPI_Pln.h>
19 #include <GeomAPI_Pnt.h>
21 //=================================================================================================
22 bool GeomValidators_ZeroOffset::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
23 const std::list<std::string>& theArguments,
24 std::string& theError) const
26 if(theArguments.size() != 9) {
27 theError = "Wrong number of validator arguments in xml(expected 9).";
31 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
33 std::string aSelectedMethod;
34 if(theFeature->string(*anIt)) {
35 aSelectedMethod = theFeature->string(*anIt)->value();
38 std::string aCreationMethod = *anIt;
41 ListOfShape aFacesList;
42 if(theFeature->selection(*anIt)) {
43 AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
44 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
45 if(aConstruction.get()) {
46 int aSketchFacesNum = aConstruction->facesNum();
47 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
48 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
49 aFacesList.push_back(aFace);
52 } else if(theFeature->selectionList(*anIt)) {
53 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
54 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
55 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
56 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
57 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
58 aFacesList.push_back(aFaceShape);
59 } else { // This may be the whole sketch result selected, check and get faces.
60 ResultPtr aContext = aFaceSel->context();
61 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
62 if(!aContextShape.get()) {
65 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
66 if(!aConstruction.get()) {
69 int aFacesNum = aConstruction->facesNum();
70 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
71 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
72 aFacesList.push_back(aFaceShape);
80 double aFromSize = 0.0;
82 if(theFeature->real(*anIt)) {
83 aToSize = theFeature->real(*anIt)->value();
86 if(theFeature->real(*anIt)) {
87 aFromSize = theFeature->real(*anIt)->value();
91 if(aSelectedMethod == aCreationMethod) {
92 if(aToSize == -aFromSize) {
93 theError = "ToSize = -FromSize.";
100 std::shared_ptr<GeomAPI_Shape> aToShape;
101 std::shared_ptr<GeomAPI_Shape> aFromShape;
103 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
105 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
106 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
107 aToShape = anAttrSel->context()->shape();
112 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
114 aToSize = anAttrDouble->value();
118 anAttrSel = theFeature->selection(*anIt);
120 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
121 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
122 aFromShape = anAttrSel->context()->shape();
127 anAttrDouble = theFeature->real(*anIt);
129 aFromSize = anAttrDouble->value();
132 bool isPlanesCoincident = false;
133 if(!aFromShape.get() && !aToShape.get()) {
134 isPlanesCoincident = true;
135 } else if(aFromShape.get() && aToShape.get()) {
136 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
137 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
139 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
140 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
143 isPlanesCoincident = aFromPln->isCoincident(aToPln);
146 std::shared_ptr<GeomAPI_Face> aFace;
147 if(aFromShape.get()) {
148 aFace.reset(new GeomAPI_Face(aFromShape));
150 aFace.reset(new GeomAPI_Face(aToShape));
152 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
154 for(ListOfShape::const_iterator anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
155 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
156 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
157 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
158 if(aPln->isCoincident(aSketchPln)) {
159 isPlanesCoincident = true;
166 if(isPlanesCoincident && aFromSize == -aToSize) {
167 theError = "FromSize = -ToSize and bounding planes are coincident.";
174 //=================================================================================================
175 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
177 if(theAttribute == "from_object" || theAttribute == "to_object") {