Salome HOME
The external edge should be removed only if it was created by the widget. So, the...
[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 <ModuleBase_Definitions.h>
14
15 #include <PartSet_Tools.h>
16 #include <SketchPlugin_Feature.h>
17
18 #include <SketchPlugin_ConstraintRigid.h>
19
20 #include <XGUI_Workshop.h>
21
22 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
23                                                          ModuleBase_IWorkshop* theWorkshop,
24                                                          const Config_WidgetAPI* theData,
25                                                          const std::string& theParentId)
26 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId)
27 {
28 }
29
30 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
31 {
32   ObjectPtr aSelectedObject = theSelectedObject;
33   GeomShapePtr aShape = theShape;
34
35   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
36   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
37     return false;
38   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
39           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
40   if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull()) {
41     aSelectedObject = PartSet_Tools::findFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
42                                                   theSelectedObject, mySketch);
43     if (!aSelectedObject.get()) {
44       // Processing of external (non-sketch) object
45       aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
46                                                   theSelectedObject, mySketch);
47       if (aSelectedObject.get())
48         myExternalObject = aSelectedObject;
49     }
50   } else {
51     // Processing of sketch object
52     DataPtr aData = myFeature->data();
53     if (aShape) {
54       AttributePtr aAttr = aData->attribute(attributeID());
55       AttributeRefAttrPtr aRefAttr = 
56         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
57       if (aRefAttr) {
58         // it is possible that the point feature is selected. It should be used itself
59         // instead of searching an attribute for the shape
60         bool aShapeIsResult = false;
61         /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
62         if (aResult.get() != NULL) {
63           GeomShapePtr aShapePtr = aResult->shape();
64           // it is important to call isEqual of the shape of result.
65           // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
66           // GeomAPI_Shape. It is important to use the realization of the isEqual method from
67           // GeomAPI_Vertex class
68           aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
69         }*/
70
71         AttributePtr aPntAttr;
72         if (!aShapeIsResult) {
73           TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
74           aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
75         }
76         // this is an alternative, whether the attribute should be set or object in the attribute
77         // the first check is the attribute because the object already exist
78         // the object is set only if there is no selected attribute
79         // test case is - preselection for distance operation, which contains two points selected on lines
80         if (aPntAttr)
81           aRefAttr->setAttr(aPntAttr);
82         else if (aSelectedObject)
83           aRefAttr->setObject(aSelectedObject);
84         return true;
85       }
86     }
87   }
88   return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
89 }
90
91 //********************************************************************
92 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
93 {
94   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
95   removeExternal();
96 }
97
98 //********************************************************************
99 void PartSet_WidgetShapeSelector::removeExternal()
100 {
101   if (myExternalObject.get()) {
102     DocumentPtr aDoc = myExternalObject->document();
103     FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
104     if (aFeature.get() != NULL) {
105       QObjectPtrList anObjects;
106       anObjects.append(aFeature);
107       // the external feature should be removed with all references, sketch feature should be ignored
108       std::set<FeaturePtr> anIgnoredFeatures;
109       anIgnoredFeatures.insert(sketch());
110       XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
111     }
112     myExternalObject = ObjectPtr();
113   }
114 }