1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationCreateFeature.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 <SketchPlugin_Line.h>
29 #include <SketchPlugin_Point.h>
31 #include <Config_PointerMessage.h>
32 #include <Config_ModuleReader.h>
33 #include <Config_WidgetReader.h>
34 #include <Events_Loop.h>
35 #include <Events_Message.h>
36 #include <Events_Error.h>
38 #include <GeomAPI_Shape.h>
40 #include <AIS_ListOfInteractive.hxx>
41 #include <AIS_DimensionSelectionMode.hxx>
44 #include <QMouseEvent>
51 /*!Create and return new instance of XGUI_Module*/
52 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
54 return new PartSet_Module(theWshop);
57 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
59 myWorkshop = theWshop;
60 myListener = new PartSet_Listener(this);
62 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
64 connect(anOperationMgr, SIGNAL(operationStarted()),
65 this, SLOT(onOperationStarted()));
67 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
68 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
70 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
71 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
72 this, SLOT(onContextMenuCommand(const QString&, bool)));
74 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
75 this, SLOT(onMousePressed(QMouseEvent*)));
76 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
77 this, SLOT(onMouseReleased(QMouseEvent*)));
78 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
79 this, SLOT(onMouseMoved(QMouseEvent*)));
80 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
81 this, SLOT(onKeyRelease(QKeyEvent*)));
84 PartSet_Module::~PartSet_Module()
88 XGUI_Workshop* PartSet_Module::workshop() const
93 void PartSet_Module::createFeatures()
95 Config_ModuleReader aXMLReader = Config_ModuleReader();
97 myFeaturesInFiles = aXMLReader.featuresInFiles();
100 void PartSet_Module::featureCreated(QAction* theFeature)
102 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
105 QStringList PartSet_Module::nestedFeatures(QString)
107 return QStringList();
110 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
112 return myFeaturesInFiles[theFeatureId];
118 void PartSet_Module::onFeatureTriggered()
120 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
121 // myWorkshop->viewer()->activeView());
123 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
124 // myWorkshop->viewer()->activeView());
125 QAction* aCmd = dynamic_cast<QAction*>(sender());
126 //Do nothing on uncheck
127 if(aCmd->isCheckable() && !aCmd->isChecked())
129 launchOperation(aCmd->data().toString());
132 void PartSet_Module::launchOperation(const QString& theCmdId)
134 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
135 sendOperation(anOperation);
138 void PartSet_Module::onOperationStarted()
140 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
141 myWorkshop->operationMgr()->currentOperation());
143 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
144 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
145 aPropPanel, SLOT(onFocusActivated(const std::string&)));
149 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
153 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
155 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
156 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
157 aPropPanel, SLOT(onFocusActivated(const std::string&)));
161 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
163 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
164 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
165 editFeature(aFeatures.first());
169 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
171 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
172 myWorkshop->operationMgr()->currentOperation());
175 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
176 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
177 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
179 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
183 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
185 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
186 myWorkshop->operationMgr()->currentOperation());
189 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
190 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
191 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
193 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
197 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
199 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
200 myWorkshop->operationMgr()->currentOperation());
202 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
205 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
207 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
208 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
210 aPreviewOp->keyReleased(theEvent->key());
214 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
216 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
217 myWorkshop->actionsMgr()->update();
219 //PartSet_TestOCC::testSelection(myWorkshop);
222 void PartSet_Module::onFitAllView()
224 myWorkshop->viewer()->fitAll();
227 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
229 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
230 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
233 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
234 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
235 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected(TopAbs_VERTEX);
236 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted(TopAbs_VERTEX);
237 aPreviewOp->init(theFeature, aSelected, aHighlighted);
239 anOperation->setEditingFeature(theFeature);
241 sendOperation(anOperation);
242 myWorkshop->actionsMgr()->updateCheckState();
245 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
247 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
248 aViewer->enableMultiselection(theEnabled);
251 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
253 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
255 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
257 for (; anIt != aLast; anIt++) {
258 activateFeature((*anIt).feature(), false);
261 aDisplayer->stopSelection(theFeatures, isStop, false);
263 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
264 aViewer->enableSelection(!isStop);
266 aDisplayer->updateViewer();
269 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
271 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
272 aDisplayer->setSelected(theFeatures, false);
273 aDisplayer->updateViewer();
276 void PartSet_Module::onCloseLocalContext()
278 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
279 aDisplayer->closeLocalContexts();
282 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
284 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
285 visualizePreview(theFeature, isDisplay, false);
287 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
289 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
291 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
292 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
293 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
295 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
296 anIt = aList.begin(), aLast = aList.end();
297 for (; anIt != aLast; anIt++) {
298 FeaturePtr aFeature = (*anIt).first;
299 visualizePreview(aFeature, false, false);
301 aDisplayer->updateViewer();
305 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
306 theMode == PartSet_OperationSketchBase::FM_Deactivation)
307 activateFeature(theFeature, true);
310 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
312 // get operation xml description
313 std::string aStdCmdId = theCmdId;
314 if (aStdCmdId == PartSet_OperationEditLine::Type())
315 aStdCmdId = SKETCH_LINE_KIND;
316 std::string aPluginFileName = featureFile(aStdCmdId);
317 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
318 aWdgReader.readAll();
319 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
320 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
322 // create the operation
323 ModuleBase_Operation* anOperation = 0;
324 if (theCmdId == PartSet_OperationSketch::Type()) {
325 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
328 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
330 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
332 aSketch = aPrevOp->sketch();
333 if (theCmdId == SKETCH_LINE_KIND || theCmdId == SKETCH_POINT_KIND)
334 anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
335 else if (theCmdId == PartSet_OperationEditLine::Type())
336 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
337 else if (theCmdId == PartSet_OperationConstraint::Type())
338 anOperation = new PartSet_OperationConstraint(theCmdId.c_str(), this, aSketch);
342 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
344 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
345 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
347 // connect the operation
348 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
350 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
351 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
352 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
353 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
354 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
355 this, SLOT(onMultiSelectionEnabled(bool)));
357 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
358 this, SLOT(onMultiSelectionEnabled(bool)));
359 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
360 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
361 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
362 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
364 connect(aPreviewOp, SIGNAL(closeLocalContext()),
365 this, SLOT(onCloseLocalContext()));
367 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
369 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
370 this, SLOT(onPlaneSelected(double, double, double)));
371 connect(aSketchOp, SIGNAL(fitAllView()),
372 this, SLOT(onFitAllView()));
379 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
381 //TODO(sbh): Implement static method to extract event id [SEID]
382 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
383 Config_PointerMessage aMessage(aModuleEvent, this);
384 aMessage.setPointer(theOperation);
385 Events_Loop::loop()->send(aMessage);
388 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
389 const bool isUpdateViewer)
391 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
395 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
399 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
401 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
402 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
403 theFeature, aPreviewOp->sketch(),
404 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
405 aDisplayer->getAISObject(theFeature));
407 int aSelectionMode = -1;
408 if (theFeature->getKind() == SKETCH_CONSTRAINT_LENGTH_KIND) {
409 aSelectionMode = AIS_DSM_Text;
411 aDisplayer->redisplay(theFeature, anAIS, aSelectionMode, false);
414 aDisplayer->erase(theFeature, false);
417 aDisplayer->updateViewer();
420 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
422 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
423 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
425 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
426 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
431 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
433 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
437 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
441 FeaturePtr aFeature = aPreviewOp->feature();
442 if (!aFeature || aFeature->getKind() != theCmdId)
445 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
446 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
447 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
449 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
450 anIt = aList.begin(), aLast = aList.end();
451 for (; anIt != aLast; anIt++) {
452 FeaturePtr aFeature = (*anIt).first;
453 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
454 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
455 aFeature, aPreviewOp->sketch(),
456 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
457 aDisplayer->getAISObject(aFeature));
459 aDisplayer->redisplay(aFeature, anAIS, -1, false);
460 aDisplayer->activateInLocalContext(aFeature, aModes, false);
462 aDisplayer->updateViewer();
465 void PartSet_Module::editFeature(FeaturePtr theFeature)
470 // if (theFeature->getKind() == SKETCH_KIND) {
471 FeaturePtr aFeature = theFeature;
472 if (XGUI_Tools::isModelObject(aFeature)) {
473 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
474 aFeature = aObject->featureRef();
478 onLaunchOperation(aFeature->getKind(), aFeature);
479 updateCurrentPreview(aFeature->getKind());