1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetSubShapeSelector.cpp
4 // Created: 21 Jul 2016
5 // Author: Natalia ERMOLAEVA
7 #include "PartSet_WidgetSubShapeSelector.h"
8 #include "PartSet_Tools.h"
10 #include <ModuleBase_ISelection.h>
11 #include <ModuleBase_ViewerPrs.h>
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_Tools.h>
15 #include <GeomDataAPI_Point2D.h>
17 #include <GeomDataAPI_Point.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <ModelGeomAlgo_Point2D.h>
22 #include <ModelGeomAlgo_Point2D.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_Point.h>
27 #include <ModuleBase_IViewWindow.h>
28 #include <ModuleBase_IWorkshop.h>
29 #include <ModuleBase_IModule.h>
31 #include <Config_WidgetAPI.h>
33 #include <XGUI_Tools.h>
34 #include <XGUI_Workshop.h>
35 #include <XGUI_Displayer.h>
38 #include <QMouseEvent>
40 PartSet_WidgetSubShapeSelector::PartSet_WidgetSubShapeSelector(QWidget* theParent,
41 ModuleBase_IWorkshop* theWorkshop,
42 const Config_WidgetAPI* theData)
43 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
45 myCurrentSubShape = std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs());
48 PartSet_WidgetSubShapeSelector::~PartSet_WidgetSubShapeSelector()
50 myCashedShapes.clear();
53 //********************************************************************
54 void PartSet_WidgetSubShapeSelector::activateCustom()
56 ModuleBase_WidgetShapeSelector::activateCustom();
58 myWorkshop->module()->activateCustomPrs(myFeature,
59 ModuleBase_IModule::CustomizeHighlightedObjects, true);
62 //********************************************************************
63 void PartSet_WidgetSubShapeSelector::deactivate()
65 ModuleBase_WidgetShapeSelector::deactivate();
67 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
70 //********************************************************************
71 void PartSet_WidgetSubShapeSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
72 QMouseEvent* theEvent)
74 ModuleBase_ISelection* aSelect = myWorkshop->selection();
75 QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
77 if (!aHighlighted.empty()) {
78 ModuleBase_ViewerPrsPtr aPrs = aHighlighted.first();
79 if (aPrs.get() && aPrs->object().get()) {
80 ObjectPtr anObject = aPrs->object();
81 if (myCashedShapes.find(anObject) == myCashedShapes.end())
82 fillObjectShapes(anObject);
83 const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
84 if (!aShapes.empty()) {
85 gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
86 std::shared_ptr<GeomAPI_Pnt> aPoint(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
88 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
89 for (; anIt != aLast; anIt++) {
90 GeomShapePtr aBaseShape = *anIt;
91 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
92 if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, aPoint, aProjectedPoint)) {
93 XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
94 if (myCurrentSubShape->object() != anObject ||
95 myCurrentSubShape->shape() != aBaseShape) {
96 myCurrentSubShape->setObject(anObject);
97 myCurrentSubShape->setShape(aBaseShape);
98 myWorkshop->module()->customizeObject(myFeature,
99 ModuleBase_IModule::CustomizeHighlightedObjects, true);
102 XGUI_Tools::workshop(myWorkshop)->displayer()->updateViewer();;
111 //********************************************************************
112 void PartSet_WidgetSubShapeSelector::getHighlighted(
113 QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues)
115 if (myCurrentSubShape.get() && myCurrentSubShape->object().get())
116 theValues.append(myCurrentSubShape);
119 //********************************************************************
120 void PartSet_WidgetSubShapeSelector::fillObjectShapes(const ObjectPtr& theObject)
122 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
125 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
126 std::set<GeomShapePtr> anEdgeShapes;
128 ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
129 if (!anEdgeShapes.empty()) {
130 GeomShapePtr aFeatureShape = *anEdgeShapes.begin();
132 // coincidences to the feature
133 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
134 ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
135 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
136 // layed on feature coincidences to divide it on several shapes
137 FeaturePtr aSketch = sketch();
138 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
139 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
140 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
141 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
142 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
143 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
144 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
145 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
146 std::set<std::shared_ptr<GeomAPI_Pnt> > aPoints;
147 ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
148 aX->dir(), aY, aPoints);
150 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPoints, aShapes);
152 myCashedShapes[theObject] = aShapes;
155 //********************************************************************
156 /*bool PartSet_WidgetSubShapeSelector::activateSelectionAndFilters(bool toActivate)
158 bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
160 if (!myUseSketchPlane) {
161 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
162 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(aWorkshop->module());
163 bool isUsePlaneFilterOnly = !toActivate;
164 aModule->sketchMgr()->activatePlaneFilter(isUsePlaneFilterOnly);
166 return aHasSelectionFilter;
169 //********************************************************************
170 bool PartSet_WidgetSubShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
172 bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
174 ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
175 aValid = myExternalObjectMgr->isValidObject(anObject);
180 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
181 ObjectPtr& theObject,
182 GeomShapePtr& theShape)
184 ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
186 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theObject);
187 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
188 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
189 // there is no a sketch feature is selected, but the shape exists, try to create an exernal object
190 // TODO: unite with the same functionality in PartSet_WidgetSubShapeSelector
191 if (aSPFeature.get() == NULL) {
192 ObjectPtr anExternalObject = ObjectPtr();
193 if (myExternalObjectMgr->useExternal()) {
194 GeomShapePtr aShape = theShape;
196 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
198 aShape = aResult->shape();
200 if (aShape.get() != NULL && !aShape->isNull())
201 anExternalObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
203 /// the object is null if the selected feature is "external"(not sketch entity feature of the
204 /// current sketch) and it is not created by object manager
205 theObject = anExternalObject;
209 //********************************************************************
210 void PartSet_WidgetSubShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
213 ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
214 myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);