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>
20 #include <ModuleBase_FilterMulti.h>
21 #include <ModuleBase_FilterCustom.h>
22 #include <ModuleBase_FilterNoConsructionSubShapes.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Session.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Dir.h>
34 #include <XGUI_Displayer.h>
35 #include <XGUI_Workshop.h>
36 #include <XGUI_OperationMgr.h>
37 #include <XGUI_PropertyPanel.h>
38 #include <XGUI_ModuleConnector.h>
39 #include <XGUI_Tools.h>
41 #include <SketchPlugin_Feature.h>
42 #include <SketchPlugin_Sketch.h>
43 #include <SketchPlugin_Line.h>
44 //#include <SketchPlugin_Arc.h>
45 //#include <SketchPlugin_Circle.h>
46 #include <SketchPlugin_ConstraintLength.h>
47 #include <SketchPlugin_ConstraintDistance.h>
48 #include <SketchPlugin_ConstraintParallel.h>
49 #include <SketchPlugin_ConstraintPerpendicular.h>
50 #include <SketchPlugin_ConstraintRadius.h>
51 //#include <SketchPlugin_ConstraintRigid.h>
53 #include <Events_Loop.h>
55 #include <StdSelect_TypeOfFace.hxx>
56 #include <TopoDS_Vertex.hxx>
58 #include <TopoDS_Shape.hxx>
59 #include <BRep_Tool.hxx>
62 #include <QMouseEvent>
65 #include <QApplication>
66 #include <QMessageBox>
67 #include <QMainWindow>
69 #include <GeomAlgoAPI_FaceBuilder.h>
70 #include <GeomDataAPI_Dir.h>
76 /*!Create and return new instance of XGUI_Module*/
77 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
79 return new PartSet_Module(theWshop);
82 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
83 : ModuleBase_IModule(theWshop),
84 myRestartingMode(RM_None)
86 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
87 mySketchMgr = new PartSet_SketcherMgr(this);
89 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
90 XGUI_Workshop* aWorkshop = aConnector->workshop();
92 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
93 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
94 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
95 this, SLOT(onOperationActivatedByPreselection()));
97 ModuleBase_IViewer* aViewer = theWshop->viewer();
98 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
99 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
104 PartSet_Module::~PartSet_Module()
106 if (!myDocumentShapeFilter.IsNull())
107 myDocumentShapeFilter.Nullify();
110 void PartSet_Module::registerValidators()
112 //Registering of validators
113 SessionPtr aMgr = ModelAPI_Session::get();
114 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
115 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
116 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
117 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
118 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
119 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
120 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
121 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
122 aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
125 void PartSet_Module::registerFilters()
127 //Registering of selection filters
128 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
129 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
131 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
132 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
133 aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
134 Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
135 aFactory->registerFilter("NoConstructionSubShapesFilter",
136 new ModuleBase_FilterCustom(aSelectFilter));
139 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
141 if (theOperation->isEditOperation())
143 // the selection is cleared after commit the create operation
144 // in order to do not use the same selected objects in the restarted operation
145 // for common behaviour, the selection is cleared even if the operation is not restarted
146 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
147 if (!aContext.IsNull())
148 aContext->ClearSelected();
150 /// Restart sketcher operations automatically
151 FeaturePtr aFeature = theOperation->feature();
152 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
153 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
154 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
155 myRestartingMode == RM_EmptyFeatureUsed)) {
156 myLastOperationId = theOperation->id();
157 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
159 launchOperation(myLastOperationId);
161 breakOperationSequence();
164 void PartSet_Module::breakOperationSequence()
166 myLastOperationId = "";
167 myLastFeature = FeaturePtr();
168 myRestartingMode = RM_None;
171 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
173 breakOperationSequence();
176 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
178 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
179 mySketchMgr->startSketch(theOperation);
181 if (myDocumentShapeFilter.IsNull())
182 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
183 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
186 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
188 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
189 mySketchMgr->stopSketch(theOperation);
191 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
194 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
196 bool aCanDisplay = false;
197 if (mySketchMgr->activeSketch()) {
198 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
200 if (aFeature.get() != NULL) {
201 if (aFeature == mySketchMgr->activeSketch()) {
205 aCanDisplay = mySketchMgr->sketchOperationIdList().contains(aFeature->getKind().c_str());
210 aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
214 void PartSet_Module::addViewerItems(QMenu* theMenu) const
216 if (!isSketchOperationActive() && !isSketchFeatureOperationActive())
218 ModuleBase_ISelection* aSelection = myWorkshop->selection();
219 QObjectPtrList aObjects = aSelection->selectedPresentations();
220 if (aObjects.size() > 0) {
221 bool hasFeature = false;
222 foreach(ObjectPtr aObject, aObjects)
224 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
225 if (aFeature.get() != NULL) {
230 theMenu->addAction(action("DELETE_PARTSET_CMD"));
234 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
236 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
237 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
238 (theOperation->isEditOperation())) {
239 // we have to manually activate the sketch label in edit mode
240 aPanel->activateWidget(aPanel->modelWidgets().first());
244 // Restart last operation type
245 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
246 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
247 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
248 // Initialise new line with first point equal to end of previous
249 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
251 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
252 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
253 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
255 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
256 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
257 aWgt->attributeID(), aPoint->x(), aPoint->y());
258 aPanel->activateNextWidget(aPnt2dWgt);
263 // Start editing constraint
264 if (theOperation->isEditOperation()) {
265 // TODO: #391 - to be removed
266 std::string aId = theOperation->id().toStdString();
267 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
268 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
269 (aId == SketchPlugin_ConstraintLength::ID()) ||
270 (aId == SketchPlugin_ConstraintDistance::ID())) {
271 // Find and activate widget for management of point for dimension line position
272 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
273 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
274 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
276 aPanel->activateWidget(aPntWgt);
287 void PartSet_Module::onSelectionChanged()
289 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
293 bool isSketcherOp = false;
294 // An edit operation is enable only if the current opeation is the sketch operation
295 if (mySketchMgr->activeSketch()) {
296 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
297 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
300 // Editing of constraints can be done on selection
301 ModuleBase_ISelection* aSelect = myWorkshop->selection();
302 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
303 if (aSelected.size() == 1) {
304 ModuleBase_ViewerPrs aPrs = aSelected.first();
305 ObjectPtr aObject = aPrs.object();
306 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
308 std::string aId = aFeature->getKind();
309 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
310 (aId == SketchPlugin_ConstraintLength::ID()) ||
311 (aId == SketchPlugin_ConstraintDistance::ID())) {
312 editFeature(aFeature);
319 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
321 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
322 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
323 anOpMgr->onKeyReleased(theEvent);
326 void PartSet_Module::onEnterReleased()
328 myRestartingMode = RM_EmptyFeatureUsed;
331 void PartSet_Module::onOperationActivatedByPreselection()
333 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
334 if(aOperation && isSketchFeatureOperationActive()) {
335 // Set final definitions if they are necessary
336 //propertyPanelDefined(aOperation);
338 /// Commit sketcher operations automatically
339 aOperation->commit();
343 void PartSet_Module::onNoMoreWidgets()
345 if (isSketchFeatureOperationActive()) {
346 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
348 if (myRestartingMode != RM_Forbided)
349 myRestartingMode = RM_LastFeatureUsed;
350 aOperation->commit();
355 void PartSet_Module::onVertexSelected()
357 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
358 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
359 /// If last line finished on vertex the lines creation sequence has to be break
360 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
361 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
362 if (aWidgets.last() == aPanel->activeWidget()) {
363 myRestartingMode = RM_Forbided;
368 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
369 Config_WidgetAPI* theWidgetApi, std::string theParentId,
370 QList<ModuleBase_ModelWidget*>& theModelWidgets)
372 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
373 XGUI_Workshop* aWorkshop = aConnector->workshop();
374 if (theType == "sketch-start-label") {
375 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
376 aWgt->setWorkshop(aWorkshop);
377 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
378 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
379 theModelWidgets.append(aWgt);
380 return aWgt->getControl();
382 } else if (theType == "sketch-2dpoint_selector") {
383 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
384 aWgt->setWorkshop(aWorkshop);
385 aWgt->setSketch(mySketchMgr->activeSketch());
387 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
389 theModelWidgets.append(aWgt);
390 return aWgt->getControl();
392 } if (theType == "point2ddistance") {
393 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
394 aWgt->setWorkshop(aWorkshop);
395 aWgt->setSketch(mySketchMgr->activeSketch());
397 theModelWidgets.append(aWgt);
398 return aWgt->getControl();
400 } if (theType == "sketch_shape_selector") {
401 PartSet_WidgetShapeSelector* aWgt =
402 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
403 aWgt->setSketcher(mySketchMgr->activeSketch());
405 theModelWidgets.append(aWgt);
406 return aWgt->getControl();
408 } if (theType == "sketch_constraint_shape_selector") {
409 PartSet_WidgetConstraintShapeSelector* aWgt =
410 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
411 aWgt->setSketcher(mySketchMgr->activeSketch());
413 theModelWidgets.append(aWgt);
414 return aWgt->getControl();
420 bool PartSet_Module::isSketchOperationActive() const
422 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
424 bool isSketchOp = aOperation && aOperation->id().toStdString() == SketchPlugin_Sketch::ID();
428 bool PartSet_Module::isSketchFeatureOperationActive() const
430 bool isCurrentSketchOp = false;
431 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
433 FeaturePtr aFeature = aOperation->feature();
434 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
435 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
436 isCurrentSketchOp = aSPFeature.get() != NULL;
438 return isCurrentSketchOp;
441 void PartSet_Module::createActions()
443 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
444 addAction("DELETE_PARTSET_CMD", aAction);
447 QAction* PartSet_Module::action(const QString& theId) const
449 if (myActions.contains(theId))
450 return myActions[theId];
454 void PartSet_Module::addAction(const QString& theId, QAction* theAction)
456 if (myActions.contains(theId))
457 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
458 theAction->setData(theId);
459 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
460 myActions[theId] = theAction;
463 void PartSet_Module::onAction(bool isChecked)
465 QAction* aAction = static_cast<QAction*>(sender());
466 QString anId = aAction->data().toString();
468 if (anId == "DELETE_PARTSET_CMD") {
473 void PartSet_Module::deleteObjects()
475 bool isSketchOp = isSketchOperationActive();
476 if (!isSketchOp && !isSketchFeatureOperationActive())
479 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
480 XGUI_Workshop* aWorkshop = aConnector->workshop();
482 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
483 if (!isSketchOp && anOpMgr->canStopOperation()) {
484 ModuleBase_Operation* aCurrentOp = anOpMgr->currentOperation();
489 // sketch feature should be skipped, only sub-features can be removed
490 // when sketch operation is active
491 CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
493 ModuleBase_ISelection* aSel = aConnector->selection();
494 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
496 std::set<FeaturePtr> aRefFeatures;
497 foreach (ObjectPtr aObj, aSelectedObj)
499 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
501 // TODO: check for what there is this condition. It is placed here historicaly because
502 // ther is this condition during remove features.
504 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
505 if (aFeature.get() != NULL) {
506 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
511 if (!aRefFeatures.empty()) {
512 QStringList aRefNames;
513 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
514 aLast = aRefFeatures.end();
515 for (; anIt != aLast; anIt++) {
516 FeaturePtr aFeature = (*anIt);
517 if (aFeature == aSketch)
519 aRefNames.append((*anIt)->name().c_str());
521 if (!aRefNames.empty()) {
522 QString aNames = aRefNames.join(", ");
524 QMainWindow* aDesktop = aWorkshop->desktop();
525 QMessageBox::StandardButton aRes = QMessageBox::warning(
526 aDesktop, tr("Delete features"),
527 QString(tr("Selected features are used in the following features: %1.\
528 These features will be deleted also. Would you like to continue?")).arg(aNames),
529 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
530 if (aRes != QMessageBox::Yes)
535 SessionPtr aMgr = ModelAPI_Session::get();
536 aMgr->startOperation();
537 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
538 aLast = aRefFeatures.end();
539 for (; anIt != aLast; anIt++) {
540 FeaturePtr aRefFeature = (*anIt);
541 if (aRefFeature == aSketch)
543 aRefFeature->document()->removeFeature(aRefFeature);
546 foreach (ObjectPtr aObj, aSelectedObj)
548 DocumentPtr aDoc = aObj->document();
549 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
551 // if (aDoc == aMgr->activeDocument()) {
555 //FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
556 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
557 if (aFeature.get() != NULL) {
558 aDoc->removeFeature(aFeature);
562 aWorkshop->displayer()->updateViewer();
563 //myDisplayer->updateViewer();
564 aMgr->finishOperation();