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()
97 if (!myDocumentShapeFilter.IsNull())
98 myDocumentShapeFilter.Nullify();
99 if (!myPlaneFilter.IsNull())
100 myPlaneFilter.Nullify();
103 void PartSet_Module::registerValidators()
105 //Registering of validators
106 SessionPtr aMgr = ModelAPI_Session::get();
107 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
108 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
109 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
110 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
111 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
112 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
116 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation)
118 if (theOperation->isEditOperation())
120 /// Restart sketcher operations automatically
121 FeaturePtr aFeature = theOperation->feature();
122 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
123 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
124 if (aSPFeature && myRestartingMode) {
125 myLastOperationId = theOperation->id();
126 myLastFeature = theOperation->feature();
127 launchOperation(myLastOperationId);
129 breakOperationSequence();
133 void PartSet_Module::breakOperationSequence()
135 myLastOperationId = "";
136 myLastFeature = FeaturePtr();
137 myRestartingMode = false;
141 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
143 breakOperationSequence();
146 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
148 myRestartingMode = true;
149 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
150 // Display all sketcher sub-Objects
151 myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
152 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
153 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
155 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
156 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
157 std::list<ResultPtr> aResults = aFeature->results();
158 std::list<ResultPtr>::const_iterator aIt;
159 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
160 aDisplayer->display((*aIt), false);
162 aDisplayer->display(aFeature);
164 // Hide sketcher result
165 std::list<ResultPtr> aResults = myCurrentSketch->results();
166 std::list<ResultPtr>::const_iterator aIt;
167 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
168 aDisplayer->erase((*aIt), false);
170 aDisplayer->erase(myCurrentSketch);
173 if (myPlaneFilter.IsNull())
174 myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
175 myWorkshop->viewer()->addSelectionFilter(myPlaneFilter);
176 if (theOperation->isEditOperation()) {
177 // If it is editing of sketch then it means that plane is already defined
178 std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
179 myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
182 if (myDocumentShapeFilter.IsNull())
183 myDocumentShapeFilter = new ModuleBase_ShapeDocumentFilter(myWorkshop);
184 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
187 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
189 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
190 DataPtr aData = myCurrentSketch->data();
191 if ((!aData) || (!aData->isValid())) {
192 // The sketch was aborted
193 myCurrentSketch = CompositeFeaturePtr();
196 // Hide all sketcher sub-Objects
197 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
198 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
199 for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
200 FeaturePtr aFeature = myCurrentSketch->subFeature(i);
201 std::list<ResultPtr> aResults = aFeature->results();
202 std::list<ResultPtr>::const_iterator aIt;
203 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
204 aDisplayer->erase((*aIt), false);
206 aDisplayer->erase(aFeature, false);
208 // Display sketcher result
209 std::list<ResultPtr> aResults = myCurrentSketch->results();
210 std::list<ResultPtr>::const_iterator aIt;
211 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
212 aDisplayer->display((*aIt), false);
214 aDisplayer->display(myCurrentSketch);
216 myCurrentSketch = CompositeFeaturePtr();
217 myWorkshop->viewer()->removeSelectionFilter(myPlaneFilter);
219 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
222 void PartSet_Module::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
224 myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
228 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
230 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
231 // Restart last operation type
232 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
233 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
234 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
235 // Initialise new line with first point equal to end of previous
236 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
238 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
239 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
240 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
242 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
243 PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(),
244 aWgt->attributeID(), aPoint->x(), aPoint->y());
245 theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
250 // Start editing constraint
251 if (theOperation->isEditOperation()) {
252 std::string aId = theOperation->id().toStdString();
253 if (sketchOperationIdList().contains(QString(aId.c_str()))) {
254 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
255 (aId == SketchPlugin_ConstraintLength::ID()) ||
256 (aId == SketchPlugin_ConstraintDistance::ID())) {
257 // Find and activate widget for management of point for dimension line position
258 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
259 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
260 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
262 aPanel->activateWidget(aPntWgt);
273 void PartSet_Module::onSelectionChanged()
275 // Editing of constraints can be done on selection
276 ModuleBase_ISelection* aSelect = myWorkshop->selection();
277 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
278 if (aSelected.size() == 1) {
279 ModuleBase_ViewerPrs aPrs = aSelected.first();
280 ObjectPtr aObject = aPrs.object();
281 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
283 std::string aId = aFeature->getKind();
284 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
285 (aId == SketchPlugin_ConstraintLength::ID()) ||
286 (aId == SketchPlugin_ConstraintDistance::ID())) {
287 editFeature(aFeature);
293 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
295 if (!(theEvent->buttons() & Qt::LeftButton))
297 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
298 // Use only for sketch operations
299 if (aOperation && myCurrentSketch) {
300 if (!PartSet_Tools::sketchPlane(myCurrentSketch))
303 bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
304 bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
306 // Avoid non-sketch operations
307 if ((!isSketchOpe) && (!isSketcher))
310 bool isEditing = aOperation->isEditOperation();
312 // Ignore creation sketch operation
313 if ((!isSketcher) && (!isEditing))
316 // Remember highlighted objects for editing
317 ModuleBase_ISelection* aSelect = myWorkshop->selection();
318 QList<ModuleBase_ViewerPrs> aObjects = aSelect->getHighlighted();
319 myEditingFeatures.clear();
320 myEditingAttr.clear();
321 if (aObjects.size() > 0) {
322 foreach(ModuleBase_ViewerPrs aPrs, aObjects) {
323 FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first().object());
325 myEditingFeatures.append(aFeature);
326 TopoDS_Shape aShape = aPrs.shape();
327 if (!aShape.IsNull()) {
328 if (aShape.ShapeType() == TopAbs_VERTEX) {
329 AttributePtr aAttr = PartSet_Tools::findAttributeBy2dPoint(myEditingFeatures.first(),
330 aShape, myCurrentSketch);
332 myEditingAttr.append(aAttr);
338 // If nothing highlighted - return
339 if (myEditingFeatures.size() == 0)
344 get2dPoint(theWnd, theEvent, myCurX, myCurY);
346 myWorkshop->viewer()->enableSelection(false);
350 } else if (isSketchOpe && isEditing) {
351 // If selected another object
355 get2dPoint(theWnd, theEvent, myCurX, myCurY);
357 myWorkshop->viewer()->enableSelection(false);
359 // This is necessary in order to finalize previous operation
360 QApplication::processEvents();
367 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
368 double& theX, double& theY)
370 Handle(V3d_View) aView = theWnd->v3dView();
371 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
372 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
376 void PartSet_Module::launchEditing()
378 if (myEditingFeatures.size() == 1) {
379 FeaturePtr aFeature = myEditingFeatures.first();
380 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
381 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
383 editFeature(aSPFeature);
388 /// Returns new instance of operation object (used in createOperation for customization)
389 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
391 if (theFeatureId == PartSet_OperationSketch::Type()) {
392 return new PartSet_OperationSketch(theFeatureId.c_str(), this);
394 return ModuleBase_IModule::getNewOperation(theFeatureId);
398 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
400 myWorkshop->viewer()->enableSelection(true);
402 myIsDragging = false;
404 myWorkshop->currentOperation()->commit();
409 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
412 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
413 if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
414 return; // No edit operation activated
416 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
417 Handle(V3d_View) aView = theWnd->v3dView();
418 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
420 PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
421 double dX = aX - myCurX;
422 double dY = aY - myCurY;
424 if ((aOperation->id().toStdString() == SketchPlugin_Line::ID()) &&
425 (myEditingAttr.size() > 0) &&
426 myEditingAttr.first()) {
427 // probably we have prehighlighted point
428 AttributePtr aAttr = myEditingAttr.first();
429 std::string aAttrId = aAttr->id();
430 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
431 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
432 // Find corresponded widget to provide dragging
433 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
434 if (aWgt->attributeID() == aAttrId) {
435 PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
437 aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
443 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
444 std::dynamic_pointer_cast<SketchPlugin_Feature>(aOperation->feature());
445 if (aSketchFeature) {
446 aSketchFeature->move(dX, dY);
447 ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent);
448 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
458 QStringList PartSet_Module::sketchOperationIdList() const
461 aIds << SketchPlugin_Line::ID().c_str();
462 aIds << SketchPlugin_Point::ID().c_str();
463 aIds << SketchPlugin_Arc::ID().c_str();
464 aIds << SketchPlugin_Circle::ID().c_str();
465 aIds << SketchPlugin_ConstraintLength::ID().c_str();
466 aIds << SketchPlugin_ConstraintDistance::ID().c_str();
467 aIds << SketchPlugin_ConstraintRigid::ID().c_str();
468 aIds << SketchPlugin_ConstraintRadius::ID().c_str();
469 aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
470 aIds << SketchPlugin_ConstraintParallel::ID().c_str();
474 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
476 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
477 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
478 /// If last line finished on vertex the lines creation sequence has to be break
479 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
480 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
481 if (aWidgets.last() == aPanel->activeWidget()) {
482 breakOperationSequence();
487 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
488 Config_WidgetAPI* theWidgetApi, std::string theParentId,
489 QList<ModuleBase_ModelWidget*>& theModelWidgets)
491 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
492 XGUI_Workshop* aWorkshop = aConnector->workshop();
493 if (theType == "sketch-start-label") {
494 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
495 aWgt->setWorkshop(aWorkshop);
496 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
497 this, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
498 theModelWidgets.append(aWgt);
499 return aWgt->getControl();
501 } else if (theType == "sketch-2dpoint_selector") {
502 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
503 aWgt->setWorkshop(aWorkshop);
504 aWgt->setSketch(myCurrentSketch);
506 connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)),
507 this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
509 theModelWidgets.append(aWgt);
510 return aWgt->getControl();
512 } if (theType == "point2ddistance") {
513 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
514 aWgt->setWorkshop(aWorkshop);
515 aWgt->setSketch(myCurrentSketch);
517 theModelWidgets.append(aWgt);
518 return aWgt->getControl();
520 } if (theType == "sketch_shape_selector") {
521 PartSet_WidgetShapeSelector* aWgt =
522 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
523 aWgt->setSketcher(myCurrentSketch);
525 theModelWidgets.append(aWgt);
526 return aWgt->getControl();
528 } if (theType == "sketch_constraint_shape_selector") {
529 PartSet_WidgetConstraintShapeSelector* aWgt =
530 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
531 aWgt->setSketcher(myCurrentSketch);
533 theModelWidgets.append(aWgt);
534 return aWgt->getControl();