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);
215 void PartSet_Module::addViewerItems(QMenu* theMenu) const
217 if (!isSketchOperationActive() && !isSketchFeatureOperationActive())
219 ModuleBase_ISelection* aSelection = myWorkshop->selection();
220 QObjectPtrList aObjects = aSelection->selectedPresentations();
221 if (aObjects.size() > 0) {
222 bool hasFeature = false;
223 foreach(ObjectPtr aObject, aObjects)
225 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
226 if (aFeature.get() != NULL) {
231 theMenu->addAction(action("DELETE_PARTSET_CMD"));
235 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
237 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
238 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
239 (theOperation->isEditOperation())) {
240 // we have to manually activate the sketch label in edit mode
241 aPanel->activateWidget(aPanel->modelWidgets().first());
245 // Restart last operation type
246 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
247 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
248 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
249 // Initialise new line with first point equal to end of previous
250 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
252 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
253 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
254 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
256 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
257 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
258 aWgt->attributeID(), aPoint->x(), aPoint->y());
259 aPanel->activateNextWidget(aPnt2dWgt);
264 // Start editing constraint
265 if (theOperation->isEditOperation()) {
266 // TODO: #391 - to be removed
267 std::string aId = theOperation->id().toStdString();
268 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
269 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
270 (aId == SketchPlugin_ConstraintLength::ID()) ||
271 (aId == SketchPlugin_ConstraintDistance::ID())) {
272 // Find and activate widget for management of point for dimension line position
273 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
274 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
275 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
277 aPanel->activateWidget(aPntWgt);
288 void PartSet_Module::onSelectionChanged()
290 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
294 bool isSketcherOp = false;
295 // An edit operation is enable only if the current opeation is the sketch operation
296 if (mySketchMgr->activeSketch()) {
297 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
298 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
301 // Editing of constraints can be done on selection
302 ModuleBase_ISelection* aSelect = myWorkshop->selection();
303 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
304 if (aSelected.size() == 1) {
305 ModuleBase_ViewerPrs aPrs = aSelected.first();
306 ObjectPtr aObject = aPrs.object();
307 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
309 std::string aId = aFeature->getKind();
310 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
311 (aId == SketchPlugin_ConstraintLength::ID()) ||
312 (aId == SketchPlugin_ConstraintDistance::ID())) {
313 editFeature(aFeature);
320 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
322 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
323 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
324 anOpMgr->onKeyReleased(theEvent);
327 void PartSet_Module::onEnterReleased()
329 myRestartingMode = RM_EmptyFeatureUsed;
332 void PartSet_Module::onOperationActivatedByPreselection()
334 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
335 if(aOperation && isSketchFeatureOperationActive()) {
336 // Set final definitions if they are necessary
337 //propertyPanelDefined(aOperation);
339 /// Commit sketcher operations automatically
340 aOperation->commit();
344 void PartSet_Module::onNoMoreWidgets()
346 if (isSketchFeatureOperationActive()) {
347 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
349 if (myRestartingMode != RM_Forbided)
350 myRestartingMode = RM_LastFeatureUsed;
351 aOperation->commit();
356 void PartSet_Module::onVertexSelected()
358 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
359 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
360 /// If last line finished on vertex the lines creation sequence has to be break
361 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
362 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
363 if (aWidgets.last() == aPanel->activeWidget()) {
364 myRestartingMode = RM_Forbided;
369 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
370 Config_WidgetAPI* theWidgetApi, std::string theParentId,
371 QList<ModuleBase_ModelWidget*>& theModelWidgets)
373 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
374 XGUI_Workshop* aWorkshop = aConnector->workshop();
375 if (theType == "sketch-start-label") {
376 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
377 aWgt->setWorkshop(aWorkshop);
378 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
379 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
380 theModelWidgets.append(aWgt);
381 return aWgt->getControl();
383 } else if (theType == "sketch-2dpoint_selector") {
384 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
385 aWgt->setWorkshop(aWorkshop);
386 aWgt->setSketch(mySketchMgr->activeSketch());
388 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
390 theModelWidgets.append(aWgt);
391 return aWgt->getControl();
393 } if (theType == "point2ddistance") {
394 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
395 aWgt->setWorkshop(aWorkshop);
396 aWgt->setSketch(mySketchMgr->activeSketch());
398 theModelWidgets.append(aWgt);
399 return aWgt->getControl();
401 } if (theType == "sketch_shape_selector") {
402 PartSet_WidgetShapeSelector* aWgt =
403 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
404 aWgt->setSketcher(mySketchMgr->activeSketch());
406 theModelWidgets.append(aWgt);
407 return aWgt->getControl();
409 } if (theType == "sketch_constraint_shape_selector") {
410 PartSet_WidgetConstraintShapeSelector* aWgt =
411 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
412 aWgt->setSketcher(mySketchMgr->activeSketch());
414 theModelWidgets.append(aWgt);
415 return aWgt->getControl();
421 bool PartSet_Module::isSketchOperationActive() const
423 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
425 bool isSketchOp = aOperation && aOperation->id().toStdString() == SketchPlugin_Sketch::ID();
429 bool PartSet_Module::isSketchFeatureOperationActive() const
431 bool isCurrentSketchOp = false;
432 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
434 FeaturePtr aFeature = aOperation->feature();
435 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
436 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
437 isCurrentSketchOp = aSPFeature.get() != NULL;
439 return isCurrentSketchOp;
442 void PartSet_Module::createActions()
444 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
445 addAction("DELETE_PARTSET_CMD", aAction);
448 QAction* PartSet_Module::action(const QString& theId) const
450 if (myActions.contains(theId))
451 return myActions[theId];
455 void PartSet_Module::addAction(const QString& theId, QAction* theAction)
457 if (myActions.contains(theId))
458 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
459 theAction->setData(theId);
460 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
461 myActions[theId] = theAction;
464 void PartSet_Module::onAction(bool isChecked)
466 QAction* aAction = static_cast<QAction*>(sender());
467 QString anId = aAction->data().toString();
469 if (anId == "DELETE_PARTSET_CMD") {
474 void PartSet_Module::deleteObjects()
476 bool isSketchOp = isSketchOperationActive();
477 if (!isSketchOp && !isSketchFeatureOperationActive())
480 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
481 XGUI_Workshop* aWorkshop = aConnector->workshop();
483 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
484 if (!isSketchOp && anOpMgr->canStopOperation()) {
485 ModuleBase_Operation* aCurrentOp = anOpMgr->currentOperation();
490 // sketch feature should be skipped, only sub-features can be removed
491 // when sketch operation is active
492 CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
494 ModuleBase_ISelection* aSel = aConnector->selection();
495 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
497 std::set<FeaturePtr> aRefFeatures;
498 foreach (ObjectPtr aObj, aSelectedObj)
500 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
502 // TODO: check for what there is this condition. It is placed here historicaly because
503 // ther is this condition during remove features.
505 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
506 if (aFeature.get() != NULL) {
507 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
512 if (!aRefFeatures.empty()) {
513 QStringList aRefNames;
514 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
515 aLast = aRefFeatures.end();
516 for (; anIt != aLast; anIt++) {
517 FeaturePtr aFeature = (*anIt);
518 if (aFeature == aSketch)
520 aRefNames.append((*anIt)->name().c_str());
522 if (!aRefNames.empty()) {
523 QString aNames = aRefNames.join(", ");
525 QMainWindow* aDesktop = aWorkshop->desktop();
526 QMessageBox::StandardButton aRes = QMessageBox::warning(
527 aDesktop, tr("Delete features"),
528 QString(tr("Selected features are used in the following features: %1.\
529 These features will be deleted also. Would you like to continue?")).arg(aNames),
530 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
531 if (aRes != QMessageBox::Yes)
536 SessionPtr aMgr = ModelAPI_Session::get();
537 aMgr->startOperation();
538 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
539 aLast = aRefFeatures.end();
540 for (; anIt != aLast; anIt++) {
541 FeaturePtr aRefFeature = (*anIt);
542 if (aRefFeature == aSketch)
544 aRefFeature->document()->removeFeature(aRefFeature);
547 foreach (ObjectPtr aObj, aSelectedObj)
549 DocumentPtr aDoc = aObj->document();
550 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
552 // if (aDoc == aMgr->activeDocument()) {
556 //FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
557 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
558 if (aFeature.get() != NULL) {
559 aDoc->removeFeature(aFeature);
563 aWorkshop->displayer()->updateViewer();
564 //myDisplayer->updateViewer();
565 aMgr->finishOperation();