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 <SketchPlugin_ConstraintRigid.h>
20 #include <XGUI_Workshop.h>
22 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
23 ModuleBase_IWorkshop* theWorkshop,
24 const Config_WidgetAPI* theData,
25 const std::string& theParentId)
26 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId)
30 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
32 ObjectPtr aSelectedObject = theSelectedObject;
33 GeomShapePtr aShape = theShape;
35 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
36 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
38 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
39 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
40 if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull()) {
41 // Processing of external (non-sketch) object
42 createExternal(theSelectedObject, theShape);
44 aSelectedObject = myExternalObject;
48 // Processing of sketch object
49 DataPtr aData = myFeature->data();
51 AttributePtr aAttr = aData->attribute(attributeID());
52 AttributeRefAttrPtr aRefAttr =
53 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
55 // it is possible that the point feature is selected. It should be used itself
56 // instead of searching an attribute for the shape
57 bool aShapeIsResult = false;
58 /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
59 if (aResult.get() != NULL) {
60 GeomShapePtr aShapePtr = aResult->shape();
61 // it is important to call isEqual of the shape of result.
62 // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is
63 // GeomAPI_Shape. It is important to use the realization of the isEqual method from
64 // GeomAPI_Vertex class
65 aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
68 AttributePtr aPntAttr;
69 if (!aShapeIsResult) {
70 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
71 aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
73 // this is an alternative, whether the attribute should be set or object in the attribute
74 // the first check is the attribute because the object already exist
75 // the object is set only if there is no selected attribute
76 // test case is - preselection for distance operation, which contains two points selected on lines
78 aRefAttr->setAttr(aPntAttr);
79 else if (aSelectedObject)
80 aRefAttr->setObject(aSelectedObject);
85 return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
88 //********************************************************************
89 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
91 ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
95 //********************************************************************
96 void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
97 GeomShapePtr theShape)
99 ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
100 theSelectedObject, mySketch);
101 if (aObj != myExternalObject) {
103 myExternalObject = aObj;
107 //********************************************************************
108 void PartSet_WidgetShapeSelector::removeExternal()
110 if (myExternalObject.get()) {
111 DocumentPtr aDoc = myExternalObject->document();
112 FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
113 if (aFeature.get() != NULL) {
114 // 1. check whether the external object can be deleted
115 // It should not be deleted if there are references to the object from other features,
116 // which are not the sketch or a rigid constraints.
117 std::set<FeaturePtr> aRefFeatures;
118 aFeature->document()->refsToFeature(aFeature, aRefFeatures, false);
119 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
120 aLast = aRefFeatures.end();
121 bool aReferenceExist = false;
122 CompositeFeaturePtr aSketch = sketch();
123 for (; anIt != aLast && !aReferenceExist; anIt++) {
124 FeaturePtr aFeature = (*anIt);
125 aReferenceExist = aFeature != aSketch &&
126 aFeature->getKind() != SketchPlugin_ConstraintRigid::ID();
131 // 2. delete external object
132 QObjectPtrList anObjects;
133 anObjects.append(aFeature);
134 // the external feature should be removed with all references, sketch feature should be ignored
135 std::set<FeaturePtr> anIgnoredFeatures;
136 anIgnoredFeatures.insert(sketch());
137 XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
139 myExternalObject = ObjectPtr();