1 #include "PartSet_Module.h"
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_WidgetSketchLabel.h>
4 #include <PartSet_Validators.h>
5 #include <PartSet_Tools.h>
6 #include <PartSet_WidgetPoint2D.h>
7 #include <PartSet_WidgetPoint2dDistance.h>
8 #include <PartSet_WidgetShapeSelector.h>
10 #include <ModuleBase_Operation.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_IViewWindow.h>
13 #include <ModuleBase_IPropertyPanel.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Session.h>
21 #include <GeomDataAPI_Point2D.h>
22 #include <GeomDataAPI_Point.h>
23 #include <GeomDataAPI_Dir.h>
25 #include <XGUI_MainWindow.h>
26 #include <XGUI_Displayer.h>
27 #include <XGUI_Viewer.h>
28 #include <XGUI_Workshop.h>
29 #include <XGUI_OperationMgr.h>
30 #include <XGUI_ViewPort.h>
31 #include <XGUI_ActionsMgr.h>
32 #include <XGUI_ViewerProxy.h>
33 #include <XGUI_ContextMenuMgr.h>
34 #include <XGUI_PropertyPanel.h>
35 #include <XGUI_ModuleConnector.h>
36 #include <XGUI_Tools.h>
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Sketch.h>
40 #include <SketchPlugin_Point.h>
41 #include <SketchPlugin_Arc.h>
42 #include <SketchPlugin_Circle.h>
43 #include <SketchPlugin_ConstraintLength.h>
44 #include <SketchPlugin_ConstraintDistance.h>
45 #include <SketchPlugin_ConstraintParallel.h>
46 #include <SketchPlugin_ConstraintPerpendicular.h>
47 #include <SketchPlugin_ConstraintRadius.h>
48 #include <SketchPlugin_ConstraintRigid.h>
50 #include <StdSelect_TypeOfFace.hxx>
53 #include <QMouseEvent>
56 #include <QApplication>
58 #include <GeomAlgoAPI_FaceBuilder.h>
59 #include <GeomDataAPI_Dir.h>
66 /*!Create and return new instance of XGUI_Module*/
67 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
69 return new PartSet_Module(theWshop);
72 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
73 : ModuleBase_IModule(theWshop),
74 myIsDragging(false), myRestartingMode(true), myDragDone(false)
76 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
77 ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
78 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
79 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
81 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
82 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
84 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
85 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
89 PartSet_Module::~PartSet_Module()
93 void PartSet_Module::registerValidators()
95 //Registering of validators
96 SessionPtr aMgr = ModelAPI_Session::get();
97 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
98 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
99 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
100 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
101 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
102 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
106 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation)
108 if (theOperation->isEditOperation())
110 /// Restart sketcher operations automatically
111 FeaturePtr aFeature = theOperation->feature();
112 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
113 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
114 if (aSPFeature && myRestartingMode) {
115 myLastOperationId = theOperation->id();
116 myLastFeature = theOperation->feature();
117 launchOperation(myLastOperationId);
119 breakOperationSequence();
123 void PartSet_Module::breakOperationSequence()
125 myLastOperationId = "";
126 myLastFeature = FeaturePtr();
127 myRestartingMode = false;
131 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
133 breakOperationSequence();
136 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
138 myRestartingMode = true;
139 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
140 // Display all sketcher sub-Objects
141 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
142 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
143 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
145 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
146 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
147 std::list<ResultPtr> aResults = aFeature->results();
148 std::list<ResultPtr>::const_iterator aIt;
149 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
150 aDisplayer->display((*aIt), false);
152 aDisplayer->display(aFeature);
154 // Hide sketcher result
155 std::list<ResultPtr> aResults = myCurrentSketch->results();
156 std::list<ResultPtr>::const_iterator aIt;
157 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
158 aDisplayer->erase((*aIt), false);
160 aDisplayer->erase(myCurrentSketch);
164 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
166 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
167 // Hide all sketcher sub-Objects
168 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
169 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
171 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
172 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
173 std::list<ResultPtr> aResults = aFeature->results();
174 std::list<ResultPtr>::const_iterator aIt;
175 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
176 aDisplayer->erase((*aIt), false);
178 aDisplayer->erase(aFeature, false);
180 // Display sketcher result
181 std::list<ResultPtr> aResults = myCurrentSketch->results();
182 std::list<ResultPtr>::const_iterator aIt;
183 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
184 aDisplayer->display((*aIt), false);
186 aDisplayer->display(myCurrentSketch);
188 myCurrentSketch = CompositeFeaturePtr();
194 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
196 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
197 ModuleBase_ModelWidget* aWgt = theOperation->propertyPanel()->activeWidget();
198 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
199 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
201 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
202 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
203 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
205 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
206 PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(),
207 SketchPlugin_Line::START_ID(), aPoint->x(), aPoint->y());
208 theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
216 void PartSet_Module::onSelectionChanged()
220 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
222 if (!(theEvent->buttons() & Qt::LeftButton))
224 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
225 // Use only for sketch operations
226 if (aOperation && myCurrentSketch) {
227 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
230 bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
231 bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
233 // Avoid non-sketch operations
234 if ((!isSketchOpe) && (!isSketcher))
237 bool isEditing = aOperation->isEditOperation();
239 // Ignore creation sketch operation
240 if ((!isSketcher) && (!isEditing))
243 // Remember highlighted objects for editing
244 ModuleBase_ISelection* aSelect = myWorkshop->selection();
245 QList<ModuleBase_ViewerPrs> aObjects = aSelect->getHighlighted();
246 myEditingFeatures.clear();
247 if (aObjects.size() > 0) {
248 foreach(ModuleBase_ViewerPrs aPrs, aObjects) {
249 FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first().object());
251 myEditingFeatures.append(aFeature);
254 // If nothing highlighted - return
255 if (myEditingFeatures.size() == 0)
259 CompositeFeaturePtr aSketch =
260 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aOperation->feature());
261 if (!PartSet_Tools::sketchPlane(aSketch))
264 //myCurrentSketch = aOperation->feature();
266 get2dPoint(theWnd, theEvent, myCurX, myCurY);
268 myWorkshop->viewer()->enableSelection(false);
272 } else if (isSketchOpe && isEditing) {
276 get2dPoint(theWnd, theEvent, myCurX, myCurY);
278 myWorkshop->viewer()->enableSelection(false);
280 // This is necessary in order to finalize previous operation
281 QApplication::processEvents();
283 //QTimer::singleShot(10, this, SLOT(launchEditing()));
289 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
290 double& theX, double& theY)
292 Handle(V3d_View) aView = theWnd->v3dView();
293 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
294 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
298 void PartSet_Module::launchEditing()
300 if (myEditingFeatures.size() == 1) {
301 FeaturePtr aFeature = myEditingFeatures.first();
302 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
303 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
305 editFeature(aSPFeature);
310 /// Returns new instance of operation object (used in createOperation for customization)
311 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
313 if (theFeatureId == PartSet_OperationSketch::Type()) {
314 return new PartSet_OperationSketch(theFeatureId.c_str(), this);
316 return ModuleBase_IModule::getNewOperation(theFeatureId);
320 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
323 myIsDragging = false;
324 myWorkshop->viewer()->enableSelection(true);
326 myWorkshop->currentOperation()->commit();
331 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
334 Handle(V3d_View) aView = theWnd->v3dView();
335 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
337 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
338 double dX = myCurX - aX;
339 double dY = myCurY - aY;
341 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
342 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
343 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
344 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
345 PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
347 aWgt2d->setPoint(aWgt2d->x() - dX, aWgt2d->y() - dY);
357 QStringList PartSet_Module::sketchOperationIdList() const
360 aIds << SketchPlugin_Line::ID().c_str();
361 aIds << SketchPlugin_Point::ID().c_str();
362 aIds << SketchPlugin_Arc::ID().c_str();
363 aIds << SketchPlugin_Circle::ID().c_str();
364 aIds << SketchPlugin_ConstraintLength::ID().c_str();
365 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
366 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
367 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
368 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
369 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
373 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
375 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
376 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
377 /// If last line finished on vertex the lines creation sequence has to be break
378 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
379 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
380 if (aWidgets.last() == aPanel->activeWidget()) {
381 breakOperationSequence();
382 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
383 PartSet_Tools::setConstraints(myCurrentSketch, aOperation->feature(),
384 SketchPlugin_Line::END_ID(), aPnt2dWgt->x(), aPnt2dWgt->y());
389 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
390 Config_WidgetAPI* theWidgetApi, std::string theParentId,
391 QList<ModuleBase_ModelWidget*>& theModelWidgets)
393 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
394 XGUI_Workshop* aWorkshop = aConnector->workshop();
395 if (theType == "sketch-start-label") {
396 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
397 aWgt->setWorkshop(aWorkshop);
398 theModelWidgets.append(aWgt);
399 return aWgt->getControl();
401 } else if (theType == "sketch-2dpoint_selector") {
402 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
403 aWgt->setWorkshop(aWorkshop);
404 aWgt->setSketch(myCurrentSketch);
406 connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)),
407 this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
409 theModelWidgets.append(aWgt);
410 return aWgt->getControl();
412 } if (theType == "point2ddistance") {
413 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
414 aWgt->setWorkshop(aWorkshop);
415 aWgt->setSketch(myCurrentSketch);
417 theModelWidgets.append(aWgt);
418 return aWgt->getControl();
420 } if (theType == "sketch_shape_selector") {
421 PartSet_WidgetShapeSelector* aWgt =
422 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
423 aWgt->setSketcher(myCurrentSketch);
425 theModelWidgets.append(aWgt);
426 return aWgt->getControl();