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 aAIS->AddFilter(myDocumentShapeFilter);
162 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
166 XGUI_Workshop* aXWshp = xWorkshop();
167 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
169 XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
171 // Activate results of current feature for selection
172 FeaturePtr aFeature = theOperation->feature();
173 XGUI_Displayer* aDisplayer = aXWshp->displayer();
174 std::list<ResultPtr> aResults = aFeature->results();
175 std::list<ResultPtr>::const_iterator aIt;
176 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
177 aDisplayer->activate(*aIt);
180 Handle(AIS_InteractiveContext) aAIS = xWorkshop()->viewer()->AISContext();
181 aAIS->RemoveFilter(myDocumentShapeFilter);
185 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
187 QList<ObjectPtr> aFeatures = workshop()->selection()->selectedObjects();
188 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
189 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
191 editFeature(aFeature);
195 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
197 XGUI_Workshop* aXWshp = xWorkshop();
198 PartSet_OperationSketchBase* aPreviewOp =
199 dynamic_cast<PartSet_OperationSketchBase*>(workshop()->currentOperation());
200 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
201 if (aPreviewOp && (!aView.IsNull())) {
202 ModuleBase_ISelection* aSelection = workshop()->selection();
203 // Initialise operation with preliminary selection
204 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
205 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
207 aPreviewOp->mousePressed(theEvent, aView, aSelected, aHighlighted);
211 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
213 PartSet_OperationSketchBase* aPreviewOp =
214 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
215 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
216 if (aPreviewOp && (!aView.IsNull())) {
217 ModuleBase_ISelection* aSelection = workshop()->selection();
218 // Initialise operation with preliminary selection
219 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
220 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
222 aPreviewOp->mouseReleased(theEvent, aView, aSelected, aHighlighted);
226 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
228 PartSet_OperationSketchBase* aPreviewOp =
229 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
230 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
231 if (aPreviewOp && (!aView.IsNull()))
232 aPreviewOp->mouseMoved(theEvent, aView);
235 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
237 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
238 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
240 aPreviewOp->keyReleased(theEvent->key());
244 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
246 PartSet_OperationSketchBase* aPreviewOp =
247 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
248 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
249 if (aPreviewOp && (!aView.IsNull())) {
250 ModuleBase_ISelection* aSelection = workshop()->selection();
251 // Initialise operation with preliminary selection
252 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
253 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
254 aPreviewOp->mouseDoubleClick(theEvent, aView, aSelected, aHighlighted);
258 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
261 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
262 xWorkshop()->actionsMgr()->update();
264 //PartSet_TestOCC::testSelection(myWorkshop);
267 void PartSet_Module::onFitAllView()
269 myWorkshop->viewer()->fitAll();
272 void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
274 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
276 std::string aKind = aFeature ? aFeature->getKind() : "";
277 ModuleBase_Operation* anOperation = createOperation(theName, aKind);
279 PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
281 PartSet_OperationFeatureCreate* aCreateOp = dynamic_cast<PartSet_OperationFeatureCreate*>(anOperation);
283 aCreateOp->initFeature(aFeature);
285 anOperation->setFeature(aFeature);
287 ModuleBase_ISelection* aSelection = workshop()->selection();
288 // Initialise operation with preliminary selection
289 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
290 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
291 aSketchOp->initSelection(aSelected, aHighlighted);
292 } else if (aFeature) {
293 anOperation->setFeature(aFeature);
294 //Deactivate result of current feature in order to avoid its selection
295 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
296 std::list<ResultPtr> aResults = aFeature->results();
297 std::list<ResultPtr>::const_iterator aIt;
298 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
299 aDisplayer->deactivate(*aIt);
302 sendOperation(anOperation);
303 xWorkshop()->actionsMgr()->updateCheckState();
306 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
308 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
309 aViewer->enableMultiselection(theEnabled);
312 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
314 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
316 foreach(ObjectPtr aObject, theFeatures)
318 activateFeature(aObject, false);
321 aDisplayer->stopSelection(theFeatures, isStop, false);
323 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
324 aViewer->enableSelection(!isStop);
326 aDisplayer->updateViewer();
329 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
331 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
332 aDisplayer->setSelected(theFeatures, false);
333 aDisplayer->updateViewer();
336 void PartSet_Module::onCloseLocalContext()
338 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
339 aDisplayer->deactivateObjectsOutOfContext();
340 aDisplayer->closeLocalContexts();
343 void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
345 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
346 ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
347 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
349 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
350 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
351 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
352 std::list<FeaturePtr>::iterator aSFIt;
353 for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
354 std::list<ResultPtr> aResults = (*aSFIt)->results();
355 std::list<ResultPtr>::iterator aIt;
356 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
358 aDisplayer->activateInLocalContext((*aIt), aModes, false);
360 aDisplayer->erase((*aIt), false);
363 aDisplayer->erase((*aSFIt), false);
365 aDisplayer->deactivateObjectsOutOfContext();
368 ModelAPI_EventCreator::get()->sendUpdated(
369 theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
372 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
373 const std::string& theFeatureKind)
375 // create the operation
376 ModuleBase_Operation* anOperation = 0;
377 if (theCmdId == PartSet_OperationSketch::Type()) {
378 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
380 ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
382 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
384 aSketch = aPrevOp->sketch();
386 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
387 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
388 } else if (theCmdId == PartSet_OperationFeatureEditMulti::Type()) {
389 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
390 } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
391 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
396 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
399 // set operation description and list of widgets corresponding to the feature xml definition
400 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
402 std::string aPluginFileName = featureFile(aFeatureKind);
403 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
404 aWdgReader.readAll();
405 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
406 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
408 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
409 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
411 // connect the operation
412 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
414 connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
415 SLOT(onFeatureConstructed(ObjectPtr, int)));
416 connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
417 SLOT(onRestartOperation(std::string, ObjectPtr)));
418 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
419 SLOT(onMultiSelectionEnabled(bool)));
421 connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
422 SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
423 connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
424 SLOT(onSetSelection(const QList<ObjectPtr>&)));
426 connect(aPreviewOp, SIGNAL(closeLocalContext()), this, SLOT(onCloseLocalContext()));
428 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
430 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
431 SLOT(onPlaneSelected(double, double, double)));
432 connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
439 //void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
441 // static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
442 // boost::shared_ptr<Config_PointerMessage> aMessage =
443 // boost::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
444 // aMessage->setPointer(theOperation);
445 // Events_Loop::loop()->send(aMessage);
448 void PartSet_Module::activateFeature(ObjectPtr theFeature, const bool isUpdateViewer)
450 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
451 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
453 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
454 std::list<int> aModes = aPreviewOp->getSelectionModes(theFeature);
455 aDisplayer->activateInLocalContext(theFeature, aModes, isUpdateViewer);
457 // If this is a Sketcher then activate objects (planar faces) outside of context
458 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
460 Handle(StdSelect_FaceFilter) aFilter = new StdSelect_FaceFilter(StdSelect_Plane);
461 aDisplayer->activateObjectsOutOfContext(aModes, aFilter);
463 aDisplayer->deactivateObjectsOutOfContext();
468 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
470 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
474 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
478 FeaturePtr aFeature = aPreviewOp->feature();
479 if (!aFeature || aFeature->getKind() != theCmdId)
482 XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
483 // Hide result of sketch
484 std::list<ResultPtr> aResults = aFeature->results();
485 std::list<ResultPtr>::const_iterator aIt;
486 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
487 aDisplayer->erase(*aIt, false);
489 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
490 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
492 std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
493 for (; anIt != aLast; anIt++) {
494 boost::shared_ptr<SketchPlugin_Feature> aSPFeature = boost::dynamic_pointer_cast<
495 SketchPlugin_Feature>(*anIt);
498 std::list<ResultPtr> aResults = aSPFeature->results();
499 std::list<ResultPtr>::const_iterator aRIt;
500 for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
501 aDisplayer->display((*aRIt), false);
502 aDisplayer->activateInLocalContext((*aRIt), aModes, false);
504 aDisplayer->display(aSPFeature, false);
505 aDisplayer->activateInLocalContext(aSPFeature, aModes, false);
507 aDisplayer->updateViewer();
510 void PartSet_Module::editFeature(FeaturePtr theFeature)
515 // if (theFeature->getKind() == SKETCH_KIND) {
516 //FeaturePtr aFeature = theFeature;
517 //if (XGUI_Tools::isModelObject(aFeature)) {
518 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
519 // aFeature = aObject->featureRef();
523 onRestartOperation(theFeature->getKind(), theFeature);
524 updateCurrentPreview(theFeature->getKind());
529 void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
531 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
533 PartSet_OperationSketchBase* aPreviewOp =
534 dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
538 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
539 aFeature->data()->attribute(theAttribute));
541 PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
545 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
546 Config_WidgetAPI* theWidgetApi,
547 QList<ModuleBase_ModelWidget*>& theModelWidgets)
549 if (theType == "sketch-start-label") {
550 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
551 aWgt->setOperationsMgr(xWorkshop()->operationMgr());
552 theModelWidgets.append(aWgt);
553 return aWgt->getControl();
559 XGUI_Workshop* PartSet_Module::xWorkshop() const
561 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
563 return aConnector->workshop();