1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationFeatureCreate.h>
4 #include <PartSet_OperationFeatureEditMulti.h>
5 #include <PartSet_OperationFeatureEdit.h>
6 #include <ModuleBase_Operation.h>
7 #include <ModuleBase_OperationDescription.h>
8 #include <ModuleBase_WidgetFactory.h>
9 #include <PartSet_Listener.h>
10 #include <PartSet_TestOCC.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModelAPI_Object.h>
15 #include <ModelAPI_Data.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <PartSet_Tools.h>
19 #include <XGUI_MainWindow.h>
20 #include <XGUI_Displayer.h>
21 #include <XGUI_Viewer.h>
22 #include <XGUI_Workshop.h>
23 #include <XGUI_OperationMgr.h>
24 #include <XGUI_SelectionMgr.h>
25 #include <XGUI_ViewPort.h>
26 #include <XGUI_ActionsMgr.h>
27 #include <XGUI_ViewerProxy.h>
28 #include <XGUI_ContextMenuMgr.h>
29 #include <XGUI_PropertyPanel.h>
30 #include <XGUI_ModuleConnector.h>
31 #include <XGUI_Tools.h>
33 #include <SketchPlugin_Line.h>
35 #include <Config_PointerMessage.h>
36 #include <Config_ModuleReader.h>
37 #include <Config_WidgetReader.h>
38 #include <Events_Loop.h>
39 #include <Events_Message.h>
40 #include <Events_Error.h>
42 #include <GeomAPI_Shape.h>
44 #include <AIS_ListOfInteractive.hxx>
45 //#include <AIS_DimensionSelectionMode.hxx>
48 #include <QMouseEvent>
55 /*!Create and return new instance of XGUI_Module*/
56 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
58 return new PartSet_Module(theWshop);
61 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
63 myWorkshop = theWshop;
64 myListener = new PartSet_Listener(this);
66 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
68 connect(anOperationMgr, SIGNAL(operationStarted()),
69 this, SLOT(onOperationStarted()));
71 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
72 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
74 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
75 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
76 this, SLOT(onContextMenuCommand(const QString&, bool)));
78 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
79 this, SLOT(onMousePressed(QMouseEvent*)));
80 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
81 this, SLOT(onMouseReleased(QMouseEvent*)));
82 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
83 this, SLOT(onMouseMoved(QMouseEvent*)));
84 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
85 this, SLOT(onKeyRelease(QKeyEvent*)));
86 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
87 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
90 PartSet_Module::~PartSet_Module()
94 XGUI_Workshop* PartSet_Module::workshop() const
99 void PartSet_Module::createFeatures()
101 Config_ModuleReader aXMLReader = Config_ModuleReader();
102 aXMLReader.readAll();
103 myFeaturesInFiles = aXMLReader.featuresInFiles();
106 void PartSet_Module::featureCreated(QAction* theFeature)
108 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
111 QStringList PartSet_Module::nestedFeatures(QString)
113 return QStringList();
116 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
118 return myFeaturesInFiles[theFeatureId];
124 void PartSet_Module::onFeatureTriggered()
126 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
127 // myWorkshop->viewer()->activeView());
129 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
130 // myWorkshop->viewer()->activeView());
131 QAction* aCmd = dynamic_cast<QAction*>(sender());
132 //Do nothing on uncheck
133 if(aCmd->isCheckable() && !aCmd->isChecked())
135 launchOperation(aCmd->data().toString());
138 void PartSet_Module::launchOperation(const QString& theCmdId)
140 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
141 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
143 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
144 // Initialise operation with preliminary selection
145 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
146 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
147 aPreviewOp->initSelection(aSelected, aHighlighted);
149 sendOperation(anOperation);
152 void PartSet_Module::onOperationStarted()
154 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
155 myWorkshop->operationMgr()->currentOperation());
157 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
158 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
159 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
163 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
167 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
169 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
170 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
171 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
175 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
177 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
178 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
179 editFeature(aFeatures.first());
183 void PartSet_Module::onMousePressed(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->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
197 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
199 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
200 myWorkshop->operationMgr()->currentOperation());
203 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
204 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
205 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
207 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
211 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
213 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
214 myWorkshop->operationMgr()->currentOperation());
216 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
219 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
221 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
222 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
224 aPreviewOp->keyReleased(theEvent->key());
228 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
230 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
231 myWorkshop->operationMgr()->currentOperation());
234 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
235 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
236 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
237 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
242 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
244 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
245 myWorkshop->actionsMgr()->update();
247 //PartSet_TestOCC::testSelection(myWorkshop);
250 void PartSet_Module::onFitAllView()
252 myWorkshop->viewer()->fitAll();
255 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
257 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
258 theFeature ? theFeature->getKind() : "");
259 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
262 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
263 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
264 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
265 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
266 aPreviewOp->initFeature(theFeature);
267 aPreviewOp->initSelection(aSelected, aHighlighted);
269 anOperation->setEditingFeature(theFeature);
271 sendOperation(anOperation);
272 myWorkshop->actionsMgr()->updateCheckState();
275 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
277 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
278 aViewer->enableMultiselection(theEnabled);
281 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
283 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
285 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
286 for (; anIt != aLast; anIt++) {
287 activateFeature((*anIt), false);
290 aDisplayer->stopSelection(theFeatures, isStop, false);
292 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
293 aViewer->enableSelection(!isStop);
295 aDisplayer->updateViewer();
298 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
300 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
301 aDisplayer->setSelected(theFeatures, false);
302 aDisplayer->updateViewer();
305 void PartSet_Module::onCloseLocalContext()
307 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
308 aDisplayer->closeLocalContexts();
311 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
313 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
314 visualizePreview(theFeature, isDisplay, false);
316 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
318 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
320 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
321 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
322 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
324 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
326 for (; anIt != aLast; anIt++)
327 visualizePreview(*anIt, 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 (theCmdId == PartSet_OperationFeatureEditMulti::Type())
354 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
355 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
356 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
360 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
363 // set operation description and list of widgets corresponding to the feature xml definition
364 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
366 std::string aPluginFileName = featureFile(aFeatureKind);
367 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
368 aWdgReader.readAll();
369 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
370 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
372 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
373 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
374 // myWorkshop->moduleConnector());
375 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
376 //qDeleteAll(aContent->children());
377 //aFactory.createWidget(aContent);
379 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
380 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
382 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
384 // connect the operation
385 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
387 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
388 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
389 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
390 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
391 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
392 this, SLOT(onMultiSelectionEnabled(bool)));
394 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
395 this, SLOT(onMultiSelectionEnabled(bool)));
396 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
397 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
398 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
399 this, SLOT(onSetSelection(const QFeatureList&)));
401 connect(aPreviewOp, SIGNAL(closeLocalContext()),
402 this, SLOT(onCloseLocalContext()));
404 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
406 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
407 this, SLOT(onPlaneSelected(double, double, double)));
408 connect(aSketchOp, SIGNAL(fitAllView()),
409 this, SLOT(onFitAllView()));
416 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
418 //TODO(sbh): Implement static method to extract event id [SEID]
419 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
420 Config_PointerMessage aMessage(aModuleEvent, this);
421 aMessage.setPointer(theOperation);
422 Events_Loop::loop()->send(aMessage);
425 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
426 const bool isUpdateViewer)
428 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
432 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
436 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
438 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
439 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
442 Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(theFeature));
443 aDisplayer->redisplay(theFeature, anAIS, false);
447 aDisplayer->erase(theFeature, false);
450 aDisplayer->updateViewer();
453 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
455 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
456 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
458 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
459 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
464 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
466 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
470 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
474 FeaturePtr aFeature = aPreviewOp->feature();
475 if (!aFeature || aFeature->getKind() != theCmdId)
478 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
479 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
480 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
482 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
484 for (; anIt != aLast; anIt++) {
485 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
486 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
489 visualizePreview(*anIt, true, false);
490 aDisplayer->activateInLocalContext(*anIt, aModes, false);
492 aDisplayer->updateViewer();
495 void PartSet_Module::editFeature(FeaturePtr theFeature)
500 // if (theFeature->getKind() == SKETCH_KIND) {
501 FeaturePtr aFeature = theFeature;
502 if (XGUI_Tools::isModelObject(aFeature)) {
503 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
504 aFeature = aObject->featureRef();
508 onLaunchOperation(aFeature->getKind(), aFeature);
509 updateCurrentPreview(aFeature->getKind());
514 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
516 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
517 myWorkshop->operationMgr()->currentOperation());
521 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
522 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
524 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
525 aPoint->x(), aPoint->y());