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