1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationSketchLine.h>
4 #include <PartSet_OperationEditLine.h>
5 #include <PartSet_OperationConstraint.h>
6 #include <ModuleBase_Operation.h>
7 #include <ModuleBase_OperationDescription.h>
8 #include <PartSet_Listener.h>
9 #include <PartSet_TestOCC.h>
10 #include <PartSet_Presentation.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModelAPI_Object.h>
15 #include <XGUI_MainWindow.h>
16 #include <XGUI_Displayer.h>
17 #include <XGUI_Viewer.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_OperationMgr.h>
20 #include <XGUI_SelectionMgr.h>
21 #include <XGUI_ViewPort.h>
22 #include <XGUI_ActionsMgr.h>
23 #include <XGUI_ViewerProxy.h>
24 #include <XGUI_ContextMenuMgr.h>
25 #include <XGUI_PropertyPanel.h>
26 #include <XGUI_Tools.h>
28 #include <Config_PointerMessage.h>
29 #include <Config_ModuleReader.h>
30 #include <Config_WidgetReader.h>
31 #include <Events_Loop.h>
32 #include <Events_Message.h>
33 #include <Events_Error.h>
35 #include <GeomAPI_Shape.h>
37 #include <AIS_ListOfInteractive.hxx>
38 #include <AIS_DimensionSelectionMode.hxx>
41 #include <QMouseEvent>
48 /*!Create and return new instance of XGUI_Module*/
49 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
51 return new PartSet_Module(theWshop);
54 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
56 myWorkshop = theWshop;
57 myListener = new PartSet_Listener(this);
59 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
61 connect(anOperationMgr, SIGNAL(operationStarted()),
62 this, SLOT(onOperationStarted()));
64 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
65 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
67 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
68 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
69 this, SLOT(onContextMenuCommand(const QString&, bool)));
71 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
72 this, SLOT(onMousePressed(QMouseEvent*)));
73 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
74 this, SLOT(onMouseReleased(QMouseEvent*)));
75 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
76 this, SLOT(onMouseMoved(QMouseEvent*)));
77 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
78 this, SLOT(onKeyRelease(QKeyEvent*)));
81 PartSet_Module::~PartSet_Module()
85 XGUI_Workshop* PartSet_Module::workshop() const
90 void PartSet_Module::createFeatures()
92 Config_ModuleReader aXMLReader = Config_ModuleReader();
94 myFeaturesInFiles = aXMLReader.featuresInFiles();
97 void PartSet_Module::featureCreated(QAction* theFeature)
99 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
102 QStringList PartSet_Module::nestedFeatures(QString)
104 return QStringList();
107 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
109 return myFeaturesInFiles[theFeatureId];
115 void PartSet_Module::onFeatureTriggered()
117 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
118 // myWorkshop->viewer()->activeView());
120 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
121 // myWorkshop->viewer()->activeView());
122 QAction* aCmd = dynamic_cast<QAction*>(sender());
123 //Do nothing on uncheck
124 if(aCmd->isCheckable() && !aCmd->isChecked())
126 launchOperation(aCmd->data().toString());
129 void PartSet_Module::launchOperation(const QString& theCmdId)
131 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
132 sendOperation(anOperation);
135 void PartSet_Module::onOperationStarted()
137 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
138 myWorkshop->operationMgr()->currentOperation());
140 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
141 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
142 aPropPanel, SLOT(onFocusActivated(const std::string&)));
146 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
150 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
152 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
153 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
154 aPropPanel, SLOT(onFocusActivated(const std::string&)));
158 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
160 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
161 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
162 editFeature(aFeatures.first());
166 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
168 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
169 myWorkshop->operationMgr()->currentOperation());
172 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
173 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
174 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
176 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
180 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
182 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
183 myWorkshop->operationMgr()->currentOperation());
186 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
187 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
188 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
190 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
194 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
196 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
197 myWorkshop->operationMgr()->currentOperation());
199 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
202 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
204 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
205 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
207 aPreviewOp->keyReleased(theEvent->key());
211 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
213 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
214 myWorkshop->actionsMgr()->update();
216 //PartSet_TestOCC::testSelection(myWorkshop);
219 void PartSet_Module::onFitAllView()
221 myWorkshop->viewer()->fitAll();
224 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
226 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
227 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
230 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
231 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
232 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected(TopAbs_VERTEX);
233 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted(TopAbs_VERTEX);
234 aPreviewOp->init(theFeature, aSelected, aHighlighted);
236 anOperation->setFeature(theFeature);
238 sendOperation(anOperation);
239 myWorkshop->actionsMgr()->updateCheckState();
242 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
244 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
245 aViewer->enableMultiselection(theEnabled);
248 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
250 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
252 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
254 for (; anIt != aLast; anIt++) {
255 activateFeature((*anIt).feature(), false);
258 aDisplayer->stopSelection(theFeatures, isStop, false);
260 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
261 aViewer->enableSelection(!isStop);
263 aDisplayer->updateViewer();
266 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
268 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
269 aDisplayer->setSelected(theFeatures, false);
270 aDisplayer->updateViewer();
273 void PartSet_Module::onCloseLocalContext()
275 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
276 aDisplayer->closeLocalContexts();
279 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
281 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
282 visualizePreview(theFeature, isDisplay, false);
284 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
286 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
288 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
289 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
290 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
292 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
293 anIt = aList.begin(), aLast = aList.end();
294 for (; anIt != aLast; anIt++) {
295 FeaturePtr aFeature = (*anIt).first;
296 visualizePreview(aFeature, false, false);
298 aDisplayer->updateViewer();
302 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
303 theMode == PartSet_OperationSketchBase::FM_Deactivation)
304 activateFeature(theFeature, true);
307 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
309 // get operation xml description
310 std::string aStdCmdId = theCmdId;
311 if (aStdCmdId == PartSet_OperationEditLine::Type())
312 aStdCmdId = PartSet_OperationSketchLine::Type();
313 std::string aPluginFileName = featureFile(aStdCmdId);
314 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
315 aWdgReader.readAll();
316 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
317 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
319 // create the operation
320 ModuleBase_Operation* anOperation = 0;
321 if (theCmdId == PartSet_OperationSketch::Type()) {
322 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
325 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
327 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
329 aSketch = aPrevOp->sketch();
330 if (theCmdId == PartSet_OperationSketchLine::Type())
331 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
332 else if (theCmdId == PartSet_OperationEditLine::Type())
333 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
334 else if (theCmdId == PartSet_OperationConstraint::Type())
335 anOperation = new PartSet_OperationConstraint(theCmdId.c_str(), this, aSketch);
339 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
341 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
342 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
344 // connect the operation
345 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
347 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
348 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
349 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
350 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
351 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
352 this, SLOT(onMultiSelectionEnabled(bool)));
354 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
355 this, SLOT(onMultiSelectionEnabled(bool)));
356 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
357 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
358 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
359 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
361 connect(aPreviewOp, SIGNAL(closeLocalContext()),
362 this, SLOT(onCloseLocalContext()));
364 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
366 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
367 this, SLOT(onPlaneSelected(double, double, double)));
368 connect(aSketchOp, SIGNAL(fitAllView()),
369 this, SLOT(onFitAllView()));
376 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
378 //TODO(sbh): Implement static method to extract event id [SEID]
379 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
380 Config_PointerMessage aMessage(aModuleEvent, this);
381 aMessage.setPointer(theOperation);
382 Events_Loop::loop()->send(aMessage);
385 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
386 const bool isUpdateViewer)
388 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
392 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
396 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
398 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
399 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
400 theFeature, aPreviewOp->sketch(),
401 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
402 aDisplayer->getAISObject(theFeature));
404 int aSelectionMode = -1;
405 if (theFeature->getKind() == SKETCH_CONSTRAINT_LENGTH_KIND) {
406 aSelectionMode = AIS_DSM_Text;
408 aDisplayer->redisplay(theFeature, anAIS, aSelectionMode, false);
411 aDisplayer->erase(theFeature, false);
414 aDisplayer->updateViewer();
417 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
419 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
420 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
422 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
423 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
428 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
430 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
434 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
438 FeaturePtr aFeature = aPreviewOp->feature();
439 if (!aFeature || aFeature->getKind() != theCmdId)
442 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
443 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
444 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
446 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
447 anIt = aList.begin(), aLast = aList.end();
448 for (; anIt != aLast; anIt++) {
449 FeaturePtr aFeature = (*anIt).first;
450 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
451 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
452 aFeature, aPreviewOp->sketch(),
453 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
454 aDisplayer->getAISObject(aFeature));
456 aDisplayer->redisplay(aFeature, anAIS, -1, false);
457 aDisplayer->activateInLocalContext(aFeature, aModes, false);
459 aDisplayer->updateViewer();
462 void PartSet_Module::editFeature(FeaturePtr theFeature)
467 // if (theFeature->getKind() == SKETCH_KIND) {
468 FeaturePtr aFeature = theFeature;
469 if (XGUI_Tools::isModelObject(aFeature)) {
470 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
471 aFeature = aObject->featureRef();
475 onLaunchOperation(aFeature->getKind(), aFeature);
476 updateCurrentPreview(aFeature->getKind());