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 <Events_Loop.h>
52 #include <StdSelect_TypeOfFace.hxx>
53 #include <TopoDS_Vertex.hxx>
55 #include <TopoDS_Shape.hxx>
56 #include <BRep_Tool.hxx>
59 #include <QMouseEvent>
62 #include <QApplication>
64 #include <GeomAlgoAPI_FaceBuilder.h>
65 #include <GeomDataAPI_Dir.h>
72 /*!Create and return new instance of XGUI_Module*/
73 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
75 return new PartSet_Module(theWshop);
78 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
79 : ModuleBase_IModule(theWshop),
80 myIsDragging(false), myRestartingMode(true), myDragDone(false)
82 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
83 ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
84 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
85 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
87 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
88 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
90 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
91 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
95 PartSet_Module::~PartSet_Module()
99 void PartSet_Module::registerValidators()
101 //Registering of validators
102 SessionPtr aMgr = ModelAPI_Session::get();
103 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
104 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
105 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
106 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
107 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
108 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
112 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation)
114 if (theOperation->isEditOperation())
116 /// Restart sketcher operations automatically
117 FeaturePtr aFeature = theOperation->feature();
118 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
119 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
120 if (aSPFeature && myRestartingMode) {
121 myLastOperationId = theOperation->id();
122 myLastFeature = theOperation->feature();
123 launchOperation(myLastOperationId);
125 breakOperationSequence();
129 void PartSet_Module::breakOperationSequence()
131 myLastOperationId = "";
132 myLastFeature = FeaturePtr();
133 myRestartingMode = false;
137 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
139 breakOperationSequence();
142 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
144 myRestartingMode = true;
145 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
146 // Display all sketcher sub-Objects
147 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
148 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
149 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
151 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
152 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
153 std::list<ResultPtr> aResults = aFeature->results();
154 std::list<ResultPtr>::const_iterator aIt;
155 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
156 aDisplayer->display((*aIt), false);
158 aDisplayer->display(aFeature);
160 // Hide sketcher result
161 std::list<ResultPtr> aResults = myCurrentSketch->results();
162 std::list<ResultPtr>::const_iterator aIt;
163 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
164 aDisplayer->erase((*aIt), false);
166 aDisplayer->erase(myCurrentSketch);
169 if (myPlaneFilter.IsNull())
170 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
171 myWorkshop->viewer()->addSelectionFilter(myPlaneFilter);
172 if (theOperation->isEditOperation()) {
173 // If it is editing of sketch then it means that plane is already defined
174 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
175 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
180 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
182 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
183 DataPtr aData = myCurrentSketch->data();
184 if ((!aData) || (!aData->isValid())) {
185 // The sketch was aborted
186 myCurrentSketch = CompositeFeaturePtr();
189 // Hide all sketcher sub-Objects
190 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
191 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
192 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
193 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
194 std::list<ResultPtr> aResults = aFeature->results();
195 std::list<ResultPtr>::const_iterator aIt;
196 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
197 aDisplayer->erase((*aIt), false);
199 aDisplayer->erase(aFeature, false);
201 // Display sketcher result
202 std::list<ResultPtr> aResults = myCurrentSketch->results();
203 std::list<ResultPtr>::const_iterator aIt;
204 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
205 aDisplayer->display((*aIt), false);
207 aDisplayer->display(myCurrentSketch);
209 myCurrentSketch = CompositeFeaturePtr();
210 myWorkshop->viewer()->removeSelectionFilter(myPlaneFilter);
214 void PartSet_Module::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
216 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
220 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
222 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
223 // Restart last operation type
224 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
225 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
226 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
227 // Initialise new line with first point equal to end of previous
228 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
230 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
231 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
232 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
234 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
235 PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(),
236 aWgt->attributeID(), aPoint->x(), aPoint->y());
237 theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
242 // Start editing constraint
243 if (theOperation->isEditOperation()) {
244 std::string aId = theOperation->id().toStdString();
245 if (sketchOperationIdList().contains(QString(aId.c_str()))) {
246 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
247 (aId == SketchPlugin_ConstraintLength::ID()) ||
248 (aId == SketchPlugin_ConstraintDistance::ID())) {
249 // Find and activate widget for management of point for dimension line position
250 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
251 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
252 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
254 aPanel->activateWidget(aPntWgt);
265 void PartSet_Module::onSelectionChanged()
267 // Editing of constraints can be done on selection
268 ModuleBase_ISelection* aSelect = myWorkshop->selection();
269 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
270 if (aSelected.size() == 1) {
271 ModuleBase_ViewerPrs aPrs = aSelected.first();
272 ObjectPtr aObject = aPrs.object();
273 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
275 std::string aId = aFeature->getKind();
276 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
277 (aId == SketchPlugin_ConstraintLength::ID()) ||
278 (aId == SketchPlugin_ConstraintDistance::ID())) {
279 editFeature(aFeature);
285 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
287 if (!(theEvent->buttons() & Qt::LeftButton))
289 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
290 // Use only for sketch operations
291 if (aOperation && myCurrentSketch) {
292 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
295 bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
296 bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
298 // Avoid non-sketch operations
299 if ((!isSketchOpe) && (!isSketcher))
302 bool isEditing = aOperation->isEditOperation();
304 // Ignore creation sketch operation
305 if ((!isSketcher) && (!isEditing))
308 // Remember highlighted objects for editing
309 ModuleBase_ISelection* aSelect = myWorkshop->selection();
310 QList<ModuleBase_ViewerPrs> aObjects = aSelect->getHighlighted();
311 myEditingFeatures.clear();
312 myEditingAttr.clear();
313 if (aObjects.size() > 0) {
314 foreach(ModuleBase_ViewerPrs aPrs, aObjects) {
315 FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first().object());
317 myEditingFeatures.append(aFeature);
318 TopoDS_Shape aShape = aPrs.shape();
319 if (!aShape.IsNull()) {
320 if (aShape.ShapeType() == TopAbs_VERTEX) {
321 AttributePtr aAttr = PartSet_Tools::findAttributeBy2dPoint(myEditingFeatures.first(),
322 aShape, myCurrentSketch);
324 myEditingAttr.append(aAttr);
330 // If nothing highlighted - return
331 if (myEditingFeatures.size() == 0)
336 get2dPoint(theWnd, theEvent, myCurX, myCurY);
338 myWorkshop->viewer()->enableSelection(false);
342 } else if (isSketchOpe && isEditing) {
343 // If selected another object
347 get2dPoint(theWnd, theEvent, myCurX, myCurY);
349 myWorkshop->viewer()->enableSelection(false);
351 // This is necessary in order to finalize previous operation
352 QApplication::processEvents();
359 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
360 double& theX, double& theY)
362 Handle(V3d_View) aView = theWnd->v3dView();
363 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
364 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
368 void PartSet_Module::launchEditing()
370 if (myEditingFeatures.size() == 1) {
371 FeaturePtr aFeature = myEditingFeatures.first();
372 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
373 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
375 editFeature(aSPFeature);
380 /// Returns new instance of operation object (used in createOperation for customization)
381 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
383 if (theFeatureId == PartSet_OperationSketch::Type()) {
384 return new PartSet_OperationSketch(theFeatureId.c_str(), this);
386 return ModuleBase_IModule::getNewOperation(theFeatureId);
390 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
392 myWorkshop->viewer()->enableSelection(true);
394 myIsDragging = false;
396 myWorkshop->currentOperation()->commit();
401 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
404 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
405 if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
406 return; // No edit operation activated
408 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
409 Handle(V3d_View) aView = theWnd->v3dView();
410 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
412 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
413 double dX = aX - myCurX;
414 double dY = aY - myCurY;
416 if ((aOperation->id().toStdString() == SketchPlugin_Line::ID()) &&
417 (myEditingAttr.size() > 0) &&
418 myEditingAttr.first()) {
419 // probably we have prehighlighted point
420 AttributePtr aAttr = myEditingAttr.first();
421 std::string aAttrId = aAttr->id();
422 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
423 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
424 // Find corresponded widget to provide dragging
425 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
426 if (aWgt->attributeID() == aAttrId) {
427 PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
429 aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
435 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
436 std::dynamic_pointer_cast<SketchPlugin_Feature>(aOperation->feature());
437 if (aSketchFeature) {
438 aSketchFeature->move(dX, dY);
439 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent);
440 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_MOVED));
441 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
451 QStringList PartSet_Module::sketchOperationIdList() const
454 aIds << SketchPlugin_Line::ID().c_str();
455 aIds << SketchPlugin_Point::ID().c_str();
456 aIds << SketchPlugin_Arc::ID().c_str();
457 aIds << SketchPlugin_Circle::ID().c_str();
458 aIds << SketchPlugin_ConstraintLength::ID().c_str();
459 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
460 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
461 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
462 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
463 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
467 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
469 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
470 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
471 /// If last line finished on vertex the lines creation sequence has to be break
472 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
473 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
474 if (aWidgets.last() == aPanel->activeWidget()) {
475 breakOperationSequence();
480 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
481 Config_WidgetAPI* theWidgetApi, std::string theParentId,
482 QList<ModuleBase_ModelWidget*>& theModelWidgets)
484 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
485 XGUI_Workshop* aWorkshop = aConnector->workshop();
486 if (theType == "sketch-start-label") {
487 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
488 aWgt->setWorkshop(aWorkshop);
489 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
490 this, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
491 theModelWidgets.append(aWgt);
492 return aWgt->getControl();
494 } else if (theType == "sketch-2dpoint_selector") {
495 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
496 aWgt->setWorkshop(aWorkshop);
497 aWgt->setSketch(myCurrentSketch);
499 connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)),
500 this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
502 theModelWidgets.append(aWgt);
503 return aWgt->getControl();
505 } if (theType == "point2ddistance") {
506 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
507 aWgt->setWorkshop(aWorkshop);
508 aWgt->setSketch(myCurrentSketch);
510 theModelWidgets.append(aWgt);
511 return aWgt->getControl();
513 } if (theType == "sketch_shape_selector") {
514 PartSet_WidgetShapeSelector* aWgt =
515 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
516 aWgt->setSketcher(myCurrentSketch);
518 theModelWidgets.append(aWgt);
519 return aWgt->getControl();
521 } if (theType == "sketch_constraint_shape_selector") {
522 PartSet_WidgetConstraintShapeSelector* aWgt =
523 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
524 aWgt->setSketcher(myCurrentSketch);
526 theModelWidgets.append(aWgt);
527 return aWgt->getControl();