Salome HOME
Issue #2027 Sketcher Trim Feature
[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 #include <GeomAPI_Edge.h>
19 #include <GeomAPI_Pnt2d.h>
20
21 #include <GeomAlgoAPI_ShapeTools.h>
22 #include <ModelGeomAlgo_Point2D.h>
23
24 #include <ModelGeomAlgo_Point2D.h>
25
26 #include <SketchPlugin_ConstraintCoincidence.h>
27 #include <SketchPlugin_Constraint.h>
28 #include <SketchPlugin_Point.h>
29
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_IWorkshop.h>
32 #include <ModuleBase_IModule.h>
33
34 #include <Config_WidgetAPI.h>
35
36 #include <XGUI_Tools.h>
37 #include <XGUI_Workshop.h>
38 #include <XGUI_Displayer.h>
39
40 #include <QWidget>
41 #include <QMouseEvent>
42
43 PartSet_WidgetSubShapeSelector::PartSet_WidgetSubShapeSelector(QWidget* theParent,
44                                                          ModuleBase_IWorkshop* theWorkshop,
45                                                          const Config_WidgetAPI* theData)
46 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
47 {
48   myCurrentSubShape = std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs());
49 }
50
51 PartSet_WidgetSubShapeSelector::~PartSet_WidgetSubShapeSelector()
52 {
53   myCashedShapes.clear();
54 }
55
56 //********************************************************************
57 void PartSet_WidgetSubShapeSelector::activateCustom()
58 {
59   ModuleBase_WidgetShapeSelector::activateCustom();
60
61   myWorkshop->module()->activateCustomPrs(myFeature,
62                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
63 }
64
65 //********************************************************************
66 void PartSet_WidgetSubShapeSelector::deactivate()
67 {
68   ModuleBase_WidgetShapeSelector::deactivate();
69
70   myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
71 }
72
73 //********************************************************************
74 void PartSet_WidgetSubShapeSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
75                                                 QMouseEvent* theEvent)
76 {
77   ModuleBase_ISelection* aSelect = myWorkshop->selection();
78   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
79
80   if (!aHighlighted.empty()) {
81     ModuleBase_ViewerPrsPtr aPrs = aHighlighted.first();
82     if (aPrs.get() && aPrs->object().get()) {
83       ObjectPtr anObject = aPrs->object();
84       if (myCashedShapes.find(anObject) == myCashedShapes.end())
85         fillObjectShapes(anObject);
86       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
87       if (!aShapes.empty()) {
88         gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
89         double aX, anY;
90         Handle(V3d_View) aView = theWindow->v3dView();
91         PartSet_Tools::convertTo2D(aPnt, mySketch, aView, aX, anY);
92         std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::convertTo3D(aX, anY, mySketch);
93
94         std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
95         for (; anIt != aLast; anIt++) {
96           GeomShapePtr aBaseShape = *anIt;
97           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
98           if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, aPoint, aProjectedPoint)) {
99             XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
100             if (myCurrentSubShape->object() != anObject ||
101                 myCurrentSubShape->shape() != aBaseShape) {
102               myCurrentSubShape->setObject(anObject);
103               myCurrentSubShape->setShape(aBaseShape);
104
105               ModuleBase_IModule* aModule = myWorkshop->module();
106
107
108               if (!aModule->isCustomPrsActivated(ModuleBase_IModule::CustomizeHighlightedObjects))
109                 aModule->activateCustomPrs(myFeature,
110                                            ModuleBase_IModule::CustomizeHighlightedObjects, true);
111               aModule->customizeObject(myFeature,
112                                        ModuleBase_IModule::CustomizeHighlightedObjects, true);
113             }
114             else
115               XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();;
116             break;
117           }
118         }
119       }
120     }
121   }
122 }
123
124 //********************************************************************
125 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
126                                                       ObjectPtr& theObject,
127                                                       GeomShapePtr& theShape)
128 {
129   ModuleBase_ISelection* aSelection = myWorkshop->selection();
130   theObject = aSelection->getResult(thePrs);
131   if (!theObject.get() && myCurrentSubShape->object())
132     theObject = myCurrentSubShape->object();
133 }
134
135 //********************************************************************
136 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetSubShapeSelector::getAttributeSelection() const
137 {
138   return QList<ModuleBase_ViewerPrsPtr>();
139 }
140
141
142 //********************************************************************
143 bool PartSet_WidgetSubShapeSelector::setSelection(
144                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
145                                           const bool theToValidate)
146 {
147   ObjectPtr aBaseObject = myCurrentSubShape->object();
148   GeomShapePtr aBaseShape = myCurrentSubShape->shape();
149   bool aResult = aBaseObject.get() && aBaseShape.get();
150   // firstly set the selection to the attribute
151   if (aResult) {
152     QList<ModuleBase_ViewerPrsPtr> aValues;
153     aValues.append(myCurrentSubShape);
154     aResult = ModuleBase_WidgetShapeSelector::setSelection(aValues, theToValidate);
155   }
156   // secondly fill additional attributes
157   if (aResult) {
158     aResult = false;
159     if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
160       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
161
162       std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
163       std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
164
165       std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
166       /// find the points in feature attributes
167       FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
168       std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
169                                                         GeomDataAPI_Point2D::typeId());
170       std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(),
171                                               aLast = a2DPointAttributes.end();
172       for (; anIt != aLast; anIt++) {
173         std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
174                                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
175         std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = anAttributePoint->pnt();
176         std::shared_ptr<GeomAPI_Pnt> aPoint3D = PartSet_Tools::convertTo3D(aPoint2D->x(),
177                                                                    aPoint2D->y(), mySketch);
178         if (aFirstPnt->isEqual(aPoint3D))
179           aFirstPointAttr = anAttributePoint;
180         else if (aLastPnt->isEqual(aPoint3D))
181           aLastPointAttr = anAttributePoint;
182       }
183
184       /// find the points in coincident features
185       PntToAttributesMap aRefAttributes = myCashedReferences[aBaseObject];
186       PntToAttributesMap::const_iterator
187         aRIt = aRefAttributes.begin(), aRLast = aRefAttributes.end();
188       for (; aRIt != aRLast; aRIt++) {
189         std::shared_ptr<GeomDataAPI_Point2D> anAttribute = aRIt->first;
190         std::shared_ptr<GeomAPI_Pnt> aPoint = aRIt->second;
191         if (!aFirstPointAttr.get() && aFirstPnt->isEqual(aPoint))
192           aFirstPointAttr = anAttribute;
193         if (!aLastPointAttr.get() && aLastPnt->isEqual(aPoint))
194           aLastPointAttr = anAttribute;
195         if (aFirstPointAttr.get() && aLastPointAttr.get())
196           break;
197       }
198       if (!aFirstPointAttr.get() || !aLastPointAttr)
199         return false;
200
201       FeaturePtr aFeature = feature();
202       AttributeRefAttrPtr anAPointAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
203                                           aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
204       AttributeRefAttrPtr aBPointAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
205                                           aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
206       anAPointAttr->setAttr(aFirstPointAttr);
207       aBPointAttr->setAttr(aLastPointAttr);
208       aResult = true;
209     }
210   }
211
212   return aResult;
213 }
214
215 //********************************************************************
216 void PartSet_WidgetSubShapeSelector::getHighlighted(
217                            QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues)
218 {
219   if (myCurrentSubShape.get() && myCurrentSubShape->object().get())
220     theValues.append(myCurrentSubShape);
221 }
222
223 //********************************************************************
224 void PartSet_WidgetSubShapeSelector::fillObjectShapes(const ObjectPtr& theObject)
225 {
226   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
227   std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
228   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
229   // current feature
230   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
231   // edges on feature
232   std::set<ResultPtr> anEdgeResults;
233   ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeResults);
234   if (!anEdgeResults.empty()) {
235     GeomShapePtr aFeatureShape = (*anEdgeResults.begin())->shape();
236
237     // coincidences to the feature
238     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
239                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
240     // layed on feature coincidences to divide it on several shapes
241     CompositeFeaturePtr aSketch = sketch();
242     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
243     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
244         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
245     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
246         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
247     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
248         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
249     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
250     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
251     ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
252                                                 aX->dir(), aY, aPoints, aPointToAttributes);
253
254     GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
255   }
256   myCashedShapes[theObject] = aShapes;
257   myCashedReferences[theObject] = aPointToAttributes;
258 }
259