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>
15 #include <ModuleBase_WidgetShapeSelector.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_Object.h>
22 #include <SketchPlugin_Sketch.h>
29 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
31 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
33 foreach (ModuleBase_ViewerPrs aPrs, aList) {
34 const TopoDS_Shape& aShape = aPrs.shape();
35 if (!aShape.IsNull()) {
36 if (aShape.ShapeType() == TopAbs_VERTEX)
43 int shapesNbLines(const ModuleBase_ISelection* theSelection)
45 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
47 foreach(ModuleBase_ViewerPrs aPrs, aList) {
48 const TopoDS_Shape& aShape = aPrs.shape();
49 if (!aShape.IsNull()) {
50 if (aShape.ShapeType() == TopAbs_EDGE) {
51 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
52 Standard_Real aStart, aEnd;
53 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
54 GeomAdaptor_Curve aAdaptor(aCurve);
55 if (aAdaptor.GetType() == GeomAbs_Line)
63 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
65 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
66 return (aCount > 0) && (aCount < 3);
69 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
71 int aCount = shapesNbLines(theSelection);
72 return (aCount > 0) && (aCount < 2);
75 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
77 int aCount = shapesNbLines(theSelection);
78 return (aCount > 0) && (aCount < 3);
81 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
83 int aCount = shapesNbLines(theSelection);
84 return (aCount > 0) && (aCount < 3);
87 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
89 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
90 ModuleBase_ViewerPrs aPrs;
92 foreach (ModuleBase_ViewerPrs aPrs, aList) {
93 const TopoDS_Shape& aShape = aPrs.shape();
94 if (!aShape.IsNull()) {
95 if (aShape.ShapeType() == TopAbs_EDGE) {
96 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
97 Standard_Real aStart, aEnd;
98 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
99 GeomAdaptor_Curve aAdaptor(aCurve);
100 if (aAdaptor.GetType() == GeomAbs_Circle)
105 return (aCount > 0) && (aCount < 2);
108 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
110 int aCount = shapesNbLines(theSelection);
111 return (aCount > 0) && (aCount < 2);
114 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
115 const std::list<std::string>& theArguments) const
117 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
119 // 1. check whether the object of the attribute is not among the feature attributes
120 // find the attribute's object
121 ObjectPtr anObject = ModuleBase_WidgetShapeSelector::getObject(theAttribute);
123 // check whether the object is not among other feature attributes
124 if (anObject.get() != NULL) {
125 // Check RefAttr attributes
126 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
127 //if (anAttrs.size() > 0) {
128 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
129 for(; anIt != anAttrs.end(); anIt++) {
130 AttributePtr anAttr = *anIt;
131 // the function parameter attribute should be skipped
132 if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
134 ObjectPtr aCurObject = ModuleBase_WidgetShapeSelector::getObject(anAttr);
135 if (aCurObject && aCurObject == anObject)
140 // 2. collect object referenced by theAttribute and ...
141 if (featureHasReferences(theAttribute)) {
142 // 3. check whether the attribute value is not among other feature attributes
143 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
144 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
145 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
146 for(; anAttr != anAttrs.end(); anAttr++) {
148 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
149 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
150 // check the object is already presented
151 if (!aRef->isObject() && aRef->attr() == theAttribute)
160 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
162 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
163 if (theAttribute->owner().get() && theAttribute->owner()->data().get())
164 theAttribute->owner()->data()->referencesToObjects(allRefs);
165 // collect object referenced by theAttribute
166 std::list<ObjectPtr>* anAttrObjs = 0;
167 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
168 for(; aRefIter != allRefs.end(); aRefIter++) {
169 if (theAttribute->id() == aRefIter->first)
170 anAttrObjs = &(aRefIter->second);
172 if (!anAttrObjs || anAttrObjs->empty())
173 return true; // theAttribute does not references to anything
174 // check with all others
175 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
176 if (theAttribute->id() == aRefIter->first)
177 continue; // do not check with myself
178 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
179 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
180 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
181 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
182 if (*aReferenced == *aReferencedByMe) // found same objects!
190 bool PartSet_SketchValidator::isValid(const ObjectPtr theObject) const
192 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
193 return aFeature->getKind() == SketchPlugin_Sketch::ID();