Salome HOME
Issue #427, 420 - crash during work through external edges.
[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     // Processing of external (non-sketch) object
42     createExternal(theSelectedObject, theShape);
43     if (myExternalObject)
44       aSelectedObject = myExternalObject;
45     else
46       return false;
47   } else {
48     // Processing of sketch object
49     DataPtr aData = myFeature->data();
50     if (aShape) {
51       AttributePtr aAttr = aData->attribute(attributeID());
52       AttributeRefAttrPtr aRefAttr = 
53         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
54       if (aRefAttr) {
55         // it is possible that the point feature is selected. It should be used itself
56         // instead of searching an attribute for the shape
57         bool aShapeIsResult = false;
58         /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
59         if (aResult.get() != NULL) {
60           GeomShapePtr aShapePtr = aResult->shape();
61           // it is important to call isEqual of the shape of result.
62           // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
63           // GeomAPI_Shape. It is important to use the realization of the isEqual method from
64           // GeomAPI_Vertex class
65           aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
66         }*/
67
68         AttributePtr aPntAttr;
69         if (!aShapeIsResult) {
70           TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
71           aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
72         }
73         // this is an alternative, whether the attribute should be set or object in the attribute
74         // the first check is the attribute because the object already exist
75         // the object is set only if there is no selected attribute
76         // test case is - preselection for distance operation, which contains two points selected on lines
77         if (aPntAttr)
78           aRefAttr->setAttr(aPntAttr);
79         else if (aSelectedObject)
80           aRefAttr->setObject(aSelectedObject);
81         return true;
82       }
83     }
84   }
85   return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
86 }
87
88 //********************************************************************
89 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
90 {
91   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
92   removeExternal();
93 }
94
95 //********************************************************************
96 void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
97                                                  GeomShapePtr theShape)
98 {
99   ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
100                                                               theSelectedObject, mySketch);
101   if (aObj != myExternalObject) {
102     removeExternal();
103     myExternalObject = aObj;
104   }
105 }
106
107 //********************************************************************
108 void PartSet_WidgetShapeSelector::removeExternal()
109 {
110   if (myExternalObject.get()) {
111     DocumentPtr aDoc = myExternalObject->document();
112     FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
113     if (aFeature.get() != NULL) {
114       // 1. check whether the external object can be deleted
115       // It should not be deleted if there are references to the object from other features,
116       // which are not the sketch or a rigid constraints.
117       std::set<FeaturePtr> aRefFeatures;
118       aFeature->document()->refsToFeature(aFeature, aRefFeatures, false);
119       std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
120                                        aLast = aRefFeatures.end();
121       bool aReferenceExist = false;
122       CompositeFeaturePtr aSketch = sketch();
123       for (; anIt != aLast && !aReferenceExist; anIt++) {
124         FeaturePtr aFeature = (*anIt);
125         aReferenceExist = aFeature != aSketch &&
126                           aFeature->getKind() != SketchPlugin_ConstraintRigid::ID();
127       }
128       if (aReferenceExist)
129         return;
130
131       // 2. delete external object
132       QObjectPtrList anObjects;
133       anObjects.append(aFeature);
134       // the external feature should be removed with all references, sketch feature should be ignored
135       std::set<FeaturePtr> anIgnoredFeatures;
136       anIgnoredFeatures.insert(sketch());
137       XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
138     }
139     myExternalObject = NULL;
140   }
141 }