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 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
18 ObjectPtr aSelectedObject = theSelectedObject;
19 GeomShapePtr aShape = theShape;
21 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
22 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
24 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
25 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
26 if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull()) {
27 // Processing of external (non-sketch) object
28 createExternal(theSelectedObject, theShape);
30 aSelectedObject = myExternalObject;
34 // Processing of sketch object
35 DataPtr aData = myFeature->data();
37 AttributePtr aAttr = aData->attribute(attributeID());
38 AttributeRefAttrPtr aRefAttr =
39 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
41 // it is possible that the point feature is selected. It should be used itself
42 // instead of searching an attribute for the shape
43 bool aShapeIsResult = false;
44 /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
45 if (aResult.get() != NULL) {
46 GeomShapePtr aShapePtr = aResult->shape();
47 // it is important to call isEqual of the shape of result.
48 // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is
49 // GeomAPI_Shape. It is important to use the realization of the isEqual method from
50 // GeomAPI_Vertex class
51 aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
54 AttributePtr aPntAttr;
55 if (!aShapeIsResult) {
56 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
57 aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
59 // this is an alternative, whether the attribute should be set or object in the attribute
60 // the first check is the attribute because the object already exist
61 // the object is set only if there is no selected attribute
62 // test case is - preselection for distance operation, which contains two points selected on lines
64 aRefAttr->setAttr(aPntAttr);
65 else if (aSelectedObject)
66 aRefAttr->setObject(aSelectedObject);
71 return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
74 //********************************************************************
75 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
77 ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
78 /// this is a temporary code, will be removed when master is merged to this branch
79 /// after merge, the external edge should be removed always, without flag checking
84 //********************************************************************
85 void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
86 GeomShapePtr theShape)
88 ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
89 theSelectedObject, mySketch);
90 if (aObj != myExternalObject) {
92 myExternalObject = aObj;
96 //********************************************************************
97 void PartSet_WidgetShapeSelector::removeExternal()
99 if (myExternalObject.get()) {
100 DocumentPtr aDoc = myExternalObject->document();
101 FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
102 if (aFeature.get() != NULL) {
103 aDoc->removeFeature(aFeature);
105 myExternalObject = NULL;