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_Session.h>
23 #include <GeomValidators_Edge.h>
25 #include <GeomDataAPI_Point2D.h>
28 bool SketchPlugin_DistanceAttrValidator::isValid(
29 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
31 // there is a check whether the feature contains a point and a linear edge or two point values
32 std::string aParamA = theArguments.front();
33 SessionPtr aMgr = ModelAPI_Session::get();
34 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
36 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
40 bool isObject = aRefAttr->isObject();
42 // an attribute is a point. A point value is valid always for the distance
45 // 1. check whether the references object is a linear
46 ObjectPtr anObject = aRefAttr->object();
48 const ModelAPI_AttributeValidator* anEdgeValidator =
49 dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
50 std::list<std::string> anArguments;
51 anArguments.push_back("circle");
52 bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
53 // the circle line is not a valid case
58 anArguments.push_back("line");
59 anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
60 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
64 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
65 // If it is a line then we have to check that first attribute id not a line
66 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
72 bool SketchPlugin_TangentAttrValidator::isValid(
73 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
75 // there is a check whether the feature contains a point and a linear edge or two point values
76 std::string aParamA = theArguments.front();
77 SessionPtr aMgr = ModelAPI_Session::get();
78 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
80 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
81 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
85 bool isObject = aRefAttr->isObject();
86 ObjectPtr anObject = aRefAttr->object();
87 if (isObject && anObject) {
88 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
90 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
91 ObjectPtr aOtherObject = aOtherAttr->object();
92 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
94 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
95 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
97 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
98 if (aOtherFea->getKind() != SketchPlugin_Line::ID())
103 // check that both have coincidence
104 FeaturePtr aConstrFeature;
105 std::set<FeaturePtr> aCoinList;
106 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
107 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
108 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
109 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
110 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
111 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
112 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
113 AttributePtr aAR = aRAttr->attr();
114 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
115 aCoinList.insert(aConstrFeature);
118 // if there is no coincidence then it is not valid
119 if (aCoinList.size() == 0)
122 // find that coincedence is the same
123 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
124 std::set<FeaturePtr>::const_iterator aCoinsIt;
125 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
126 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
127 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
128 aCoinsIt = aCoinList.find(aConstrFeature);
129 if (aCoinsIt != aCoinList.end()) {
130 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
131 AttributePtr aAR = aRAttr->attr();
132 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
140 bool SketchPlugin_NotFixedValidator::isValid(
141 const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
143 std::shared_ptr<SketchPlugin_Feature> aFeature =
144 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
148 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
152 SketchPlugin_Sketch* aSketch = aFeature->sketch();
153 int aNbFeatures = aSketch->numberOfSubs();
154 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
155 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
156 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
158 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
159 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
160 if (aRefAttr->isObject()) {
161 if (aRefAttr->object() == aRAttr->object())
163 } else if (aRefAttr->attr() == aRAttr->attr())
169 bool SketchPlugin_EqualAttrValidator::isValid(
170 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
172 std::string aParamA = theArguments.front();
173 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
174 AttributeRefAttrPtr aRefAttr[2];
175 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
178 aRefAttr[1] = aFeature->data()->refattr(aParamA);
180 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
183 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
184 std::list<std::string> anArguments;
185 for (int i = 0; i < 2; i++) {
186 ObjectPtr anObject = aRefAttr[i]->object();
187 aFeature = ModelAPI_Feature::feature(anObject);
191 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
195 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
199 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
203 // wrong type of attribute
207 if ((aType[0] == 1 && aType[1] == 2) ||
208 (aType[0] == 2 && aType[1] == 1))