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_AttributeSelectionList.h>
21 #include <ModelAPI_Object.h>
23 #include <SketchPlugin_Sketch.h>
30 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
32 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
34 foreach (ModuleBase_ViewerPrs aPrs, aList) {
35 const TopoDS_Shape& aShape = aPrs.shape();
36 if (!aShape.IsNull()) {
37 if (aShape.ShapeType() == TopAbs_VERTEX)
44 int shapesNbLines(const ModuleBase_ISelection* theSelection)
46 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
48 foreach(ModuleBase_ViewerPrs aPrs, aList) {
49 const TopoDS_Shape& aShape = aPrs.shape();
50 if (!aShape.IsNull()) {
51 if (aShape.ShapeType() == TopAbs_EDGE) {
52 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
53 Standard_Real aStart, aEnd;
54 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
55 GeomAdaptor_Curve aAdaptor(aCurve);
56 if (aAdaptor.GetType() == GeomAbs_Line)
64 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
66 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
67 return (aCount > 0) && (aCount < 3);
70 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
72 int aCount = shapesNbLines(theSelection);
73 return (aCount > 0) && (aCount < 2);
76 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
78 int aCount = shapesNbLines(theSelection);
79 return (aCount > 0) && (aCount < 3);
82 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
84 int aCount = shapesNbLines(theSelection);
85 return (aCount > 0) && (aCount < 3);
88 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
90 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
91 ModuleBase_ViewerPrs aPrs;
93 foreach (ModuleBase_ViewerPrs aPrs, aList) {
94 const TopoDS_Shape& aShape = aPrs.shape();
95 if (!aShape.IsNull()) {
96 if (aShape.ShapeType() == TopAbs_EDGE) {
97 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
98 Standard_Real aStart, aEnd;
99 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
100 GeomAdaptor_Curve aAdaptor(aCurve);
101 if (aAdaptor.GetType() == GeomAbs_Circle)
106 return (aCount > 0) && (aCount < 2);
109 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
111 int aCount = shapesNbLines(theSelection);
112 return (aCount > 0) && (aCount < 2);
115 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
116 const std::list<std::string>& theArguments) const
118 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
120 // 1. check whether the object of the attribute is not among the feature attributes
121 // find the attribute's object
122 ObjectPtr anObject = ModuleBase_WidgetShapeSelector::getObject(theAttribute);
124 // check whether the object is not among other feature attributes
125 if (anObject.get() != NULL) {
126 // Check RefAttr attributes
127 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
128 //if (anAttrs.size() > 0) {
129 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
130 for(; anIt != anAttrs.end(); anIt++) {
131 AttributePtr anAttr = *anIt;
132 // the function parameter attribute should be skipped
133 if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
135 ObjectPtr aCurObject = ModuleBase_WidgetShapeSelector::getObject(anAttr);
136 if (aCurObject && aCurObject == anObject)
141 // 2. collect object referenced by theAttribute and ...
142 if (featureHasReferences(theAttribute)) {
143 // 3. check whether the attribute value is not among other feature attributes
144 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
145 aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
146 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
147 for(; anAttr != anAttrs.end(); anAttr++) {
149 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
150 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
151 // check the object is already presented
152 if (!aRef->isObject() && aRef->attr() == theAttribute)
162 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
164 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
165 if (theAttribute->owner().get() && theAttribute->owner()->data().get())
166 theAttribute->owner()->data()->referencesToObjects(allRefs);
167 // collect object referenced by theAttribute
168 std::list<ObjectPtr>* anAttrObjs = 0;
169 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
170 for(; aRefIter != allRefs.end(); aRefIter++) {
171 if (theAttribute->id() == aRefIter->first)
172 anAttrObjs = &(aRefIter->second);
174 if (!anAttrObjs || anAttrObjs->empty())
175 return true; // theAttribute does not references to anything
176 // check with all others
177 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
178 if (theAttribute->id() == aRefIter->first)
179 continue; // do not check with myself
180 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
181 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
182 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
183 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
184 if (*aReferenced == *aReferencedByMe) // found same objects!
192 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
193 const std::list<std::string>& theArguments) const
195 bool isSketchEntities = true;
196 std::set<std::string> anEntityKinds;
197 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
198 for (; anIt != aLast; anIt++) {
199 anEntityKinds.insert(*anIt);
202 std::string anAttributeType = theAttribute->attributeType();
203 if (anAttributeType == ModelAPI_AttributeSelectionList::type()) {
204 AttributeSelectionListPtr aSelectionListAttr =
205 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
206 // it filters only selection list attributes
207 std::string aType = aSelectionListAttr->selectionType().c_str();
208 // all context objects should be sketch entities
209 int aSize = aSelectionListAttr->size();
210 for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
211 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
212 ObjectPtr anObject = aSelectAttr->context();
213 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
214 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
217 if (anAttributeType == ModelAPI_AttributeRefAttr::type()) {
218 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
219 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
220 isSketchEntities = false;
221 if (aRef->isObject()) {
222 ObjectPtr anObject = aRef->object();
223 if (anObject.get() != NULL) {
224 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
225 if (aFeature.get() != NULL)
226 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
231 return isSketchEntities;