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(const AttributePtr& theAttribute,
32 const std::list<std::string>& theArguments,
33 std::string& theError) const
35 // there is a check whether the feature contains a point and a linear edge or two point values
36 std::string aParamA = theArguments.front();
37 SessionPtr aMgr = ModelAPI_Session::get();
38 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
40 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
44 bool isObject = aRefAttr->isObject();
46 // an attribute is a point. A point value is valid always for the distance
49 // 1. check whether the references object is a linear
50 ObjectPtr anObject = aRefAttr->object();
52 const ModelAPI_AttributeValidator* aShapeValidator =
53 dynamic_cast<const GeomValidators_ShapeType*>(aFactory->validator("GeomValidators_ShapeType"));
54 std::list<std::string> anArguments;
55 anArguments.push_back("circle");
56 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments);
57 // the circle line is not a valid case
62 anArguments.push_back("line");
63 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments);
64 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
68 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
69 // If it is a line then we have to check that first attribute id not a line
70 std::shared_ptr<SketchPlugin_Feature> aSFeature =
71 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
72 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
73 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
74 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
75 aFeature->data(), aParamA, aPlane);
82 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
83 const std::list<std::string>& theArguments,
84 std::string& theError) const
86 // there is a check whether the feature contains a point and a linear edge or two point values
87 std::string aParamA = theArguments.front();
88 SessionPtr aMgr = ModelAPI_Session::get();
89 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
91 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
92 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
96 bool isObject = aRefAttr->isObject();
97 ObjectPtr anObject = aRefAttr->object();
98 if (isObject && anObject) {
99 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
101 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
102 ObjectPtr aOtherObject = aOtherAttr->object();
103 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
105 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
106 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
108 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
109 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
110 aOtherFea->getKind() != SketchPlugin_Arc::ID())
120 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
121 const std::list<std::string>& theArguments,
122 std::string& theError) const
124 std::shared_ptr<SketchPlugin_Feature> aFeature =
125 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
129 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
133 SketchPlugin_Sketch* aSketch = aFeature->sketch();
134 int aNbFeatures = aSketch->numberOfSubs();
135 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
136 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
137 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
139 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
140 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
141 if (aRefAttr->isObject()) {
142 if (aRefAttr->object() == aRAttr->object())
144 } else if (aRefAttr->attr() == aRAttr->attr())
150 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
151 const std::list<std::string>& theArguments,
152 std::string& theError) const
154 std::string aParamA = theArguments.front();
155 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
156 AttributeRefAttrPtr aRefAttr[2];
157 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
160 aRefAttr[1] = aFeature->data()->refattr(aParamA);
162 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
165 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
166 std::list<std::string> anArguments;
167 for (int i = 0; i < 2; i++) {
168 ObjectPtr anObject = aRefAttr[i]->object();
169 aFeature = ModelAPI_Feature::feature(anObject);
173 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
177 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
181 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
185 // wrong type of attribute
189 if ((aType[0] == 1 && aType[1] == 2) ||
190 (aType[0] == 2 && aType[1] == 1))
195 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
196 const std::list<std::string>& theArguments,
197 std::string& theError) const
199 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
200 AttributeSelectionListPtr aSelAttr =
201 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
205 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
206 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
207 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
209 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
210 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
211 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
212 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
213 if (aSelect->context() == *aMirIter)
220 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
221 const std::list<std::string>& theArguments,
222 std::string& theError) const
224 // there is a check whether the feature contains a point and a linear edge or two point values
225 std::string aParamA = theArguments.front();
226 SessionPtr aMgr = ModelAPI_Session::get();
227 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
229 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
230 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
234 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
238 // first attribute is a point, it may coincide with any object
239 if (!aRefAttrA->isObject())
242 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
245 if (aFeature->getKind() == SketchPlugin_Point::ID())
249 // second attribute is a point, it may coincide with any object
250 if (!aRefAttrB->isObject())
253 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
256 if (aFeature->getKind() == SketchPlugin_Point::ID())
264 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
265 const std::list<std::string>& theArguments,
266 std::string& theError) const
268 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
269 AttributeSelectionListPtr aSelAttr =
270 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
274 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
275 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
276 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
277 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
278 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
279 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
281 std::list<ObjectPtr>::iterator anObjIter;
282 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
283 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
284 anObjIter = anInitialObjects.begin();
285 for (; anObjIter != anInitialObjects.end(); anObjIter++)
286 if (aSelect->context() == *anObjIter)
288 if (anObjIter != anInitialObjects.end())
290 anObjIter = aCopiedObjects.begin();
291 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
292 if (aSelect->context() == *anObjIter)