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
13 #include <PartSet_Tools.h>
14 #include <SketchPlugin_Feature.h>
15
16 bool PartSet_WidgetShapeSelector::storeAttributeValues(ObjectPtr theSelectedObject, GeomShapePtr theShape)
17 {
18   ObjectPtr aSelectedObject = theSelectedObject;
19   GeomShapePtr aShape = theShape;
20
21   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
22   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
23     return false;
24   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
25           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
26   if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull()) {
27     // Processing of external (non-sketch) object
28     ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(aShape->impl<TopoDS_Shape>(),
29                                                                 aSelectedObject, mySketch);
30     if (aObj) {
31       PartSet_WidgetShapeSelector* that = (PartSet_WidgetShapeSelector*) this;
32       aSelectedObject = aObj;
33       myExternalObject = aObj;
34     } else 
35       return false;
36   } else {
37     // Processing of sketch object
38     DataPtr aData = myFeature->data();
39     if (aShape) {
40       AttributePtr aAttr = aData->attribute(attributeID());
41       AttributeRefAttrPtr aRefAttr = 
42         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
43       if (aRefAttr) {
44         // it is possible that the point feature is selected. It should be used itself
45         // instead of searching an attribute for the shape
46         bool aShapeIsResult = false;
47         /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
48         if (aResult.get() != NULL) {
49           GeomShapePtr aShapePtr = aResult->shape();
50           // it is important to call isEqual of the shape of result.
51           // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
52           // GeomAPI_Shape. It is important to use the realization of the isEqual method from
53           // GeomAPI_Vertex class
54           aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
55         }*/
56
57         AttributePtr aPntAttr;
58         if (!aShapeIsResult) {
59           TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
60           aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
61         }
62         // this is an alternative, whether the attribute should be set or object in the attribute
63         // the first check is the attribute because the object already exist
64         // the object is set only if there is no selected attribute
65         // test case is - preselection for distance operation, which contains two points selected on lines
66         if (aPntAttr)
67           aRefAttr->setAttr(aPntAttr);
68         else if (aSelectedObject)
69           aRefAttr->setObject(aSelectedObject);
70         return true;
71       }
72     }
73   }
74   return ModuleBase_WidgetShapeSelector::storeAttributeValues(aSelectedObject, aShape);
75 }
76