Salome HOME
Issue #1664: In the Sketcher, add the function Split a segment. Validator for selecti...
[modules/shaper.git] / src / PartSet / PartSet_WidgetSubShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetSubShapeSelector.cpp
4 // Created:     21 Jul 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_WidgetSubShapeSelector.h"
8 #include "PartSet_Tools.h"
9
10 #include <ModuleBase_ISelection.h>
11 #include <ModuleBase_ViewerPrs.h>
12
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_Tools.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <GeomDataAPI_Point.h>
18
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <ModelGeomAlgo_Point2D.h>
21
22 #include <ModelGeomAlgo_Point2D.h>
23
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_Point.h>
26
27 #include <ModuleBase_IViewWindow.h>
28 #include <ModuleBase_IWorkshop.h>
29 #include <ModuleBase_IModule.h>
30
31 #include <Config_WidgetAPI.h>
32
33 #include <XGUI_Tools.h>
34 #include <XGUI_Workshop.h>
35 #include <XGUI_Displayer.h>
36
37 #include <QWidget>
38 #include <QMouseEvent>
39
40 PartSet_WidgetSubShapeSelector::PartSet_WidgetSubShapeSelector(QWidget* theParent,
41                                                          ModuleBase_IWorkshop* theWorkshop,
42                                                          const Config_WidgetAPI* theData)
43 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
44 {
45   myCurrentSubShape = std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs());
46 }
47
48 PartSet_WidgetSubShapeSelector::~PartSet_WidgetSubShapeSelector()
49 {
50   myCashedShapes.clear();
51 }
52
53 //********************************************************************
54 void PartSet_WidgetSubShapeSelector::activateCustom()
55 {
56   ModuleBase_WidgetShapeSelector::activateCustom();
57
58   myWorkshop->module()->activateCustomPrs(myFeature,
59                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
60 }
61
62 //********************************************************************
63 void PartSet_WidgetSubShapeSelector::deactivate()
64 {
65   ModuleBase_WidgetShapeSelector::deactivate();
66
67   myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
68 }
69
70 //********************************************************************
71 void PartSet_WidgetSubShapeSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
72                                                 QMouseEvent* theEvent)
73 {
74   ModuleBase_ISelection* aSelect = myWorkshop->selection();
75   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
76
77   if (!aHighlighted.empty()) {
78     ModuleBase_ViewerPrsPtr aPrs = aHighlighted.first();
79     if (aPrs.get() && aPrs->object().get()) {
80       ObjectPtr anObject = aPrs->object();
81       if (myCashedShapes.find(anObject) == myCashedShapes.end())
82         fillObjectShapes(anObject);
83       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
84       if (!aShapes.empty()) {
85         gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
86         std::shared_ptr<GeomAPI_Pnt> aPoint(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
87
88         std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
89         for (; anIt != aLast; anIt++) {
90           GeomShapePtr aBaseShape = *anIt;
91           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
92           if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, aPoint, aProjectedPoint)) {
93             XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
94             if (myCurrentSubShape->object() != anObject ||
95                 myCurrentSubShape->shape() != aBaseShape) {
96               myCurrentSubShape->setObject(anObject);
97               myCurrentSubShape->setShape(aBaseShape);
98               myWorkshop->module()->customizeObject(myFeature,
99                                        ModuleBase_IModule::CustomizeHighlightedObjects, true);
100             }
101             else
102               XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();;
103             break;
104           }
105         }
106       }
107     }
108   }
109 }
110
111 //********************************************************************
112 void PartSet_WidgetSubShapeSelector::getHighlighted(
113                            QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues)
114 {
115   if (myCurrentSubShape.get() && myCurrentSubShape->object().get())
116     theValues.append(myCurrentSubShape);
117 }
118
119 //********************************************************************
120 void PartSet_WidgetSubShapeSelector::fillObjectShapes(const ObjectPtr& theObject)
121 {
122   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
123
124   // current feature
125   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
126   std::set<GeomShapePtr> anEdgeShapes;
127   // edges on feature
128   ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
129   if (!anEdgeShapes.empty()) {
130     GeomShapePtr aFeatureShape = *anEdgeShapes.begin();
131
132     // coincidences to the feature
133     std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
134     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
135                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
136     // layed on feature coincidences to divide it on several shapes
137     FeaturePtr aSketch = sketch();
138     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
139     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
140         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
141     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
142         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
143     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
144         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
145     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
146     std::set<std::shared_ptr<GeomAPI_Pnt> > aPoints;
147     ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
148                                                 aX->dir(), aY, aPoints);
149
150     GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPoints, aShapes);
151   }
152   myCashedShapes[theObject] = aShapes;
153 }
154
155 //********************************************************************
156 /*bool PartSet_WidgetSubShapeSelector::activateSelectionAndFilters(bool toActivate)
157 {
158   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
159                                                                            (toActivate);
160   if (!myUseSketchPlane) {
161     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
162     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
163     bool isUsePlaneFilterOnly = !toActivate;
164     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
165   }
166   return aHasSelectionFilter;
167 }
168
169 //********************************************************************
170 bool PartSet_WidgetSubShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
171 {
172   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
173   if (aValid) {
174     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
175     aValid = myExternalObjectMgr->isValidObject(anObject);
176   }
177   return aValid;
178 }
179
180 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
181                                                    ObjectPtr& theObject,
182                                                    GeomShapePtr& theShape)
183 {
184   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
185
186   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
187   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
188           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
189   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
190   // TODO: unite with the same functionality in PartSet_WidgetSubShapeSelector
191   if (aSPFeature.get() == NULL) {
192     ObjectPtr anExternalObject = ObjectPtr();
193     if (myExternalObjectMgr->useExternal()) {
194       GeomShapePtr aShape = theShape;
195       if (!aShape.get()) {
196         ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
197         if (aResult.get())
198           aShape = aResult->shape();
199       }
200       if (aShape.get() != NULL && !aShape->isNull())
201         anExternalObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
202     }
203     /// the object is null if the selected feature is "external"(not sketch entity feature of the
204     /// current sketch) and it is not created by object manager
205     theObject = anExternalObject;
206   }
207 }
208
209 //********************************************************************
210 void PartSet_WidgetSubShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
211                                                         const bool theValid)
212 {
213   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
214   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
215 }
216 */