1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "PartSet_Module.h"
4 #include <PartSet_WidgetSketchLabel.h>
5 #include <PartSet_Validators.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_WidgetPoint2d.h>
8 #include <PartSet_WidgetPoint2dDistance.h>
9 #include <PartSet_WidgetShapeSelector.h>
10 #include <PartSet_SketcherMgr.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_IViewWindow.h>
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_FilterFactory.h>
18 #include <ModuleBase_FilterLinearEdge.h>
19 #include <ModuleBase_FilterFace.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Session.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Dir.h>
32 #include <XGUI_Displayer.h>
33 #include <XGUI_Workshop.h>
34 #include <XGUI_OperationMgr.h>
35 #include <XGUI_PropertyPanel.h>
36 #include <XGUI_ModuleConnector.h>
37 #include <XGUI_Tools.h>
39 #include <SketchPlugin_Feature.h>
40 #include <SketchPlugin_Sketch.h>
41 #include <SketchPlugin_Line.h>
42 //#include <SketchPlugin_Arc.h>
43 //#include <SketchPlugin_Circle.h>
44 #include <SketchPlugin_ConstraintLength.h>
45 #include <SketchPlugin_ConstraintDistance.h>
46 #include <SketchPlugin_ConstraintParallel.h>
47 #include <SketchPlugin_ConstraintPerpendicular.h>
48 #include <SketchPlugin_ConstraintRadius.h>
49 //#include <SketchPlugin_ConstraintRigid.h>
51 #include <Events_Loop.h>
53 #include <StdSelect_TypeOfFace.hxx>
54 #include <TopoDS_Vertex.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <BRep_Tool.hxx>
60 #include <QMouseEvent>
63 #include <QApplication>
65 #include <GeomAlgoAPI_FaceBuilder.h>
66 #include <GeomDataAPI_Dir.h>
72 /*!Create and return new instance of XGUI_Module*/
73 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
75 return new PartSet_Module(theWshop);
78 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
79 : ModuleBase_IModule(theWshop),
80 myRestartingMode(RM_None)
82 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
83 mySketchMgr = new PartSet_SketcherMgr(this);
85 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
86 XGUI_Workshop* aWorkshop = aConnector->workshop();
88 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
89 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
90 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
91 this, SLOT(onOperationActivatedByPreselection()));
93 ModuleBase_IViewer* aViewer = theWshop->viewer();
94 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
95 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
98 PartSet_Module::~PartSet_Module()
100 if (!myDocumentShapeFilter.IsNull())
101 myDocumentShapeFilter.Nullify();
104 void PartSet_Module::registerValidators()
106 //Registering of validators
107 SessionPtr aMgr = ModelAPI_Session::get();
108 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
109 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
110 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
111 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
112 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
113 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
114 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
115 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
118 void PartSet_Module::registerFilters()
120 //Registering of selection filters
121 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
122 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
124 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
125 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
128 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
130 if (theOperation->isEditOperation())
132 /// Restart sketcher operations automatically
133 FeaturePtr aFeature = theOperation->feature();
134 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
135 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
136 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
137 myRestartingMode == RM_EmptyFeatureUsed)) {
138 myLastOperationId = theOperation->id();
139 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
140 launchOperation(myLastOperationId);
142 breakOperationSequence();
145 void PartSet_Module::breakOperationSequence()
147 myLastOperationId = "";
148 myLastFeature = FeaturePtr();
149 myRestartingMode = RM_None;
152 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
154 breakOperationSequence();
157 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
159 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
160 mySketchMgr->startSketch(theOperation);
162 if (myDocumentShapeFilter.IsNull())
163 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
164 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
167 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
169 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
170 mySketchMgr->stopSketch(theOperation);
172 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
176 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
178 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
179 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
180 (theOperation->isEditOperation())) {
181 // we have to manually activate the sketch label in edit mode
182 aPanel->activateWidget(aPanel->modelWidgets().first());
186 // Restart last operation type
187 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
188 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
189 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
190 // Initialise new line with first point equal to end of previous
191 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
193 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
194 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
195 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
197 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
198 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
199 aWgt->attributeID(), aPoint->x(), aPoint->y());
200 theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
205 // Start editing constraint
206 if (theOperation->isEditOperation()) {
207 std::string aId = theOperation->id().toStdString();
208 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
209 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
210 (aId == SketchPlugin_ConstraintLength::ID()) ||
211 (aId == SketchPlugin_ConstraintDistance::ID())) {
212 // Find and activate widget for management of point for dimension line position
213 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
214 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
215 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
217 aPanel->activateWidget(aPntWgt);
228 void PartSet_Module::onSelectionChanged()
230 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
234 bool isSketcherOp = false;
235 // An edit operation is enable only if the current opeation is the sketch operation
236 if (mySketchMgr->activeSketch()) {
237 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
238 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
241 // Editing of constraints can be done on selection
242 ModuleBase_ISelection* aSelect = myWorkshop->selection();
243 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
244 if (aSelected.size() == 1) {
245 ModuleBase_ViewerPrs aPrs = aSelected.first();
246 ObjectPtr aObject = aPrs.object();
247 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
249 std::string aId = aFeature->getKind();
250 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
251 (aId == SketchPlugin_ConstraintLength::ID()) ||
252 (aId == SketchPlugin_ConstraintDistance::ID())) {
253 editFeature(aFeature);
260 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
262 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
263 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
264 anOpMgr->onKeyReleased(theEvent);
267 void PartSet_Module::onEnterReleased()
269 myRestartingMode = RM_EmptyFeatureUsed;
272 void PartSet_Module::onOperationActivatedByPreselection()
274 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
278 // Set final definitions if they are necessary
279 //propertyPanelDefined(aOperation);
281 /// Commit sketcher operations automatically
282 FeaturePtr aFeature = aOperation->feature();
283 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
284 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
286 aOperation->commit();
290 void PartSet_Module::onNoMoreWidgets()
292 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
294 /// Restart sketcher operations automatically
295 FeaturePtr aFeature = aOperation->feature();
296 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
297 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
299 if (myRestartingMode != RM_Forbided)
300 myRestartingMode = RM_LastFeatureUsed;
301 aOperation->commit();
306 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
308 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
309 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
310 /// If last line finished on vertex the lines creation sequence has to be break
311 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
312 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
313 if (aWidgets.last() == aPanel->activeWidget()) {
314 myRestartingMode = RM_Forbided;
319 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
320 Config_WidgetAPI* theWidgetApi, std::string theParentId,
321 QList<ModuleBase_ModelWidget*>& theModelWidgets)
323 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
324 XGUI_Workshop* aWorkshop = aConnector->workshop();
325 if (theType == "sketch-start-label") {
326 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
327 aWgt->setWorkshop(aWorkshop);
328 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
329 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
330 theModelWidgets.append(aWgt);
331 return aWgt->getControl();
333 } else if (theType == "sketch-2dpoint_selector") {
334 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
335 aWgt->setWorkshop(aWorkshop);
336 aWgt->setSketch(mySketchMgr->activeSketch());
338 connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)),
339 this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
341 theModelWidgets.append(aWgt);
342 return aWgt->getControl();
344 } if (theType == "point2ddistance") {
345 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
346 aWgt->setWorkshop(aWorkshop);
347 aWgt->setSketch(mySketchMgr->activeSketch());
349 theModelWidgets.append(aWgt);
350 return aWgt->getControl();
352 } if (theType == "sketch_shape_selector") {
353 PartSet_WidgetShapeSelector* aWgt =
354 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
355 aWgt->setSketcher(mySketchMgr->activeSketch());
357 theModelWidgets.append(aWgt);
358 return aWgt->getControl();
360 } if (theType == "sketch_constraint_shape_selector") {
361 PartSet_WidgetConstraintShapeSelector* aWgt =
362 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
363 aWgt->setSketcher(mySketchMgr->activeSketch());
365 theModelWidgets.append(aWgt);
366 return aWgt->getControl();