]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetShapeSelector.cpp
Salome HOME
Multi-selection widget to be used in the extrusion feature.
[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   bool isValid = ModuleBase_WidgetValidated::isValid(theObj, theShape);
69   if (!isValid)
70     return false;
71
72   // the method is redefined to analize the selected shape in validators
73   SessionPtr aMgr = ModelAPI_Session::get();
74   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
75   std::list<ModelAPI_Validator*> aValidators;
76   std::list<std::list<std::string> > anArguments;
77   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
78
79   // Check the acceptability of the object and shape as validator attribute
80   AttributePtr aPntAttr;
81   DataPtr aData = myFeature->data();
82   if (theShape.get() != NULL) {
83     AttributePtr aAttr = aData->attribute(attributeID());
84     AttributeRefAttrPtr aRefAttr = 
85       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
86     if (aRefAttr) {
87       TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
88       aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theObj, aShape, mySketch);
89     }
90   }
91   // Check the acceptability of the object as attribute
92   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
93   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
94   for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
95     const ModelAPI_RefAttrValidator* aAttrValidator =
96         dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
97     if (aAttrValidator) {
98       if (aPntAttr.get() != NULL)
99       {
100         if (!aAttrValidator->isValid(myFeature, *aArgs, aPntAttr)) {
101           return false;
102         }
103       }
104       else
105       {
106         if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
107           return false;
108         }
109       }
110     }
111   }
112   return true;
113 }
114