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>
9 #include <ModuleBase_Operation.h>
11 #include <XGUI_MainWindow.h>
12 #include <XGUI_Displayer.h>
13 #include <XGUI_Viewer.h>
14 #include <XGUI_Workshop.h>
15 #include <XGUI_OperationMgr.h>
16 #include <XGUI_SelectionMgr.h>
17 #include <XGUI_ViewPort.h>
18 #include <XGUI_ActionsMgr.h>
19 #include <XGUI_ViewerProxy.h>
21 #include <Config_PointerMessage.h>
22 #include <Config_ModuleReader.h>
23 #include <Config_WidgetReader.h>
24 #include <Events_Loop.h>
25 #include <Events_Message.h>
26 #include <Events_Error.h>
28 #include <GeomAPI_Shape.h>
30 #include <AIS_ListOfInteractive.hxx>
33 #include <QMouseEvent>
41 /*!Create and return new instance of XGUI_Module*/
42 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
44 return new PartSet_Module(theWshop);
47 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
49 myWorkshop = theWshop;
50 myListener = new PartSet_Listener(this);
52 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
54 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
55 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
57 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
58 this, SLOT(onMousePressed(QMouseEvent*)));
59 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
60 this, SLOT(onMouseReleased(QMouseEvent*)));
61 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
62 this, SLOT(onMouseMoved(QMouseEvent*)));
63 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
64 this, SLOT(onKeyRelease(QKeyEvent*)));
67 PartSet_Module::~PartSet_Module()
71 XGUI_Workshop* PartSet_Module::workshop() const
76 void PartSet_Module::createFeatures()
78 Config_ModuleReader aXMLReader = Config_ModuleReader();
80 myFeaturesInFiles = aXMLReader.featuresInFiles();
83 void PartSet_Module::featureCreated(QAction* theFeature)
85 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
88 QStringList PartSet_Module::nestedFeatures(QString)
93 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
95 return myFeaturesInFiles[theFeatureId];
101 void PartSet_Module::onFeatureTriggered()
103 QAction* aCmd = dynamic_cast<QAction*>(sender());
104 //Do nothing on uncheck
105 if(aCmd->isCheckable() && !aCmd->isChecked())
107 launchOperation(aCmd->data().toString());
110 void PartSet_Module::launchOperation(const QString& theCmdId)
112 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
113 sendOperation(anOperation);
116 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
120 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
125 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
127 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
128 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
129 editFeature(aFeatures.first());
133 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
135 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
136 myWorkshop->operationMgr()->currentOperation());
139 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
140 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
141 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
143 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
147 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
149 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
150 myWorkshop->operationMgr()->currentOperation());
153 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
154 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
155 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
157 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
161 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
163 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
164 myWorkshop->operationMgr()->currentOperation());
166 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
169 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
171 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
172 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
174 aPreviewOp->keyReleased(theEvent->key());
178 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
180 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
181 myWorkshop->actionsMgr()->update();
184 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
186 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
187 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
190 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
191 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
192 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected(TopAbs_VERTEX);
193 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted(TopAbs_VERTEX);
194 aPreviewOp->init(theFeature, aSelected, aHighlighted);
196 myWorkshop->actionsMgr()->updateCheckState();
197 sendOperation(anOperation);
200 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
202 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
203 aViewer->enableMultiselection(theEnabled);
206 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
208 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
210 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
211 boost::shared_ptr<ModelAPI_Feature> aFeature;
212 for (; anIt != aLast; anIt++) {
213 activateFeature((*anIt).feature(), false);
216 aDisplayer->StopSelection(theFeatures, isStop, false);
218 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
219 aViewer->enableSelection(!isStop);
221 aDisplayer->UpdateViewer();
224 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
226 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
227 aDisplayer->SetSelected(theFeatures, false);
228 aDisplayer->UpdateViewer();
231 void PartSet_Module::onCloseLocalContext()
233 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
234 aDisplayer->CloseLocalContexts();
237 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
240 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
241 visualizePreview(theFeature, isDisplay, false);
243 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
244 boost::shared_ptr<ModelAPI_Feature> aSketch;
245 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
247 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
248 aList = aPrevOp->subPreview();
249 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
250 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
252 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
253 anIt = aList.begin(), aLast = aList.end();
254 for (; anIt != aLast; anIt++) {
255 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
256 visualizePreview(aFeature, false, false);
258 aDisplayer->UpdateViewer();
262 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
263 theMode == PartSet_OperationSketchBase::FM_Deactivation)
264 activateFeature(theFeature, true);
267 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
269 // get operation xml description
270 std::string aStdCmdId = theCmdId;
271 if (aStdCmdId == PartSet_OperationEditLine::Type())
272 aStdCmdId = PartSet_OperationSketchLine::Type();
273 std::string aPluginFileName = featureFile(aStdCmdId);
274 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
275 aWdgReader.readAll();
276 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
277 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
279 // create the operation
280 ModuleBase_Operation* anOperation;
281 if (theCmdId == PartSet_OperationSketch::Type()) {
282 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
284 else if(theCmdId == PartSet_OperationSketchLine::Type() ||
285 theCmdId == PartSet_OperationEditLine::Type()) {
286 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
287 boost::shared_ptr<ModelAPI_Feature> aSketch;
288 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
290 aSketch = aPrevOp->sketch();
291 if (theCmdId == PartSet_OperationSketchLine::Type())
292 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
294 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
297 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
299 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
300 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
302 // connect the operation
303 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
305 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
306 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
307 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
308 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
309 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
310 this, SLOT(onMultiSelectionEnabled(bool)));
312 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
313 this, SLOT(onMultiSelectionEnabled(bool)));
314 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
315 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
316 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
317 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
319 connect(aPreviewOp, SIGNAL(closeLocalContext()),
320 this, SLOT(onCloseLocalContext()));
322 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
324 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
325 this, SLOT(onPlaneSelected(double, double, double)));
332 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
334 //TODO(sbh): Implement static method to extract event id [SEID]
335 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
336 Config_PointerMessage aMessage(aModuleEvent, this);
337 aMessage.setPointer(theOperation);
338 Events_Loop::loop()->send(aMessage);
341 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
342 const bool isUpdateViewer)
344 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
348 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
352 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
354 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
355 aDisplayer->Redisplay(theFeature,
356 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
359 aDisplayer->Erase(theFeature, false);
362 aDisplayer->UpdateViewer();
365 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
366 const bool isUpdateViewer)
368 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
369 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
371 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
372 aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
377 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
379 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
383 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
387 boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
388 if (!aFeature || aFeature->getKind() != theCmdId)
391 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
392 aList = aPreviewOp->subPreview();
393 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
394 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
396 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
397 anIt = aList.begin(), aLast = aList.end();
398 for (; anIt != aLast; anIt++) {
399 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
400 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
401 aDisplayer->Redisplay(aFeature,
402 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
403 aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
405 aDisplayer->UpdateViewer();
408 void PartSet_Module::editFeature(FeaturePtr theFeature)
413 if (theFeature->getKind() == "Sketch") {
414 onLaunchOperation(theFeature->getKind(), theFeature);
415 visualizePreview(theFeature, true);