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>
57 #include <GeomAlgoAPI_FaceBuilder.h>
58 #include <GeomDataAPI_Dir.h>
65 /*!Create and return new instance of XGUI_Module*/
66 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
68 return new PartSet_Module(theWshop);
71 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
72 : ModuleBase_IModule(theWshop),
73 myIsDragging(false), myRestartingMode(true), myDragDone(false)
75 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
76 ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
77 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
78 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
80 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
81 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
83 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
84 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
88 PartSet_Module::~PartSet_Module()
92 void PartSet_Module::registerValidators()
94 //Registering of validators
95 SessionPtr aMgr = ModelAPI_Session::get();
96 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
97 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
98 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
99 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
100 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
101 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
105 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation)
107 if (theOperation->isEditOperation())
109 /// Restart sketcher operations automatically
110 FeaturePtr aFeature = theOperation->feature();
111 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
112 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
113 if (aSPFeature && myRestartingMode) {
114 myLastOperationId = theOperation->id();
115 myLastFeature = theOperation->feature();
116 launchOperation(myLastOperationId);
118 breakOperationSequence();
122 void PartSet_Module::breakOperationSequence()
124 myLastOperationId = "";
125 myLastFeature = FeaturePtr();
126 myRestartingMode = false;
130 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
132 breakOperationSequence();
135 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
137 myRestartingMode = true;
138 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
139 // Display all sketcher sub-Objects
140 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
141 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
142 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
144 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
145 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
146 std::list<ResultPtr> aResults = aFeature->results();
147 std::list<ResultPtr>::const_iterator aIt;
148 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
149 aDisplayer->display((*aIt), false);
151 aDisplayer->display(aFeature);
153 // Hide sketcher result
154 std::list<ResultPtr> aResults = myCurrentSketch->results();
155 std::list<ResultPtr>::const_iterator aIt;
156 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
157 aDisplayer->erase((*aIt), false);
159 aDisplayer->erase(myCurrentSketch);
163 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
165 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
166 // Hide all sketcher sub-Objects
167 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
168 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
170 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
171 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
172 std::list<ResultPtr> aResults = aFeature->results();
173 std::list<ResultPtr>::const_iterator aIt;
174 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
175 aDisplayer->erase((*aIt), false);
177 aDisplayer->erase(aFeature, false);
179 // Display sketcher result
180 std::list<ResultPtr> aResults = myCurrentSketch->results();
181 std::list<ResultPtr>::const_iterator aIt;
182 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
183 aDisplayer->display((*aIt), false);
185 aDisplayer->display(myCurrentSketch);
187 myCurrentSketch = CompositeFeaturePtr();
193 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
195 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
196 ModuleBase_ModelWidget* aWgt = theOperation->propertyPanel()->activeWidget();
197 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
198 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
200 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
201 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
202 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
204 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
205 PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(),
206 SketchPlugin_Line::START_ID(), aPoint->x(), aPoint->y());
207 theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
215 void PartSet_Module::onSelectionChanged()
219 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
221 if (!(theEvent->buttons() & Qt::LeftButton))
223 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
224 // Use only for sketch operations
225 if (aOperation && myCurrentSketch) {
226 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
229 bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
230 bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
232 // Avoid non-sketch operations
236 bool isEditing = aOperation->isEditOperation();
238 // Ignore creation sketch operation
239 if ((!isSketcher) && (!isEditing))
242 // Remember highlighted objects for editing
243 ModuleBase_ISelection* aSelect = myWorkshop->selection();
244 QList<ModuleBase_ViewerPrs> aObjects = aSelect->getHighlighted();
245 myEditingFeatures.clear();
246 if (aObjects.size() > 0) {
247 foreach(ModuleBase_ViewerPrs aPrs, aObjects) {
248 FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first().object());
250 myEditingFeatures.append(aFeature);
253 // If nothing highlighted - return
254 if (myEditingFeatures.size() == 0)
258 CompositeFeaturePtr aSketch =
259 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aOperation->feature());
260 if (!PartSet_Tools::sketchPlane(aSketch))
263 //myCurrentSketch = aOperation->feature();
265 get2dPoint(theWnd, theEvent, myCurX, myCurY);
267 myWorkshop->viewer()->enableSelection(false);
271 } else if (isSketchOpe && isEditing) {
274 //myCurrentSketch = aOperation->parentFeature();
276 get2dPoint(theWnd, theEvent, myCurX, myCurY);
278 myWorkshop->viewer()->enableSelection(false);
280 QTimer::singleShot(10, this, SLOT(launchEditing()));
286 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
287 double& theX, double& theY)
289 Handle(V3d_View) aView = theWnd->v3dView();
290 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
291 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
295 void PartSet_Module::launchEditing()
297 if (myEditingFeatures.size() == 1) {
298 FeaturePtr aFeature = myEditingFeatures.first();
299 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
300 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
302 editFeature(aSPFeature);
307 /// Returns new instance of operation object (used in createOperation for customization)
308 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
310 if (theFeatureId == PartSet_OperationSketch::Type()) {
311 return new PartSet_OperationSketch(theFeatureId.c_str(), this);
313 return ModuleBase_IModule::getNewOperation(theFeatureId);
317 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
320 myIsDragging = false;
321 myWorkshop->viewer()->enableSelection(true);
323 myWorkshop->currentOperation()->commit();
328 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
331 Handle(V3d_View) aView = theWnd->v3dView();
332 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
334 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
335 double dX = myCurX - aX;
336 double dY = myCurY - aY;
338 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
339 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
340 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
341 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
342 PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
344 aWgt2d->setPoint(aWgt2d->x() - dX, aWgt2d->y() - dY);
354 QStringList PartSet_Module::sketchOperationIdList() const
357 aIds << SketchPlugin_Line::ID().c_str();
358 aIds << SketchPlugin_Point::ID().c_str();
359 aIds << SketchPlugin_Arc::ID().c_str();
360 aIds << SketchPlugin_Circle::ID().c_str();
361 aIds << SketchPlugin_ConstraintLength::ID().c_str();
362 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
363 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
364 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
365 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
366 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
370 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
372 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
373 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
374 /// If last line finished on vertex the lines creation sequence has to be break
375 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
376 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
377 if (aWidgets.last() == aPanel->activeWidget()) {
378 breakOperationSequence();
379 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
380 PartSet_Tools::setConstraints(myCurrentSketch, aOperation->feature(),
381 SketchPlugin_Line::END_ID(), aPnt2dWgt->x(), aPnt2dWgt->y());
386 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
387 Config_WidgetAPI* theWidgetApi, std::string theParentId,
388 QList<ModuleBase_ModelWidget*>& theModelWidgets)
390 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
391 XGUI_Workshop* aWorkshop = aConnector->workshop();
392 if (theType == "sketch-start-label") {
393 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
394 aWgt->setWorkshop(aWorkshop);
395 theModelWidgets.append(aWgt);
396 return aWgt->getControl();
398 } else if (theType == "sketch-2dpoint_selector") {
399 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
400 aWgt->setWorkshop(aWorkshop);
401 aWgt->setSketch(myCurrentSketch);
403 connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)),
404 this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
406 theModelWidgets.append(aWgt);
407 return aWgt->getControl();
409 } if (theType == "point2ddistance") {
410 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
411 aWgt->setWorkshop(aWorkshop);
412 aWgt->setSketch(myCurrentSketch);
414 theModelWidgets.append(aWgt);
415 return aWgt->getControl();
417 } if (theType == "sketch_shape_selector") {
418 PartSet_WidgetShapeSelector* aWgt =
419 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
420 aWgt->setSketcher(myCurrentSketch);
422 theModelWidgets.append(aWgt);
423 return aWgt->getControl();