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>
19 #include <ModelAPI_Object.h>
21 #include <SketchPlugin_Sketch.h>
28 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
30 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
32 foreach (ModuleBase_ViewerPrs aPrs, aList) {
33 const TopoDS_Shape& aShape = aPrs.shape();
34 if (!aShape.IsNull()) {
35 if (aShape.ShapeType() == TopAbs_VERTEX)
42 int shapesNbLines(const ModuleBase_ISelection* theSelection)
44 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
46 foreach(ModuleBase_ViewerPrs aPrs, aList) {
47 const TopoDS_Shape& aShape = aPrs.shape();
48 if (!aShape.IsNull()) {
49 if (aShape.ShapeType() == TopAbs_EDGE) {
50 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
51 Standard_Real aStart, aEnd;
52 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
53 GeomAdaptor_Curve aAdaptor(aCurve);
54 if (aAdaptor.GetType() == GeomAbs_Line)
62 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
64 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
65 return (aCount > 0) && (aCount < 3);
68 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
70 int aCount = shapesNbLines(theSelection);
71 return (aCount > 0) && (aCount < 2);
74 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
76 int aCount = shapesNbLines(theSelection);
77 return (aCount > 0) && (aCount < 3);
80 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
82 int aCount = shapesNbLines(theSelection);
83 return (aCount > 0) && (aCount < 3);
86 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
88 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
89 ModuleBase_ViewerPrs aPrs;
91 foreach (ModuleBase_ViewerPrs aPrs, aList) {
92 const TopoDS_Shape& aShape = aPrs.shape();
93 if (!aShape.IsNull()) {
94 if (aShape.ShapeType() == TopAbs_EDGE) {
95 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
96 Standard_Real aStart, aEnd;
97 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
98 GeomAdaptor_Curve aAdaptor(aCurve);
99 if (aAdaptor.GetType() == GeomAbs_Circle)
104 return (aCount > 0) && (aCount < 2);
107 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
109 int aCount = shapesNbLines(theSelection);
110 return (aCount > 0) && (aCount < 2);
113 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
114 const std::list<std::string>& theArguments,
115 const ObjectPtr& theObject) const
117 // Check RefAttr attributes
118 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
119 theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
120 if (anAttrs.size() > 0) {
121 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
122 for(; anAttr != anAttrs.end(); anAttr++) {
124 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
125 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
126 // check the object is already presented
127 if (aRef->isObject() && aRef->object() == theObject)
132 // Check selection attributes
133 anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
134 if (anAttrs.size() > 0) {
135 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
136 for(; anAttr != anAttrs.end(); anAttr++) {
138 std::shared_ptr<ModelAPI_AttributeSelection> aRef =
139 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
140 // check the object is already presented
141 if (aRef->isInitialized() && aRef->context() == theObject)
146 // Check selection attributes
147 anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::type());
148 if (anAttrs.size() > 0) {
149 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
150 for(; anAttr != anAttrs.end(); anAttr++) {
152 std::shared_ptr<ModelAPI_AttributeReference> aRef =
153 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
154 // check the object is already presented
155 if (aRef->isInitialized() && aRef->value() == theObject)
163 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
164 const std::list<std::string>& theArguments,
165 const AttributePtr& theAttribute) const
171 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
172 const std::list<std::string>& theArguments) const
174 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
175 if (theAttribute->owner().get() && theAttribute->owner()->data().get())
176 theAttribute->owner()->data()->referencesToObjects(allRefs);
177 // collect object referenced by theAttribute
178 std::list<ObjectPtr>* anAttrObjs = 0;
179 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
180 for(; aRefIter != allRefs.end(); aRefIter++) {
181 if (theAttribute->id() == aRefIter->first)
182 anAttrObjs = &(aRefIter->second);
184 if (!anAttrObjs || anAttrObjs->empty())
185 return true; // theAttribute does not references to anything
186 // check with all others
187 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
188 if (theAttribute->id() == aRefIter->first)
189 continue; // do not check with myself
190 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
191 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
192 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
193 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
194 if (*aReferenced == *aReferencedByMe) // found same objects!
202 bool PartSet_SketchValidator::isValid(const ObjectPtr theObject) const
204 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
205 return aFeature->getKind() == SketchPlugin_Sketch::ID();