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 <PartSet_Listener.h>
7 #include <PartSet_TestOCC.h>
8 #include <PartSet_WidgetSketchLabel.h>
9 #include <PartSet_Validators.h>
10 #include <PartSet_Tools.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 #include <ModuleBase_WidgetFactory.h>
15 #include <ModuleBase_Operation.h>
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModelAPI_Data.h>
22 #include <GeomDataAPI_Point2D.h>
24 #include <XGUI_MainWindow.h>
25 #include <XGUI_Displayer.h>
26 #include <XGUI_Viewer.h>
27 #include <XGUI_Workshop.h>
28 #include <XGUI_OperationMgr.h>
29 #include <XGUI_ViewPort.h>
30 #include <XGUI_ActionsMgr.h>
31 #include <XGUI_ViewerProxy.h>
32 #include <XGUI_ContextMenuMgr.h>
33 #include <XGUI_PropertyPanel.h>
34 #include <XGUI_ModuleConnector.h>
35 #include <XGUI_Tools.h>
37 #include <SketchPlugin_Line.h>
39 #include <Config_PointerMessage.h>
40 #include <Config_ModuleReader.h>
41 #include <Config_WidgetReader.h>
42 #include <Events_Loop.h>
43 //#include <Events_Message.h>
44 //#include <Events_Error.h>
46 #include <GeomAPI_Shape.h>
47 #include <GeomAPI_AISObject.h>
48 #include <AIS_Shape.hxx>
50 #include <StdSelect_FaceFilter.hxx>
51 #include <StdSelect_TypeOfFace.hxx>
54 #include <QMouseEvent>
57 #include <GeomAlgoAPI_FaceBuilder.h>
58 #include <GeomDataAPI_Dir.h>
64 /*!Create and return new instance of XGUI_Module*/
65 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
67 return new PartSet_Module(theWshop);
70 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
71 : ModuleBase_IModule(theWshop)
73 //myWorkshop = theWshop;
74 myListener = new PartSet_Listener(this);
76 connect(myWorkshop, SIGNAL(operationStarted(ModuleBase_Operation*)),
77 this, SLOT(onOperationStarted(ModuleBase_Operation*)));
79 connect(myWorkshop, SIGNAL(operationStopped(ModuleBase_Operation*)), this,
80 SLOT(onOperationStopped(ModuleBase_Operation*)));
82 XGUI_Workshop* aXWshop = xWorkshop();
83 XGUI_ContextMenuMgr* aContextMenuMgr = aXWshop->contextMenuMgr();
84 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
85 SLOT(onContextMenuCommand(const QString&, bool)));
87 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
88 SLOT(onMousePressed(QMouseEvent*)));
89 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
90 SLOT(onMouseReleased(QMouseEvent*)));
91 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
92 SLOT(onMouseMoved(QMouseEvent*)));
93 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
94 SLOT(onKeyRelease(QKeyEvent*)));
95 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
96 SLOT(onMouseDoubleClick(QMouseEvent*)));
98 myDocumentShapeFilter = new XGUI_ShapeDocumentFilter(aXWshop->displayer());
101 PartSet_Module::~PartSet_Module()
105 void PartSet_Module::createFeatures()
107 //Registering of validators
108 SessionPtr aMgr = ModelAPI_Session::get();
109 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
110 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
111 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
112 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
113 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
114 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
116 Config_ModuleReader aXMLReader = Config_ModuleReader();
117 aXMLReader.readAll();
118 myFeaturesInFiles = aXMLReader.featuresInFiles();
121 void PartSet_Module::featureCreated(QAction* theFeature)
123 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
126 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
128 return myFeaturesInFiles[theFeatureId];
134 void PartSet_Module::onFeatureTriggered()
136 QAction* aCmd = dynamic_cast<QAction*>(sender());
137 //Do nothing on uncheck
138 if (aCmd->isCheckable() && !aCmd->isChecked())
140 launchOperation(aCmd->data().toString());
144 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
146 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
148 XGUI_Workshop* aXWshp = xWorkshop();
149 XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
150 connect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
151 SLOT(onStorePoint2D(ObjectPtr, const std::string&)), Qt::UniqueConnection);
153 XGUI_Displayer* aDisplayer = aXWshp->displayer();
154 aDisplayer->openLocalContext();
155 aDisplayer->deactivateObjectsOutOfContext();
157 Handle(AIS_InteractiveContext) aAIS = xWorkshop()->viewer()->AISContext();
158 //TODO (VSV): We have to open Local context because at neutral point filters don't work (bug 25340)
159 aAIS->AddFilter(myDocumentShapeFilter);
163 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
167 XGUI_Workshop* aXWshp = xWorkshop();
168 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
170 XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
172 // Activate results of current feature for selection
173 FeaturePtr aFeature = theOperation->feature();
174 XGUI_Displayer* aDisplayer = aXWshp->displayer();
175 std::list<ResultPtr> aResults = aFeature->results();
176 std::list<ResultPtr>::const_iterator aIt;
177 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
178 aDisplayer->activate(*aIt);
181 Handle(AIS_InteractiveContext) aAIS = xWorkshop()->viewer()->AISContext();
182 aAIS->RemoveFilter(myDocumentShapeFilter);
186 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
188 QList<ObjectPtr> aFeatures = workshop()->selection()->selectedObjects();
189 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
190 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
192 editFeature(aFeature);
196 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
198 XGUI_Workshop* aXWshp = xWorkshop();
199 PartSet_OperationSketchBase* aPreviewOp =
200 dynamic_cast<PartSet_OperationSketchBase*>(workshop()->currentOperation());
201 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
202 if (aPreviewOp && (!aView.IsNull())) {
203 ModuleBase_ISelection* aSelection = workshop()->selection();
204 // Initialise operation with preliminary selection
205 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
206 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
208 aPreviewOp->mousePressed(theEvent, aView, aSelected, aHighlighted);
212 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
214 PartSet_OperationSketchBase* aPreviewOp =
215 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
216 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
217 if (aPreviewOp && (!aView.IsNull())) {
218 ModuleBase_ISelection* aSelection = workshop()->selection();
219 // Initialise operation with preliminary selection
220 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
221 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
223 aPreviewOp->mouseReleased(theEvent, aView, aSelected, aHighlighted);
227 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
229 PartSet_OperationSketchBase* aPreviewOp =
230 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
231 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
232 if (aPreviewOp && (!aView.IsNull()))
233 aPreviewOp->mouseMoved(theEvent, aView);
236 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
238 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
239 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
241 aPreviewOp->keyReleased(theEvent->key());
245 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
247 PartSet_OperationSketchBase* aPreviewOp =
248 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
249 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
250 if (aPreviewOp && (!aView.IsNull())) {
251 ModuleBase_ISelection* aSelection = workshop()->selection();
252 // Initialise operation with preliminary selection
253 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
254 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
255 aPreviewOp->mouseDoubleClick(theEvent, aView, aSelected, aHighlighted);
259 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
262 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
263 xWorkshop()->actionsMgr()->update();
265 //PartSet_TestOCC::testSelection(myWorkshop);
268 void PartSet_Module::onFitAllView()
270 myWorkshop->viewer()->fitAll();
273 void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
275 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
277 std::string aKind = aFeature ? aFeature->getKind() : "";
278 ModuleBase_Operation* anOperation = createOperation(theName, aKind);
280 PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
282 PartSet_OperationFeatureCreate* aCreateOp = dynamic_cast<PartSet_OperationFeatureCreate*>(anOperation);
284 aCreateOp->initFeature(aFeature);
286 anOperation->setFeature(aFeature);
288 ModuleBase_ISelection* aSelection = workshop()->selection();
289 // Initialise operation with preliminary selection
290 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
291 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
292 aSketchOp->initSelection(aSelected, aHighlighted);
293 } else if (aFeature) {
294 anOperation->setFeature(aFeature);
295 //Deactivate result of current feature in order to avoid its selection
296 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
297 std::list<ResultPtr> aResults = aFeature->results();
298 std::list<ResultPtr>::const_iterator aIt;
299 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
300 aDisplayer->deactivate(*aIt);
303 sendOperation(anOperation);
304 xWorkshop()->actionsMgr()->updateCheckState();
307 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
309 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
310 aViewer->enableMultiselection(theEnabled);
313 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
315 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
317 foreach(ObjectPtr aObject, theFeatures)
319 activateFeature(aObject, false);
322 aDisplayer->stopSelection(theFeatures, isStop, false);
324 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
325 aViewer->enableSelection(!isStop);
327 aDisplayer->updateViewer();
330 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
332 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
333 aDisplayer->setSelected(theFeatures, false);
334 aDisplayer->updateViewer();
337 void PartSet_Module::onCloseLocalContext()
339 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
340 aDisplayer->deactivateObjectsOutOfContext();
341 aDisplayer->closeLocalContexts();
344 void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
346 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
347 ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
348 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
350 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
351 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
352 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
353 std::list<FeaturePtr>::iterator aSFIt;
354 for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
355 std::list<ResultPtr> aResults = (*aSFIt)->results();
356 std::list<ResultPtr>::iterator aIt;
357 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
359 aDisplayer->activateInLocalContext((*aIt), aModes, false);
361 aDisplayer->erase((*aIt), false);
364 aDisplayer->erase((*aSFIt), false);
366 aDisplayer->deactivateObjectsOutOfContext();
369 ModelAPI_EventCreator::get()->sendUpdated(
370 theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
373 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
374 const std::string& theFeatureKind)
376 // create the operation
377 ModuleBase_Operation* anOperation = 0;
378 if (theCmdId == PartSet_OperationSketch::Type()) {
379 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
381 ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
383 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
385 aSketch = aPrevOp->sketch();
387 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
388 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
389 } else if (theCmdId == PartSet_OperationFeatureEditMulti::Type()) {
390 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
391 } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
392 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
397 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
400 // set operation description and list of widgets corresponding to the feature xml definition
401 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
403 std::string aPluginFileName = featureFile(aFeatureKind);
404 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
405 aWdgReader.readAll();
406 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
407 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
409 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
410 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
412 // connect the operation
413 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
415 connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
416 SLOT(onFeatureConstructed(ObjectPtr, int)));
417 connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
418 SLOT(onRestartOperation(std::string, ObjectPtr)));
419 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
420 SLOT(onMultiSelectionEnabled(bool)));
422 connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
423 SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
424 connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
425 SLOT(onSetSelection(const QList<ObjectPtr>&)));
427 connect(aPreviewOp, SIGNAL(closeLocalContext()), this, SLOT(onCloseLocalContext()));
429 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
431 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
432 SLOT(onPlaneSelected(double, double, double)));
433 connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
440 //void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
442 // static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
443 // boost::shared_ptr<Config_PointerMessage> aMessage =
444 // boost::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
445 // aMessage->setPointer(theOperation);
446 // Events_Loop::loop()->send(aMessage);
449 void PartSet_Module::activateFeature(ObjectPtr theFeature, const bool isUpdateViewer)
451 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
452 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
454 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
455 std::list<int> aModes = aPreviewOp->getSelectionModes(theFeature);
456 aDisplayer->activateInLocalContext(theFeature, aModes, isUpdateViewer);
458 // If this is a Sketcher then activate objects (planar faces) outside of context
459 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
461 Handle(StdSelect_FaceFilter) aFilter = new StdSelect_FaceFilter(StdSelect_Plane);
462 aDisplayer->activateObjectsOutOfContext(aModes, aFilter);
464 aDisplayer->deactivateObjectsOutOfContext();
469 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
471 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
475 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
479 FeaturePtr aFeature = aPreviewOp->feature();
480 if (!aFeature || aFeature->getKind() != theCmdId)
483 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
484 // Hide result of sketch
485 std::list<ResultPtr> aResults = aFeature->results();
486 std::list<ResultPtr>::const_iterator aIt;
487 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
488 aDisplayer->erase(*aIt, false);
490 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
491 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
493 std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
494 for (; anIt != aLast; anIt++) {
495 boost::shared_ptr<SketchPlugin_Feature> aSPFeature = boost::dynamic_pointer_cast<
496 SketchPlugin_Feature>(*anIt);
499 std::list<ResultPtr> aResults = aSPFeature->results();
500 std::list<ResultPtr>::const_iterator aRIt;
501 for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
502 aDisplayer->display((*aRIt), false);
503 aDisplayer->activateInLocalContext((*aRIt), aModes, false);
505 aDisplayer->display(aSPFeature, false);
506 aDisplayer->activateInLocalContext(aSPFeature, aModes, false);
508 aDisplayer->updateViewer();
511 void PartSet_Module::editFeature(FeaturePtr theFeature)
516 // if (theFeature->getKind() == SKETCH_KIND) {
517 //FeaturePtr aFeature = theFeature;
518 //if (XGUI_Tools::isModelObject(aFeature)) {
519 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
520 // aFeature = aObject->featureRef();
524 onRestartOperation(theFeature->getKind(), theFeature);
525 updateCurrentPreview(theFeature->getKind());
530 void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
532 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
534 PartSet_OperationSketchBase* aPreviewOp =
535 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
539 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
540 aFeature->data()->attribute(theAttribute));
542 PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
546 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
547 Config_WidgetAPI* theWidgetApi,
548 QList<ModuleBase_ModelWidget*>& theModelWidgets)
550 if (theType == "sketch-start-label") {
551 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
552 aWgt->setOperationsMgr(xWorkshop()->operationMgr());
553 theModelWidgets.append(aWgt);
554 return aWgt->getControl();
560 XGUI_Workshop* PartSet_Module::xWorkshop() const
562 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
564 return aConnector->workshop();