1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Validators.cpp
4 // Created: 01 Aug 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "SketchPlugin_Validators.h"
8 #include "SketchPlugin_ConstraintDistance.h"
9 #include "SketchPlugin_ConstraintCoincidence.h"
10 #include "SketchPlugin_ConstraintRigid.h"
11 #include "SketchPlugin_Line.h"
12 #include "SketchPlugin_Arc.h"
13 #include "SketchPlugin_Circle.h"
15 #include "SketcherPrs_Tools.h"
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeRefList.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_Session.h>
25 #include <GeomValidators_Edge.h>
27 #include <GeomDataAPI_Point2D.h>
30 bool SketchPlugin_DistanceAttrValidator::isValid(
31 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
33 // there is a check whether the feature contains a point and a linear edge or two point values
34 std::string aParamA = theArguments.front();
35 SessionPtr aMgr = ModelAPI_Session::get();
36 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
38 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
42 bool isObject = aRefAttr->isObject();
44 // an attribute is a point. A point value is valid always for the distance
47 // 1. check whether the references object is a linear
48 ObjectPtr anObject = aRefAttr->object();
50 const ModelAPI_AttributeValidator* anEdgeValidator =
51 dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
52 std::list<std::string> anArguments;
53 anArguments.push_back("circle");
54 bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
55 // the circle line is not a valid case
60 anArguments.push_back("line");
61 anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
62 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
66 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
67 // If it is a line then we have to check that first attribute id not a line
68 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
74 bool SketchPlugin_TangentAttrValidator::isValid(
75 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
77 // there is a check whether the feature contains a point and a linear edge or two point values
78 std::string aParamA = theArguments.front();
79 SessionPtr aMgr = ModelAPI_Session::get();
80 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
82 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
83 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
87 bool isObject = aRefAttr->isObject();
88 ObjectPtr anObject = aRefAttr->object();
89 if (isObject && anObject) {
90 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
92 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
93 ObjectPtr aOtherObject = aOtherAttr->object();
94 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
96 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
97 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
99 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
100 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
101 aOtherFea->getKind() != SketchPlugin_Arc::ID())
106 // check that both have coincidence
107 FeaturePtr aConstrFeature;
108 std::set<FeaturePtr> aCoinList;
109 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
110 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
111 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
112 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
113 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
114 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
115 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
116 AttributePtr aAR = aRAttr->attr();
117 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
118 aCoinList.insert(aConstrFeature);
121 // if there is no coincidence then it is not valid
122 if (aCoinList.size() == 0)
125 // find that coincedence is the same
126 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
127 std::set<FeaturePtr>::const_iterator aCoinsIt;
128 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
129 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
130 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
131 aCoinsIt = aCoinList.find(aConstrFeature);
132 if (aCoinsIt != aCoinList.end()) {
133 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
134 AttributePtr aAR = aRAttr->attr();
135 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
143 bool SketchPlugin_NotFixedValidator::isValid(
144 const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
146 std::shared_ptr<SketchPlugin_Feature> aFeature =
147 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
151 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
155 SketchPlugin_Sketch* aSketch = aFeature->sketch();
156 int aNbFeatures = aSketch->numberOfSubs();
157 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
158 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
159 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
161 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
162 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
163 if (aRefAttr->isObject()) {
164 if (aRefAttr->object() == aRAttr->object())
166 } else if (aRefAttr->attr() == aRAttr->attr())
172 bool SketchPlugin_EqualAttrValidator::isValid(
173 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
175 std::string aParamA = theArguments.front();
176 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
177 AttributeRefAttrPtr aRefAttr[2];
178 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
181 aRefAttr[1] = aFeature->data()->refattr(aParamA);
183 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
186 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
187 std::list<std::string> anArguments;
188 for (int i = 0; i < 2; i++) {
189 ObjectPtr anObject = aRefAttr[i]->object();
190 aFeature = ModelAPI_Feature::feature(anObject);
194 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
198 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
202 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
206 // wrong type of attribute
210 if ((aType[0] == 1 && aType[1] == 2) ||
211 (aType[0] == 2 && aType[1] == 1))
216 bool SketchPlugin_MirrorAttrValidator::isValid(
217 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
219 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
220 AttributeSelectionListPtr aSelAttr =
221 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
225 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
226 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
227 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
229 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
230 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
231 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
232 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
233 if (aSelect->context() == *aMirIter)