Salome HOME
Union of validator and filter functionalities.
[modules/shaper.git] / src / PartSet / PartSet_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetShapeSelector.cpp
4 // Created:     27 Nov 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "PartSet_WidgetShapeSelector.h"
8
9 #include <ModelAPI_AttributeRefAttr.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12 #include <ModelAPI_RefAttrValidator.h>
13 #include <ModelAPI_ResultValidator.h>
14
15 #include <PartSet_Tools.h>
16 #include <SketchPlugin_Feature.h>
17
18 bool PartSet_WidgetShapeSelector::storeAttributeValues(ObjectPtr theSelectedObject, GeomShapePtr theShape) const
19 {
20   ObjectPtr aSelectedObject = theSelectedObject;
21   GeomShapePtr aShape = theShape;
22
23   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
24   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
25     return false;
26   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
27           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
28   if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull()) {
29     // Processing of external (non-sketch) object
30     ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(aShape->impl<TopoDS_Shape>(),
31                                                                 aSelectedObject, mySketch);
32     if (aObj) {
33       PartSet_WidgetShapeSelector* that = (PartSet_WidgetShapeSelector*) this;
34       aSelectedObject = aObj;
35     } else 
36       return false;
37   } else {
38     // Processing of sketch object
39     DataPtr aData = myFeature->data();
40     if (aShape) {
41       AttributePtr aAttr = aData->attribute(attributeID());
42       AttributeRefAttrPtr aRefAttr = 
43         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
44       if (aRefAttr) {
45         // it is possible that the point feature is selected. It should be used itself
46         // instead of searching an attribute for the shape
47         bool aShapeIsResult = false;
48         /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
49         if (aResult.get() != NULL) {
50           GeomShapePtr aShapePtr = aResult->shape();
51           // it is important to call isEqual of the shape of result.
52           // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
53           // GeomAPI_Shape. It is important to use the realization of the isEqual method from
54           // GeomAPI_Vertex class
55           aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
56         }*/
57
58         AttributePtr aPntAttr;
59         if (!aShapeIsResult) {
60           TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
61           aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
62         }
63         // this is an alternative, whether the attribute should be set or object in the attribute
64         // the first check is the attribute because the object already exist
65         // the object is set only if there is no selected attribute
66         // test case is - preselection for distance operation, which contains two points selected on lines
67         if (aPntAttr)
68           aRefAttr->setAttr(aPntAttr);
69         else if (aSelectedObject)
70           aRefAttr->setObject(aSelectedObject);
71         return true;
72       }
73     }
74   }
75   return ModuleBase_WidgetShapeSelector::storeAttributeValues(aSelectedObject, aShape);
76 }
77