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(operationStopped(ModuleBase_Operation*)),
56 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
58 connect(myWorkshop->selector(), SIGNAL(selectionChanged()),
59 this, SLOT(onSelectionChanged()));
60 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
61 this, SLOT(onMouseReleased(QMouseEvent*)));
62 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
63 this, SLOT(onMouseMoved(QMouseEvent*)));
64 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
65 this, SLOT(onKeyRelease(QKeyEvent*)));
68 PartSet_Module::~PartSet_Module()
72 XGUI_Workshop* PartSet_Module::workshop() const
77 void PartSet_Module::createFeatures()
79 Config_ModuleReader aXMLReader = Config_ModuleReader();
81 myFeaturesInFiles = aXMLReader.featuresInFiles();
84 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
86 theFeature->connectTo(this, SLOT(onFeatureTriggered()));
89 QStringList PartSet_Module::nestedFeatures(QString)
94 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
96 return myFeaturesInFiles[theFeatureId];
102 void PartSet_Module::onFeatureTriggered()
104 XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
105 //Do nothing on uncheck
106 if(aCmd->isCheckable() && !aCmd->isChecked())
108 launchOperation(aCmd->id());
111 void PartSet_Module::launchOperation(const QString& theCmdId)
113 ModuleBase_Operation* anOperation = createOperation(theCmdId);
114 sendOperation(anOperation);
117 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
121 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
124 void PartSet_Module::onSelectionChanged()
126 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
127 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
129 XGUI_SelectionMgr* aSelector = myWorkshop->selector();
131 NCollection_List<TopoDS_Shape> aList;
132 aSelector->selectedShapes(aList);
134 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
135 boost::shared_ptr<ModelAPI_Feature> aFeature;
136 // only first selected shape is processed
137 if (!aList.IsEmpty()) {
138 aFeature = aDisplayer->GetFeature(aList.First());
140 aPreviewOp->setSelected(aFeature, !aList.IsEmpty() ? aList.First() : TopoDS_Shape());
145 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
147 QPoint aPoint = theEvent->pos();
148 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
149 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
151 XGUI_SelectionMgr* aSelector = myWorkshop->selector();
153 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
154 if ( !aView3d.IsNull() ) {
155 gp_Pnt aPnt = PartSet_Tools::ConvertClickToPoint(aPoint, aView3d);
156 aPreviewOp->mouseReleased(aPnt);
162 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
164 QPoint aPoint = theEvent->pos();
165 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
166 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
168 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
169 if ( !aView3d.IsNull() ) {
170 gp_Pnt aPnt = PartSet_Tools::ConvertClickToPoint(aPoint, aView3d);
171 aPreviewOp->mouseMoved(aPnt);
176 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
178 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
179 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
181 aPreviewOp->keyReleased(theEvent->key());
185 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
187 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
189 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
191 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
193 visualizePreview(aPreviewOp->feature(), false);
197 myWorkshop->actionsMgr()->setNestedActionsEnabled(true);
200 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
202 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
203 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
206 aPreviewOp->init(theFeature);
208 sendOperation(anOperation);
211 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
214 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Abort;
215 visualizePreview(theFeature, isDisplay);
218 ModuleBase_Operation* PartSet_Module::createOperation(const QString& theCmdId)
220 std::string aStdCmdId = theCmdId.toStdString();
221 if (aStdCmdId == "EditLine")
222 aStdCmdId = "SketchLine";
223 std::string aPluginFileName = featureFile(aStdCmdId);
224 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
225 aWdgReader.readAll();
226 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
227 std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
228 ModuleBase_Operation* anOperation;
229 if (theCmdId == "Sketch" ) {
230 anOperation = new PartSet_OperationSketch(theCmdId, this);
232 else if(theCmdId == "SketchLine" || theCmdId == "EditLine") {
233 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
234 boost::shared_ptr<ModelAPI_Feature> aSketchFeature;
236 aSketchFeature = aCurOperation->feature();
237 if (theCmdId == "SketchLine")
238 anOperation = new PartSet_OperationSketchLine(theCmdId, this, aSketchFeature);
240 anOperation = new PartSet_OperationEditLine(theCmdId, this, aSketchFeature);
243 anOperation = new ModuleBase_Operation(theCmdId, this);
245 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
246 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
249 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
251 connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
252 this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
253 connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
254 this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
256 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
258 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
259 this, SLOT(onPlaneSelected(double, double, double)));
266 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
268 //TODO(sbh): Implement static method to extract event id [SEID]
269 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
270 Config_PointerMessage aMessage(aModuleEvent, this);
271 aMessage.setPointer(theOperation);
272 Events_Loop::loop()->send(aMessage);
275 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay)
277 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
281 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
285 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
287 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
288 aDisplayer->RedisplayInLocalContext(theFeature,
289 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
290 aPreviewOp->getSelectionModes(theFeature));
293 //aDisplayer->CloseLocalContexts(false);
294 aDisplayer->Erase(anOperation->feature());