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>
14 #include <Config_WidgetAPI.h>
16 #include <PartSet_Tools.h>
17 #include <SketchPlugin_Feature.h>
19 #include <SketchPlugin_ConstraintRigid.h>
21 #include <XGUI_Workshop.h>
23 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
24 ModuleBase_IWorkshop* theWorkshop,
25 const Config_WidgetAPI* theData,
26 const std::string& theParentId)
27 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId), myUseExternal(true)
29 QString aIsExternal(theData->getProperty("use_external").c_str());
30 if (!aIsExternal.isEmpty()) {
31 QString aStr = aIsExternal.toUpper();
32 myUseExternal = (aStr == "TRUE") || (aStr == "YES");
36 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
38 ObjectPtr aSelectedObject = theSelectedObject;
39 GeomShapePtr aShape = theShape;
41 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
42 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
44 // Do check using of external feature
45 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
46 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
48 // Do check that we can use external feature
49 if ((aSPFeature.get() != NULL) && aSPFeature->isExternal() && (!myUseExternal))
52 if (aSPFeature.get() == NULL && aShape.get() != NULL && !aShape->isNull() && myUseExternal) {
53 aSelectedObject = PartSet_Tools::findFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
54 theSelectedObject, mySketch);
55 if (!aSelectedObject.get()) {
56 // Processing of external (non-sketch) object
57 aSelectedObject = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
58 theSelectedObject, mySketch);
59 if (aSelectedObject.get())
60 myExternalObject = aSelectedObject;
63 // Processing of sketch object
64 DataPtr aData = myFeature->data();
66 AttributePtr aAttr = aData->attribute(attributeID());
67 AttributeRefAttrPtr aRefAttr =
68 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
70 // it is possible that the point feature is selected. It should be used itself
71 // instead of searching an attribute for the shape
72 bool aShapeIsResult = false;
73 /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
74 if (aResult.get() != NULL) {
75 GeomShapePtr aShapePtr = aResult->shape();
76 // it is important to call isEqual of the shape of result.
77 // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is
78 // GeomAPI_Shape. It is important to use the realization of the isEqual method from
79 // GeomAPI_Vertex class
80 aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
83 AttributePtr aPntAttr;
84 if (!aShapeIsResult) {
85 TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
86 aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
88 // this is an alternative, whether the attribute should be set or object in the attribute
89 // the first check is the attribute because the object already exist
90 // the object is set only if there is no selected attribute
91 // test case is - preselection for distance operation, which contains two points selected on lines
93 aRefAttr->setAttr(aPntAttr);
94 else if (aSelectedObject)
95 aRefAttr->setObject(aSelectedObject);
100 return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
103 //********************************************************************
104 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
106 ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
110 //********************************************************************
111 void PartSet_WidgetShapeSelector::removeExternal()
113 if (myExternalObject.get()) {
114 DocumentPtr aDoc = myExternalObject->document();
115 FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
116 if (aFeature.get() != NULL) {
117 QObjectPtrList anObjects;
118 anObjects.append(aFeature);
119 // the external feature should be removed with all references, sketch feature should be ignored
120 std::set<FeaturePtr> anIgnoredFeatures;
121 anIgnoredFeatures.insert(sketch());
122 XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
124 myExternalObject = ObjectPtr();