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