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 <ModuleBase_Definitions.h>
15 #include <PartSet_Tools.h>
16 #include <SketchPlugin_Feature.h>
18 #include <XGUI_Workshop.h>
20 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
21 ModuleBase_IWorkshop* theWorkshop,
22 const Config_WidgetAPI* theData,
23 const std::string& theParentId)
24 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId)
28 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
30 ObjectPtr aSelectedObject = theSelectedObject;
31 GeomShapePtr aShape = theShape;
33 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
34 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
36 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
37 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
38 if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull()) {
39 // Processing of external (non-sketch) object
40 createExternal(theSelectedObject, theShape);
42 aSelectedObject = myExternalObject;
46 // Processing of sketch object
47 DataPtr aData = myFeature->data();
49 AttributePtr aAttr = aData->attribute(attributeID());
50 AttributeRefAttrPtr aRefAttr =
51 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
53 // it is possible that the point feature is selected. It should be used itself
54 // instead of searching an attribute for the shape
55 bool aShapeIsResult = false;
56 /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
57 if (aResult.get() != NULL) {
58 GeomShapePtr aShapePtr = aResult->shape();
59 // it is important to call isEqual of the shape of result.
60 // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is
61 // GeomAPI_Shape. It is important to use the realization of the isEqual method from
62 // GeomAPI_Vertex class
63 aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
66 AttributePtr aPntAttr;
67 if (!aShapeIsResult) {
68 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
69 aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
71 // this is an alternative, whether the attribute should be set or object in the attribute
72 // the first check is the attribute because the object already exist
73 // the object is set only if there is no selected attribute
74 // test case is - preselection for distance operation, which contains two points selected on lines
76 aRefAttr->setAttr(aPntAttr);
77 else if (aSelectedObject)
78 aRefAttr->setObject(aSelectedObject);
83 return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
86 //********************************************************************
87 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
89 ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
93 //********************************************************************
94 void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
95 GeomShapePtr theShape)
97 ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
98 theSelectedObject, mySketch);
99 if (aObj != myExternalObject) {
101 myExternalObject = aObj;
105 //********************************************************************
106 void PartSet_WidgetShapeSelector::removeExternal()
108 if (myExternalObject.get()) {
109 DocumentPtr aDoc = myExternalObject->document();
110 FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
111 if (aFeature.get() != NULL) {
112 QObjectPtrList anObjects;
113 anObjects.append(aFeature);
114 // the external feature should be removed with all references, sketch feature should be ignored
115 std::set<FeaturePtr> anIgnoredFeatures;
116 anIgnoredFeatures.insert(sketch());
117 XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
119 myExternalObject = ObjectPtr();