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 if(aFace->isFace() && aFace->isPlanar()) {
50 aFacesList.push_back(aFace);
54 } else if(theFeature->selectionList(*anIt)) {
55 AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
56 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
57 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
58 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
59 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
60 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
61 aFacesList.push_back(aFaceShape);
63 } else { // This may be the whole sketch result selected, check and get faces.
64 ResultPtr aContext = aFaceSel->context();
65 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
66 if(!aContextShape.get()) {
69 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
70 if(!aConstruction.get()) {
73 int aFacesNum = aConstruction->facesNum();
74 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
75 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
76 if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
77 aFacesList.push_back(aFaceShape);
86 double aFromSize = 0.0;
88 if(theFeature->real(*anIt)) {
89 aToSize = theFeature->real(*anIt)->value();
92 if(theFeature->real(*anIt)) {
93 aFromSize = theFeature->real(*anIt)->value();
97 if(aSelectedMethod == aCreationMethod) {
98 if(aToSize == -aFromSize) {
99 theError = "ToSize = -FromSize.";
106 std::shared_ptr<GeomAPI_Shape> aToShape;
107 std::shared_ptr<GeomAPI_Shape> aFromShape;
109 std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
111 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
112 if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
113 aToShape = anAttrSel->context()->shape();
118 std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
120 aToSize = anAttrDouble->value();
124 anAttrSel = theFeature->selection(*anIt);
126 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
127 if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
128 aFromShape = anAttrSel->context()->shape();
133 anAttrDouble = theFeature->real(*anIt);
135 aFromSize = anAttrDouble->value();
138 bool isPlanesCoincident = false;
139 if(!aFromShape.get() && !aToShape.get()) {
140 isPlanesCoincident = true;
141 } else if(aFromShape.get() && aToShape.get()) {
142 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
143 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
145 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
146 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
149 isPlanesCoincident = aFromPln->isCoincident(aToPln);
152 std::shared_ptr<GeomAPI_Face> aFace;
153 if(aFromShape.get()) {
154 aFace.reset(new GeomAPI_Face(aFromShape));
156 aFace.reset(new GeomAPI_Face(aToShape));
158 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
160 for(ListOfShape::const_iterator anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
161 std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
162 std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
163 std::shared_ptr<GeomAPI_Pln> aSketchPln = aSketchFace->getPlane();
164 if(aPln->isCoincident(aSketchPln)) {
165 isPlanesCoincident = true;
172 if(isPlanesCoincident && aFromSize == -aToSize) {
173 theError = "FromSize = -ToSize and bounding planes are coincident.";
180 //=================================================================================================
181 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
183 if(theAttribute == "from_object" || theAttribute == "to_object") {