Salome HOME
cea864a6af9dcfe1aa1ac198b7ba02b723f7639c
[modules/shaper.git] / src / PartSet / PartSet_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetShapeSelector.cpp
4 // Created:     27 Nov 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "PartSet_WidgetShapeSelector.h"
8
9 #include <ModelAPI_AttributeRefAttr.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12
13 #include <ModuleBase_Definitions.h>
14 #include <Config_WidgetAPI.h>
15
16 #include <PartSet_Tools.h>
17 #include <PartSet_ExternalObjectsMgr.h>
18 #include <SketchPlugin_Feature.h>
19
20 #include <SketchPlugin_ConstraintRigid.h>
21
22 #include <XGUI_Workshop.h>
23
24 PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
25                                                          ModuleBase_IWorkshop* theWorkshop,
26                                                          const Config_WidgetAPI* theData,
27                                                          const std::string& theParentId)
28 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId)
29 {
30   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), true);
31 }
32
33 PartSet_WidgetShapeSelector::~PartSet_WidgetShapeSelector()
34 {
35   delete myExternalObjectMgr;
36 }
37
38 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
39 {
40   ObjectPtr aSelectedObject = theSelectedObject;
41   //GeomShapePtr aShape = theShape;
42
43   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
44   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
45     return false;
46   // Do check using of external feature
47   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
48           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
49
50   // Do check that we can use external feature
51   if ((aSPFeature.get() != NULL) && aSPFeature->isExternal() && (!myExternalObjectMgr->useExternal()))
52     return false;
53
54   if (aSPFeature.get() == NULL && theShape.get() != NULL && !theShape->isNull() && myExternalObjectMgr->useExternal()) {
55     aSelectedObject = myExternalObjectMgr->externalObject(theSelectedObject, theShape, sketch());
56   } else {
57     // Processing of sketch object
58     if (theShape.get()) {
59       setPointAttribute(theSelectedObject, theShape);
60       return true;
61     }
62   }
63   return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, theShape);
64 }
65
66 //********************************************************************
67 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
68 {
69   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
70   myExternalObjectMgr->removeExternal(sketch(), myFeature);
71 }
72
73 //********************************************************************
74 void PartSet_WidgetShapeSelector::setPointAttribute(ObjectPtr theSelectedObject, GeomShapePtr theShape)
75 {
76   DataPtr aData = myFeature->data();
77   AttributePtr aAttr = aData->attribute(attributeID());
78   AttributeRefAttrPtr aRefAttr = 
79     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
80   if (aRefAttr) {
81     // it is possible that the point feature is selected. It should be used itself
82     // instead of searching an attribute for the shape
83     bool aShapeIsResult = false;
84     /*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
85     if (aResult.get() != NULL) {
86       GeomShapePtr aShapePtr = aResult->shape();
87       // it is important to call isEqual of the shape of result.
88       // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is 
89       // GeomAPI_Shape. It is important to use the realization of the isEqual method from
90       // GeomAPI_Vertex class
91       aShapeIsResult = aShapePtr.get() != NULL && aShapePtr->isEqual(theShape);
92     }*/
93
94     AttributePtr aPntAttr;
95     if (!aShapeIsResult) {
96       TopoDS_Shape aTDSShape = theShape->impl<TopoDS_Shape>();
97       aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theSelectedObject, aTDSShape, mySketch);
98     }
99     // this is an alternative, whether the attribute should be set or object in the attribute
100     // the first check is the attribute because the object already exist
101     // the object is set only if there is no selected attribute
102     // test case is - preselection for distance operation, which contains two points selected on lines
103     if (aPntAttr)
104       aRefAttr->setAttr(aPntAttr);
105     else
106       aRefAttr->setObject(theSelectedObject);
107   }
108 }