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::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
218 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
219 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
222 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
223 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
224 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected(TopAbs_VERTEX);
225 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted(TopAbs_VERTEX);
226 aPreviewOp->init(theFeature, aSelected, aHighlighted);
228 sendOperation(anOperation);
229 myWorkshop->actionsMgr()->updateCheckState();
232 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
234 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
235 aViewer->enableMultiselection(theEnabled);
238 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
240 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
242 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
243 boost::shared_ptr<ModelAPI_Feature> aFeature;
244 for (; anIt != aLast; anIt++) {
245 activateFeature((*anIt).feature(), false);
248 aDisplayer->StopSelection(theFeatures, isStop, false);
250 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
251 aViewer->enableSelection(!isStop);
253 aDisplayer->UpdateViewer();
256 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
258 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
259 aDisplayer->SetSelected(theFeatures, false);
260 aDisplayer->UpdateViewer();
263 void PartSet_Module::onCloseLocalContext()
265 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
266 aDisplayer->CloseLocalContexts();
269 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
272 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
273 visualizePreview(theFeature, isDisplay, false);
275 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
276 boost::shared_ptr<ModelAPI_Feature> aSketch;
277 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
279 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
280 aList = aPrevOp->subPreview();
281 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
282 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
284 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
285 anIt = aList.begin(), aLast = aList.end();
286 for (; anIt != aLast; anIt++) {
287 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
288 visualizePreview(aFeature, false, false);
290 aDisplayer->UpdateViewer();
294 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
295 theMode == PartSet_OperationSketchBase::FM_Deactivation)
296 activateFeature(theFeature, true);
299 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
301 // get operation xml description
302 std::string aStdCmdId = theCmdId;
303 if (aStdCmdId == PartSet_OperationEditLine::Type())
304 aStdCmdId = PartSet_OperationSketchLine::Type();
305 std::string aPluginFileName = featureFile(aStdCmdId);
306 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
307 aWdgReader.readAll();
308 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
309 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
311 // create the operation
312 ModuleBase_Operation* anOperation;
313 if (theCmdId == PartSet_OperationSketch::Type()) {
314 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
316 else if(theCmdId == PartSet_OperationSketchLine::Type() ||
317 theCmdId == PartSet_OperationEditLine::Type()) {
318 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
319 boost::shared_ptr<ModelAPI_Feature> aSketch;
320 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
322 aSketch = aPrevOp->sketch();
323 if (theCmdId == PartSet_OperationSketchLine::Type())
324 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
326 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
329 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
331 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
332 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
334 // connect the operation
335 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
337 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
338 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
339 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
340 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
341 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
342 this, SLOT(onMultiSelectionEnabled(bool)));
344 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
345 this, SLOT(onMultiSelectionEnabled(bool)));
346 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
347 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
348 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
349 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
351 connect(aPreviewOp, SIGNAL(closeLocalContext()),
352 this, SLOT(onCloseLocalContext()));
354 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
356 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
357 this, SLOT(onPlaneSelected(double, double, double)));
364 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
366 //TODO(sbh): Implement static method to extract event id [SEID]
367 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
368 Config_PointerMessage aMessage(aModuleEvent, this);
369 aMessage.setPointer(theOperation);
370 Events_Loop::loop()->send(aMessage);
373 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
374 const bool isUpdateViewer)
376 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
380 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
384 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
386 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
387 aDisplayer->Redisplay(theFeature,
388 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
391 aDisplayer->Erase(theFeature, false);
394 aDisplayer->UpdateViewer();
397 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
398 const bool isUpdateViewer)
400 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
401 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
403 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
404 aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
409 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
411 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
415 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
419 boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
420 if (!aFeature || aFeature->getKind() != theCmdId)
423 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
424 aList = aPreviewOp->subPreview();
425 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
426 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
428 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
429 anIt = aList.begin(), aLast = aList.end();
430 for (; anIt != aLast; anIt++) {
431 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
432 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
433 aDisplayer->Redisplay(aFeature,
434 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
435 aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
437 aDisplayer->UpdateViewer();
440 void PartSet_Module::editFeature(FeaturePtr theFeature)
445 if (theFeature->getKind() == "Sketch") {
446 FeaturePtr aFeature = theFeature;
447 if (XGUI_Tools::isModelObject(aFeature)) {
448 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
449 aFeature = aObject->featureRef();
453 onLaunchOperation(aFeature->getKind(), aFeature);
454 updateCurrentPreview(aFeature->getKind());