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