1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationCreateFeature.h>
4 #include <PartSet_OperationEditFeature.h>
5 #include <PartSet_OperationEditConstraint.h>
6 #include <PartSet_OperationCreateConstraint.h>
7 #include <ModuleBase_Operation.h>
8 #include <ModuleBase_OperationDescription.h>
9 #include <ModuleBase_WidgetFactory.h>
10 #include <PartSet_Listener.h>
11 #include <PartSet_TestOCC.h>
12 #include <PartSet_Presentation.h>
14 #include <ModuleBase_Operation.h>
15 #include <ModelAPI_Object.h>
17 #include <XGUI_MainWindow.h>
18 #include <XGUI_Displayer.h>
19 #include <XGUI_Viewer.h>
20 #include <XGUI_Workshop.h>
21 #include <XGUI_OperationMgr.h>
22 #include <XGUI_SelectionMgr.h>
23 #include <XGUI_ViewPort.h>
24 #include <XGUI_ActionsMgr.h>
25 #include <XGUI_ViewerProxy.h>
26 #include <XGUI_ContextMenuMgr.h>
27 #include <XGUI_PropertyPanel.h>
28 #include <XGUI_ModuleConnector.h>
29 #include <XGUI_Tools.h>
31 #include <SketchPlugin_Line.h>
33 #include <Config_PointerMessage.h>
34 #include <Config_ModuleReader.h>
35 #include <Config_WidgetReader.h>
36 #include <Events_Loop.h>
37 #include <Events_Message.h>
38 #include <Events_Error.h>
40 #include <GeomAPI_Shape.h>
42 #include <AIS_ListOfInteractive.hxx>
43 //#include <AIS_DimensionSelectionMode.hxx>
46 #include <QMouseEvent>
53 /*!Create and return new instance of XGUI_Module*/
54 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
56 return new PartSet_Module(theWshop);
59 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
61 myWorkshop = theWshop;
62 myListener = new PartSet_Listener(this);
64 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
66 connect(anOperationMgr, SIGNAL(operationStarted()),
67 this, SLOT(onOperationStarted()));
69 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
70 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
72 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
73 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
74 this, SLOT(onContextMenuCommand(const QString&, bool)));
76 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
77 this, SLOT(onMousePressed(QMouseEvent*)));
78 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
79 this, SLOT(onMouseReleased(QMouseEvent*)));
80 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
81 this, SLOT(onMouseMoved(QMouseEvent*)));
82 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
83 this, SLOT(onKeyRelease(QKeyEvent*)));
84 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
85 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
89 PartSet_Module::~PartSet_Module()
93 XGUI_Workshop* PartSet_Module::workshop() const
98 void PartSet_Module::createFeatures()
100 Config_ModuleReader aXMLReader = Config_ModuleReader();
101 aXMLReader.readAll();
102 myFeaturesInFiles = aXMLReader.featuresInFiles();
105 void PartSet_Module::featureCreated(QAction* theFeature)
107 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
110 QStringList PartSet_Module::nestedFeatures(QString)
112 return QStringList();
115 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
117 return myFeaturesInFiles[theFeatureId];
123 void PartSet_Module::onFeatureTriggered()
125 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
126 // myWorkshop->viewer()->activeView());
128 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
129 // myWorkshop->viewer()->activeView());
130 QAction* aCmd = dynamic_cast<QAction*>(sender());
131 //Do nothing on uncheck
132 if(aCmd->isCheckable() && !aCmd->isChecked())
134 launchOperation(aCmd->data().toString());
137 void PartSet_Module::launchOperation(const QString& theCmdId)
139 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
140 sendOperation(anOperation);
143 void PartSet_Module::onOperationStarted()
145 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
146 myWorkshop->operationMgr()->currentOperation());
148 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
149 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
150 aPropPanel, SLOT(onFocusActivated(const std::string&)));
154 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
158 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
160 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
161 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
162 aPropPanel, SLOT(onFocusActivated(const std::string&)));
166 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
168 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
169 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
170 editFeature(aFeatures.first());
174 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
176 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
177 myWorkshop->operationMgr()->currentOperation());
180 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
181 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
182 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
184 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
188 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
190 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
191 myWorkshop->operationMgr()->currentOperation());
194 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
195 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
196 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
198 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
202 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
204 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
205 myWorkshop->operationMgr()->currentOperation());
207 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
210 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
212 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
213 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
215 aPreviewOp->keyReleased(theEvent->key());
219 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
221 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
222 myWorkshop->operationMgr()->currentOperation());
225 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
226 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
227 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
228 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
233 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
235 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
236 myWorkshop->actionsMgr()->update();
238 //PartSet_TestOCC::testSelection(myWorkshop);
241 void PartSet_Module::onFitAllView()
243 myWorkshop->viewer()->fitAll();
246 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
248 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
249 theFeature ? theFeature->getKind() : "");
250 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
253 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
254 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
255 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected(TopAbs_VERTEX);
256 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted(TopAbs_VERTEX);
257 aPreviewOp->init(theFeature, aSelected, aHighlighted);
259 anOperation->setEditingFeature(theFeature);
261 sendOperation(anOperation);
262 myWorkshop->actionsMgr()->updateCheckState();
265 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
267 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
268 aViewer->enableMultiselection(theEnabled);
271 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
273 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
275 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
277 for (; anIt != aLast; anIt++) {
278 activateFeature((*anIt).feature(), false);
281 aDisplayer->stopSelection(theFeatures, isStop, false);
283 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
284 aViewer->enableSelection(!isStop);
286 aDisplayer->updateViewer();
289 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
291 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
292 aDisplayer->setSelected(theFeatures, false);
293 aDisplayer->updateViewer();
296 void PartSet_Module::onCloseLocalContext()
298 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
299 aDisplayer->closeLocalContexts();
302 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
304 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
305 visualizePreview(theFeature, isDisplay, false);
307 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
309 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
311 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
312 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
313 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
315 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
316 anIt = aList.begin(), aLast = aList.end();
317 for (; anIt != aLast; anIt++) {
318 FeaturePtr aFeature = (*anIt).first;
319 visualizePreview(aFeature, false, false);
321 aDisplayer->updateViewer();
325 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
326 theMode == PartSet_OperationSketchBase::FM_Deactivation)
327 activateFeature(theFeature, true);
330 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
331 const std::string& theFeatureKind)
333 // create the operation
334 ModuleBase_Operation* anOperation = 0;
335 if (theCmdId == PartSet_OperationSketch::Type()) {
336 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
339 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
341 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
343 aSketch = aPrevOp->sketch();
344 if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
345 anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
346 else if (theCmdId == PartSet_OperationEditFeature::Type())
347 anOperation = new PartSet_OperationEditFeature(theCmdId.c_str(), this, aSketch);
348 else if (PartSet_OperationCreateConstraint::canProcessKind(theCmdId))
349 anOperation = new PartSet_OperationCreateConstraint(theCmdId.c_str(), this, aSketch);
350 else if (theCmdId == PartSet_OperationEditConstraint::Type())
351 anOperation = new PartSet_OperationEditConstraint(theCmdId.c_str(), theFeatureKind, this, aSketch);
355 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
358 // set operation description and list of widgets corresponding to the feature xml definition
359 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
361 std::string aPluginFileName = featureFile(aFeatureKind);
362 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
363 aWdgReader.readAll();
364 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
365 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
367 QString aXmlRepr = QString::fromStdString(aXmlCfg);
368 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
369 myWorkshop->moduleConnector());
370 QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
371 qDeleteAll(aContent->children());
372 aFactory.createWidget(aContent);
374 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
376 anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
378 // connect the operation
379 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
381 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
382 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
383 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
384 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
385 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
386 this, SLOT(onMultiSelectionEnabled(bool)));
388 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
389 this, SLOT(onMultiSelectionEnabled(bool)));
390 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
391 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
392 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
393 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
395 connect(aPreviewOp, SIGNAL(closeLocalContext()),
396 this, SLOT(onCloseLocalContext()));
398 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
400 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
401 this, SLOT(onPlaneSelected(double, double, double)));
402 connect(aSketchOp, SIGNAL(fitAllView()),
403 this, SLOT(onFitAllView()));
410 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
412 //TODO(sbh): Implement static method to extract event id [SEID]
413 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
414 Config_PointerMessage aMessage(aModuleEvent, this);
415 aMessage.setPointer(theOperation);
416 Events_Loop::loop()->send(aMessage);
419 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
420 const bool isUpdateViewer)
422 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
426 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
430 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
432 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
433 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
434 theFeature, aPreviewOp->sketch(),
435 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
436 aDisplayer->getAISObject(theFeature));
438 aDisplayer->redisplay(theFeature, anAIS, false);
441 aDisplayer->erase(theFeature, false);
444 aDisplayer->updateViewer();
447 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
449 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
450 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
452 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
453 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
458 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
460 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
464 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
468 FeaturePtr aFeature = aPreviewOp->feature();
469 if (!aFeature || aFeature->getKind() != theCmdId)
472 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
473 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
474 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
476 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
477 anIt = aList.begin(), aLast = aList.end();
478 for (; anIt != aLast; anIt++) {
479 FeaturePtr aFeature = (*anIt).first;
480 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
481 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
482 aFeature, aPreviewOp->sketch(),
483 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
484 aDisplayer->getAISObject(aFeature));
486 aDisplayer->redisplay(aFeature, anAIS, false);
487 aDisplayer->activateInLocalContext(aFeature, aModes, false);
489 aDisplayer->updateViewer();
492 void PartSet_Module::editFeature(FeaturePtr theFeature)
497 // if (theFeature->getKind() == SKETCH_KIND) {
498 FeaturePtr aFeature = theFeature;
499 if (XGUI_Tools::isModelObject(aFeature)) {
500 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
501 aFeature = aObject->featureRef();
505 onLaunchOperation(aFeature->getKind(), aFeature);
506 updateCurrentPreview(aFeature->getKind());