Salome HOME
46c20aceac60ed3d84ba0ecfaa805ece786dc4d7
[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         std::shared_ptr<GeomAPI_Pnt> aPoint(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
90
91         std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
92         for (; anIt != aLast; anIt++) {
93           GeomShapePtr aBaseShape = *anIt;
94           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
95           if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, aPoint, aProjectedPoint)) {
96             XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
97             if (myCurrentSubShape->object() != anObject ||
98                 myCurrentSubShape->shape() != aBaseShape) {
99               myCurrentSubShape->setObject(anObject);
100               myCurrentSubShape->setShape(aBaseShape);
101               myWorkshop->module()->customizeObject(myFeature,
102                                        ModuleBase_IModule::CustomizeHighlightedObjects, true);
103             }
104             else
105               XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();;
106             break;
107           }
108         }
109       }
110     }
111   }
112 }
113
114 //********************************************************************
115 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
116                                                       ObjectPtr& theObject,
117                                                       GeomShapePtr& theShape)
118 {
119   ModuleBase_ISelection* aSelection = myWorkshop->selection();
120   theObject = aSelection->getResult(thePrs);
121   if (!theObject.get() && myCurrentSubShape->object())
122     theObject = myCurrentSubShape->object();
123 }
124
125 //********************************************************************
126 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetSubShapeSelector::getAttributeSelection() const
127 {
128   return QList<ModuleBase_ViewerPrsPtr>();
129 }
130
131
132 //********************************************************************
133 bool PartSet_WidgetSubShapeSelector::setSelection(
134                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
135                                           const bool theToValidate)
136 {
137   //if (theToValidate)
138   //  bool aResult = ModuleBase_WidgetShapeSelector::setSelection(theValues, theToValidate);
139   //else {
140   // the sub-shape is selected, initial shape is not highlighted/selected, we need to use
141   // the sub-shape to fill attribute;
142   ObjectPtr aBaseObject = myCurrentSubShape->object();
143   GeomShapePtr aBaseShape = myCurrentSubShape->shape();
144   bool aResult = aBaseObject.get() && aBaseShape.get();
145   // firstly set the selection to the attribute
146   if (aResult) {
147     QList<ModuleBase_ViewerPrsPtr> aValues;
148     aValues.append(myCurrentSubShape);
149     aResult = ModuleBase_WidgetShapeSelector::setSelection(aValues, theToValidate);
150   }
151   // secondly fill additional attributes
152   if (aResult) {
153     if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
154       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
155
156       std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
157       std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
158       std::shared_ptr<GeomAPI_Pnt2d> aFirstPnt2D = PartSet_Tools::convertTo2D(mySketch, aFirstPnt);
159       std::shared_ptr<GeomAPI_Pnt2d> aLastPnt2D = PartSet_Tools::convertTo2D(mySketch, aLastPnt);
160
161
162       /// find the points in feature attributes
163       FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
164       std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
165                                                         GeomDataAPI_Point2D::typeId());
166       std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(), 
167                                               aLast = a2DPointAttributes.end();
168       std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
169       for (; anIt != aLast; anIt++) {
170         std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = 
171                                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
172         if (aFirstPnt2D->isEqual(anAttributePoint->pnt()))
173           aFirstPointAttr = anAttributePoint;
174         else if (aLastPnt2D->isEqual(anAttributePoint->pnt()))
175           aLastPointAttr = anAttributePoint;
176       }
177       /// find the points in coincident features
178       if (!aFirstPointAttr.get() || !aLastPointAttr.get()) {
179         std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes = myCashedReferences[aBaseObject];
180         std::set<std::shared_ptr<GeomDataAPI_Point2D> >::const_iterator aRefIt = aRefAttributes.begin(),
181                                                                         aRefLast = aRefAttributes.end();
182         for (; aRefIt != aRefLast; aRefIt++) {
183           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = *aRefIt;
184           double anX = anAttributePoint->x();
185           double anY = anAttributePoint->y();
186           if (!aFirstPointAttr.get() && aFirstPnt2D->isEqual(anAttributePoint->pnt()))
187               aFirstPointAttr = anAttributePoint;
188           if (!aLastPointAttr.get() && aLastPnt2D->isEqual(anAttributePoint->pnt()))
189               aLastPointAttr = anAttributePoint;
190           if (aFirstPointAttr.get() && aLastPointAttr.get())
191             break;
192         }
193       }
194
195       if (!aFirstPointAttr.get() || !aLastPointAttr)
196         return false;
197
198       FeaturePtr aFeature = feature();
199       AttributeRefAttrPtr anAPointAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
200                                           aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
201       AttributeRefAttrPtr aBPointAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
202                                           aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
203       anAPointAttr->setAttr(aFirstPointAttr);
204       aBPointAttr->setAttr(aLastPointAttr);
205     }
206   }
207   //}
208
209   return aResult;
210 }
211
212 //********************************************************************
213 void PartSet_WidgetSubShapeSelector::getHighlighted(
214                            QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues)
215 {
216   if (myCurrentSubShape.get() && myCurrentSubShape->object().get())
217     theValues.append(myCurrentSubShape);
218 }
219
220 //********************************************************************
221 void PartSet_WidgetSubShapeSelector::fillObjectShapes(const ObjectPtr& theObject)
222 {
223   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
224   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
225
226   // current feature
227   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
228   std::set<GeomShapePtr> anEdgeShapes;
229   // edges on feature
230   ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
231   if (!anEdgeShapes.empty()) {
232     GeomShapePtr aFeatureShape = *anEdgeShapes.begin();
233
234     // coincidences to the feature
235     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
236                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
237     // layed on feature coincidences to divide it on several shapes
238     CompositeFeaturePtr aSketch = sketch();
239     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
240     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
241         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
242     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
243         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
244     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
245         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
246     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
247     std::set<std::shared_ptr<GeomAPI_Pnt> > aPoints;
248     ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
249                                                 aX->dir(), aY, aPoints);
250
251     GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPoints, aShapes);
252   }
253   myCashedShapes[theObject] = aShapes;
254   myCashedReferences[theObject] = aRefAttributes;
255 }
256
257 //********************************************************************
258 /*bool PartSet_WidgetSubShapeSelector::activateSelectionAndFilters(bool toActivate)
259 {
260   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
261                                                                            (toActivate);
262   if (!myUseSketchPlane) {
263     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
264     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
265     bool isUsePlaneFilterOnly = !toActivate;
266     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
267   }
268   return aHasSelectionFilter;
269 }
270
271 //********************************************************************
272 bool PartSet_WidgetSubShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
273 {
274   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
275   if (aValid) {
276     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
277     aValid = myExternalObjectMgr->isValidObject(anObject);
278   }
279   return aValid;
280 }
281
282 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
283                                                    ObjectPtr& theObject,
284                                                    GeomShapePtr& theShape)
285 {
286   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
287
288   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
289   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
290           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
291   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
292   // TODO: unite with the same functionality in PartSet_WidgetSubShapeSelector
293   if (aSPFeature.get() == NULL) {
294     ObjectPtr anExternalObject = ObjectPtr();
295     if (myExternalObjectMgr->useExternal()) {
296       GeomShapePtr aShape = theShape;
297       if (!aShape.get()) {
298         ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
299         if (aResult.get())
300           aShape = aResult->shape();
301       }
302       if (aShape.get() != NULL && !aShape->isNull())
303         anExternalObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
304     }
305     /// the object is null if the selected feature is "external"(not sketch entity feature of the
306     /// current sketch) and it is not created by object manager
307     theObject = anExternalObject;
308   }
309 }
310
311 //********************************************************************
312 void PartSet_WidgetSubShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
313                                                         const bool theValid)
314 {
315   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
316   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
317 }
318 */