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