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 <ModelAPI_Data.h>
14 #include <ModelAPI_Validator.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_Session.h>
19 #include <GeomValidators_Edge.h>
21 #include <GeomDataAPI_Point2D.h>
23 bool SketchPlugin_DistanceAttrValidator::isValid(
24 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
26 // there is a check whether the feature contains a point and a linear edge or two point values
27 std::string aParamA = theArguments.front();
28 SessionPtr aMgr = ModelAPI_Session::get();
29 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
31 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
35 bool isObject = aRefAttr->isObject();
37 // an attribute is a point. A point value is valid always for the distance
40 // 1. check whether the references object is a linear
41 ObjectPtr anObject = aRefAttr->object();
43 const ModelAPI_AttributeValidator* anEdgeValidator =
44 dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
45 std::list<std::string> anArguments;
46 anArguments.push_back("circle");
47 bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
48 // the circle line is not a valid case
53 anArguments.push_back("line");
54 anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
55 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
59 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
60 // If it is a line then we have to check that first attribute id not a line
61 std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(aFeature->data(), aParamA);
67 bool SketchPlugin_TangentAttrValidator::isValid(
68 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
70 // there is a check whether the feature contains a point and a linear edge or two point values
71 std::string aParamA = theArguments.front();
72 SessionPtr aMgr = ModelAPI_Session::get();
73 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
75 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
76 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
80 bool isObject = aRefAttr->isObject();
81 ObjectPtr anObject = aRefAttr->object();
82 if (isObject && anObject) {
83 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
85 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
86 ObjectPtr aOtherObject = aOtherAttr->object();
87 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
89 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
90 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
92 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
93 if (aOtherFea->getKind() != SketchPlugin_Line::ID())
98 // check that both have coincidence
99 FeaturePtr aConstrFeature;
100 std::set<FeaturePtr> aCoinList;
101 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
102 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
103 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
104 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
105 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
106 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
107 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
108 AttributePtr aAR = aRAttr->attr();
109 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
110 aCoinList.insert(aConstrFeature);
113 // if there is no coincidence then it is not valid
114 if (aCoinList.size() == 0)
117 // find that coincedence is the same
118 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
119 std::set<FeaturePtr>::const_iterator aCoinsIt;
120 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
121 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
122 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
123 aCoinsIt = aCoinList.find(aConstrFeature);
124 if (aCoinsIt != aCoinList.end()) {
125 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
126 AttributePtr aAR = aRAttr->attr();
127 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())