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>
34 #include <QMouseEvent>
36 PartSet_WidgetSubShapeSelector::PartSet_WidgetSubShapeSelector(QWidget* theParent,
37 ModuleBase_IWorkshop* theWorkshop,
38 const Config_WidgetAPI* theData)
39 : PartSet_WidgetShapeSelector(theParent, theWorkshop, theData)
41 //myUseSketchPlane = theData->getBooleanAttribute("use_sketch_plane", true);
42 //myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"), true);
45 PartSet_WidgetSubShapeSelector::~PartSet_WidgetSubShapeSelector()
49 //********************************************************************
50 void PartSet_WidgetSubShapeSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
51 QMouseEvent* theEvent)
53 ModuleBase_ISelection* aSelect = myWorkshop->selection();
54 QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
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()));
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);
79 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
83 //********************************************************************
84 void PartSet_WidgetSubShapeSelector::getHighlighted(
85 QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues)
87 if (myCurrentSubShape.get() && myCurrentSubShape->object().get())
88 theValues.append(myCurrentSubShape);
91 //********************************************************************
92 void PartSet_WidgetSubShapeSelector::fillObjectShapes(const ObjectPtr& theObject)
94 std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
97 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
98 std::set<GeomShapePtr> anEdgeShapes;
100 ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
101 if (!anEdgeShapes.empty()) {
102 GeomShapePtr aFeatureShape = *anEdgeShapes.begin();
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);
122 GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPoints, aShapes);
124 myCashedShapes[theObject] = aShapes;
127 //********************************************************************
128 /*bool PartSet_WidgetSubShapeSelector::activateSelectionAndFilters(bool toActivate)
130 bool aHasSelectionFilter = ModuleBase_WidgetShapeSelector::activateSelectionAndFilters
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);
138 return aHasSelectionFilter;
141 //********************************************************************
142 bool PartSet_WidgetSubShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
144 bool aValid = ModuleBase_WidgetShapeSelector::isValidSelectionCustom(thePrs);
146 ObjectPtr anObject = myWorkshop->selection()->getResult(thePrs);
147 aValid = myExternalObjectMgr->isValidObject(anObject);
152 void PartSet_WidgetSubShapeSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
153 ObjectPtr& theObject,
154 GeomShapePtr& theShape)
156 ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
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;
168 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
170 aShape = aResult->shape();
172 if (aShape.get() != NULL && !aShape->isNull())
173 anExternalObject = myExternalObjectMgr->externalObject(theObject, aShape, sketch(), myIsInValidate);
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;
181 //********************************************************************
182 void PartSet_WidgetSubShapeSelector::restoreAttributeValue(const AttributePtr& theAttribute,
185 ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
186 myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);