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