Salome HOME
76561e4e6e79b1dc42cda0ec95525736c9ba9e83
[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 #include <ModelAPI_RefAttrValidator.h>
13 #include <ModelAPI_ResultValidator.h>
14
15 #include <PartSet_Tools.h>
16 #include <SketchPlugin_Feature.h>
17
18
19 bool PartSet_WidgetShapeSelector::storeValueCustom() const
20 {
21   if (!mySelectedObject)
22     return false;
23
24   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
25   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
26     return false;
27   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
28           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
29   if ((!aSPFeature) && (!myShape->isNull())) {
30     // Processing of external (non-sketch) object
31     ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(myShape->impl<TopoDS_Shape>(),
32                                                                 mySelectedObject, mySketch);
33     if (aObj) {
34       PartSet_WidgetShapeSelector* that = (PartSet_WidgetShapeSelector*) this;
35       that->mySelectedObject = aObj;
36     } else 
37       return false;
38   } else {
39     // Processing of sketch object
40     DataPtr aData = myFeature->data();
41     if (myShape) {
42       AttributePtr aAttr = aData->attribute(attributeID());
43       AttributeRefAttrPtr aRefAttr = 
44         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
45       if (aRefAttr) {
46         TopoDS_Shape aShape = myShape->impl<TopoDS_Shape>();
47         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(mySelectedObject, aShape, mySketch);
48
49         // this is an alternative, whether the attribute should be set or object in the attribute
50         // the first check is the attribute because the object already exist
51         // the object is set only if there is no selected attribute
52         // test case is - preselection for distance operation, which contains two points selected on lines
53         if (aPntAttr)
54           aRefAttr->setAttr(aPntAttr);
55         else if (mySelectedObject)
56           aRefAttr->setObject(mySelectedObject);
57         updateObject(myFeature);
58         return true;
59       }
60     }
61   }
62   return ModuleBase_WidgetShapeSelector::storeValueCustom();
63 }
64
65 //********************************************************************
66 bool PartSet_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
67 {
68   // the method is redefined to analize the selected shape in validators
69   SessionPtr aMgr = ModelAPI_Session::get();
70   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
71   std::list<ModelAPI_Validator*> aValidators;
72   std::list<std::list<std::string> > anArguments;
73   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
74
75   // Check the type of selected object
76   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
77   bool isValid = true;
78   for (; aValidator != aValidators.end(); aValidator++) {
79     const ModelAPI_ResultValidator* aResValidator =
80         dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
81     if (aResValidator) {
82       isValid = false;
83       if (aResValidator->isValid(theObj)) {
84         isValid = true;
85         break;
86       }
87     }
88   }
89   if (!isValid)
90     return false;
91
92   // Check the acceptability of the object and shape as validator attribute
93   AttributePtr aPntAttr;
94   DataPtr aData = myFeature->data();
95   if (theShape.get() != NULL) {
96     AttributePtr aAttr = aData->attribute(attributeID());
97     AttributeRefAttrPtr aRefAttr = 
98       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
99     if (aRefAttr) {
100       TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
101       aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theObj, aShape, mySketch);
102     }
103   }
104   // Check the acceptability of the object as attribute
105   aValidator = aValidators.begin();
106   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
107   for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
108     const ModelAPI_RefAttrValidator* aAttrValidator =
109         dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
110     if (aAttrValidator) {
111       if (aPntAttr.get() != NULL)
112       {
113         if (!aAttrValidator->isValid(myFeature, *aArgs, aPntAttr)) {
114           return false;
115         }
116       }
117       else
118       {
119         if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
120           return false;
121         }
122       }
123     }
124   }
125   return true;
126 }
127
128 //*********************************************
129 bool PartSet_WidgetConstraintShapeSelector::storeValueCustom() const
130 {
131   FeaturePtr aFeature = ModelAPI_Feature::feature(mySelectedObject);
132   if (aFeature) {
133     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
134             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
135     if ((!aSPFeature) && (!myShape->isNull())) {
136       ObjectPtr aObj = PartSet_Tools::createFixedObjectByExternal(myShape->impl<TopoDS_Shape>(),
137                                                                   mySelectedObject, mySketch);
138       if (aObj) {
139         PartSet_WidgetConstraintShapeSelector* that = (PartSet_WidgetConstraintShapeSelector*) this;
140         that->mySelectedObject = aObj;
141       } else 
142         return false;
143     }
144   }
145   return ModuleBase_WidgetShapeSelector::storeValueCustom();
146 }