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() &&
99 aOtherFea->getKind() != SketchPlugin_Arc::ID())
104 // check that both have coincidence
105 FeaturePtr aConstrFeature;
106 std::set<FeaturePtr> aCoinList;
107 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
108 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
109 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
110 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
111 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
112 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
113 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
114 AttributePtr aAR = aRAttr->attr();
115 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
116 aCoinList.insert(aConstrFeature);
119 // if there is no coincidence then it is not valid
120 if (aCoinList.size() == 0)
123 // find that coincedence is the same
124 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
125 std::set<FeaturePtr>::const_iterator aCoinsIt;
126 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
127 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
128 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
129 aCoinsIt = aCoinList.find(aConstrFeature);
130 if (aCoinsIt != aCoinList.end()) {
131 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
132 AttributePtr aAR = aRAttr->attr();
133 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
141 bool SketchPlugin_NotFixedValidator::isValid(
142 const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
144 std::shared_ptr<SketchPlugin_Feature> aFeature =
145 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
149 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
153 SketchPlugin_Sketch* aSketch = aFeature->sketch();
154 int aNbFeatures = aSketch->numberOfSubs();
155 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
156 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
157 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
159 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
160 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
161 if (aRefAttr->isObject()) {
162 if (aRefAttr->object() == aRAttr->object())
164 } else if (aRefAttr->attr() == aRAttr->attr())
170 bool SketchPlugin_EqualAttrValidator::isValid(
171 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
173 std::string aParamA = theArguments.front();
174 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
175 AttributeRefAttrPtr aRefAttr[2];
176 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
179 aRefAttr[1] = aFeature->data()->refattr(aParamA);
181 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
184 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
185 std::list<std::string> anArguments;
186 for (int i = 0; i < 2; i++) {
187 ObjectPtr anObject = aRefAttr[i]->object();
188 aFeature = ModelAPI_Feature::feature(anObject);
192 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
196 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
200 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
204 // wrong type of attribute
208 if ((aType[0] == 1 && aType[1] == 2) ||
209 (aType[0] == 2 && aType[1] == 1))