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