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);
70 bool SketchPlugin_TangentAttrValidator::isValid(
71 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
73 // there is a check whether the feature contains a point and a linear edge or two point values
74 std::string aParamA = theArguments.front();
75 SessionPtr aMgr = ModelAPI_Session::get();
76 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
78 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
79 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
83 bool isObject = aRefAttr->isObject();
84 ObjectPtr anObject = aRefAttr->object();
85 if (isObject && anObject) {
86 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
88 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
89 ObjectPtr aOtherObject = aOtherAttr->object();
90 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
92 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
93 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
95 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
96 if (aOtherFea->getKind() != SketchPlugin_Line::ID())
101 // check that both have coincidence
102 FeaturePtr aConstrFeature;
103 std::set<FeaturePtr> aCoinList;
104 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
105 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
106 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
107 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
108 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
109 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
110 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
111 AttributePtr aAR = aRAttr->attr();
112 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
113 aCoinList.insert(aConstrFeature);
116 // if there is no coincidence then it is not valid
117 if (aCoinList.size() == 0)
120 // find that coincedence is the same
121 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
122 std::set<FeaturePtr>::const_iterator aCoinsIt;
123 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
124 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
125 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
126 aCoinsIt = aCoinList.find(aConstrFeature);
127 if (aCoinsIt != aCoinList.end()) {
128 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
129 AttributePtr aAR = aRAttr->attr();
130 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())