Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / PartSet / PartSet_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetMultiSelector.cpp
4 // Created:     15 Apr 2015
5 // Author:      Natalia Ermolaeva
6
7 #include "PartSet_WidgetMultiSelector.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 <ModuleBase_Tools.h>
15 #include <ModuleBase_IWorkshop.h>
16 #include <ModuleBase_ISelection.h>
17
18 #include <Config_WidgetAPI.h>
19
20 #include <PartSet_Tools.h>
21 #include <PartSet_ExternalObjectsMgr.h>
22 #include <SketchPlugin_Feature.h>
23
24 #include <SketchPlugin_ConstraintRigid.h>
25
26 #include <XGUI_Workshop.h>
27
28 #include <QComboBox>
29
30 PartSet_WidgetMultiSelector::PartSet_WidgetMultiSelector(QWidget* theParent,
31                                                          ModuleBase_IWorkshop* theWorkshop,
32                                                          const Config_WidgetAPI* theData,
33                                                          const std::string& theParentId)
34 : ModuleBase_WidgetMultiSelector(theParent, theWorkshop, theData, theParentId)
35 {
36   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), false);
37 }
38
39 PartSet_WidgetMultiSelector::~PartSet_WidgetMultiSelector()
40 {
41   delete myExternalObjectMgr;
42 }
43
44 //********************************************************************
45 void PartSet_WidgetMultiSelector::restoreAttributeValue(const bool theValid)
46 {
47   ModuleBase_WidgetMultiSelector::restoreAttributeValue(theValid);
48   myExternalObjectMgr->removeExternal(sketch(), myFeature);
49 }
50
51 //********************************************************************
52 bool PartSet_WidgetMultiSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
53 {
54   TopoDS_Shape aShape = thePrs.shape();
55   if ((myTypeCombo->count() > 1) && (!aShape.IsNull())) {
56     TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->currentText());
57     if (aShape.ShapeType() != aType)
58       return false;
59   }
60   ResultPtr aResult;
61   if (!thePrs.owner().IsNull()) {
62     ObjectPtr anObject = myWorkshop->selection()->getSelectableObject(thePrs.owner());
63     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
64   }
65   else {
66     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
67   }
68
69
70   if (myFeature) {
71     // We can not select a result of our feature
72     const std::list<ResultPtr>& aResList = myFeature->results();
73     std::list<ResultPtr>::const_iterator aIt;
74     bool isSkipSelf = false;
75     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
76       if ((*aIt) == aResult) {
77         isSkipSelf = true;
78         break;
79       }
80     }
81     if(isSkipSelf)
82       return false;
83   }
84
85   GeomShapePtr aGShape = GeomShapePtr();
86   const TopoDS_Shape& aTDSShape = thePrs.shape();
87   // if only result is selected, an empty shape is set to the model
88   if (aTDSShape.IsNull()) {
89     //aSelectionListAttr->append(aResult, GeomShapePtr());
90   }
91   else {
92     aGShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
93     //GeomShapePtr aShape(new GeomAPI_Shape());
94     aGShape->setImpl(new TopoDS_Shape(aTDSShape));
95     // We can not select a result of our feature
96     if (aGShape->isEqual(aResult->shape())) {
97       //aSelectionListAttr->append(aResult, GeomShapePtr());
98       aGShape = GeomShapePtr();
99     }
100     else {
101       //aSelectionListAttr->append(aResult, aShape);
102     }
103   }
104
105   setObject(aResult, aGShape);
106   return true;
107 }
108
109 //********************************************************************
110 bool PartSet_WidgetMultiSelector::setObject(const ObjectPtr& theSelectedObject,
111                                             const GeomShapePtr& theShape)
112 {
113   ObjectPtr aSelectedObject = theSelectedObject;
114   GeomShapePtr aShape = theShape;
115
116   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
117   //if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
118   //  return false;
119
120   // Do check using of external feature
121   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
122           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
123
124   // Do check that we can use external feature
125   if ((aSPFeature.get() != NULL) && aSPFeature->isExternal() && (!myExternalObjectMgr->useExternal()))
126     return false;
127
128   if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull() && myExternalObjectMgr->useExternal()) {
129     aSelectedObject = myExternalObjectMgr->externalObject(theSelectedObject, theShape, sketch());
130   }
131
132
133   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aSelectedObject);
134
135   DataPtr aData = myFeature->data();
136   AttributeSelectionListPtr aSelectionListAttr = 
137     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
138
139   aSelectionListAttr->append(aResult, aShape);
140
141   return true;
142 }