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 /// Returns list of unique objects by sum of objects from List1 and List2
73 QList<ObjectPtr> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
74 const QList<ModuleBase_ViewerPrs>& theList2)
76 QList<ObjectPtr> aRes;
77 foreach (ModuleBase_ViewerPrs aPrs, theList1) {
78 if (!aRes.contains(aPrs.object()))
79 aRes.append(aPrs.object());
81 foreach (ModuleBase_ViewerPrs aPrs, theList2) {
82 if (!aRes.contains(aPrs.object()))
83 aRes.append(aPrs.object());
88 /*!Create and return new instance of XGUI_Module*/
89 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
91 return new PartSet_Module(theWshop);
94 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
95 : ModuleBase_IModule(theWshop),
96 myIsDragging(false), myRestartingMode(LastFeatureUse), myDragDone(false)
98 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
99 ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
100 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
101 this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
103 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
104 this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
106 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
107 this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
109 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
110 XGUI_Workshop* aWorkshop = aConnector->workshop();
112 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
113 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
116 PartSet_Module::~PartSet_Module()
118 if (!myDocumentShapeFilter.IsNull())
119 myDocumentShapeFilter.Nullify();
120 if (!myPlaneFilter.IsNull())
121 myPlaneFilter.Nullify();
124 void PartSet_Module::registerValidators()
126 //Registering of validators
127 SessionPtr aMgr = ModelAPI_Session::get();
128 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
129 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
130 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
131 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
132 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
133 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
137 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation)
139 if (theOperation->isEditOperation())
141 /// Restart sketcher operations automatically
142 FeaturePtr aFeature = theOperation->feature();
143 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
144 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
145 if (aSPFeature && (myRestartingMode != None)) {
146 myLastOperationId = theOperation->id();
147 myLastFeature = myRestartingMode == LastFeatureUse ? theOperation->feature() : FeaturePtr();
148 launchOperation(myLastOperationId);
150 breakOperationSequence();
154 void PartSet_Module::breakOperationSequence()
156 myLastOperationId = "";
157 myLastFeature = FeaturePtr();
158 myRestartingMode = None;
162 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
164 breakOperationSequence();
167 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
169 myRestartingMode = LastFeatureUse;
170 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
171 // Display all sketcher sub-Objects
172 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
173 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
174 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
176 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
177 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
178 std::list<ResultPtr> aResults = aFeature->results();
179 std::list<ResultPtr>::const_iterator aIt;
180 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
181 aDisplayer->display((*aIt), false);
183 aDisplayer->display(aFeature);
185 // Hide sketcher result
186 std::list<ResultPtr> aResults = myCurrentSketch->results();
187 std::list<ResultPtr>::const_iterator aIt;
188 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
189 aDisplayer->erase((*aIt), false);
191 aDisplayer->erase(myCurrentSketch);
194 if (myPlaneFilter.IsNull())
195 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
196 myWorkshop->viewer()->addSelectionFilter(myPlaneFilter);
197 if (theOperation->isEditOperation()) {
198 // If it is editing of sketch then it means that plane is already defined
199 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
200 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
203 if (myDocumentShapeFilter.IsNull())
204 myDocumentShapeFilter = new ModuleBase_ShapeDocumentFilter(myWorkshop);
205 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
208 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
210 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
211 DataPtr aData = myCurrentSketch->data();
212 if ((!aData) || (!aData->isValid())) {
213 // The sketch was aborted
214 myCurrentSketch = CompositeFeaturePtr();
217 // Hide all sketcher sub-Objects
218 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
219 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
220 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
221 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
222 std::list<ResultPtr> aResults = aFeature->results();
223 std::list<ResultPtr>::const_iterator aIt;
224 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
225 aDisplayer->erase((*aIt), false);
227 aDisplayer->erase(aFeature, false);
229 // Display sketcher result
230 std::list<ResultPtr> aResults = myCurrentSketch->results();
231 std::list<ResultPtr>::const_iterator aIt;
232 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
233 aDisplayer->display((*aIt), false);
235 aDisplayer->display(myCurrentSketch);
237 myCurrentSketch = CompositeFeaturePtr();
238 myWorkshop->viewer()->removeSelectionFilter(myPlaneFilter);
240 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
243 void PartSet_Module::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
245 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
249 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
251 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
252 // Restart last operation type
253 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
254 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
255 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
256 // Initialise new line with first point equal to end of previous
257 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
259 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
260 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
261 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
263 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
264 PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(),
265 aWgt->attributeID(), aPoint->x(), aPoint->y());
266 theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
271 // Start editing constraint
272 if (theOperation->isEditOperation()) {
273 std::string aId = theOperation->id().toStdString();
274 if (sketchOperationIdList().contains(QString(aId.c_str()))) {
275 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
276 (aId == SketchPlugin_ConstraintLength::ID()) ||
277 (aId == SketchPlugin_ConstraintDistance::ID())) {
278 // Find and activate widget for management of point for dimension line position
279 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
280 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
281 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
283 aPanel->activateWidget(aPntWgt);
294 void PartSet_Module::onSelectionChanged()
296 // Editing of constraints can be done on selection
297 ModuleBase_ISelection* aSelect = myWorkshop->selection();
298 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
299 if (aSelected.size() == 1) {
300 ModuleBase_ViewerPrs aPrs = aSelected.first();
301 ObjectPtr aObject = aPrs.object();
302 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
304 std::string aId = aFeature->getKind();
305 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
306 (aId == SketchPlugin_ConstraintLength::ID()) ||
307 (aId == SketchPlugin_ConstraintDistance::ID())) {
308 editFeature(aFeature);
314 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
316 if (!(theEvent->buttons() & Qt::LeftButton))
319 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
320 // Use only for sketch operations
321 if (aOperation && myCurrentSketch) {
322 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
325 bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
326 bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
328 // Avoid non-sketch operations
329 if ((!isSketchOpe) && (!isSketcher))
332 bool isEditing = aOperation->isEditOperation();
334 // Ignore creation sketch operation
335 if ((!isSketcher) && (!isEditing))
338 if (theEvent->modifiers()) {
339 // If user performs multiselection
340 if (isSketchOpe && (!isSketcher))
341 if (!aOperation->commit())
345 // Remember highlighted objects for editing
346 ModuleBase_ISelection* aSelect = myWorkshop->selection();
347 QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
348 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
349 myEditingFeatures.clear();
350 myEditingAttr.clear();
351 if ((aHighlighted.size() == 0) && (aSelected.size() == 0)) {
352 if (isSketchOpe && (!isSketcher))
353 // commit previous operation
354 if (!aOperation->commit())
359 QObjectPtrList aSelObjects = getSumList(aHighlighted, aSelected);
360 if ((aHighlighted.size() == 1) && (aSelected.size() == 0)) {
361 // Move by selected shape (vertex). Can be used only for single selection
362 foreach(ModuleBase_ViewerPrs aPrs, aHighlighted) {
363 FeaturePtr aFeature = ModelAPI_Feature::feature(aHighlighted.first().object());
365 myEditingFeatures.append(aFeature);
366 TopoDS_Shape aShape = aPrs.shape();
367 if (!aShape.IsNull()) {
368 if (aShape.ShapeType() == TopAbs_VERTEX) {
369 AttributePtr aAttr = PartSet_Tools::findAttributeBy2dPoint(myEditingFeatures.first(),
370 aShape, myCurrentSketch);
372 myEditingAttr.append(aAttr);
378 // Provide multi-selection. Can be used only for features
379 foreach (ObjectPtr aObj, aSelObjects) {
380 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
381 if (aFeature && (!myEditingFeatures.contains(aFeature)))
382 myEditingFeatures.append(aFeature);
386 // If nothing highlighted - return
387 if (myEditingFeatures.size() == 0)
392 get2dPoint(theWnd, theEvent, myCurX, myCurY);
394 myWorkshop->viewer()->enableSelection(false);
397 } else if (isSketchOpe && isEditing) {
398 // If selected another object
402 get2dPoint(theWnd, theEvent, myCurX, myCurY);
404 myWorkshop->viewer()->enableSelection(false);
406 // This is necessary in order to finalize previous operation
407 QApplication::processEvents();
414 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
415 double& theX, double& theY)
417 Handle(V3d_View) aView = theWnd->v3dView();
418 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
419 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
423 void PartSet_Module::launchEditing()
425 if (myEditingFeatures.size() > 0) {
426 FeaturePtr aFeature = myEditingFeatures.first();
427 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
428 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
430 editFeature(aSPFeature);
435 /// Returns new instance of operation object (used in createOperation for customization)
436 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
438 if (theFeatureId == PartSet_OperationSketch::Type()) {
439 return new PartSet_OperationSketch(theFeatureId.c_str(), this);
441 return ModuleBase_IModule::getNewOperation(theFeatureId);
445 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
447 myWorkshop->viewer()->enableSelection(true);
449 myIsDragging = false;
451 myWorkshop->currentOperation()->commit();
452 myEditingFeatures.clear();
453 myEditingAttr.clear();
459 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
462 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
463 if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
464 return; // No edit operation activated
466 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
467 Handle(V3d_View) aView = theWnd->v3dView();
468 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
470 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
471 double dX = aX - myCurX;
472 double dY = aY - myCurY;
474 if ((aOperation->id().toStdString() == SketchPlugin_Line::ID()) &&
475 (myEditingAttr.size() == 1) &&
476 myEditingAttr.first()) {
477 // probably we have prehighlighted point
478 AttributePtr aAttr = myEditingAttr.first();
479 std::string aAttrId = aAttr->id();
480 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
481 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
482 // Find corresponded widget to provide dragging
483 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
484 if (aWgt->attributeID() == aAttrId) {
485 PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
487 aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
493 foreach(FeaturePtr aFeature, myEditingFeatures) {
494 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
495 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
496 if (aSketchFeature) {
497 aSketchFeature->move(dX, dY);
498 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent);
501 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_MOVED));
502 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
510 void PartSet_Module::onEnterReleased()
512 myRestartingMode = LastFeatureEmpty;
515 QStringList PartSet_Module::sketchOperationIdList() const
518 aIds << SketchPlugin_Line::ID().c_str();
519 aIds << SketchPlugin_Point::ID().c_str();
520 aIds << SketchPlugin_Arc::ID().c_str();
521 aIds << SketchPlugin_Circle::ID().c_str();
522 aIds << SketchPlugin_ConstraintLength::ID().c_str();
523 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
524 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
525 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
526 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
527 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
531 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
533 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
534 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
535 /// If last line finished on vertex the lines creation sequence has to be break
536 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
537 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
538 if (aWidgets.last() == aPanel->activeWidget()) {
539 breakOperationSequence();
544 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
545 Config_WidgetAPI* theWidgetApi, std::string theParentId,
546 QList<ModuleBase_ModelWidget*>& theModelWidgets)
548 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
549 XGUI_Workshop* aWorkshop = aConnector->workshop();
550 if (theType == "sketch-start-label") {
551 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
552 aWgt->setWorkshop(aWorkshop);
553 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
554 this, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
555 theModelWidgets.append(aWgt);
556 return aWgt->getControl();
558 } else if (theType == "sketch-2dpoint_selector") {
559 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
560 aWgt->setWorkshop(aWorkshop);
561 aWgt->setSketch(myCurrentSketch);
563 connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)),
564 this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
566 theModelWidgets.append(aWgt);
567 return aWgt->getControl();
569 } if (theType == "point2ddistance") {
570 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
571 aWgt->setWorkshop(aWorkshop);
572 aWgt->setSketch(myCurrentSketch);
574 theModelWidgets.append(aWgt);
575 return aWgt->getControl();
577 } if (theType == "sketch_shape_selector") {
578 PartSet_WidgetShapeSelector* aWgt =
579 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
580 aWgt->setSketcher(myCurrentSketch);
582 theModelWidgets.append(aWgt);
583 return aWgt->getControl();
585 } if (theType == "sketch_constraint_shape_selector") {
586 PartSet_WidgetConstraintShapeSelector* aWgt =
587 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
588 aWgt->setSketcher(myCurrentSketch);
590 theModelWidgets.append(aWgt);
591 return aWgt->getControl();