]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSubShapeSelector.cpp
Salome HOME
Issue #1664: In the Sketcher, add the function Split a segment. AttrRefAttribute...
[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 bool PartSet_WidgetSubShapeSelector::setSelection(
116                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
117                                           const bool theToValidate)
118 {
119   bool aResult = ModuleBase_WidgetShapeSelector::setSelection(theValues, theToValidate);
120
121   if (aResult && !theToValidate) {
122     ObjectPtr aBaseObject = myCurrentSubShape->object();
123     GeomShapePtr aBaseShape = myCurrentSubShape->shape();
124
125     if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
126       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
127
128       std::shared_ptr<GeomAPI_Pln> aSketchPlane = PartSet_Tools::sketchPlane(mySketch);
129       std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
130       std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
131       std::shared_ptr<GeomAPI_Pnt2d> aFirstPnt2D = aFirstPnt->to2D(aSketchPlane);
132       std::shared_ptr<GeomAPI_Pnt2d> aLastPnt2D = aLastPnt->to2D(aSketchPlane);
133
134       /// find the points in feature attributes
135       FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
136       std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
137                                                         GeomDataAPI_Point2D::typeId());
138       std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(), 
139                                               aLast = a2DPointAttributes.end();
140       std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
141       for (; anIt != aLast; anIt++) {
142         std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = 
143                                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
144         if (aFirstPnt2D->isEqual(anAttributePoint->pnt()))
145           aFirstPointAttr = anAttributePoint;
146         else if (aLastPnt2D->isEqual(anAttributePoint->pnt()))
147           aLastPointAttr = anAttributePoint;
148       }
149       /// find the points in coincident features
150       if (!aFirstPointAttr.get() || !aLastPointAttr.get()) {
151         std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes = myCashedReferences[aBaseObject];
152         std::set<std::shared_ptr<GeomDataAPI_Point2D> >::const_iterator aRefIt = aRefAttributes.begin(),
153                                                                         aRefLast = aRefAttributes.end();
154         for (; aRefIt != aRefLast; aRefIt++) {
155           std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint = *aRefIt;
156           if (!aFirstPointAttr.get() && aFirstPnt2D->isEqual(anAttributePoint->pnt()))
157               aFirstPointAttr = anAttributePoint;
158           if (!aLastPointAttr.get() && aLastPnt2D->isEqual(anAttributePoint->pnt()))
159               aLastPointAttr = anAttributePoint;
160           if (aFirstPointAttr.get() && aLastPointAttr.get())
161             break;
162         }
163       }
164
165       FeaturePtr aFeature = feature();
166       AttributeRefAttrPtr anAPointAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
167                                          aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
168       AttributeRefAttrPtr aBPointAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
169                                          aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
170       anAPointAttr->setAttr(aFirstPointAttr);
171       aBPointAttr->setAttr(aLastPointAttr);
172     }
173   }
174
175   return aResult;
176 }
177
178 //********************************************************************
179 void PartSet_WidgetSubShapeSelector::getHighlighted(
180                            QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues)
181 {
182   if (myCurrentSubShape.get() && myCurrentSubShape->object().get())
183     theValues.append(myCurrentSubShape);
184 }
185
186 //********************************************************************
187 void PartSet_WidgetSubShapeSelector::fillObjectShapes(const ObjectPtr& theObject)
188 {
189   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
190   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
191
192   // current feature
193   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
194   std::set<GeomShapePtr> anEdgeShapes;
195   // edges on feature
196   ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
197   if (!anEdgeShapes.empty()) {
198     GeomShapePtr aFeatureShape = *anEdgeShapes.begin();
199
200     // coincidences to the feature
201     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
202                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
203     // layed on feature coincidences to divide it on several shapes
204     FeaturePtr aSketch = sketch();
205     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
206     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
207         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
208     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
209         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
210     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
211         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
212     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
213     std::set<std::shared_ptr<GeomAPI_Pnt> > aPoints;
214     ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
215                                                 aX->dir(), aY, aPoints);
216
217     GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPoints, aShapes);
218   }
219   myCashedShapes[theObject] = aShapes;
220   myCashedReferences[theObject] = aRefAttributes;
221 }
222
223 //********************************************************************
224 /*bool PartSet_WidgetSubShapeSelector::activateSelectionAndFilters(bool toActivate)
225 {
226   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
227                                                                            (toActivate);
228   if (!myUseSketchPlane) {
229     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
230     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
231     bool isUsePlaneFilterOnly = !toActivate;
232     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
233   }
234   return aHasSelectionFilter;
235 }
236
237 //********************************************************************
238 bool PartSet_WidgetSubShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
239 {
240   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
241   if (aValid) {
242     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
243     aValid = myExternalObjectMgr->isValidObject(anObject);
244   }
245   return aValid;
246 }
247
248 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
249                                                    ObjectPtr& theObject,
250                                                    GeomShapePtr& theShape)
251 {
252   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
253
254   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
255   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
256           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
257   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
258   // TODO: unite with the same functionality in PartSet_WidgetSubShapeSelector
259   if (aSPFeature.get() == NULL) {
260     ObjectPtr anExternalObject = ObjectPtr();
261     if (myExternalObjectMgr->useExternal()) {
262       GeomShapePtr aShape = theShape;
263       if (!aShape.get()) {
264         ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
265         if (aResult.get())
266           aShape = aResult->shape();
267       }
268       if (aShape.get() != NULL && !aShape->isNull())
269         anExternalObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
270     }
271     /// the object is null if the selected feature is "external"(not sketch entity feature of the
272     /// current sketch) and it is not created by object manager
273     theObject = anExternalObject;
274   }
275 }
276
277 //********************************************************************
278 void PartSet_WidgetSubShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
279                                                         const bool theValid)
280 {
281   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
282   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
283 }
284 */