Salome HOME
45fe790e76675c38f733199dba43de5e6853eeb2
[modules/shaper.git] / 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) const
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     } else 
34       return false;
35   } else {
36     // Processing of sketch object
37     DataPtr aData = myFeature->data();
38     if (aShape) {
39       AttributePtr aAttr = aData->attribute(attributeID());
40       AttributeRefAttrPtr aRefAttr = 
41         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
42       if (aRefAttr) {
43         // it is possible that the point feature is selected. It should be used itself
44         // instead of searching an attribute for the shape
45         bool aShapeIsResult = false;
46         /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
47         if (aResult.get() != NULL) {
48           GeomShapePtr aShapePtr = aResult->shape();
49           // it is important to call isEqual of the shape of result.
50           // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
51           // GeomAPI_Shape. It is important to use the realization of the isEqual method from
52           // GeomAPI_Vertex class
53           aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
54         }*/
55
56         AttributePtr aPntAttr;
57         if (!aShapeIsResult) {
58           TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
59           aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
60         }
61         // this is an alternative, whether the attribute should be set or object in the attribute
62         // the first check is the attribute because the object already exist
63         // the object is set only if there is no selected attribute
64         // test case is - preselection for distance operation, which contains two points selected on lines
65         if (aPntAttr)
66           aRefAttr->setAttr(aPntAttr);
67         else if (aSelectedObject)
68           aRefAttr->setObject(aSelectedObject);
69         return true;
70       }
71     }
72   }
73   return ModuleBase_WidgetShapeSelector::storeAttributeValues(aSelectedObject, aShape);
74 }
75