Salome HOME
Issue #591 - Highlight of the first argument of constraints
[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 #include <XGUI_ModuleConnector.h>
25 #include <XGUI_Displayer.h>
26 #include <XGUI_SelectionMgr.h>
27 #include <XGUI_Selection.h>
28 #include <SelectMgr_IndexedMapOfOwner.hxx>
29 #include <StdSelect_BRepOwner.hxx>
30
31 //#define DEBUG_EMPTY_SHAPE
32
33 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
34                                                          ModuleBase_IWorkshop* theWorkshop,
35                                                          const Config_WidgetAPI* theData,
36                                                          const std::string& theParentId)
37 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId)
38 {
39   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), true);
40 }
41
42 PartSet_WidgetShapeSelector::~PartSet_WidgetShapeSelector()
43 {
44   delete myExternalObjectMgr;
45 }
46
47 //********************************************************************
48 bool PartSet_WidgetShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
49 {
50   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
51   if (aValid) {
52     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
53     aValid = myExternalObjectMgr->isValidObject(anObject);
54   }
55   return aValid;
56 }
57
58 //********************************************************************
59 void PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
60 {
61   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
62   // Do check using of external feature
63   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
64           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
65   // Processing of sketch object
66 #ifdef DEBUG_EMPTY_SHAPE
67   if (aSPFeature.get() != NULL && theShape.get()) {
68     setPointAttribute(theSelectedObject, theShape);
69 #else
70   if (aSPFeature.get() != NULL) {
71     GeomShapePtr aShape = theShape;
72     if (!aShape.get()) {
73       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
74       if (aResult.get()) {
75         aShape = aResult->shape();
76       }
77     }
78     setPointAttribute(theSelectedObject, aShape);
79 #endif
80   }
81   else
82     ModuleBase_WidgetShapeSelector::setObject(theSelectedObject, theShape);
83 }
84
85 void PartSet_WidgetShapeSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
86                                                    ObjectPtr& theObject,
87                                                    GeomShapePtr& theShape)
88 {
89   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
90
91   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
92   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
93           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
94   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
95   // TODO: unite with the same functionality in PartSet_WidgetShapeSelector
96   if (aSPFeature.get() == NULL && myExternalObjectMgr->useExternal()) {
97     GeomShapePtr aShape = theShape;
98     if (!aShape.get()) {
99       ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
100       if (aResult.get())
101         aShape = aResult->shape();
102     }
103     if (aShape.get() != NULL && !aShape->isNull())
104       theObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch());
105   }
106 }
107
108 //********************************************************************
109 GeomShapePtr PartSet_WidgetShapeSelector::getShape() const
110 {
111   // an empty shape by default
112   DataPtr aData = myFeature->data();
113   AttributePtr anAttribute = aData->attribute(attributeID());
114   GeomShapePtr aShape = PartSet_Tools::findShapeBy2DPoint(anAttribute, myWorkshop);
115
116   if (!aShape.get())
117     aShape = ModuleBase_WidgetShapeSelector::getShape();
118   return aShape;
119 }
120
121 //********************************************************************
122 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
123 {
124   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
125   myExternalObjectMgr->removeExternal(sketch(), myFeature);
126 }
127
128 //********************************************************************
129 void PartSet_WidgetShapeSelector::setPointAttribute(ObjectPtr theSelectedObject, GeomShapePtr theShape)
130 {
131   DataPtr aData = myFeature->data();
132   AttributePtr aAttr = aData->attribute(attributeID());
133   AttributeRefAttrPtr aRefAttr = 
134     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
135   if (aRefAttr) {
136     // it is possible that the point feature is selected. It should be used itself
137     // instead of searching an attribute for the shape
138     bool aShapeIsResult = false;
139     /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
140     if (aResult.get() != NULL) {
141       GeomShapePtr aShapePtr = aResult->shape();
142       // it is important to call isEqual of the shape of result.
143       // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
144       // GeomAPI_Shape. It is important to use the realization of the isEqual method from
145       // GeomAPI_Vertex class
146       aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
147     }*/
148
149     AttributePtr aPntAttr;
150     if (!aShapeIsResult) {
151       TopoDS_Shape aTDSShape = theShape->impl<TopoDS_Shape>();
152       aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theSelectedObject, aTDSShape, mySketch);
153     }
154     // this is an alternative, whether the attribute should be set or object in the attribute
155     // the first check is the attribute because the object already exist
156     // the object is set only if there is no selected attribute
157     // test case is - preselection for distance operation, which contains two points selected on lines
158     if (aPntAttr)
159       aRefAttr->setAttr(aPntAttr);
160     else
161       aRefAttr->setObject(theSelectedObject);
162   }
163 }