Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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::setObject(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     createExternal(theSelectedObject, theShape);
29     if (myExternalObject)
30       aSelectedObject = myExternalObject;
31     else
32       return false;
33   } else {
34     // Processing of sketch object
35     DataPtr aData = myFeature->data();
36     if (aShape) {
37       AttributePtr aAttr = aData->attribute(attributeID());
38       AttributeRefAttrPtr aRefAttr = 
39         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
40       if (aRefAttr) {
41         // it is possible that the point feature is selected. It should be used itself
42         // instead of searching an attribute for the shape
43         bool aShapeIsResult = false;
44         /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
45         if (aResult.get() != NULL) {
46           GeomShapePtr aShapePtr = aResult->shape();
47           // it is important to call isEqual of the shape of result.
48           // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
49           // GeomAPI_Shape. It is important to use the realization of the isEqual method from
50           // GeomAPI_Vertex class
51           aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
52         }*/
53
54         AttributePtr aPntAttr;
55         if (!aShapeIsResult) {
56           TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
57           aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
58         }
59         // this is an alternative, whether the attribute should be set or object in the attribute
60         // the first check is the attribute because the object already exist
61         // the object is set only if there is no selected attribute
62         // test case is - preselection for distance operation, which contains two points selected on lines
63         if (aPntAttr)
64           aRefAttr->setAttr(aPntAttr);
65         else if (aSelectedObject)
66           aRefAttr->setObject(aSelectedObject);
67         return true;
68       }
69     }
70   }
71   return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
72 }
73
74 //********************************************************************
75 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
76 {
77   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
78   removeExternal();
79 }
80
81 //********************************************************************
82 void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
83                                                  GeomShapePtr theShape)
84 {
85   ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
86                                                               theSelectedObject, mySketch);
87   if (aObj != myExternalObject) {
88     removeExternal();
89     myExternalObject = aObj;
90   }
91 }
92
93 //********************************************************************
94 void PartSet_WidgetShapeSelector::removeExternal()
95 {
96   if (myExternalObject.get()) {
97     DocumentPtr aDoc = myExternalObject->document();
98     FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
99     if (aFeature.get() != NULL) {
100       aDoc->removeFeature(aFeature);
101     }
102     myExternalObject = NULL;
103   }
104 }