]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetShapeSelector.cpp
Salome HOME
External edges correction for sketch operations.
[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 <PartSet_Tools.h>
14 #include <SketchPlugin_Feature.h>
15
16 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
17 {
18   ObjectPtr aSelectedObject = theSelectedObject;
19   GeomShapePtr aShape = theShape;
20
21   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aSelectedObject);
22   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
23     return false;
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);
29     if (myExternalObject)
30       aSelectedObject = myExternalObject;
31     else
32       return false;
33   } else {
34     // Processing of sketch object
35     DataPtr aData = myFeature->data();
36     if (aShape) {
37       AttributePtr aAttr = aData->attribute(attributeID());
38       AttributeRefAttrPtr aRefAttr = 
39         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
40       if (aRefAttr) {
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);
52         }*/
53
54         AttributePtr aPntAttr;
55         if (!aShapeIsResult) {
56           TopoDS_Shape aTDSShape = aShape->impl<TopoDS_Shape>();
57           aPntAttr = PartSet_Tools::findAttributeBy2dPoint(aSelectedObject, aTDSShape, mySketch);
58         }
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
63         if (aPntAttr)
64           aRefAttr->setAttr(aPntAttr);
65         else if (aSelectedObject)
66           aRefAttr->setObject(aSelectedObject);
67         return true;
68       }
69     }
70   }
71   return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
72 }
73
74 //********************************************************************
75 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
76 {
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
80   if (!theValid)
81     removeExternal();
82 }
83
84 //********************************************************************
85 void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
86                                                  GeomShapePtr theShape)
87 {
88   ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(theShape->impl<TopoDS_Shape>(),
89                                                               theSelectedObject, mySketch);
90   if (aObj != myExternalObject) {
91     removeExternal();
92     myExternalObject = aObj;
93   }
94 }
95
96 //********************************************************************
97 void PartSet_WidgetShapeSelector::removeExternal()
98 {
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);
104     }
105     myExternalObject = NULL;
106   }
107 }