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_Line.h"
11 #include "SketchPlugin_Arc.h"
13 #include "SketcherPrs_Tools.h"
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Validator.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_Session.h>
21 #include <GeomValidators_Edge.h>
23 #include <GeomDataAPI_Point2D.h>
26 bool SketchPlugin_DistanceAttrValidator::isValid(
27 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
29 // there is a check whether the feature contains a point and a linear edge or two point values
30 std::string aParamA = theArguments.front();
31 SessionPtr aMgr = ModelAPI_Session::get();
32 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
34 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
38 bool isObject = aRefAttr->isObject();
40 // an attribute is a point. A point value is valid always for the distance
43 // 1. check whether the references object is a linear
44 ObjectPtr anObject = aRefAttr->object();
46 const ModelAPI_AttributeValidator* anEdgeValidator =
47 dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
48 std::list<std::string> anArguments;
49 anArguments.push_back("circle");
50 bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
51 // the circle line is not a valid case
56 anArguments.push_back("line");
57 anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
58 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
62 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
63 // If it is a line then we have to check that first attribute id not a line
64 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
73 bool SketchPlugin_TangentAttrValidator::isValid(
74 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
76 // there is a check whether the feature contains a point and a linear edge or two point values
77 std::string aParamA = theArguments.front();
78 SessionPtr aMgr = ModelAPI_Session::get();
79 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
81 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
82 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
86 bool isObject = aRefAttr->isObject();
87 ObjectPtr anObject = aRefAttr->object();
88 if (isObject && anObject) {
89 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
91 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
92 ObjectPtr aOtherObject = aOtherAttr->object();
93 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
95 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
96 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
98 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
99 if (aOtherFea->getKind() != SketchPlugin_Line::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())