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 std::string aCircleError;
57 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
58 // the circle line is not a valid case
63 anArguments.push_back("line");
64 std::string aLineError;
65 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
66 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
70 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
71 // If it is a line then we have to check that first attribute id not a line
72 std::shared_ptr<SketchPlugin_Feature> aSFeature =
73 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
74 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
75 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
76 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
77 aFeature->data(), aParamA, aPlane);
84 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
85 const std::list<std::string>& theArguments,
86 std::string& theError) const
88 // there is a check whether the feature contains a point and a linear edge or two point values
89 std::string aParamA = theArguments.front();
90 SessionPtr aMgr = ModelAPI_Session::get();
91 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
93 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
94 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
98 bool isObject = aRefAttr->isObject();
99 ObjectPtr anObject = aRefAttr->object();
100 if (isObject && anObject) {
101 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
103 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
104 ObjectPtr aOtherObject = aOtherAttr->object();
105 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
107 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
108 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
110 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
111 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
112 aOtherFea->getKind() != SketchPlugin_Arc::ID())
122 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
123 const std::list<std::string>& theArguments,
124 std::string& theError) const
126 std::shared_ptr<SketchPlugin_Feature> aFeature =
127 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
131 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
135 SketchPlugin_Sketch* aSketch = aFeature->sketch();
136 int aNbFeatures = aSketch->numberOfSubs();
137 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
138 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
139 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
141 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
142 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
143 if (aRefAttr->isObject()) {
144 if (aRefAttr->object() == aRAttr->object())
146 } else if (aRefAttr->attr() == aRAttr->attr())
152 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
153 const std::list<std::string>& theArguments,
154 std::string& theError) const
156 std::string aParamA = theArguments.front();
157 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
158 AttributeRefAttrPtr aRefAttr[2];
159 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
162 aRefAttr[1] = aFeature->data()->refattr(aParamA);
164 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
167 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
168 std::list<std::string> anArguments;
169 for (int i = 0; i < 2; i++) {
170 ObjectPtr anObject = aRefAttr[i]->object();
171 aFeature = ModelAPI_Feature::feature(anObject);
175 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
179 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
183 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
187 // wrong type of attribute
191 if ((aType[0] == 1 && aType[1] == 2) ||
192 (aType[0] == 2 && aType[1] == 1))
197 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
198 const std::list<std::string>& theArguments,
199 std::string& theError) const
201 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
202 AttributeRefListPtr aSelAttr =
203 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
207 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
208 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
209 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
211 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
212 ObjectPtr aSelObject = aSelAttr->object(anInd);
213 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
214 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
215 if (aSelObject == *aMirIter)
222 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
223 const std::list<std::string>& theArguments,
224 std::string& theError) const
226 // there is a check whether the feature contains a point and a linear edge or two point values
227 std::string aParamA = theArguments.front();
228 SessionPtr aMgr = ModelAPI_Session::get();
229 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
231 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
232 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
236 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
240 // first attribute is a point, it may coincide with any object
241 if (!aRefAttrA->isObject())
244 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
247 if (aFeature->getKind() == SketchPlugin_Point::ID())
251 // second attribute is a point, it may coincide with any object
252 if (!aRefAttrB->isObject())
255 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
258 if (aFeature->getKind() == SketchPlugin_Point::ID())
266 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
267 const std::list<std::string>& theArguments,
268 std::string& theError) const
270 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
271 AttributeRefListPtr aSelAttr =
272 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
276 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
277 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
278 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
279 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
280 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
281 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
283 std::list<ObjectPtr>::iterator anObjIter;
284 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
285 ObjectPtr aSelObject = aSelAttr->object(anInd);
286 anObjIter = anInitialObjects.begin();
287 for (; anObjIter != anInitialObjects.end(); anObjIter++)
288 if (aSelObject == *anObjIter)
290 if (anObjIter != anInitialObjects.end())
292 anObjIter = aCopiedObjects.begin();
293 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
294 if (aSelObject == *anObjIter)