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>
12 #include <XGUI_MainWindow.h>
13 #include <XGUI_Displayer.h>
14 #include <XGUI_Viewer.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_OperationMgr.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <XGUI_ViewPort.h>
19 #include <XGUI_ActionsMgr.h>
20 #include <XGUI_ViewerProxy.h>
21 #include <XGUI_ContextMenuMgr.h>
22 #include <XGUI_PropertyPanel.h>
24 #include <Config_PointerMessage.h>
25 #include <Config_ModuleReader.h>
26 #include <Config_WidgetReader.h>
27 #include <Events_Loop.h>
28 #include <Events_Message.h>
29 #include <Events_Error.h>
31 #include <GeomAPI_Shape.h>
33 #include <AIS_ListOfInteractive.hxx>
36 #include <QMouseEvent>
43 /*!Create and return new instance of XGUI_Module*/
44 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
46 return new PartSet_Module(theWshop);
49 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
51 myWorkshop = theWshop;
52 myListener = new PartSet_Listener(this);
54 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
56 connect(anOperationMgr, SIGNAL(operationStarted()),
57 this, SLOT(onOperationStarted()));
59 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
60 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
62 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
63 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
64 this, SLOT(onContextMenuCommand(const QString&, bool)));
66 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
67 this, SLOT(onMousePressed(QMouseEvent*)));
68 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
69 this, SLOT(onMouseReleased(QMouseEvent*)));
70 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
71 this, SLOT(onMouseMoved(QMouseEvent*)));
72 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
73 this, SLOT(onKeyRelease(QKeyEvent*)));
76 PartSet_Module::~PartSet_Module()
80 XGUI_Workshop* PartSet_Module::workshop() const
85 void PartSet_Module::createFeatures()
87 Config_ModuleReader aXMLReader = Config_ModuleReader();
89 myFeaturesInFiles = aXMLReader.featuresInFiles();
92 void PartSet_Module::featureCreated(QAction* theFeature)
94 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
97 QStringList PartSet_Module::nestedFeatures(QString)
102 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
104 return myFeaturesInFiles[theFeatureId];
110 void PartSet_Module::onFeatureTriggered()
112 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
113 // myWorkshop->viewer()->activeView());
115 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
116 // myWorkshop->viewer()->activeView());
117 QAction* aCmd = dynamic_cast<QAction*>(sender());
118 //Do nothing on uncheck
119 if(aCmd->isCheckable() && !aCmd->isChecked())
121 launchOperation(aCmd->data().toString());
124 void PartSet_Module::launchOperation(const QString& theCmdId)
126 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
127 sendOperation(anOperation);
130 void PartSet_Module::onOperationStarted()
132 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
133 myWorkshop->operationMgr()->currentOperation());
135 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
136 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
137 aPropPanel, SLOT(onFocusActivated(const std::string&)));
141 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
145 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
147 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
148 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
149 aPropPanel, SLOT(onFocusActivated(const std::string&)));
153 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
155 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
156 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
157 editFeature(aFeatures.first());
161 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
163 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
164 myWorkshop->operationMgr()->currentOperation());
167 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
168 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
169 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
171 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
175 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
177 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
178 myWorkshop->operationMgr()->currentOperation());
181 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
182 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
183 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
185 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
189 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
191 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
192 myWorkshop->operationMgr()->currentOperation());
194 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
197 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
199 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
200 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
202 aPreviewOp->keyReleased(theEvent->key());
206 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
208 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
209 myWorkshop->actionsMgr()->update();
211 //PartSet_TestOCC::testSelection(myWorkshop);
214 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
216 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
217 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
220 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
221 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
222 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected(TopAbs_VERTEX);
223 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted(TopAbs_VERTEX);
224 aPreviewOp->init(theFeature, aSelected, aHighlighted);
226 sendOperation(anOperation);
227 myWorkshop->actionsMgr()->updateCheckState();
230 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
232 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
233 aViewer->enableMultiselection(theEnabled);
236 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
238 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
240 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
241 boost::shared_ptr<ModelAPI_Feature> aFeature;
242 for (; anIt != aLast; anIt++) {
243 activateFeature((*anIt).feature(), false);
246 aDisplayer->StopSelection(theFeatures, isStop, false);
248 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
249 aViewer->enableSelection(!isStop);
251 aDisplayer->UpdateViewer();
254 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
256 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
257 aDisplayer->SetSelected(theFeatures, false);
258 aDisplayer->UpdateViewer();
261 void PartSet_Module::onCloseLocalContext()
263 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
264 aDisplayer->CloseLocalContexts();
267 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
270 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
271 visualizePreview(theFeature, isDisplay, false);
273 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
274 boost::shared_ptr<ModelAPI_Feature> aSketch;
275 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
277 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
278 aList = aPrevOp->subPreview();
279 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
280 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
282 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
283 anIt = aList.begin(), aLast = aList.end();
284 for (; anIt != aLast; anIt++) {
285 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
286 visualizePreview(aFeature, false, false);
288 aDisplayer->UpdateViewer();
292 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
293 theMode == PartSet_OperationSketchBase::FM_Deactivation)
294 activateFeature(theFeature, true);
297 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
299 // get operation xml description
300 std::string aStdCmdId = theCmdId;
301 if (aStdCmdId == PartSet_OperationEditLine::Type())
302 aStdCmdId = PartSet_OperationSketchLine::Type();
303 std::string aPluginFileName = featureFile(aStdCmdId);
304 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
305 aWdgReader.readAll();
306 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
307 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
309 // create the operation
310 ModuleBase_Operation* anOperation;
311 if (theCmdId == PartSet_OperationSketch::Type()) {
312 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
314 else if(theCmdId == PartSet_OperationSketchLine::Type() ||
315 theCmdId == PartSet_OperationEditLine::Type()) {
316 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
317 boost::shared_ptr<ModelAPI_Feature> aSketch;
318 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
320 aSketch = aPrevOp->sketch();
321 if (theCmdId == PartSet_OperationSketchLine::Type())
322 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
324 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
327 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
329 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
330 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
332 // connect the operation
333 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
335 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
336 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
337 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
338 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
339 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
340 this, SLOT(onMultiSelectionEnabled(bool)));
342 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
343 this, SLOT(onMultiSelectionEnabled(bool)));
344 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
345 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
346 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
347 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
349 connect(aPreviewOp, SIGNAL(closeLocalContext()),
350 this, SLOT(onCloseLocalContext()));
352 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
354 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
355 this, SLOT(onPlaneSelected(double, double, double)));
362 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
364 //TODO(sbh): Implement static method to extract event id [SEID]
365 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
366 Config_PointerMessage aMessage(aModuleEvent, this);
367 aMessage.setPointer(theOperation);
368 Events_Loop::loop()->send(aMessage);
371 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
372 const bool isUpdateViewer)
374 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
378 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
382 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
384 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
385 aDisplayer->Redisplay(theFeature,
386 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
389 aDisplayer->Erase(theFeature, false);
392 aDisplayer->UpdateViewer();
395 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
396 const bool isUpdateViewer)
398 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
399 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
401 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
402 aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
407 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
409 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
413 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
417 boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
418 if (!aFeature || aFeature->getKind() != theCmdId)
421 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
422 aList = aPreviewOp->subPreview();
423 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
424 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
426 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
427 anIt = aList.begin(), aLast = aList.end();
428 for (; anIt != aLast; anIt++) {
429 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
430 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
431 aDisplayer->Redisplay(aFeature,
432 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
433 aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
435 aDisplayer->UpdateViewer();
438 void PartSet_Module::editFeature(FeaturePtr theFeature)
443 if (theFeature->getKind() == "Sketch") {
444 onLaunchOperation(theFeature->getKind(), theFeature);
445 updateCurrentPreview(theFeature->getKind());