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);
81 bool SketchPlugin_TangentAttrValidator::isValid(
82 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
84 // there is a check whether the feature contains a point and a linear edge or two point values
85 std::string aParamA = theArguments.front();
86 SessionPtr aMgr = ModelAPI_Session::get();
87 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
89 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
90 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
94 bool isObject = aRefAttr->isObject();
95 ObjectPtr anObject = aRefAttr->object();
96 if (isObject && anObject) {
97 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
99 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
100 ObjectPtr aOtherObject = aOtherAttr->object();
101 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
103 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
104 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
106 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
107 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
108 aOtherFea->getKind() != SketchPlugin_Arc::ID())
118 bool SketchPlugin_NotFixedValidator::isValid(
119 const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
121 std::shared_ptr<SketchPlugin_Feature> aFeature =
122 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
126 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
130 SketchPlugin_Sketch* aSketch = aFeature->sketch();
131 int aNbFeatures = aSketch->numberOfSubs();
132 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
133 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
134 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
136 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
137 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
138 if (aRefAttr->isObject()) {
139 if (aRefAttr->object() == aRAttr->object())
141 } else if (aRefAttr->attr() == aRAttr->attr())
147 bool SketchPlugin_EqualAttrValidator::isValid(
148 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
150 std::string aParamA = theArguments.front();
151 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
152 AttributeRefAttrPtr aRefAttr[2];
153 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
156 aRefAttr[1] = aFeature->data()->refattr(aParamA);
158 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
161 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
162 std::list<std::string> anArguments;
163 for (int i = 0; i < 2; i++) {
164 ObjectPtr anObject = aRefAttr[i]->object();
165 aFeature = ModelAPI_Feature::feature(anObject);
169 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
173 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
177 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
181 // wrong type of attribute
185 if ((aType[0] == 1 && aType[1] == 2) ||
186 (aType[0] == 2 && aType[1] == 1))
191 bool SketchPlugin_MirrorAttrValidator::isValid(
192 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
194 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
195 AttributeSelectionListPtr aSelAttr =
196 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
200 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
201 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
202 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
204 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
205 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
206 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
207 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
208 if (aSelect->context() == *aMirIter)
215 bool SketchPlugin_CoincidenceAttrValidator::isValid(
216 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
218 // there is a check whether the feature contains a point and a linear edge or two point values
219 std::string aParamA = theArguments.front();
220 SessionPtr aMgr = ModelAPI_Session::get();
221 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
223 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
224 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
228 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
232 // first attribute is a point, it may coincide with any object
233 if (!aRefAttrA->isObject())
236 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
239 if (aFeature->getKind() == SketchPlugin_Point::ID())
243 // second attribute is a point, it may coincide with any object
244 if (!aRefAttrB->isObject())
247 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
250 if (aFeature->getKind() == SketchPlugin_Point::ID())
258 bool SketchPlugin_CopyValidator::isValid(
259 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
261 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
262 AttributeSelectionListPtr aSelAttr =
263 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
267 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
268 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
269 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
270 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
271 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
272 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
274 std::list<ObjectPtr>::iterator anObjIter;
275 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
276 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
277 anObjIter = anInitialObjects.begin();
278 for (; anObjIter != anInitialObjects.end(); anObjIter++)
279 if (aSelect->context() == *anObjIter)
281 if (anObjIter != anInitialObjects.end())
283 anObjIter = aCopiedObjects.begin();
284 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
285 if (aSelect->context() == *anObjIter)