1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationCreateFeature.h>
4 #include <PartSet_OperationCreateConstraint.h>
5 #include <PartSet_OperationFeatureCreate.h>
6 #include <PartSet_OperationFeatureEditMulti.h>
7 #include <PartSet_OperationFeatureEdit.h>
8 #include <ModuleBase_Operation.h>
9 #include <ModuleBase_OperationDescription.h>
10 #include <ModuleBase_WidgetFactory.h>
11 #include <PartSet_Listener.h>
12 #include <PartSet_TestOCC.h>
14 #include <ModuleBase_Operation.h>
15 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Data.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <PartSet_Tools.h>
21 #include <XGUI_MainWindow.h>
22 #include <XGUI_Displayer.h>
23 #include <XGUI_Viewer.h>
24 #include <XGUI_Workshop.h>
25 #include <XGUI_OperationMgr.h>
26 #include <XGUI_SelectionMgr.h>
27 #include <XGUI_ViewPort.h>
28 #include <XGUI_ActionsMgr.h>
29 #include <XGUI_ViewerProxy.h>
30 #include <XGUI_ContextMenuMgr.h>
31 #include <XGUI_PropertyPanel.h>
32 #include <XGUI_ModuleConnector.h>
33 #include <XGUI_Tools.h>
35 #include <SketchPlugin_Line.h>
37 #include <Config_PointerMessage.h>
38 #include <Config_ModuleReader.h>
39 #include <Config_WidgetReader.h>
40 #include <Events_Loop.h>
41 #include <Events_Message.h>
42 #include <Events_Error.h>
44 #include <GeomAPI_Shape.h>
46 #include <AIS_ListOfInteractive.hxx>
47 //#include <AIS_DimensionSelectionMode.hxx>
50 #include <QMouseEvent>
57 /*!Create and return new instance of XGUI_Module*/
58 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
60 return new PartSet_Module(theWshop);
63 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
65 myWorkshop = theWshop;
66 myListener = new PartSet_Listener(this);
68 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
70 connect(anOperationMgr, SIGNAL(operationStarted()),
71 this, SLOT(onOperationStarted()));
73 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
74 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
76 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
77 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
78 this, SLOT(onContextMenuCommand(const QString&, bool)));
80 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
81 this, SLOT(onMousePressed(QMouseEvent*)));
82 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
83 this, SLOT(onMouseReleased(QMouseEvent*)));
84 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
85 this, SLOT(onMouseMoved(QMouseEvent*)));
86 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
87 this, SLOT(onKeyRelease(QKeyEvent*)));
88 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
89 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
92 PartSet_Module::~PartSet_Module()
96 XGUI_Workshop* PartSet_Module::workshop() const
101 void PartSet_Module::createFeatures()
103 Config_ModuleReader aXMLReader = Config_ModuleReader();
104 aXMLReader.readAll();
105 myFeaturesInFiles = aXMLReader.featuresInFiles();
108 void PartSet_Module::featureCreated(QAction* theFeature)
110 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
113 QStringList PartSet_Module::nestedFeatures(QString)
115 return QStringList();
118 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
120 return myFeaturesInFiles[theFeatureId];
126 void PartSet_Module::onFeatureTriggered()
128 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
129 // myWorkshop->viewer()->activeView());
131 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
132 // myWorkshop->viewer()->activeView());
133 QAction* aCmd = dynamic_cast<QAction*>(sender());
134 //Do nothing on uncheck
135 if(aCmd->isCheckable() && !aCmd->isChecked())
137 launchOperation(aCmd->data().toString());
140 void PartSet_Module::launchOperation(const QString& theCmdId)
142 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
143 sendOperation(anOperation);
146 void PartSet_Module::onOperationStarted()
148 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
149 myWorkshop->operationMgr()->currentOperation());
151 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
152 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
153 aPropPanel, SLOT(onFocusActivated(const std::string&)));
155 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
156 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
160 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
164 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
166 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
167 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
168 aPropPanel, SLOT(onFocusActivated(const std::string&)));
169 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
170 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
174 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
176 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
177 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
178 editFeature(aFeatures.first());
182 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
184 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
185 myWorkshop->operationMgr()->currentOperation());
188 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
189 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
190 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
192 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
196 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
198 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
199 myWorkshop->operationMgr()->currentOperation());
202 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
203 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
204 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
206 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
210 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
212 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
213 myWorkshop->operationMgr()->currentOperation());
215 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
218 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
220 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
221 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
223 aPreviewOp->keyReleased(theEvent->key());
227 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
229 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
230 myWorkshop->operationMgr()->currentOperation());
233 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
234 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
235 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
236 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
241 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
243 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
244 myWorkshop->actionsMgr()->update();
246 //PartSet_TestOCC::testSelection(myWorkshop);
249 void PartSet_Module::onFitAllView()
251 myWorkshop->viewer()->fitAll();
254 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
256 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
257 theFeature ? theFeature->getKind() : "");
258 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
261 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
262 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
263 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
264 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
265 aPreviewOp->init(theFeature, aSelected, aHighlighted);
267 anOperation->setEditingFeature(theFeature);
269 sendOperation(anOperation);
270 myWorkshop->actionsMgr()->updateCheckState();
273 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
275 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
276 aViewer->enableMultiselection(theEnabled);
279 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
281 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
283 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
284 for (; anIt != aLast; anIt++) {
285 activateFeature((*anIt), false);
288 aDisplayer->stopSelection(theFeatures, isStop, false);
290 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
291 aViewer->enableSelection(!isStop);
293 aDisplayer->updateViewer();
296 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
298 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
299 aDisplayer->setSelected(theFeatures, false);
300 aDisplayer->updateViewer();
303 void PartSet_Module::onCloseLocalContext()
305 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
306 aDisplayer->closeLocalContexts();
309 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
311 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
312 visualizePreview(theFeature, isDisplay, false);
314 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
316 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
318 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
319 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
320 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
322 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
323 anIt = aList.begin(), aLast = aList.end();
324 for (; anIt != aLast; anIt++) {
325 FeaturePtr aFeature = (*anIt).first;
326 visualizePreview(aFeature, false, false);
328 aDisplayer->updateViewer();
332 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
333 theMode == PartSet_OperationSketchBase::FM_Deactivation)
334 activateFeature(theFeature, true);
337 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
338 const std::string& theFeatureKind)
340 // create the operation
341 ModuleBase_Operation* anOperation = 0;
342 if (theCmdId == PartSet_OperationSketch::Type()) {
343 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
346 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
348 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
350 aSketch = aPrevOp->sketch();
351 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
352 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
353 else if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
354 anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
355 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
356 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
357 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
358 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
359 else if (PartSet_OperationCreateConstraint::canProcessKind(theCmdId))
360 anOperation = new PartSet_OperationCreateConstraint(theCmdId.c_str(), this, aSketch);
364 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
367 // set operation description and list of widgets corresponding to the feature xml definition
368 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
370 std::string aPluginFileName = featureFile(aFeatureKind);
371 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
372 aWdgReader.readAll();
373 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
374 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
376 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
377 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
378 // myWorkshop->moduleConnector());
379 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
380 //qDeleteAll(aContent->children());
381 //aFactory.createWidget(aContent);
383 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
384 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
386 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
388 // connect the operation
389 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
391 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
392 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
393 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
394 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
395 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
396 this, SLOT(onMultiSelectionEnabled(bool)));
398 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
399 this, SLOT(onMultiSelectionEnabled(bool)));
400 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
401 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
402 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
403 this, SLOT(onSetSelection(const QFeatureList&)));
405 connect(aPreviewOp, SIGNAL(closeLocalContext()),
406 this, SLOT(onCloseLocalContext()));
408 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
410 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
411 this, SLOT(onPlaneSelected(double, double, double)));
412 connect(aSketchOp, SIGNAL(fitAllView()),
413 this, SLOT(onFitAllView()));
420 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
422 //TODO(sbh): Implement static method to extract event id [SEID]
423 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
424 Config_PointerMessage aMessage(aModuleEvent, this);
425 aMessage.setPointer(theOperation);
426 Events_Loop::loop()->send(aMessage);
429 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
430 const bool isUpdateViewer)
432 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
436 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
440 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
442 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
443 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
446 Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(theFeature));
447 aDisplayer->redisplay(theFeature, anAIS, false);
451 aDisplayer->erase(theFeature, false);
454 aDisplayer->updateViewer();
457 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
459 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
460 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
462 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
463 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
468 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
470 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
474 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
478 FeaturePtr aFeature = aPreviewOp->feature();
479 if (!aFeature || aFeature->getKind() != theCmdId)
482 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
483 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
484 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
486 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
487 anIt = aList.begin(), aLast = aList.end();
488 for (; anIt != aLast; anIt++) {
489 FeaturePtr aFeature = (*anIt).first;
490 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
491 boost::dynamic_pointer_cast<SketchPlugin_Feature>((*anIt).first);
495 Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(aFeature));
497 aDisplayer->redisplay(aFeature, anAIS, false);
498 aDisplayer->activateInLocalContext(aFeature, aModes, false);
500 aDisplayer->updateViewer();
503 void PartSet_Module::editFeature(FeaturePtr theFeature)
508 // if (theFeature->getKind() == SKETCH_KIND) {
509 FeaturePtr aFeature = theFeature;
510 if (XGUI_Tools::isModelObject(aFeature)) {
511 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
512 aFeature = aObject->featureRef();
516 onLaunchOperation(aFeature->getKind(), aFeature);
517 updateCurrentPreview(aFeature->getKind());
522 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
524 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
525 myWorkshop->operationMgr()->currentOperation());
529 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
530 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
532 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
533 aPoint->x(), aPoint->y());