Salome HOME
Preparations for Split operation. It includes:
[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 <QWidget>
34 #include <QMouseEvent>
35
36 PartSet_WidgetSubShapeSelector::PartSet_WidgetSubShapeSelector(QWidget* theParent,
37                                                          ModuleBase_IWorkshop* theWorkshop,
38                                                          const Config_WidgetAPI* theData)
39 : PartSet_WidgetShapeSelector(theParent, theWorkshop, theData)
40 {
41   //myUseSketchPlane = theData->getBooleanAttribute("use_sketch_plane", true);
42   //myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), true);
43 }
44
45 PartSet_WidgetSubShapeSelector::~PartSet_WidgetSubShapeSelector()
46 {
47 }
48
49 //********************************************************************
50 void PartSet_WidgetSubShapeSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
51                                                 QMouseEvent* theEvent)
52 {
53   ModuleBase_ISelection* aSelect = myWorkshop->selection();
54   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
55
56   if (aHighlighted.empty()) {
57     ModuleBase_ViewerPrsPtr aPrs = aHighlighted.first();
58     if (aPrs.get() && aPrs->object().get()) {
59       ObjectPtr anObject = aPrs->object();
60       if (myCashedShapes.find(anObject) == myCashedShapes.end())
61         fillObjectShapes(anObject);
62       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
63       if (!aShapes.empty()) {
64         gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
65         std::shared_ptr<GeomAPI_Vertex> aVertexShape(new GeomAPI_Vertex(aPnt.X(), aPnt.Y(), aPnt.Z()));
66
67         std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
68         for (; anIt != aLast; anIt++) {
69           GeomShapePtr aBaseShape = *anIt;
70           if (GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aVertexShape, aBaseShape)) {
71             myCurrentSubShape->setObject(anObject);
72             myCurrentSubShape->setShape(aBaseShape);
73             break;
74           }
75         }
76       }
77     }
78   }
79   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
80                                         true);
81 }
82
83 //********************************************************************
84 void PartSet_WidgetSubShapeSelector::getHighlighted(
85                            QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues)
86 {
87   if (myCurrentSubShape.get() && myCurrentSubShape->object().get())
88     theValues.append(myCurrentSubShape);
89 }
90
91 //********************************************************************
92 void PartSet_WidgetSubShapeSelector::fillObjectShapes(const ObjectPtr& theObject)
93 {
94   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
95
96   // current feature
97   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
98   std::set<GeomShapePtr> anEdgeShapes;
99   // edges on feature
100   ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
101   if (!anEdgeShapes.empty()) {
102     GeomShapePtr aFeatureShape = *anEdgeShapes.begin();
103
104     // coincidences to the feature
105     std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
106     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
107                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
108     // layed on feature coincidences to divide it on several shapes
109     FeaturePtr aSketch = sketch();
110     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
111     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
112         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
113     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
114         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
115     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
116         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
117     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
118     std::set<std::shared_ptr<GeomAPI_Pnt> > aPoints;
119     ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
120                                                 aX->dir(), aY, aPoints);
121
122     GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPoints, aShapes);
123   }
124   myCashedShapes[theObject] = aShapes;
125 }
126
127 //********************************************************************
128 /*bool PartSet_WidgetSubShapeSelector::activateSelectionAndFilters(bool toActivate)
129 {
130   bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
131                                                                            (toActivate);
132   if (!myUseSketchPlane) {
133     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
134     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
135     bool isUsePlaneFilterOnly = !toActivate;
136     aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
137   }
138   return aHasSelectionFilter;
139 }
140
141 //********************************************************************
142 bool PartSet_WidgetSubShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
143 {
144   bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
145   if (aValid) {
146     ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
147     aValid = myExternalObjectMgr->isValidObject(anObject);
148   }
149   return aValid;
150 }
151
152 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
153                                                    ObjectPtr& theObject,
154                                                    GeomShapePtr& theShape)
155 {
156   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
157
158   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
159   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
160           std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
161   // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
162   // TODO: unite with the same functionality in PartSet_WidgetSubShapeSelector
163   if (aSPFeature.get() == NULL) {
164     ObjectPtr anExternalObject = ObjectPtr();
165     if (myExternalObjectMgr->useExternal()) {
166       GeomShapePtr aShape = theShape;
167       if (!aShape.get()) {
168         ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
169         if (aResult.get())
170           aShape = aResult->shape();
171       }
172       if (aShape.get() != NULL && !aShape->isNull())
173         anExternalObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
174     }
175     /// the object is null if the selected feature is "external"(not sketch entity feature of the
176     /// current sketch) and it is not created by object manager
177     theObject = anExternalObject;
178   }
179 }
180
181 //********************************************************************
182 void PartSet_WidgetSubShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
183                                                         const bool theValid)
184 {
185   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
186   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
187 }
188 */