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>
29 #include <ModelAPI_ShapeValidator.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Dir.h>
35 #include <XGUI_Displayer.h>
36 #include <XGUI_Workshop.h>
37 #include <XGUI_OperationMgr.h>
38 #include <XGUI_PropertyPanel.h>
39 #include <XGUI_ModuleConnector.h>
40 #include <XGUI_Tools.h>
42 #include <SketchPlugin_Feature.h>
43 #include <SketchPlugin_Sketch.h>
44 #include <SketchPlugin_Line.h>
45 //#include <SketchPlugin_Arc.h>
46 //#include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_ConstraintLength.h>
48 #include <SketchPlugin_ConstraintDistance.h>
49 #include <SketchPlugin_ConstraintParallel.h>
50 #include <SketchPlugin_ConstraintPerpendicular.h>
51 #include <SketchPlugin_ConstraintRadius.h>
52 //#include <SketchPlugin_ConstraintRigid.h>
54 #include <Events_Loop.h>
55 #include <Config_PropManager.h>
57 #include <StdSelect_TypeOfFace.hxx>
58 #include <TopoDS_Vertex.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <BRep_Tool.hxx>
64 #include <QMouseEvent>
67 #include <QApplication>
68 #include <QMessageBox>
69 #include <QMainWindow>
71 #include <GeomAlgoAPI_FaceBuilder.h>
72 #include <GeomDataAPI_Dir.h>
78 /*!Create and return new instance of XGUI_Module*/
79 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
81 return new PartSet_Module(theWshop);
84 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
85 : ModuleBase_IModule(theWshop),
86 myRestartingMode(RM_None)
88 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
89 mySketchMgr = new PartSet_SketcherMgr(this);
91 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
92 XGUI_Workshop* aWorkshop = aConnector->workshop();
94 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
95 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
96 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
97 this, SLOT(onOperationActivatedByPreselection()));
99 ModuleBase_IViewer* aViewer = theWshop->viewer();
100 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
101 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
106 PartSet_Module::~PartSet_Module()
108 if (!myDocumentShapeFilter.IsNull())
109 myDocumentShapeFilter.Nullify();
112 void PartSet_Module::registerValidators()
114 //Registering of validators
115 SessionPtr aMgr = ModelAPI_Session::get();
116 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
117 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
118 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
119 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
120 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
121 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
122 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
123 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
124 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
125 aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
128 void PartSet_Module::registerFilters()
130 //Registering of selection filters
131 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
132 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
134 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
135 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
136 aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
137 Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
138 aFactory->registerFilter("NoConstructionSubShapesFilter",
139 new ModuleBase_FilterCustom(aSelectFilter));
142 void PartSet_Module::registerProperties()
144 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
146 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
147 Config_Prop::Integer, SKETCH_WIDTH);
150 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
152 if (theOperation->isEditOperation())
154 // the selection is cleared after commit the create operation
155 // in order to do not use the same selected objects in the restarted operation
156 // for common behaviour, the selection is cleared even if the operation is not restarted
157 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
158 if (!aContext.IsNull())
159 aContext->ClearSelected();
161 /// Restart sketcher operations automatically
162 FeaturePtr aFeature = theOperation->feature();
163 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
164 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
165 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
166 myRestartingMode == RM_EmptyFeatureUsed)) {
167 myLastOperationId = theOperation->id();
168 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
170 launchOperation(myLastOperationId);
172 breakOperationSequence();
175 void PartSet_Module::breakOperationSequence()
177 myLastOperationId = "";
178 myLastFeature = FeaturePtr();
179 myRestartingMode = RM_None;
182 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
184 breakOperationSequence();
187 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
189 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
190 mySketchMgr->startSketch(theOperation);
192 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
193 mySketchMgr->startNestedSketch(theOperation);
196 if (myDocumentShapeFilter.IsNull())
197 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
198 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
201 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
203 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
204 mySketchMgr->stopSketch(theOperation);
206 else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
207 mySketchMgr->stopNestedSketch(theOperation);
209 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
212 bool PartSet_Module::canUndo() const
214 bool aCanUndo = false;
215 SessionPtr aMgr = ModelAPI_Session::get();
216 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
217 aCanUndo = !aMgr->isOperation();
218 if (!aCanUndo) // check the enable state additionally by sketch manager
219 aCanUndo = aMgr->canUndo();
224 bool PartSet_Module::canRedo() const
226 bool aCanRedo = false;
227 SessionPtr aMgr = ModelAPI_Session::get();
228 if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
229 aCanRedo = !aMgr->isOperation();
230 if (!aCanRedo) // check the enable state additionally by sketch manager
231 aCanRedo = aMgr->canRedo();
236 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
238 bool aCanDisplay = false;
239 if (!mySketchMgr->canDisplayObject(theObject))
241 CompositeFeaturePtr aSketchFeature = mySketchMgr->activeSketch();
242 if (aSketchFeature.get() != NULL) {
243 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
245 // MPV: the second and third conditions to avoid crash on exit for application
246 if (aFeature.get() != NULL && aFeature->data().get() && aFeature->data()->isValid()) {
247 if (aFeature == aSketchFeature) {
250 else if (aSketchFeature.get() && aSketchFeature->data().get() &&
251 aSketchFeature->data()->isValid()) {
252 for (int i = 0; i < aSketchFeature->numberOfSubs() && !aCanDisplay; i++) {
253 FeaturePtr aSubFeature = aSketchFeature->subFeature(i);
254 std::list<ResultPtr> aResults = aSubFeature->results();
255 std::list<ResultPtr>::const_iterator aIt;
256 for (aIt = aResults.begin(); aIt != aResults.end() && !aCanDisplay; ++aIt) {
257 if (theObject == (*aIt))
260 if (aSubFeature == theObject)
267 aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
272 void PartSet_Module::addViewerItems(QMenu* theMenu) const
274 if (!PartSet_SketcherMgr::isSketchOperation(myWorkshop->currentOperation()) &&
275 !isSketchFeatureOperationActive())
277 ModuleBase_ISelection* aSelection = myWorkshop->selection();
278 QObjectPtrList aObjects = aSelection->selectedPresentations();
279 if (aObjects.size() > 0) {
280 bool hasFeature = false;
281 foreach(ObjectPtr aObject, aObjects)
283 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
284 if (aFeature.get() != NULL) {
289 theMenu->addAction(action("DELETE_PARTSET_CMD"));
293 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
295 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
296 if (PartSet_SketcherMgr::isSketchOperation(theOperation) && (theOperation->isEditOperation())) {
297 // we have to manually activate the sketch label in edit mode
298 aPanel->activateWidget(aPanel->modelWidgets().first());
302 // Restart last operation type
303 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
304 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
305 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
306 // Initialise new line with first point equal to end of previous
307 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
309 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
310 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
311 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
313 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
314 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
315 aWgt->attributeID(), aPoint->x(), aPoint->y());
316 aPanel->activateNextWidget(aPnt2dWgt);
321 // Start editing constraint
322 if (theOperation->isEditOperation()) {
323 // TODO: #391 - to be removed
324 std::string aId = theOperation->id().toStdString();
325 if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
326 PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
327 // Find and activate widget for management of point for dimension line position
328 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
329 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
330 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
332 aPanel->activateWidget(aPntWgt);
342 void PartSet_Module::onSelectionChanged()
344 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
348 bool isSketcherOp = false;
349 // An edit operation is enable only if the current opeation is the sketch operation
350 if (mySketchMgr->activeSketch()) {
351 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
352 isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
355 // Editing of constraints can be done on selection
356 ModuleBase_ISelection* aSelect = myWorkshop->selection();
357 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
358 if (aSelected.size() == 1) {
359 ModuleBase_ViewerPrs aPrs = aSelected.first();
360 ObjectPtr aObject = aPrs.object();
361 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
363 std::string aId = aFeature->getKind();
364 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
365 (aId == SketchPlugin_ConstraintLength::ID()) ||
366 (aId == SketchPlugin_ConstraintDistance::ID())) {
367 editFeature(aFeature);
374 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
376 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
377 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
378 anOpMgr->onKeyReleased(theEvent);
381 void PartSet_Module::onEnterReleased()
383 myRestartingMode = RM_EmptyFeatureUsed;
386 void PartSet_Module::onOperationActivatedByPreselection()
388 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
389 if(aOperation && isSketchFeatureOperationActive()) {
390 // Set final definitions if they are necessary
391 //propertyPanelDefined(aOperation);
393 /// Commit sketcher operations automatically
394 aOperation->commit();
398 void PartSet_Module::onNoMoreWidgets()
400 if (isSketchFeatureOperationActive()) {
401 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
403 if (myRestartingMode != RM_Forbided)
404 myRestartingMode = RM_LastFeatureUsed;
405 aOperation->commit();
410 void PartSet_Module::onVertexSelected()
412 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
413 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
414 /// If last line finished on vertex the lines creation sequence has to be break
415 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
416 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
417 if (aWidgets.last() == aPanel->activeWidget()) {
418 myRestartingMode = RM_Forbided;
423 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
424 Config_WidgetAPI* theWidgetApi, std::string theParentId,
425 QList<ModuleBase_ModelWidget*>& theModelWidgets)
427 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
428 XGUI_Workshop* aWorkshop = aConnector->workshop();
429 if (theType == "sketch-start-label") {
430 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
431 aWgt->setWorkshop(aWorkshop);
432 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
433 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
434 theModelWidgets.append(aWgt);
435 return aWgt->getControl();
437 } else if (theType == "sketch-2dpoint_selector") {
438 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
439 aWgt->setWorkshop(aWorkshop);
440 aWgt->setSketch(mySketchMgr->activeSketch());
442 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
444 theModelWidgets.append(aWgt);
445 return aWgt->getControl();
447 } if (theType == "point2ddistance") {
448 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
449 aWgt->setWorkshop(aWorkshop);
450 aWgt->setSketch(mySketchMgr->activeSketch());
452 theModelWidgets.append(aWgt);
453 return aWgt->getControl();
455 } if (theType == "sketch_shape_selector") {
456 PartSet_WidgetShapeSelector* aWgt =
457 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
458 aWgt->setSketcher(mySketchMgr->activeSketch());
460 theModelWidgets.append(aWgt);
461 return aWgt->getControl();
463 } if (theType == "sketch_constraint_shape_selector") {
464 PartSet_WidgetConstraintShapeSelector* aWgt =
465 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
466 aWgt->setSketcher(mySketchMgr->activeSketch());
468 theModelWidgets.append(aWgt);
469 return aWgt->getControl();
475 bool PartSet_Module::isSketchFeatureOperationActive() const
477 bool isCurrentSketchOp = false;
478 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
480 FeaturePtr aFeature = aOperation->feature();
481 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
482 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
483 isCurrentSketchOp = aSPFeature.get() != NULL;
485 return isCurrentSketchOp;
488 void PartSet_Module::createActions()
490 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
491 addAction("DELETE_PARTSET_CMD", aAction);
494 QAction* PartSet_Module::action(const QString& theId) const
496 if (myActions.contains(theId))
497 return myActions[theId];
501 void PartSet_Module::addAction(const QString& theId, QAction* theAction)
503 if (myActions.contains(theId))
504 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
505 theAction->setData(theId);
506 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
507 myActions[theId] = theAction;
510 void PartSet_Module::onAction(bool isChecked)
512 QAction* aAction = static_cast<QAction*>(sender());
513 QString anId = aAction->data().toString();
515 if (anId == "DELETE_PARTSET_CMD") {
520 void PartSet_Module::deleteObjects()
522 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(myWorkshop->currentOperation());
523 if (!isSketchOp && !isSketchFeatureOperationActive())
526 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
527 XGUI_Workshop* aWorkshop = aConnector->workshop();
529 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
530 if (!isSketchOp && anOpMgr->canStopOperation()) {
531 ModuleBase_Operation* aCurrentOp = anOpMgr->currentOperation();
536 // sketch feature should be skipped, only sub-features can be removed
537 // when sketch operation is active
538 CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
540 ModuleBase_ISelection* aSel = aConnector->selection();
541 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
543 std::set<FeaturePtr> aRefFeatures;
544 foreach (ObjectPtr aObj, aSelectedObj)
546 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
548 // TODO: check for what there is this condition. It is placed here historicaly because
549 // ther is this condition during remove features.
551 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
552 if (aFeature.get() != NULL) {
553 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
558 QString aDescription = tr("Delete");
559 if (!aRefFeatures.empty()) {
560 QStringList aRefNames;
561 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
562 aLast = aRefFeatures.end();
563 for (; anIt != aLast; anIt++) {
564 FeaturePtr aFeature = (*anIt);
565 if (aFeature == aSketch)
567 aRefNames.append((*anIt)->name().c_str());
569 if (!aRefNames.empty()) {
570 QString aNames = aRefNames.join(", ");
571 aDescription += aNames.prepend(" ");
573 QMainWindow* aDesktop = aWorkshop->desktop();
574 QMessageBox::StandardButton aRes = QMessageBox::warning(
575 aDesktop, tr("Delete features"),
576 QString(tr("Selected features are used in the following features: %1.\
577 These features will be deleted also. Would you like to continue?")).arg(aNames),
578 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
579 if (aRes != QMessageBox::Yes)
584 SessionPtr aMgr = ModelAPI_Session::get();
585 aMgr->startOperation(aDescription.toStdString());
586 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
587 aLast = aRefFeatures.end();
588 for (; anIt != aLast; anIt++) {
589 FeaturePtr aRefFeature = (*anIt);
590 if (aRefFeature == aSketch)
592 aRefFeature->document()->removeFeature(aRefFeature);
595 foreach (ObjectPtr aObj, aSelectedObj)
597 DocumentPtr aDoc = aObj->document();
598 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
600 // if (aDoc == aMgr->activeDocument()) {
604 //FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
605 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
606 if (aFeature.get() != NULL) {
607 aDoc->removeFeature(aFeature);
611 aWorkshop->displayer()->updateViewer();
612 //myDisplayer->updateViewer();
613 aMgr->finishOperation();