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"
14 #include "SketchPlugin_Point.h"
16 #include "SketcherPrs_Tools.h"
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Session.h>
26 #include <GeomValidators_ShapeType.h>
28 #include <GeomDataAPI_Point2D.h>
31 bool SketchPlugin_DistanceAttrValidator::isValid(
32 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
34 // there is a check whether the feature contains a point and a linear edge or two point values
35 std::string aParamA = theArguments.front();
36 SessionPtr aMgr = ModelAPI_Session::get();
37 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
39 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
43 bool isObject = aRefAttr->isObject();
45 // an attribute is a point. A point value is valid always for the distance
48 // 1. check whether the references object is a linear
49 ObjectPtr anObject = aRefAttr->object();
51 const ModelAPI_AttributeValidator* aShapeValidator =
52 dynamic_cast<const GeomValidators_ShapeType*>(aFactory->validator("GeomValidators_ShapeType"));
53 std::list<std::string> anArguments;
54 anArguments.push_back("circle");
55 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments);
56 // the circle line is not a valid case
61 anArguments.push_back("line");
62 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments);
63 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
67 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
68 // If it is a line then we have to check that first attribute id not a line
69 std::shared_ptr<SketchPlugin_Feature> aSFeature =
70 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
71 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
72 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
73 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
74 aFeature->data(), aParamA, aPlane);
80 bool SketchPlugin_TangentAttrValidator::isValid(
81 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
83 // there is a check whether the feature contains a point and a linear edge or two point values
84 std::string aParamA = theArguments.front();
85 SessionPtr aMgr = ModelAPI_Session::get();
86 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
88 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
89 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
93 bool isObject = aRefAttr->isObject();
94 ObjectPtr anObject = aRefAttr->object();
95 if (isObject && anObject) {
96 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
98 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
99 ObjectPtr aOtherObject = aOtherAttr->object();
100 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
102 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
103 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
105 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
106 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
107 aOtherFea->getKind() != SketchPlugin_Arc::ID())
112 // check that both have coincidence
113 FeaturePtr aConstrFeature;
114 std::set<FeaturePtr> aCoinList;
115 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
116 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
117 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
118 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
119 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
120 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
121 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
122 AttributePtr aAR = aRAttr->attr();
123 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
124 aCoinList.insert(aConstrFeature);
127 // if there is no coincidence then it is not valid
128 if (aCoinList.size() == 0)
131 // find that coincedence is the same
132 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
133 std::set<FeaturePtr>::const_iterator aCoinsIt;
134 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
135 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
136 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
137 aCoinsIt = aCoinList.find(aConstrFeature);
138 if (aCoinsIt != aCoinList.end()) {
139 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
140 AttributePtr aAR = aRAttr->attr();
141 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
149 bool SketchPlugin_NotFixedValidator::isValid(
150 const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
152 std::shared_ptr<SketchPlugin_Feature> aFeature =
153 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
157 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
161 SketchPlugin_Sketch* aSketch = aFeature->sketch();
162 int aNbFeatures = aSketch->numberOfSubs();
163 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
164 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
165 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
167 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
168 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
169 if (aRefAttr->isObject()) {
170 if (aRefAttr->object() == aRAttr->object())
172 } else if (aRefAttr->attr() == aRAttr->attr())
178 bool SketchPlugin_EqualAttrValidator::isValid(
179 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
181 std::string aParamA = theArguments.front();
182 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
183 AttributeRefAttrPtr aRefAttr[2];
184 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
187 aRefAttr[1] = aFeature->data()->refattr(aParamA);
189 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
192 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
193 std::list<std::string> anArguments;
194 for (int i = 0; i < 2; i++) {
195 ObjectPtr anObject = aRefAttr[i]->object();
196 aFeature = ModelAPI_Feature::feature(anObject);
200 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
204 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
208 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
212 // wrong type of attribute
216 if ((aType[0] == 1 && aType[1] == 2) ||
217 (aType[0] == 2 && aType[1] == 1))
222 bool SketchPlugin_MirrorAttrValidator::isValid(
223 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
225 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
226 AttributeSelectionListPtr aSelAttr =
227 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
231 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
232 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
233 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
235 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
236 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
237 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
238 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
239 if (aSelect->context() == *aMirIter)
246 bool SketchPlugin_CoincidenceAttrValidator::isValid(
247 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
249 // there is a check whether the feature contains a point and a linear edge or two point values
250 std::string aParamA = theArguments.front();
251 SessionPtr aMgr = ModelAPI_Session::get();
252 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
254 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
255 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
259 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
263 // first attribute is a point, it may coincide with any object
264 if (!aRefAttrA->isObject())
267 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
270 if (aFeature->getKind() == SketchPlugin_Point::ID())
274 // second attribute is a point, it may coincide with any object
275 if (!aRefAttrB->isObject())
278 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
281 if (aFeature->getKind() == SketchPlugin_Point::ID())
289 bool SketchPlugin_CopyValidator::isValid(
290 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
292 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
293 AttributeSelectionListPtr aSelAttr =
294 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
298 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
299 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
300 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
301 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
302 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
303 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
305 std::list<ObjectPtr>::iterator anObjIter;
306 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
307 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
308 anObjIter = anInitialObjects.begin();
309 for (; anObjIter != anInitialObjects.end(); anObjIter++)
310 if (aSelect->context() == *anObjIter)
312 if (anObjIter != anInitialObjects.end())
314 anObjIter = aCopiedObjects.begin();
315 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
316 if (aSelect->context() == *anObjIter)