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 <GeomValidators_Tools.h>
15 #include <ModuleBase_ISelection.h>
16 #include <ModuleBase_WidgetShapeSelector.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeSelection.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_Object.h>
24 #include <SketchPlugin_Sketch.h>
31 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
33 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
35 foreach (ModuleBase_ViewerPrs aPrs, aList) {
36 const TopoDS_Shape& aShape = aPrs.shape();
37 if (!aShape.IsNull()) {
38 if (aShape.ShapeType() == TopAbs_VERTEX)
45 int shapesNbLines(const ModuleBase_ISelection* theSelection)
47 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
49 foreach(ModuleBase_ViewerPrs aPrs, aList) {
50 const TopoDS_Shape& aShape = aPrs.shape();
51 if (!aShape.IsNull()) {
52 if (aShape.ShapeType() == TopAbs_EDGE) {
53 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
54 Standard_Real aStart, aEnd;
55 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
56 GeomAdaptor_Curve aAdaptor(aCurve);
57 if (aAdaptor.GetType() == GeomAbs_Line)
65 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
67 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
68 return (aCount > 0) && (aCount < 3);
71 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
73 int aCount = shapesNbLines(theSelection);
74 return (aCount > 0) && (aCount < 2);
77 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
79 int aCount = shapesNbLines(theSelection);
80 return (aCount > 0) && (aCount < 3);
83 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
85 int aCount = shapesNbLines(theSelection);
86 return (aCount > 0) && (aCount < 3);
89 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
91 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
92 ModuleBase_ViewerPrs aPrs;
94 foreach (ModuleBase_ViewerPrs aPrs, aList) {
95 const TopoDS_Shape& aShape = aPrs.shape();
96 if (!aShape.IsNull()) {
97 if (aShape.ShapeType() == TopAbs_EDGE) {
98 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
99 Standard_Real aStart, aEnd;
100 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
101 GeomAdaptor_Curve aAdaptor(aCurve);
102 if (aAdaptor.GetType() == GeomAbs_Circle)
107 return (aCount > 0) && (aCount < 2);
110 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
112 int aCount = shapesNbLines(theSelection);
113 return (aCount > 0) && (aCount < 2);
116 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
117 const std::list<std::string>& theArguments) const
119 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
121 // the type of validated attributes should be equal, attributes with different types are not validated
122 // Check RefAttr attributes
123 std::string anAttrType = theAttribute->attributeType();
124 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
126 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
127 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
128 bool isObject = anAttr->isObject();
129 ObjectPtr anObject = anAttr->object();
130 AttributePtr anAttributeAttr = anAttr->attr();
132 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
133 if (anAttrs.size() > 0) {
134 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
135 for(; anAttr != anAttrs.end(); anAttr++) {
136 if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
137 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
138 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
139 if (aRef->isObject() != isObject)
142 if (aRef->object() == anObject)
145 else { // the attribute reference
146 if (aRef->attr() == theAttribute)
153 else if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
154 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
155 ResultPtr aContext = anAttr->context();
156 GeomShapePtr aShape = anAttr->value();
158 // Check selection attributes
159 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
160 if (anAttrs.size() > 0) {
161 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
162 for(; anAttr != anAttrs.end(); anAttr++) {
163 if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
164 std::shared_ptr<ModelAPI_AttributeSelection> aRef =
165 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
166 // check the object is already presented
167 if (aRef->context() == aContext) {
168 bool aHasShape = aShape.get() != NULL;
169 if (!aHasShape || aRef->value()->isEqual(aShape))
176 else if (anAttrType == ModelAPI_AttributeReference::typeId()) {
177 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
178 ObjectPtr anObject = anAttr->value();
179 // Check selection attributes
180 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
181 if (anAttrs.size() > 0) {
182 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
183 for(; anAttr != anAttrs.end(); anAttr++) {
184 if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
185 std::shared_ptr<ModelAPI_AttributeReference> aRef =
186 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
187 // check the object is already presented
188 if (aRef->value() == anObject)
195 return !featureHasReferences(theAttribute);
198 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
200 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
201 if (theAttribute->owner().get() && theAttribute->owner()->data().get())
202 theAttribute->owner()->data()->referencesToObjects(allRefs);
203 // collect object referenced by theAttribute
204 std::list<ObjectPtr>* anAttrObjs = 0;
205 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
206 for(; aRefIter != allRefs.end(); aRefIter++) {
207 if (theAttribute->id() == aRefIter->first)
208 anAttrObjs = &(aRefIter->second);
210 if (!anAttrObjs || anAttrObjs->empty())
211 return false; // theAttribute does not references to anything
212 // check with all others
213 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
214 if (theAttribute->id() == aRefIter->first)
215 continue; // do not check with myself
216 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
217 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
218 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
219 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
220 if (*aReferenced == *aReferencedByMe) // found same objects!
228 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
229 const std::list<std::string>& theArguments) const
231 bool isSketchEntities = true;
232 std::set<std::string> anEntityKinds;
233 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
234 for (; anIt != aLast; anIt++) {
235 anEntityKinds.insert(*anIt);
238 std::string anAttributeType = theAttribute->attributeType();
239 if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
240 AttributeSelectionListPtr aSelectionListAttr =
241 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
242 // it filters only selection list attributes
243 std::string aType = aSelectionListAttr->selectionType().c_str();
244 // all context objects should be sketch entities
245 int aSize = aSelectionListAttr->size();
246 for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
247 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
248 ObjectPtr anObject = aSelectAttr->context();
249 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
250 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
253 if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
254 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
255 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
256 isSketchEntities = false;
257 if (aRef->isObject()) {
258 ObjectPtr anObject = aRef->object();
259 if (anObject.get() != NULL) {
260 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
261 if (aFeature.get() != NULL)
262 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
267 return isSketchEntities;