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>
11 #include <ModelAPI_Object.h>
13 #include <XGUI_MainWindow.h>
14 #include <XGUI_Displayer.h>
15 #include <XGUI_Viewer.h>
16 #include <XGUI_Workshop.h>
17 #include <XGUI_OperationMgr.h>
18 #include <XGUI_SelectionMgr.h>
19 #include <XGUI_ViewPort.h>
20 #include <XGUI_ActionsMgr.h>
21 #include <XGUI_ViewerProxy.h>
22 #include <XGUI_ContextMenuMgr.h>
23 #include <XGUI_PropertyPanel.h>
24 #include <XGUI_Tools.h>
26 #include <Config_PointerMessage.h>
27 #include <Config_ModuleReader.h>
28 #include <Config_WidgetReader.h>
29 #include <Events_Loop.h>
30 #include <Events_Message.h>
31 #include <Events_Error.h>
33 #include <GeomAPI_Shape.h>
35 #include <AIS_ListOfInteractive.hxx>
38 #include <QMouseEvent>
45 /*!Create and return new instance of XGUI_Module*/
46 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
48 return new PartSet_Module(theWshop);
51 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
53 myWorkshop = theWshop;
54 myListener = new PartSet_Listener(this);
56 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
58 connect(anOperationMgr, SIGNAL(operationStarted()),
59 this, SLOT(onOperationStarted()));
61 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
62 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
64 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
65 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
66 this, SLOT(onContextMenuCommand(const QString&, bool)));
68 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
69 this, SLOT(onMousePressed(QMouseEvent*)));
70 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
71 this, SLOT(onMouseReleased(QMouseEvent*)));
72 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
73 this, SLOT(onMouseMoved(QMouseEvent*)));
74 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
75 this, SLOT(onKeyRelease(QKeyEvent*)));
78 PartSet_Module::~PartSet_Module()
82 XGUI_Workshop* PartSet_Module::workshop() const
87 void PartSet_Module::createFeatures()
89 Config_ModuleReader aXMLReader = Config_ModuleReader();
91 myFeaturesInFiles = aXMLReader.featuresInFiles();
94 void PartSet_Module::featureCreated(QAction* theFeature)
96 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
99 QStringList PartSet_Module::nestedFeatures(QString)
101 return QStringList();
104 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
106 return myFeaturesInFiles[theFeatureId];
112 void PartSet_Module::onFeatureTriggered()
114 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
115 // myWorkshop->viewer()->activeView());
117 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
118 // myWorkshop->viewer()->activeView());
119 QAction* aCmd = dynamic_cast<QAction*>(sender());
120 //Do nothing on uncheck
121 if(aCmd->isCheckable() && !aCmd->isChecked())
123 launchOperation(aCmd->data().toString());
126 void PartSet_Module::launchOperation(const QString& theCmdId)
128 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
129 sendOperation(anOperation);
132 void PartSet_Module::onOperationStarted()
134 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
135 myWorkshop->operationMgr()->currentOperation());
137 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
138 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
139 aPropPanel, SLOT(onFocusActivated(const std::string&)));
143 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
147 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
149 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
150 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
151 aPropPanel, SLOT(onFocusActivated(const std::string&)));
155 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
157 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
158 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
159 editFeature(aFeatures.first());
163 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
165 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
166 myWorkshop->operationMgr()->currentOperation());
169 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
170 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
171 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
173 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
177 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
179 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
180 myWorkshop->operationMgr()->currentOperation());
183 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
184 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
185 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
187 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
191 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
193 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
194 myWorkshop->operationMgr()->currentOperation());
196 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
199 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
201 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
202 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
204 aPreviewOp->keyReleased(theEvent->key());
208 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
210 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
211 myWorkshop->actionsMgr()->update();
213 //PartSet_TestOCC::testSelection(myWorkshop);
216 void PartSet_Module::onFitAllView()
218 myWorkshop->viewer()->fitAll();
221 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
223 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
224 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
227 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
228 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
229 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected(TopAbs_VERTEX);
230 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted(TopAbs_VERTEX);
231 aPreviewOp->init(theFeature, aSelected, aHighlighted);
233 sendOperation(anOperation);
234 myWorkshop->actionsMgr()->updateCheckState();
237 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
239 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
240 aViewer->enableMultiselection(theEnabled);
243 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
245 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
247 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
248 boost::shared_ptr<ModelAPI_Feature> aFeature;
249 for (; anIt != aLast; anIt++) {
250 activateFeature((*anIt).feature(), false);
253 aDisplayer->StopSelection(theFeatures, isStop, false);
255 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
256 aViewer->enableSelection(!isStop);
258 aDisplayer->UpdateViewer();
261 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
263 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
264 aDisplayer->SetSelected(theFeatures, false);
265 aDisplayer->UpdateViewer();
268 void PartSet_Module::onCloseLocalContext()
270 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
271 aDisplayer->CloseLocalContexts();
274 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
277 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
278 visualizePreview(theFeature, isDisplay, false);
280 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
281 boost::shared_ptr<ModelAPI_Feature> aSketch;
282 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
284 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
285 aList = aPrevOp->subPreview();
286 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
287 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
289 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
290 anIt = aList.begin(), aLast = aList.end();
291 for (; anIt != aLast; anIt++) {
292 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
293 visualizePreview(aFeature, false, false);
295 aDisplayer->UpdateViewer();
299 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
300 theMode == PartSet_OperationSketchBase::FM_Deactivation)
301 activateFeature(theFeature, true);
304 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
306 // get operation xml description
307 std::string aStdCmdId = theCmdId;
308 if (aStdCmdId == PartSet_OperationEditLine::Type())
309 aStdCmdId = PartSet_OperationSketchLine::Type();
310 std::string aPluginFileName = featureFile(aStdCmdId);
311 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
312 aWdgReader.readAll();
313 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
314 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
316 // create the operation
317 ModuleBase_Operation* anOperation;
318 if (theCmdId == PartSet_OperationSketch::Type()) {
319 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
321 else if(theCmdId == PartSet_OperationSketchLine::Type() ||
322 theCmdId == PartSet_OperationEditLine::Type()) {
323 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
324 boost::shared_ptr<ModelAPI_Feature> aSketch;
325 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
327 aSketch = aPrevOp->sketch();
328 if (theCmdId == PartSet_OperationSketchLine::Type())
329 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
331 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
334 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
336 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
337 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
339 // connect the operation
340 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
342 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
343 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
344 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
345 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
346 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
347 this, SLOT(onMultiSelectionEnabled(bool)));
349 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
350 this, SLOT(onMultiSelectionEnabled(bool)));
351 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
352 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
353 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
354 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
356 connect(aPreviewOp, SIGNAL(closeLocalContext()),
357 this, SLOT(onCloseLocalContext()));
359 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
361 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
362 this, SLOT(onPlaneSelected(double, double, double)));
363 connect(aSketchOp, SIGNAL(fitAllView()),
364 this, SLOT(onFitAllView()));
371 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
373 //TODO(sbh): Implement static method to extract event id [SEID]
374 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
375 Config_PointerMessage aMessage(aModuleEvent, this);
376 aMessage.setPointer(theOperation);
377 Events_Loop::loop()->send(aMessage);
380 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
381 const bool isUpdateViewer)
383 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
387 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
391 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
393 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
394 aDisplayer->Redisplay(theFeature,
395 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
398 aDisplayer->Erase(theFeature, false);
401 aDisplayer->UpdateViewer();
404 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
405 const bool isUpdateViewer)
407 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
408 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
410 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
411 aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
416 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
418 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
422 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
426 boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
427 if (!aFeature || aFeature->getKind() != theCmdId)
430 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
431 aList = aPreviewOp->subPreview();
432 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
433 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
435 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
436 anIt = aList.begin(), aLast = aList.end();
437 for (; anIt != aLast; anIt++) {
438 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
439 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
440 aDisplayer->Redisplay(aFeature,
441 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
442 aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
444 aDisplayer->UpdateViewer();
447 void PartSet_Module::editFeature(FeaturePtr theFeature)
452 if (theFeature->getKind() == "Sketch") {
453 FeaturePtr aFeature = theFeature;
454 if (XGUI_Tools::isModelObject(aFeature)) {
455 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
456 aFeature = aObject->featureRef();
460 onLaunchOperation(aFeature->getKind(), aFeature);
461 updateCurrentPreview(aFeature->getKind());