1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationSketchLine.h>
4 #include <PartSet_OperationEditLine.h>
5 #include <ModuleBase_Operation.h>
6 #include <ModuleBase_OperationDescription.h>
7 #include <PartSet_Listener.h>
8 #include <PartSet_TestOCC.h>
10 #include <ModuleBase_Operation.h>
12 #include <XGUI_MainWindow.h>
13 #include <XGUI_Displayer.h>
14 #include <XGUI_Viewer.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_OperationMgr.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <XGUI_ViewPort.h>
19 #include <XGUI_ActionsMgr.h>
20 #include <XGUI_ViewerProxy.h>
21 #include <XGUI_ContextMenuMgr.h>
22 #include <XGUI_PropertyPanel.h>
24 #include <Config_PointerMessage.h>
25 #include <Config_ModuleReader.h>
26 #include <Config_WidgetReader.h>
27 #include <Events_Loop.h>
28 #include <Events_Message.h>
29 #include <Events_Error.h>
31 #include <GeomAPI_Shape.h>
33 #include <AIS_ListOfInteractive.hxx>
36 #include <QMouseEvent>
43 /*!Create and return new instance of XGUI_Module*/
44 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
46 return new PartSet_Module(theWshop);
49 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
51 myWorkshop = theWshop;
52 myListener = new PartSet_Listener(this);
54 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
56 connect(anOperationMgr, SIGNAL(operationStarted()),
57 this, SLOT(onOperationStarted()));
59 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
60 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
62 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
63 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
64 this, SLOT(onContextMenuCommand(const QString&, bool)));
66 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
67 this, SLOT(onMousePressed(QMouseEvent*)));
68 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
69 this, SLOT(onMouseReleased(QMouseEvent*)));
70 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
71 this, SLOT(onMouseMoved(QMouseEvent*)));
72 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
73 this, SLOT(onKeyRelease(QKeyEvent*)));
76 PartSet_Module::~PartSet_Module()
80 XGUI_Workshop* PartSet_Module::workshop() const
85 void PartSet_Module::createFeatures()
87 Config_ModuleReader aXMLReader = Config_ModuleReader();
89 myFeaturesInFiles = aXMLReader.featuresInFiles();
92 void PartSet_Module::featureCreated(QAction* theFeature)
94 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
97 QStringList PartSet_Module::nestedFeatures(QString)
102 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
104 return myFeaturesInFiles[theFeatureId];
110 void PartSet_Module::onFeatureTriggered()
112 //PartSet_TestOCC::testSelection_OCC(myWorkshop->viewer()->AISContext(),
113 // myWorkshop->viewer()->activeView());
115 //PartSet_TestOCC::testErase_OCC(myWorkshop->viewer()->AISContext(),
116 // myWorkshop->viewer()->activeView());
118 QAction* aCmd = dynamic_cast<QAction*>(sender());
119 //Do nothing on uncheck
120 if(aCmd->isCheckable() && !aCmd->isChecked())
122 launchOperation(aCmd->data().toString());
125 void PartSet_Module::launchOperation(const QString& theCmdId)
127 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
128 sendOperation(anOperation);
131 void PartSet_Module::onOperationStarted()
133 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
134 myWorkshop->operationMgr()->currentOperation());
136 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
137 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
138 aPropPanel, SLOT(onFocusActivated(const std::string&)));
142 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
146 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
148 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
149 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
150 aPropPanel, SLOT(onFocusActivated(const std::string&)));
154 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
156 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
157 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
158 editFeature(aFeatures.first());
162 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
164 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
165 myWorkshop->operationMgr()->currentOperation());
168 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
169 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
170 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
172 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
176 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
178 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
179 myWorkshop->operationMgr()->currentOperation());
182 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
183 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
184 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
186 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
190 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
192 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
193 myWorkshop->operationMgr()->currentOperation());
195 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
198 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
200 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
201 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
203 aPreviewOp->keyReleased(theEvent->key());
207 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
209 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
210 myWorkshop->actionsMgr()->update();
212 //PartSet_TestOCC::testSelection(myWorkshop);
215 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
217 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
218 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
221 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
222 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
223 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected(TopAbs_VERTEX);
224 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted(TopAbs_VERTEX);
225 aPreviewOp->init(theFeature, aSelected, aHighlighted);
227 sendOperation(anOperation);
228 myWorkshop->actionsMgr()->updateCheckState();
231 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
233 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
234 aViewer->enableMultiselection(theEnabled);
237 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
239 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
241 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
242 boost::shared_ptr<ModelAPI_Feature> aFeature;
243 for (; anIt != aLast; anIt++) {
244 activateFeature((*anIt).feature(), false);
247 aDisplayer->StopSelection(theFeatures, isStop, false);
249 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
250 aViewer->enableSelection(!isStop);
252 aDisplayer->UpdateViewer();
255 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
257 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
258 aDisplayer->SetSelected(theFeatures, false);
259 aDisplayer->UpdateViewer();
262 void PartSet_Module::onCloseLocalContext()
264 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
265 aDisplayer->CloseLocalContexts();
268 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
271 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
272 visualizePreview(theFeature, isDisplay, false);
274 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
275 boost::shared_ptr<ModelAPI_Feature> aSketch;
276 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
278 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
279 aList = aPrevOp->subPreview();
280 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
281 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
283 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
284 anIt = aList.begin(), aLast = aList.end();
285 for (; anIt != aLast; anIt++) {
286 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
287 visualizePreview(aFeature, false, false);
289 aDisplayer->UpdateViewer();
293 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
294 theMode == PartSet_OperationSketchBase::FM_Deactivation)
295 activateFeature(theFeature, true);
298 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
300 // get operation xml description
301 std::string aStdCmdId = theCmdId;
302 if (aStdCmdId == PartSet_OperationEditLine::Type())
303 aStdCmdId = PartSet_OperationSketchLine::Type();
304 std::string aPluginFileName = featureFile(aStdCmdId);
305 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
306 aWdgReader.readAll();
307 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
308 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
310 // create the operation
311 ModuleBase_Operation* anOperation;
312 if (theCmdId == PartSet_OperationSketch::Type()) {
313 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
315 else if(theCmdId == PartSet_OperationSketchLine::Type() ||
316 theCmdId == PartSet_OperationEditLine::Type()) {
317 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
318 boost::shared_ptr<ModelAPI_Feature> aSketch;
319 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
321 aSketch = aPrevOp->sketch();
322 if (theCmdId == PartSet_OperationSketchLine::Type())
323 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
325 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
328 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
330 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
331 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
333 // connect the operation
334 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
336 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
337 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
338 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
339 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
340 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
341 this, SLOT(onMultiSelectionEnabled(bool)));
343 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
344 this, SLOT(onMultiSelectionEnabled(bool)));
345 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
346 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
347 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
348 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
350 connect(aPreviewOp, SIGNAL(closeLocalContext()),
351 this, SLOT(onCloseLocalContext()));
353 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
355 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
356 this, SLOT(onPlaneSelected(double, double, double)));
363 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
365 //TODO(sbh): Implement static method to extract event id [SEID]
366 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
367 Config_PointerMessage aMessage(aModuleEvent, this);
368 aMessage.setPointer(theOperation);
369 Events_Loop::loop()->send(aMessage);
372 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
373 const bool isUpdateViewer)
375 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
379 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
383 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
385 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
386 aDisplayer->Redisplay(theFeature,
387 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
390 aDisplayer->Erase(theFeature, false);
393 aDisplayer->UpdateViewer();
396 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
397 const bool isUpdateViewer)
399 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
400 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
402 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
403 aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
408 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
410 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
414 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
418 boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
419 if (!aFeature || aFeature->getKind() != theCmdId)
422 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
423 aList = aPreviewOp->subPreview();
424 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
425 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
427 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
428 anIt = aList.begin(), aLast = aList.end();
429 for (; anIt != aLast; anIt++) {
430 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
431 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
432 aDisplayer->Redisplay(aFeature,
433 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
434 aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
436 aDisplayer->UpdateViewer();
439 void PartSet_Module::editFeature(FeaturePtr theFeature)
444 if (theFeature->getKind() == "Sketch") {
445 onLaunchOperation(theFeature->getKind(), theFeature);
446 updateCurrentPreview(theFeature->getKind());