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"
9 #include "PartSet_Tools.h"
12 #include <TopoDS_Edge.hxx>
13 #include <BRep_Tool.hxx>
14 #include <GeomAdaptor_Curve.hxx>
15 #include <GeomAbs_CurveType.hxx>
16 #include <GeomValidators_Tools.h>
17 #include <ModuleBase_ISelection.h>
18 #include <ModuleBase_WidgetShapeSelector.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_Session.h>
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_ConstraintCoincidence.h>
30 #include <SketchPlugin_Arc.h>
31 #include <GeomAPI_Edge.h>
38 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
40 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
42 foreach (ModuleBase_ViewerPrs aPrs, aList) {
43 const TopoDS_Shape& aShape = aPrs.shape();
44 if (!aShape.IsNull()) {
45 if (aShape.ShapeType() == TopAbs_VERTEX)
52 int shapesNbLines(const ModuleBase_ISelection* theSelection)
54 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
56 foreach(ModuleBase_ViewerPrs aPrs, aList) {
57 const TopoDS_Shape& aShape = aPrs.shape();
58 if (!aShape.IsNull()) {
59 if (aShape.ShapeType() == TopAbs_EDGE) {
60 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
61 Standard_Real aStart, aEnd;
62 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
63 GeomAdaptor_Curve aAdaptor(aCurve);
64 if (aAdaptor.GetType() == GeomAbs_Line)
72 bool PartSet_DistanceSelection::isValid(const ModuleBase_ISelection* theSelection) const
74 int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
75 return (aCount > 0) && (aCount < 3);
78 bool PartSet_LengthSelection::isValid(const ModuleBase_ISelection* theSelection) const
80 int aCount = shapesNbLines(theSelection);
84 bool PartSet_PerpendicularSelection::isValid(const ModuleBase_ISelection* theSelection) const
86 int aCount = shapesNbLines(theSelection);
87 return (aCount > 0) && (aCount < 3);
90 bool PartSet_ParallelSelection::isValid(const ModuleBase_ISelection* theSelection) const
92 int aCount = shapesNbLines(theSelection);
93 return (aCount > 0) && (aCount < 3);
96 bool PartSet_RadiusSelection::isValid(const ModuleBase_ISelection* theSelection) const
98 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
99 ModuleBase_ViewerPrs aPrs;
101 foreach (ModuleBase_ViewerPrs aPrs, aList) {
102 const TopoDS_Shape& aShape = aPrs.shape();
103 if (!aShape.IsNull()) {
104 if (aShape.ShapeType() == TopAbs_EDGE) {
105 TopoDS_Edge aEdge = TopoDS::Edge(aShape);
106 Standard_Real aStart, aEnd;
107 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
108 GeomAdaptor_Curve aAdaptor(aCurve);
109 if (aAdaptor.GetType() == GeomAbs_Circle)
114 return (aCount == 1);
117 bool PartSet_RigidSelection::isValid(const ModuleBase_ISelection* theSelection) const
119 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
120 return (aList.count() == 1);
124 bool PartSet_CoincidentSelection::isValid(const ModuleBase_ISelection* theSelection) const
126 int aCount = shapesNbPoints(theSelection);
127 return (aCount > 0) && (aCount < 3);
130 bool PartSet_HVDirSelection::isValid(const ModuleBase_ISelection* theSelection) const
132 int aCount = shapesNbLines(theSelection);
133 return (aCount == 1);
136 bool PartSet_FilletSelection::isValid(const ModuleBase_ISelection* theSelection) const
138 int aCount = shapesNbLines(theSelection);
139 return (aCount > 0) && (aCount < 3);
142 bool PartSet_TangentSelection::isValid(const ModuleBase_ISelection* theSelection) const
144 QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
145 if ((aList.size() == 0) || (aList.size() > 2))
148 ModuleBase_ViewerPrs aPrs = aList.first();
149 const TopoDS_Shape& aShape = aPrs.shape();
153 if (aShape.ShapeType() != TopAbs_EDGE)
156 std::shared_ptr<GeomAPI_Shape> aShapePtr(new GeomAPI_Shape);
157 aShapePtr->setImpl(new TopoDS_Shape(aShape));
158 GeomAPI_Edge aEdge1(aShapePtr);
160 if (aEdge1.isLine() || aEdge1.isArc()) {
161 if (aList.size() == 2) {
162 // Check second selection
164 const TopoDS_Shape& aShape2 = aPrs.shape();
165 if (aShape2.IsNull())
168 if (aShape2.ShapeType() != TopAbs_EDGE)
171 std::shared_ptr<GeomAPI_Shape> aShapePtr2(new GeomAPI_Shape);
172 aShapePtr2->setImpl(new TopoDS_Shape(aShape2));
173 GeomAPI_Edge aEdge2(aShapePtr2);
174 if (aEdge1.isLine() && aEdge2.isArc())
176 else if (aEdge1.isArc() && aEdge2.isLine())
186 bool PartSet_AngleSelection::isValid(const ModuleBase_ISelection* theSelection) const
188 int aCount = shapesNbLines(theSelection);
189 return (aCount > 0) && (aCount < 3);
193 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
194 const std::list<std::string>& theArguments,
195 std::string& theError) const
197 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
199 // the type of validated attributes should be equal, attributes with different types are not validated
200 // Check RefAttr attributes
201 std::string anAttrType = theAttribute->attributeType();
202 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
204 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
205 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
206 bool isObject = anAttr->isObject();
207 ObjectPtr anObject = anAttr->object();
208 AttributePtr anAttributeAttr = anAttr->attr();
210 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
211 if (anAttrs.size() > 0) {
212 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
213 for(; anAttr != anAttrs.end(); anAttr++) {
214 if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
215 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
216 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
217 if (aRef->isObject() != isObject)
220 if (aRef->object() == anObject)
223 else { // the attribute reference
224 if (aRef->attr() == theAttribute)
231 else if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
232 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
233 ResultPtr aContext = anAttr->context();
234 GeomShapePtr aShape = anAttr->value();
236 // Check selection attributes
237 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
238 if (anAttrs.size() > 0) {
239 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
240 for(; anAttr != anAttrs.end(); anAttr++) {
241 if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
242 std::shared_ptr<ModelAPI_AttributeSelection> aRef =
243 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
244 // check the object is already presented
245 if (aRef->context() == aContext) {
246 bool aHasShape = aShape.get() != NULL;
247 if (!aHasShape || aRef->value()->isEqual(aShape))
254 else if (anAttrType == ModelAPI_AttributeReference::typeId()) {
255 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
256 ObjectPtr anObject = anAttr->value();
257 // Check selection attributes
258 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
259 if (anAttrs.size() > 0) {
260 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
261 for(; anAttr != anAttrs.end(); anAttr++) {
262 if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
263 std::shared_ptr<ModelAPI_AttributeReference> aRef =
264 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
265 // check the object is already presented
266 if (aRef->value() == anObject)
273 else if(anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
274 std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList =
275 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
276 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
277 if(anAttrs.size() > 0) {
278 std::list<std::shared_ptr<ModelAPI_Attribute>>::iterator anAttrItr = anAttrs.begin();
279 for(; anAttrItr != anAttrs.end(); anAttrItr++){
280 if ((*anAttrItr).get() && (*anAttrItr)->id() != theAttribute->id()){
281 std::shared_ptr<ModelAPI_AttributeSelectionList> aRefSelList =
282 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*anAttrItr);
283 for(int i = 0; i < aCurSelList->size(); i++) {
284 std::shared_ptr<ModelAPI_AttributeSelection> aCurSel = aCurSelList->value(i);
285 for(int j = 0; j < aRefSelList->size(); j++) {
286 std::shared_ptr<ModelAPI_AttributeSelection> aRefSel = aRefSelList->value(j);
287 if(aCurSel->context() == aRefSel->context()) {
288 if(aCurSel->value().get() == NULL || aRefSel->value().get() == NULL
289 || aCurSel->value()->isEqual(aRefSel->value())) {
299 else if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
300 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
301 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
302 anAttrs = aFeature->data()->attributes(ModelAPI_AttributeRefList::typeId());
303 if (anAttrs.size() > 0) {
304 std::list<std::shared_ptr<ModelAPI_Attribute>>::iterator anAttrItr = anAttrs.begin();
305 for (; anAttrItr != anAttrs.end(); anAttrItr++){
306 if ((*anAttrItr).get() && (*anAttrItr)->id() != theAttribute->id()){
307 std::shared_ptr<ModelAPI_AttributeRefList> aRefSelList =
308 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anAttrItr);
309 for (int i = 0; i < aCurSelList->size(); i++) {
310 ObjectPtr aCurSelObject = aCurSelList->object(i);
311 for (int j = 0; j < aRefSelList->size(); j++) {
312 if (aCurSelObject == aRefSelList->object(j)) {
321 return !featureHasReferences(theAttribute);
324 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
326 std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
327 if (theAttribute->owner().get() && theAttribute->owner()->data()->isValid())
328 theAttribute->owner()->data()->referencesToObjects(allRefs);
329 // collect object referenced by theAttribute
330 std::list<ObjectPtr>* anAttrObjs = 0;
331 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
332 for(; aRefIter != allRefs.end(); aRefIter++) {
333 if (theAttribute->id() == aRefIter->first)
334 anAttrObjs = &(aRefIter->second);
336 if (!anAttrObjs || anAttrObjs->empty())
337 return false; // theAttribute does not references to anything
338 // check with all others
339 for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
340 if (theAttribute->id() == aRefIter->first)
341 continue; // do not check with myself
342 std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
343 for(; aReferenced != aRefIter->second.end(); aReferenced++) {
344 std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
345 for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
346 if (*aReferenced == *aReferencedByMe) // found same objects!
354 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
355 const std::list<std::string>& theArguments,
356 std::string& theError) const
358 bool isSketchEntities = true;
359 std::set<std::string> anEntityKinds;
360 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
361 for (; anIt != aLast; anIt++) {
362 anEntityKinds.insert(*anIt);
365 std::string anAttributeType = theAttribute->attributeType();
366 if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
367 AttributeSelectionListPtr aSelectionListAttr =
368 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
369 // all context objects should be sketch entities
370 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize && isSketchEntities; i++) {
371 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
372 ObjectPtr anObject = aSelectAttr->context();
373 // a context of the selection attribute is a feature result. It can be a case when the result
374 // of the feature is null, e.g. the feature is modified and has not been executed yet.
375 // The validator returns an invalid result here. The case is an extrusion built on a sketch
376 // feature. A new sketch element creation leads to an empty result.
378 isSketchEntities = false;
380 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
381 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
385 if (anAttributeType == ModelAPI_AttributeRefList::typeId()) {
386 AttributeRefListPtr aRefListAttr =
387 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
388 // all context objects should be sketch entities
389 for (int i = 0, aSize = aRefListAttr->size(); i < aSize && isSketchEntities; i++) {
390 ObjectPtr anObject = aRefListAttr->object(i);
391 // a context of the selection attribute is a feature result. It can be a case when the result
392 // of the feature is null, e.g. the feature is modified and has not been executed yet.
393 // The validator returns an invalid result here. The case is an extrusion built on a sketch
394 // feature. A new sketch element creation leads to an empty result.
396 isSketchEntities = false;
398 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
399 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
403 if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
404 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
405 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
406 isSketchEntities = false;
407 if (aRef->isObject()) {
408 ObjectPtr anObject = aRef->object();
409 if (anObject.get() != NULL) {
410 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
411 if (aFeature.get() != NULL)
412 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
417 return isSketchEntities;
422 bool PartSet_SameTypeAttrValidator::isValid(const AttributePtr& theAttribute,
423 const std::list<std::string>& theArguments,
424 std::string& theError ) const
426 // there is a check whether the feature contains a point and a linear edge or two point values
427 std::string aParamA = theArguments.front();
428 SessionPtr aMgr = ModelAPI_Session::get();
429 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
431 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
432 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
436 bool isObject = aRefAttr->isObject();
437 ObjectPtr anObject = aRefAttr->object();
438 if (isObject && anObject) {
439 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
441 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
442 ObjectPtr aOtherObject = aOtherAttr->object();
443 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
444 return aRefFea->getKind() == aOtherFea->getKind();
449 bool PartSet_CoincidentAttr::isValid(const AttributePtr& theAttribute,
450 const std::list<std::string>& theArguments,
451 std::string& theError) const
453 // there is a check whether the feature contains a point and a linear edge or two point values
454 std::string aParamA = theArguments.front();
455 SessionPtr aMgr = ModelAPI_Session::get();
456 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
458 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
459 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
463 QList<FeaturePtr> aCoinsideLines;
465 bool isObject = aRefAttr->isObject();
466 ObjectPtr anObject = aRefAttr->object();
467 if (isObject && anObject) {
468 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
469 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
470 ObjectPtr aOtherObject = aOtherAttr->object();
471 // if the other attribute is not filled still, the result is true
472 if (!aOtherObject.get())
474 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
476 // check that both have coincidence
477 FeaturePtr aConstrFeature;
478 std::set<FeaturePtr> aCoinList;
479 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
480 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
481 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
482 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
483 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
484 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
485 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
486 AttributePtr aAR = aRAttr->attr();
487 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
488 aCoinList.insert(aConstrFeature);
489 PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
490 SketchPlugin_ConstraintCoincidence::ENTITY_A());
491 PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
492 SketchPlugin_ConstraintCoincidence::ENTITY_B());
495 // if there is no coincidence then it is not valid
496 if (aCoinList.size() == 0)
499 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(), aLast = aCoinsideLines.end();
501 for (; anIt != aLast && !aValid; anIt++) {
502 aValid = *anIt == aOtherFea;