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 AttributePtr& theAttribute,
114 const std::list<std::string>& theArguments) const
116 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
118 // 1. check whether the object of the attribute is not among the feature attributes
119 // find the attribute's object
120 ObjectPtr anObject = getObject(theAttribute);
122 // check whether the object is not among other feature attributes
123 if (anObject.get() != NULL) {
124 // Check RefAttr attributes
125 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
126 //if (anAttrs.size() > 0) {
127 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
128 for(; anIt != anAttrs.end(); anIt++) {
129 AttributePtr anAttr = *anIt;
130 // the function parameter attribute should be skipped
131 if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
133 ObjectPtr aCurObject = getObject(anAttr);
134 if (aCurObject && aCurObject == anObject)
139 // 2. collect object referenced by theAttribute and ...
140 if (featureHasReferences(theAttribute)) {
141 // 3. check whether the attribute value is not among other feature attributes
142 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
143 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
144 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
145 for(; anAttr != anAttrs.end(); anAttr++) {
147 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
148 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
149 // check the object is already presented
150 if (!aRef->isObject() && aRef->attr() == theAttribute)
159 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
161 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
162 if (theAttribute->owner().get() && theAttribute->owner()->data().get())
163 theAttribute->owner()->data()->referencesToObjects(allRefs);
164 // collect object referenced by theAttribute
165 std::list<ObjectPtr>* anAttrObjs = 0;
166 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
167 for(; aRefIter != allRefs.end(); aRefIter++) {
168 if (theAttribute->id() == aRefIter->first)
169 anAttrObjs = &(aRefIter->second);
171 if (!anAttrObjs || anAttrObjs->empty())
172 return true; // theAttribute does not references to anything
173 // check with all others
174 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
175 if (theAttribute->id() == aRefIter->first)
176 continue; // do not check with myself
177 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
178 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
179 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
180 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
181 if (*aReferenced == *aReferencedByMe) // found same objects!
189 ObjectPtr PartSet_DifferentObjectsValidator::getObject(const AttributePtr& theAttribute) const
192 std::string anAttrType = theAttribute->attributeType();
193 if (anAttrType == ModelAPI_AttributeRefAttr::type()) {
194 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
195 if (anAttr != NULL && anAttr->isObject())
196 anObject = anAttr->object();
198 if (anAttrType == ModelAPI_AttributeSelection::type()) {
199 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
200 if (anAttr != NULL && anAttr->isInitialized())
201 anObject = anAttr->context();
203 if (anAttrType == ModelAPI_AttributeReference::type()) {
204 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
205 if (anAttr.get() != NULL && anAttr->isInitialized())
206 anObject = anAttr->value();
211 bool PartSet_SketchValidator::isValid(const ObjectPtr theObject) const
213 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
214 return aFeature->getKind() == SketchPlugin_Sketch::ID();