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::onMousePressed(QMouseEvent* theEvent)
127 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
128 myWorkshop->operationMgr()->currentOperation());
131 std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
132 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
136 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
138 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
139 myWorkshop->operationMgr()->currentOperation());
142 std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
143 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
147 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
149 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
150 myWorkshop->operationMgr()->currentOperation());
152 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
155 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
157 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
158 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
160 aPreviewOp->keyReleased(theEvent->key());
164 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
166 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
167 myWorkshop->actionsMgr()->setNestedActionsEnabled(true);
170 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
172 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
173 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
176 std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
177 aPreviewOp->init(theFeature, aPresentations);
179 myWorkshop->actionsMgr()->setActionChecked(anOperation->getDescription()->operationId(), true);
180 sendOperation(anOperation);
183 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
185 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
186 aViewer->enableMultiselection(theEnabled);
189 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
191 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
193 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
194 boost::shared_ptr<ModelAPI_Feature> aFeature;
195 for (; anIt != aLast; anIt++) {
196 activateFeature((*anIt).feature(), false);
199 aDisplayer->StopSelection(theFeatures, isStop, false);
200 aDisplayer->UpdateViewer();
203 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
205 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
206 aDisplayer->SetSelected(theFeatures, false);
207 aDisplayer->UpdateViewer();
210 void PartSet_Module::onCloseLocalContext()
212 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
213 aDisplayer->CloseLocalContexts();
216 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
219 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
220 visualizePreview(theFeature, isDisplay, false);
222 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
223 boost::shared_ptr<ModelAPI_Feature> aSketch;
224 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
226 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
227 aList = aPrevOp->subPreview();
228 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
229 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
231 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
232 anIt = aList.begin(), aLast = aList.end();
233 for (; anIt != aLast; anIt++) {
234 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
235 visualizePreview(aFeature, false, false);
237 aDisplayer->UpdateViewer();
241 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
242 theMode == PartSet_OperationSketchBase::FM_Deactivation)
243 activateFeature(theFeature, true);
246 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
248 // get operation xml description
249 std::string aStdCmdId = theCmdId;
250 if (aStdCmdId == PartSet_OperationEditLine::Type())
251 aStdCmdId = PartSet_OperationSketchLine::Type();
252 std::string aPluginFileName = featureFile(aStdCmdId);
253 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
254 aWdgReader.readAll();
255 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
256 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
258 // create the operation
259 ModuleBase_Operation* anOperation;
260 if (theCmdId == PartSet_OperationSketch::Type()) {
261 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
263 else if(theCmdId == PartSet_OperationSketchLine::Type() ||
264 theCmdId == PartSet_OperationEditLine::Type()) {
265 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
266 boost::shared_ptr<ModelAPI_Feature> aSketch;
267 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
269 aSketch = aPrevOp->sketch();
270 if (theCmdId == PartSet_OperationSketchLine::Type())
271 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
273 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
276 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
278 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
279 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
281 // connect the operation
282 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
284 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
285 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
286 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
287 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
288 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
289 this, SLOT(onMultiSelectionEnabled(bool)));
291 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
292 this, SLOT(onMultiSelectionEnabled(bool)));
293 connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
294 this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
295 connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
296 this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
298 connect(aPreviewOp, SIGNAL(closeLocalContext()),
299 this, SLOT(onCloseLocalContext()));
301 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
303 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
304 this, SLOT(onPlaneSelected(double, double, double)));
311 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
313 //TODO(sbh): Implement static method to extract event id [SEID]
314 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
315 Config_PointerMessage aMessage(aModuleEvent, this);
316 aMessage.setPointer(theOperation);
317 Events_Loop::loop()->send(aMessage);
320 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
321 const bool isUpdateViewer)
323 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
327 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
331 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
333 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
334 aDisplayer->Redisplay(theFeature,
335 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
338 aDisplayer->Erase(theFeature, false);
341 aDisplayer->UpdateViewer();
344 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
345 const bool isUpdateViewer)
347 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
348 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
350 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
351 aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
356 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
358 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
362 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
366 boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
367 if (!aFeature || aFeature->getKind() != theCmdId)
370 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
371 aList = aPreviewOp->subPreview();
372 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
373 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
375 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
376 anIt = aList.begin(), aLast = aList.end();
377 for (; anIt != aLast; anIt++) {
378 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
379 boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
380 aDisplayer->Redisplay(aFeature,
381 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
382 aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
384 aDisplayer->UpdateViewer();