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>
43 #include <GeomAPI_AISObject.h>
46 #include <QMouseEvent>
53 /*!Create and return new instance of XGUI_Module*/
54 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
56 return new PartSet_Module(theWshop);
59 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
61 myWorkshop = theWshop;
62 myListener = new PartSet_Listener(this);
64 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
66 connect(anOperationMgr, SIGNAL(operationStarted()),
67 this, SLOT(onOperationStarted()));
69 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
70 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
72 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
73 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
74 this, SLOT(onContextMenuCommand(const QString&, bool)));
76 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
77 this, SLOT(onMousePressed(QMouseEvent*)));
78 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
79 this, SLOT(onMouseReleased(QMouseEvent*)));
80 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
81 this, SLOT(onMouseMoved(QMouseEvent*)));
82 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
83 this, SLOT(onKeyRelease(QKeyEvent*)));
84 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
85 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
88 PartSet_Module::~PartSet_Module()
92 XGUI_Workshop* PartSet_Module::workshop() const
97 void PartSet_Module::createFeatures()
99 Config_ModuleReader aXMLReader = Config_ModuleReader();
100 aXMLReader.readAll();
101 myFeaturesInFiles = aXMLReader.featuresInFiles();
104 void PartSet_Module::featureCreated(QAction* theFeature)
106 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
109 QStringList PartSet_Module::nestedFeatures(QString)
111 return QStringList();
114 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
116 return myFeaturesInFiles[theFeatureId];
122 void PartSet_Module::onFeatureTriggered()
124 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
125 // myWorkshop->viewer()->activeView());
127 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
128 // myWorkshop->viewer()->activeView());
129 QAction* aCmd = dynamic_cast<QAction*>(sender());
130 //Do nothing on uncheck
131 if(aCmd->isCheckable() && !aCmd->isChecked())
133 launchOperation(aCmd->data().toString());
136 void PartSet_Module::launchOperation(const QString& theCmdId)
138 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
139 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
141 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
142 // Initialise operation with preliminary selection
143 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
144 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
145 aPreviewOp->initSelection(aSelected, aHighlighted);
147 sendOperation(anOperation);
150 void PartSet_Module::onOperationStarted()
152 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
153 myWorkshop->operationMgr()->currentOperation());
155 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
156 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
157 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
161 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
165 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
167 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
168 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
169 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
173 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
175 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
176 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
177 editFeature(aFeatures.first());
181 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
183 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
184 myWorkshop->operationMgr()->currentOperation());
187 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
188 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
189 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
191 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
195 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
197 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
198 myWorkshop->operationMgr()->currentOperation());
201 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
202 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
203 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
205 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
209 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
211 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
212 myWorkshop->operationMgr()->currentOperation());
214 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
217 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
219 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
220 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
222 aPreviewOp->keyReleased(theEvent->key());
226 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
228 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
229 myWorkshop->operationMgr()->currentOperation());
232 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
233 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
234 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
235 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
240 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
242 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
243 myWorkshop->actionsMgr()->update();
245 //PartSet_TestOCC::testSelection(myWorkshop);
248 void PartSet_Module::onFitAllView()
250 myWorkshop->viewer()->fitAll();
253 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
255 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
256 theFeature ? theFeature->getKind() : "");
257 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
260 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
261 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
262 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
263 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
264 aPreviewOp->initFeature(theFeature);
265 aPreviewOp->initSelection(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::list<FeaturePtr> aList = aPrevOp->subFeatures();
319 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
320 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
322 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
324 for (; anIt != aLast; anIt++)
325 visualizePreview(*anIt, false, false);
326 aDisplayer->updateViewer();
330 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
331 theMode == PartSet_OperationSketchBase::FM_Deactivation)
332 activateFeature(theFeature, true);
335 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
336 const std::string& theFeatureKind)
338 // create the operation
339 ModuleBase_Operation* anOperation = 0;
340 if (theCmdId == PartSet_OperationSketch::Type()) {
341 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
344 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
346 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
348 aSketch = aPrevOp->sketch();
349 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
350 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
351 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
352 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
353 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
354 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
358 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
361 // set operation description and list of widgets corresponding to the feature xml definition
362 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
364 std::string aPluginFileName = featureFile(aFeatureKind);
365 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
366 aWdgReader.readAll();
367 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
368 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
370 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
371 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
372 // myWorkshop->moduleConnector());
373 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
374 //qDeleteAll(aContent->children());
375 //aFactory.createWidget(aContent);
377 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
378 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
380 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
382 // connect the operation
383 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
385 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
386 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
387 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
388 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
389 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
390 this, SLOT(onMultiSelectionEnabled(bool)));
392 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
393 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
394 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
395 this, SLOT(onSetSelection(const QFeatureList&)));
397 connect(aPreviewOp, SIGNAL(closeLocalContext()),
398 this, SLOT(onCloseLocalContext()));
400 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
402 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
403 this, SLOT(onPlaneSelected(double, double, double)));
404 connect(aSketchOp, SIGNAL(fitAllView()),
405 this, SLOT(onFitAllView()));
412 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
414 //TODO(sbh): Implement static method to extract event id [SEID]
415 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
416 Config_PointerMessage aMessage(aModuleEvent, this);
417 aMessage.setPointer(theOperation);
418 Events_Loop::loop()->send(aMessage);
421 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
422 const bool isUpdateViewer)
424 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
428 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
432 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
434 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
435 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
438 boost::shared_ptr<GeomAPI_AISObject> anAIS =
439 aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
440 aDisplayer->redisplay(theFeature, anAIS, false);
444 aDisplayer->erase(theFeature, false);
447 aDisplayer->updateViewer();
450 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
452 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
453 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
455 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
456 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
461 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
463 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
467 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
471 FeaturePtr aFeature = aPreviewOp->feature();
472 if (!aFeature || aFeature->getKind() != theCmdId)
475 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
476 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
477 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
479 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
481 for (; anIt != aLast; anIt++) {
482 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
483 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
486 visualizePreview(*anIt, true, false);
487 aDisplayer->activateInLocalContext(*anIt, aModes, false);
489 aDisplayer->updateViewer();
492 void PartSet_Module::editFeature(FeaturePtr theFeature)
497 // if (theFeature->getKind() == SKETCH_KIND) {
498 FeaturePtr aFeature = theFeature;
499 if (XGUI_Tools::isModelObject(aFeature)) {
500 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
501 aFeature = aObject->featureRef();
505 onLaunchOperation(aFeature->getKind(), aFeature);
506 updateCurrentPreview(aFeature->getKind());
511 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
513 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
514 myWorkshop->operationMgr()->currentOperation());
518 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
519 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
521 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
522 aPoint->x(), aPoint->y());
525 bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
527 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
528 XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
530 ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
532 return !aActMgr->isNested(theCmdId);
534 PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
536 QStringList aConstraintList;
537 aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
538 <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
539 return aConstraintList.contains(theCmdId);
541 QStringList aList = aActMgr->nestedCommands(aOperation->id());
542 return aList.contains(theCmdId);