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"
14 #include "SketcherPrs_Tools.h"
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_Session.h>
22 #include <GeomValidators_Edge.h>
24 #include <GeomDataAPI_Point2D.h>
27 bool SketchPlugin_DistanceAttrValidator::isValid(
28 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
30 // there is a check whether the feature contains a point and a linear edge or two point values
31 std::string aParamA = theArguments.front();
32 SessionPtr aMgr = ModelAPI_Session::get();
33 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
35 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
39 bool isObject = aRefAttr->isObject();
41 // an attribute is a point. A point value is valid always for the distance
44 // 1. check whether the references object is a linear
45 ObjectPtr anObject = aRefAttr->object();
47 const ModelAPI_AttributeValidator* anEdgeValidator =
48 dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
49 std::list<std::string> anArguments;
50 anArguments.push_back("circle");
51 bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
52 // the circle line is not a valid case
57 anArguments.push_back("line");
58 anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
59 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
63 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
64 // If it is a line then we have to check that first attribute id not a line
65 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
71 bool SketchPlugin_TangentAttrValidator::isValid(
72 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
74 // there is a check whether the feature contains a point and a linear edge or two point values
75 std::string aParamA = theArguments.front();
76 SessionPtr aMgr = ModelAPI_Session::get();
77 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
79 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
80 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
84 bool isObject = aRefAttr->isObject();
85 ObjectPtr anObject = aRefAttr->object();
86 if (isObject && anObject) {
87 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
89 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
90 ObjectPtr aOtherObject = aOtherAttr->object();
91 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
93 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
94 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
96 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
97 if (aOtherFea->getKind() != SketchPlugin_Line::ID())
102 // check that both have coincidence
103 FeaturePtr aConstrFeature;
104 std::set<FeaturePtr> aCoinList;
105 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
106 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
107 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
108 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
109 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
110 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
111 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
112 AttributePtr aAR = aRAttr->attr();
113 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
114 aCoinList.insert(aConstrFeature);
117 // if there is no coincidence then it is not valid
118 if (aCoinList.size() == 0)
121 // find that coincedence is the same
122 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
123 std::set<FeaturePtr>::const_iterator aCoinsIt;
124 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
125 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
126 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
127 aCoinsIt = aCoinList.find(aConstrFeature);
128 if (aCoinsIt != aCoinList.end()) {
129 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
130 AttributePtr aAR = aRAttr->attr();
131 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
139 bool SketchPlugin_NotFixedValidator::isValid(
140 const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
142 std::shared_ptr<SketchPlugin_Feature> aFeature =
143 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
147 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
151 SketchPlugin_Sketch* aSketch = aFeature->sketch();
152 int aNbFeatures = aSketch->numberOfSubs();
153 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
154 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
155 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
157 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
158 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
159 if (aRefAttr->isObject()) {
160 if (aRefAttr->object() == aRAttr->object())
162 } else if (aRefAttr->attr() == aRAttr->attr())