1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetShapeSelector.cpp
4 // Created: 27 Nov 2014
5 // Author: Vitaly Smetannikov
7 #include "PartSet_WidgetShapeSelector.h"
9 #include <ModelAPI_AttributeRefAttr.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
13 #include <PartSet_Tools.h>
14 #include <SketchPlugin_Feature.h>
16 #include <ModuleBase_IWorkshop.h>
17 #include <XGUI_ModuleConnector.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_Displayer.h>
21 bool PartSet_WidgetShapeSelector::storeAttributeValues(ObjectPtr theSelectedObject, GeomShapePtr theShape)
23 ObjectPtr aSelectedObject = theSelectedObject;
24 GeomShapePtr aShape = theShape;
26 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
27 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
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);
35 aSelectedObject = myExternalObject;
39 // Processing of sketch object
40 DataPtr aData = myFeature->data();
42 AttributePtr aAttr = aData->attribute(attributeID());
43 AttributeRefAttrPtr aRefAttr =
44 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
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);
59 AttributePtr aPntAttr;
60 if (!aShapeIsResult) {
61 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
62 aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
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
69 aRefAttr->setAttr(aPntAttr);
70 else if (aSelectedObject)
71 aRefAttr->setObject(aSelectedObject);
76 return ModuleBase_WidgetShapeSelector::storeAttributeValues(aSelectedObject, aShape);
79 //********************************************************************
80 void PartSet_WidgetShapeSelector::storeAttributeValue()
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);
87 ModuleBase_WidgetShapeSelector::storeAttributeValue();
90 //********************************************************************
91 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
93 ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
97 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
98 XGUI_Workshop* aWorkshop = aConnector->workshop();
99 aWorkshop->displayer()->enableUpdateViewer(false);//->erase(myExternalObject);
100 aWorkshop->displayer()->enableUpdateViewer(true);*/
103 //********************************************************************
104 void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
105 GeomShapePtr theShape)
107 ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
108 theSelectedObject, mySketch);
109 if (aObj != myExternalObject) {
111 myExternalObject = aObj;
115 //********************************************************************
116 void PartSet_WidgetShapeSelector::removeExternal()
118 if (myExternalObject.get()) {
119 DocumentPtr aDoc = myExternalObject->document();
120 FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
121 if (aFeature.get() != NULL) {
122 aDoc->removeFeature(aFeature);
124 myExternalObject = NULL;