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>
54 #include <Config_PropManager.h>
56 #include <StdSelect_TypeOfFace.hxx>
57 #include <TopoDS_Vertex.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <BRep_Tool.hxx>
63 #include <QMouseEvent>
66 #include <QApplication>
67 #include <QMessageBox>
68 #include <QMainWindow>
70 #include <GeomAlgoAPI_FaceBuilder.h>
71 #include <GeomDataAPI_Dir.h>
77 /*!Create and return new instance of XGUI_Module*/
78 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
80 return new PartSet_Module(theWshop);
83 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
84 : ModuleBase_IModule(theWshop),
85 myRestartingMode(RM_None)
87 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
88 mySketchMgr = new PartSet_SketcherMgr(this);
90 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
91 XGUI_Workshop* aWorkshop = aConnector->workshop();
93 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
94 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
95 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
96 this, SLOT(onOperationActivatedByPreselection()));
98 ModuleBase_IViewer* aViewer = theWshop->viewer();
99 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
100 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
105 PartSet_Module::~PartSet_Module()
107 if (!myDocumentShapeFilter.IsNull())
108 myDocumentShapeFilter.Nullify();
111 void PartSet_Module::registerValidators()
113 //Registering of validators
114 SessionPtr aMgr = ModelAPI_Session::get();
115 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
116 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
117 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
118 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
119 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
120 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
121 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
122 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
123 aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
126 void PartSet_Module::registerFilters()
128 //Registering of selection filters
129 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
130 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
132 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
133 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
134 aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
135 Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
136 aFactory->registerFilter("NoConstructionSubShapesFilter",
137 new ModuleBase_FilterCustom(aSelectFilter));
140 void PartSet_Module::registerProperties()
142 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
144 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
145 Config_Prop::Integer, SKETCH_WIDTH);
148 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
150 if (theOperation->isEditOperation())
152 // the selection is cleared after commit the create operation
153 // in order to do not use the same selected objects in the restarted operation
154 // for common behaviour, the selection is cleared even if the operation is not restarted
155 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
156 if (!aContext.IsNull())
157 aContext->ClearSelected();
159 /// Restart sketcher operations automatically
160 FeaturePtr aFeature = theOperation->feature();
161 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
162 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
163 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
164 myRestartingMode == RM_EmptyFeatureUsed)) {
165 myLastOperationId = theOperation->id();
166 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
168 launchOperation(myLastOperationId);
170 breakOperationSequence();
173 void PartSet_Module::breakOperationSequence()
175 myLastOperationId = "";
176 myLastFeature = FeaturePtr();
177 myRestartingMode = RM_None;
180 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
182 breakOperationSequence();
185 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
187 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
188 mySketchMgr->startSketch(theOperation);
190 if (myDocumentShapeFilter.IsNull())
191 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
192 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
195 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
197 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
198 mySketchMgr->stopSketch(theOperation);
200 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
203 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
205 bool aCanDisplay = false;
206 if (mySketchMgr->activeSketch()) {
207 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
209 if (aFeature.get() != NULL) {
210 if (aFeature == mySketchMgr->activeSketch()) {
214 aCanDisplay = mySketchMgr->sketchOperationIdList().contains(aFeature->getKind().c_str());
219 aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
224 void PartSet_Module::addViewerItems(QMenu* theMenu) const
226 if (!isSketchOperationActive() && !isSketchFeatureOperationActive())
228 ModuleBase_ISelection* aSelection = myWorkshop->selection();
229 QObjectPtrList aObjects = aSelection->selectedPresentations();
230 if (aObjects.size() > 0) {
231 bool hasFeature = false;
232 foreach(ObjectPtr aObject, aObjects)
234 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
235 if (aFeature.get() != NULL) {
240 theMenu->addAction(action("DELETE_PARTSET_CMD"));
244 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
246 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
247 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
248 (theOperation->isEditOperation())) {
249 // we have to manually activate the sketch label in edit mode
250 aPanel->activateWidget(aPanel->modelWidgets().first());
254 // Restart last operation type
255 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
256 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
257 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
258 // Initialise new line with first point equal to end of previous
259 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
261 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
262 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
263 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
265 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
266 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
267 aWgt->attributeID(), aPoint->x(), aPoint->y());
268 aPanel->activateNextWidget(aPnt2dWgt);
273 // Start editing constraint
274 if (theOperation->isEditOperation()) {
275 // TODO: #391 - to be removed
276 std::string aId = theOperation->id().toStdString();
277 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
278 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
279 (aId == SketchPlugin_ConstraintLength::ID()) ||
280 (aId == SketchPlugin_ConstraintDistance::ID())) {
281 // Find and activate widget for management of point for dimension line position
282 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
283 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
284 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
286 aPanel->activateWidget(aPntWgt);
297 void PartSet_Module::onSelectionChanged()
299 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
303 bool isSketcherOp = false;
304 // An edit operation is enable only if the current opeation is the sketch operation
305 if (mySketchMgr->activeSketch()) {
306 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
307 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
310 // Editing of constraints can be done on selection
311 ModuleBase_ISelection* aSelect = myWorkshop->selection();
312 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
313 if (aSelected.size() == 1) {
314 ModuleBase_ViewerPrs aPrs = aSelected.first();
315 ObjectPtr aObject = aPrs.object();
316 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
318 std::string aId = aFeature->getKind();
319 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
320 (aId == SketchPlugin_ConstraintLength::ID()) ||
321 (aId == SketchPlugin_ConstraintDistance::ID())) {
322 editFeature(aFeature);
329 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
331 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
332 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
333 anOpMgr->onKeyReleased(theEvent);
336 void PartSet_Module::onEnterReleased()
338 myRestartingMode = RM_EmptyFeatureUsed;
341 void PartSet_Module::onOperationActivatedByPreselection()
343 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
344 if(aOperation && isSketchFeatureOperationActive()) {
345 // Set final definitions if they are necessary
346 //propertyPanelDefined(aOperation);
348 /// Commit sketcher operations automatically
349 aOperation->commit();
353 void PartSet_Module::onNoMoreWidgets()
355 if (isSketchFeatureOperationActive()) {
356 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
358 if (myRestartingMode != RM_Forbided)
359 myRestartingMode = RM_LastFeatureUsed;
360 aOperation->commit();
365 void PartSet_Module::onVertexSelected()
367 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
368 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
369 /// If last line finished on vertex the lines creation sequence has to be break
370 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
371 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
372 if (aWidgets.last() == aPanel->activeWidget()) {
373 myRestartingMode = RM_Forbided;
378 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
379 Config_WidgetAPI* theWidgetApi, std::string theParentId,
380 QList<ModuleBase_ModelWidget*>& theModelWidgets)
382 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
383 XGUI_Workshop* aWorkshop = aConnector->workshop();
384 if (theType == "sketch-start-label") {
385 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
386 aWgt->setWorkshop(aWorkshop);
387 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
388 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
389 theModelWidgets.append(aWgt);
390 return aWgt->getControl();
392 } else if (theType == "sketch-2dpoint_selector") {
393 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
394 aWgt->setWorkshop(aWorkshop);
395 aWgt->setSketch(mySketchMgr->activeSketch());
397 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
399 theModelWidgets.append(aWgt);
400 return aWgt->getControl();
402 } if (theType == "point2ddistance") {
403 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
404 aWgt->setWorkshop(aWorkshop);
405 aWgt->setSketch(mySketchMgr->activeSketch());
407 theModelWidgets.append(aWgt);
408 return aWgt->getControl();
410 } if (theType == "sketch_shape_selector") {
411 PartSet_WidgetShapeSelector* aWgt =
412 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
413 aWgt->setSketcher(mySketchMgr->activeSketch());
415 theModelWidgets.append(aWgt);
416 return aWgt->getControl();
418 } if (theType == "sketch_constraint_shape_selector") {
419 PartSet_WidgetConstraintShapeSelector* aWgt =
420 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
421 aWgt->setSketcher(mySketchMgr->activeSketch());
423 theModelWidgets.append(aWgt);
424 return aWgt->getControl();
430 bool PartSet_Module::isSketchOperationActive() const
432 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
434 bool isSketchOp = aOperation && aOperation->id().toStdString() == SketchPlugin_Sketch::ID();
438 bool PartSet_Module::isSketchFeatureOperationActive() const
440 bool isCurrentSketchOp = false;
441 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
443 FeaturePtr aFeature = aOperation->feature();
444 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
445 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
446 isCurrentSketchOp = aSPFeature.get() != NULL;
448 return isCurrentSketchOp;
451 void PartSet_Module::createActions()
453 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
454 addAction("DELETE_PARTSET_CMD", aAction);
457 QAction* PartSet_Module::action(const QString& theId) const
459 if (myActions.contains(theId))
460 return myActions[theId];
464 void PartSet_Module::addAction(const QString& theId, QAction* theAction)
466 if (myActions.contains(theId))
467 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
468 theAction->setData(theId);
469 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
470 myActions[theId] = theAction;
473 void PartSet_Module::onAction(bool isChecked)
475 QAction* aAction = static_cast<QAction*>(sender());
476 QString anId = aAction->data().toString();
478 if (anId == "DELETE_PARTSET_CMD") {
483 void PartSet_Module::deleteObjects()
485 bool isSketchOp = isSketchOperationActive();
486 if (!isSketchOp && !isSketchFeatureOperationActive())
489 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
490 XGUI_Workshop* aWorkshop = aConnector->workshop();
492 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
493 if (!isSketchOp && anOpMgr->canStopOperation()) {
494 ModuleBase_Operation* aCurrentOp = anOpMgr->currentOperation();
499 // sketch feature should be skipped, only sub-features can be removed
500 // when sketch operation is active
501 CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
503 ModuleBase_ISelection* aSel = aConnector->selection();
504 QObjectPtrList aSelectedObj = aSel->selectedPresentations();
506 std::set<FeaturePtr> aRefFeatures;
507 foreach (ObjectPtr aObj, aSelectedObj)
509 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
511 // TODO: check for what there is this condition. It is placed here historicaly because
512 // ther is this condition during remove features.
514 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
515 if (aFeature.get() != NULL) {
516 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
521 if (!aRefFeatures.empty()) {
522 QStringList aRefNames;
523 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
524 aLast = aRefFeatures.end();
525 for (; anIt != aLast; anIt++) {
526 FeaturePtr aFeature = (*anIt);
527 if (aFeature == aSketch)
529 aRefNames.append((*anIt)->name().c_str());
531 if (!aRefNames.empty()) {
532 QString aNames = aRefNames.join(", ");
534 QMainWindow* aDesktop = aWorkshop->desktop();
535 QMessageBox::StandardButton aRes = QMessageBox::warning(
536 aDesktop, tr("Delete features"),
537 QString(tr("Selected features are used in the following features: %1.\
538 These features will be deleted also. Would you like to continue?")).arg(aNames),
539 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
540 if (aRes != QMessageBox::Yes)
545 SessionPtr aMgr = ModelAPI_Session::get();
546 aMgr->startOperation("DeletePartSet");
547 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
548 aLast = aRefFeatures.end();
549 for (; anIt != aLast; anIt++) {
550 FeaturePtr aRefFeature = (*anIt);
551 if (aRefFeature == aSketch)
553 aRefFeature->document()->removeFeature(aRefFeature);
556 foreach (ObjectPtr aObj, aSelectedObj)
558 DocumentPtr aDoc = aObj->document();
559 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
561 // if (aDoc == aMgr->activeDocument()) {
565 //FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
566 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
567 if (aFeature.get() != NULL) {
568 aDoc->removeFeature(aFeature);
572 aWorkshop->displayer()->updateViewer();
573 //myDisplayer->updateViewer();
574 aMgr->finishOperation();