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>
20 #include <SketchPlugin_Sketch.h>
27 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
29 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
31 foreach (ModuleBase_ViewerPrs aPrs, aList) {
32 const TopoDS_Shape& aShape = aPrs.shape();
33 if (!aShape.IsNull()) {
34 if (aShape.ShapeType() == TopAbs_VERTEX)
41 int shapesNbLines(const ModuleBase_ISelection* theSelection)
43 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
45 foreach(ModuleBase_ViewerPrs aPrs, aList) {
46 const TopoDS_Shape& aShape = aPrs.shape();
47 if (!aShape.IsNull()) {
48 if (aShape.ShapeType() == TopAbs_EDGE) {
49 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
50 Standard_Real aStart, aEnd;
51 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
52 GeomAdaptor_Curve aAdaptor(aCurve);
53 if (aAdaptor.GetType() == GeomAbs_Line)
61 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
63 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
64 return (aCount > 0) && (aCount < 3);
67 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
69 int aCount = shapesNbLines(theSelection);
70 return (aCount > 0) && (aCount < 2);
73 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
75 int aCount = shapesNbLines(theSelection);
76 return (aCount > 0) && (aCount < 3);
79 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
81 int aCount = shapesNbLines(theSelection);
82 return (aCount > 0) && (aCount < 3);
85 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
87 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
88 ModuleBase_ViewerPrs aPrs;
90 foreach (ModuleBase_ViewerPrs aPrs, aList) {
91 const TopoDS_Shape& aShape = aPrs.shape();
92 if (!aShape.IsNull()) {
93 if (aShape.ShapeType() == TopAbs_EDGE) {
94 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
95 Standard_Real aStart, aEnd;
96 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
97 GeomAdaptor_Curve aAdaptor(aCurve);
98 if (aAdaptor.GetType() == GeomAbs_Circle)
103 return (aCount > 0) && (aCount < 2);
106 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
108 int aCount = shapesNbLines(theSelection);
109 return (aCount > 0) && (aCount < 2);
112 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
113 const std::list<std::string>& theArguments,
114 const ObjectPtr& theObject) const
116 // Check RefAttr attributes
117 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
118 theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
119 if (anAttrs.size() > 0) {
120 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
121 for(; anAttr != anAttrs.end(); anAttr++) {
123 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
124 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
125 // check the object is already presented
126 if (aRef->isObject() && aRef->object() == theObject)
131 // Check selection attributes
132 anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
133 if (anAttrs.size() > 0) {
134 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
135 for(; anAttr != anAttrs.end(); anAttr++) {
137 std::shared_ptr<ModelAPI_AttributeSelection> aRef =
138 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
139 // check the object is already presented
140 if (aRef->isInitialized() && aRef->context() == theObject)
145 // Check selection attributes
146 anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::type());
147 if (anAttrs.size() > 0) {
148 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
149 for(; anAttr != anAttrs.end(); anAttr++) {
151 std::shared_ptr<ModelAPI_AttributeReference> aRef =
152 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
153 // check the object is already presented
154 if (aRef->isInitialized() && aRef->value() == theObject)
162 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
163 const std::list<std::string>& theArguments,
164 const AttributePtr& theAttribute) const
170 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
171 const std::list<std::string>& theArguments) const
177 bool PartSet_SketchValidator::isValid(const ObjectPtr theObject) const
179 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
180 return aFeature->getKind() == SketchPlugin_Sketch::ID();