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_Tools.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>
22 #include <Config_PointerMessage.h>
23 #include <Config_ModuleReader.h>
24 #include <Config_WidgetReader.h>
25 #include <Events_Loop.h>
26 #include <Events_Message.h>
27 #include <Events_Error.h>
29 #include <GeomAPI_Shape.h>
31 #include <AIS_ListOfInteractive.hxx>
34 #include <QMouseEvent>
42 /*!Create and return new instance of XGUI_Module*/
43 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
45 return new PartSet_Module(theWshop);
48 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
50 myWorkshop = theWshop;
51 myListener = new PartSet_Listener(this);
53 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
55 connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
56 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
57 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
59 connect(myWorkshop->selector(), SIGNAL(selectionChanged()),
60 this, SLOT(onSelectionChanged()));
61 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
62 this, SLOT(onMouseReleased(QMouseEvent*)));
63 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
64 this, SLOT(onMouseMoved(QMouseEvent*)));
65 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
66 this, SLOT(onKeyRelease(QKeyEvent*)));
69 PartSet_Module::~PartSet_Module()
73 void PartSet_Module::createFeatures()
75 Config_ModuleReader aXMLReader = Config_ModuleReader();
77 myFeaturesInFiles = aXMLReader.featuresInFiles();
80 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
82 theFeature->connectTo(this, SLOT(onFeatureTriggered()));
85 QStringList PartSet_Module::nestedFeatures(QString)
90 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
92 return myFeaturesInFiles[theFeatureId];
98 void PartSet_Module::onFeatureTriggered()
100 XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
101 //Do nothing on uncheck
102 if(aCmd->isCheckable() && !aCmd->isChecked())
104 launchOperation(aCmd->id());
107 void PartSet_Module::launchOperation(const QString& theCmdId)
109 ModuleBase_Operation* anOperation = createOperation(theCmdId);
110 sendOperation(anOperation);
113 void PartSet_Module::onOperationStarted()
115 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
117 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
119 visualizePreview(aPreviewOp->feature(), true);
121 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
122 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
123 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
124 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
126 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
128 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
129 this, SLOT(onPlaneSelected(double, double, double)));
134 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
138 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
140 // visualizePreview(false);
143 void PartSet_Module::onSelectionChanged()
145 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
146 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
148 XGUI_SelectionMgr* aSelector = myWorkshop->selector();
150 NCollection_List<TopoDS_Shape> aList;
151 aSelector->selectedShapes(aList);
153 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
154 boost::shared_ptr<ModelAPI_Feature> aFeature;
155 if (!aList.IsEmpty()) {
156 const TopoDS_Shape& aShape = aList.First();
157 aFeature = aDisplayer->GetFeature(aShape);
158 aPreviewOp->setSelected(aFeature, aShape);
164 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
166 QPoint aPoint = theEvent->pos();
167 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
168 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
170 XGUI_SelectionMgr* aSelector = myWorkshop->selector();
172 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
173 if ( !aView3d.IsNull() ) {
174 gp_Pnt aPnt = PartSet_Tools::ConvertClickToPoint(aPoint, aView3d);
175 aPreviewOp->mouseReleased(aPnt);
181 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
183 QPoint aPoint = theEvent->pos();
184 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
185 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
187 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
188 if ( !aView3d.IsNull() ) {
189 gp_Pnt aPnt = PartSet_Tools::ConvertClickToPoint(aPoint, aView3d);
190 aPreviewOp->mouseMoved(aPnt);
195 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
197 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
198 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
200 aPreviewOp->keyReleased(theEvent->key());
204 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
206 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
208 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
210 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
212 visualizePreview(aPreviewOp->feature(), false);
216 myWorkshop->actionsMgr()->setNestedActionsEnabled(true);
219 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
221 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
222 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
225 aPreviewOp->init(theFeature);
227 sendOperation(anOperation);
230 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
233 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Abort;
234 visualizePreview(theFeature, isDisplay);
237 ModuleBase_Operation* PartSet_Module::createOperation(const QString& theCmdId)
239 std::string aStdCmdId = theCmdId.toStdString();
240 if (aStdCmdId == "EditLine")
241 aStdCmdId = "SketchLine";
242 std::string aPluginFileName = featureFile(aStdCmdId);
243 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
244 aWdgReader.readAll();
245 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
246 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
247 ModuleBase_Operation* anOperation;
248 if (theCmdId == "Sketch" ) {
249 anOperation = new PartSet_OperationSketch(theCmdId, this);
251 else if(theCmdId == "SketchLine" || theCmdId == "EditLine") {
252 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
253 boost::shared_ptr<ModelAPI_Feature> aSketchFeature;
255 aSketchFeature = aCurOperation->feature();
256 if (theCmdId == "SketchLine")
257 anOperation = new PartSet_OperationSketchLine(theCmdId, this, aSketchFeature);
259 anOperation = new PartSet_OperationEditLine(theCmdId, this, aSketchFeature);
262 anOperation = new ModuleBase_Operation(theCmdId, this);
264 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
265 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
270 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
272 //TODO(sbh): Implement static method to extract event id [SEID]
273 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
274 Config_PointerMessage aMessage(aModuleEvent, this);
275 aMessage.setPointer(theOperation);
276 Events_Loop::loop()->send(aMessage);
279 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay)
281 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
285 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
289 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
291 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
293 aDisplayer->RedisplayInLocalContext(theFeature, aPreview->impl<TopoDS_Shape>(),
294 aPreviewOp->getSelectionModes(theFeature));
298 //aDisplayer->CloseLocalContexts(false);
299 aDisplayer->Erase(anOperation->feature());