1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_Validators.cpp
4 // Created: 09 July 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_Validators.h"
10 #include <TopoDS_Edge.hxx>
11 #include <BRep_Tool.hxx>
12 #include <GeomAdaptor_Curve.hxx>
13 #include <GeomAbs_CurveType.hxx>
14 #include <ModuleBase_ISelection.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeReference.h>
25 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
27 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
29 foreach (ModuleBase_ViewerPrs aPrs, aList) {
30 const TopoDS_Shape& aShape = aPrs.shape();
31 if (!aShape.IsNull()) {
32 if (aShape.ShapeType() == TopAbs_VERTEX)
39 int shapesNbLines(const ModuleBase_ISelection* theSelection)
41 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
43 foreach(ModuleBase_ViewerPrs aPrs, aList) {
44 const TopoDS_Shape& aShape = aPrs.shape();
45 if (!aShape.IsNull()) {
46 if (aShape.ShapeType() == TopAbs_EDGE) {
47 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
48 Standard_Real aStart, aEnd;
49 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
50 GeomAdaptor_Curve aAdaptor(aCurve);
51 if (aAdaptor.GetType() == GeomAbs_Line)
59 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
61 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
62 return (aCount > 0) && (aCount < 3);
65 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
67 int aCount = shapesNbLines(theSelection);
68 return (aCount > 0) && (aCount < 2);
71 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
73 int aCount = shapesNbLines(theSelection);
74 return (aCount > 0) && (aCount < 3);
77 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
79 int aCount = shapesNbLines(theSelection);
80 return (aCount > 0) && (aCount < 3);
83 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
85 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
86 ModuleBase_ViewerPrs aPrs;
88 foreach (ModuleBase_ViewerPrs aPrs, aList) {
89 const TopoDS_Shape& aShape = aPrs.shape();
90 if (!aShape.IsNull()) {
91 if (aShape.ShapeType() == TopAbs_EDGE) {
92 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
93 Standard_Real aStart, aEnd;
94 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
95 GeomAdaptor_Curve aAdaptor(aCurve);
96 if (aAdaptor.GetType() == GeomAbs_Circle)
101 return (aCount > 0) && (aCount < 2);
104 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
106 int aCount = shapesNbLines(theSelection);
107 return (aCount > 0) && (aCount < 2);
110 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
111 const std::list<std::string>& theArguments,
112 const ObjectPtr& theObject) const
114 // Check RefAttr attributes
115 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
116 theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
117 if (anAttrs.size() > 0) {
118 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
119 for(; anAttr != anAttrs.end(); anAttr++) {
121 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
122 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
123 // check the object is already presented
124 if (aRef->isObject() && aRef->object() == theObject)
129 // Check selection attributes
130 anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
131 if (anAttrs.size() > 0) {
132 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
133 for(; anAttr != anAttrs.end(); anAttr++) {
135 std::shared_ptr<ModelAPI_AttributeSelection> aRef =
136 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
137 // check the object is already presented
138 if (aRef->isInitialized() && aRef->context() == theObject)
143 // Check selection attributes
144 anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::type());
145 if (anAttrs.size() > 0) {
146 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
147 for(; anAttr != anAttrs.end(); anAttr++) {
149 std::shared_ptr<ModelAPI_AttributeReference> aRef =
150 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
151 // check the object is already presented
152 if (aRef->isInitialized() && aRef->value() == theObject)
160 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
161 const std::list<std::string>& theArguments,
162 const AttributePtr& theAttribute) const
168 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
169 const std::list<std::string>& theArguments) const