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 <PartSet_OperationFeatureCreate.h>
8 #include <ModuleBase_Operation.h>
9 #include <ModuleBase_OperationDescription.h>
10 #include <ModuleBase_WidgetFactory.h>
11 #include <PartSet_Listener.h>
12 #include <PartSet_TestOCC.h>
13 #include <PartSet_Presentation.h>
15 #include <ModuleBase_Operation.h>
16 #include <ModelAPI_Object.h>
18 #include <XGUI_MainWindow.h>
19 #include <XGUI_Displayer.h>
20 #include <XGUI_Viewer.h>
21 #include <XGUI_Workshop.h>
22 #include <XGUI_OperationMgr.h>
23 #include <XGUI_SelectionMgr.h>
24 #include <XGUI_ViewPort.h>
25 #include <XGUI_ActionsMgr.h>
26 #include <XGUI_ViewerProxy.h>
27 #include <XGUI_ContextMenuMgr.h>
28 #include <XGUI_PropertyPanel.h>
29 #include <XGUI_ModuleConnector.h>
30 #include <XGUI_Tools.h>
32 #include <SketchPlugin_Line.h>
34 #include <Config_PointerMessage.h>
35 #include <Config_ModuleReader.h>
36 #include <Config_WidgetReader.h>
37 #include <Events_Loop.h>
38 #include <Events_Message.h>
39 #include <Events_Error.h>
41 #include <GeomAPI_Shape.h>
43 #include <AIS_ListOfInteractive.hxx>
44 //#include <AIS_DimensionSelectionMode.hxx>
47 #include <QMouseEvent>
54 /*!Create and return new instance of XGUI_Module*/
55 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
57 return new PartSet_Module(theWshop);
60 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
62 myWorkshop = theWshop;
63 myListener = new PartSet_Listener(this);
65 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
67 connect(anOperationMgr, SIGNAL(operationStarted()),
68 this, SLOT(onOperationStarted()));
70 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
71 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
73 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
74 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
75 this, SLOT(onContextMenuCommand(const QString&, bool)));
77 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
78 this, SLOT(onMousePressed(QMouseEvent*)));
79 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
80 this, SLOT(onMouseReleased(QMouseEvent*)));
81 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
82 this, SLOT(onMouseMoved(QMouseEvent*)));
83 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
84 this, SLOT(onKeyRelease(QKeyEvent*)));
85 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
86 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
90 PartSet_Module::~PartSet_Module()
94 XGUI_Workshop* PartSet_Module::workshop() const
99 void PartSet_Module::createFeatures()
101 Config_ModuleReader aXMLReader = Config_ModuleReader();
102 aXMLReader.readAll();
103 myFeaturesInFiles = aXMLReader.featuresInFiles();
106 void PartSet_Module::featureCreated(QAction* theFeature)
108 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
111 QStringList PartSet_Module::nestedFeatures(QString)
113 return QStringList();
116 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
118 return myFeaturesInFiles[theFeatureId];
124 void PartSet_Module::onFeatureTriggered()
126 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
127 // myWorkshop->viewer()->activeView());
129 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
130 // myWorkshop->viewer()->activeView());
131 QAction* aCmd = dynamic_cast<QAction*>(sender());
132 //Do nothing on uncheck
133 if(aCmd->isCheckable() && !aCmd->isChecked())
135 launchOperation(aCmd->data().toString());
138 void PartSet_Module::launchOperation(const QString& theCmdId)
140 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
141 sendOperation(anOperation);
144 void PartSet_Module::onOperationStarted()
146 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
147 myWorkshop->operationMgr()->currentOperation());
149 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
150 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
151 aPropPanel, SLOT(onFocusActivated(const std::string&)));
155 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
159 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
161 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
162 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
163 aPropPanel, SLOT(onFocusActivated(const std::string&)));
167 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
169 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
170 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
171 editFeature(aFeatures.first());
175 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
177 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
178 myWorkshop->operationMgr()->currentOperation());
181 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
182 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
183 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
185 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
189 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
191 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
192 myWorkshop->operationMgr()->currentOperation());
195 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
196 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
197 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
199 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
203 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
205 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
206 myWorkshop->operationMgr()->currentOperation());
208 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
211 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
213 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
214 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
216 aPreviewOp->keyReleased(theEvent->key());
220 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
222 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
223 myWorkshop->operationMgr()->currentOperation());
226 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
227 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
228 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
229 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
234 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
236 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
237 myWorkshop->actionsMgr()->update();
239 //PartSet_TestOCC::testSelection(myWorkshop);
242 void PartSet_Module::onFitAllView()
244 myWorkshop->viewer()->fitAll();
247 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
249 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
250 theFeature ? theFeature->getKind() : "");
251 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
254 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
255 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
256 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected(TopAbs_VERTEX);
257 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted(TopAbs_VERTEX);
258 aPreviewOp->init(theFeature, aSelected, aHighlighted);
260 anOperation->setEditingFeature(theFeature);
262 sendOperation(anOperation);
263 myWorkshop->actionsMgr()->updateCheckState();
266 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
268 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
269 aViewer->enableMultiselection(theEnabled);
272 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
274 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
276 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
278 for (; anIt != aLast; anIt++) {
279 activateFeature((*anIt).feature(), false);
282 aDisplayer->stopSelection(theFeatures, isStop, false);
284 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
285 aViewer->enableSelection(!isStop);
287 aDisplayer->updateViewer();
290 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
292 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
293 aDisplayer->setSelected(theFeatures, false);
294 aDisplayer->updateViewer();
297 void PartSet_Module::onCloseLocalContext()
299 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
300 aDisplayer->closeLocalContexts();
303 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
305 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
306 visualizePreview(theFeature, isDisplay, false);
308 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
310 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
312 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
313 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
314 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
316 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
317 anIt = aList.begin(), aLast = aList.end();
318 for (; anIt != aLast; anIt++) {
319 FeaturePtr aFeature = (*anIt).first;
320 visualizePreview(aFeature, false, false);
322 aDisplayer->updateViewer();
326 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
327 theMode == PartSet_OperationSketchBase::FM_Deactivation)
328 activateFeature(theFeature, true);
331 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
332 const std::string& theFeatureKind)
334 // create the operation
335 ModuleBase_Operation* anOperation = 0;
336 if (theCmdId == PartSet_OperationSketch::Type()) {
337 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
340 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
342 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
344 aSketch = aPrevOp->sketch();
345 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
346 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
347 else if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
348 anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
349 else if (theCmdId == PartSet_OperationEditFeature::Type())
350 anOperation = new PartSet_OperationEditFeature(theCmdId.c_str(), this, aSketch);
351 else if (PartSet_OperationCreateConstraint::canProcessKind(theCmdId))
352 anOperation = new PartSet_OperationCreateConstraint(theCmdId.c_str(), this, aSketch);
353 else if (theCmdId == PartSet_OperationEditConstraint::Type())
354 anOperation = new PartSet_OperationEditConstraint(theCmdId.c_str(), theFeatureKind, this, aSketch);
358 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
361 // set operation description and list of widgets corresponding to the feature xml definition
362 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
364 std::string aPluginFileName = featureFile(aFeatureKind);
365 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
366 aWdgReader.readAll();
367 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
368 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
370 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
371 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
372 // myWorkshop->moduleConnector());
373 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
374 //qDeleteAll(aContent->children());
375 //aFactory.createWidget(aContent);
377 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
378 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
380 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
382 // connect the operation
383 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
385 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
386 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
387 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
388 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
389 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
390 this, SLOT(onMultiSelectionEnabled(bool)));
392 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
393 this, SLOT(onMultiSelectionEnabled(bool)));
394 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
395 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
396 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
397 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
399 connect(aPreviewOp, SIGNAL(closeLocalContext()),
400 this, SLOT(onCloseLocalContext()));
402 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
404 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
405 this, SLOT(onPlaneSelected(double, double, double)));
406 connect(aSketchOp, SIGNAL(fitAllView()),
407 this, SLOT(onFitAllView()));
414 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
416 //TODO(sbh): Implement static method to extract event id [SEID]
417 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
418 Config_PointerMessage aMessage(aModuleEvent, this);
419 aMessage.setPointer(theOperation);
420 Events_Loop::loop()->send(aMessage);
423 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
424 const bool isUpdateViewer)
426 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
430 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
434 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
436 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
437 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
438 theFeature, aPreviewOp->sketch(),
439 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
440 aDisplayer->getAISObject(theFeature));
442 aDisplayer->redisplay(theFeature, anAIS, false);
445 aDisplayer->erase(theFeature, false);
448 aDisplayer->updateViewer();
451 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
453 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
454 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
456 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
457 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
462 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
464 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
468 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
472 FeaturePtr aFeature = aPreviewOp->feature();
473 if (!aFeature || aFeature->getKind() != theCmdId)
476 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
477 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
478 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
480 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
481 anIt = aList.begin(), aLast = aList.end();
482 for (; anIt != aLast; anIt++) {
483 FeaturePtr aFeature = (*anIt).first;
484 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
485 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
486 aFeature, aPreviewOp->sketch(),
487 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
488 aDisplayer->getAISObject(aFeature));
490 aDisplayer->redisplay(aFeature, anAIS, false);
491 aDisplayer->activateInLocalContext(aFeature, aModes, false);
493 aDisplayer->updateViewer();
496 void PartSet_Module::editFeature(FeaturePtr theFeature)
501 // if (theFeature->getKind() == SKETCH_KIND) {
502 FeaturePtr aFeature = theFeature;
503 if (XGUI_Tools::isModelObject(aFeature)) {
504 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
505 aFeature = aObject->featureRef();
509 onLaunchOperation(aFeature->getKind(), aFeature);
510 updateCurrentPreview(aFeature->getKind());