1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "PartSet_Module.h"
4 #include <PartSet_WidgetSketchLabel.h>
5 #include <PartSet_Validators.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_WidgetPoint2d.h>
8 #include <PartSet_WidgetPoint2dDistance.h>
9 #include <PartSet_WidgetShapeSelector.h>
10 #include <PartSet_SketcherMgr.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_IViewWindow.h>
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_FilterFactory.h>
18 #include <ModuleBase_FilterLinearEdge.h>
19 #include <ModuleBase_FilterFace.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Session.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Dir.h>
32 #include <XGUI_Displayer.h>
33 #include <XGUI_Workshop.h>
34 #include <XGUI_OperationMgr.h>
35 #include <XGUI_PropertyPanel.h>
36 #include <XGUI_ModuleConnector.h>
37 #include <XGUI_Tools.h>
39 #include <SketchPlugin_Feature.h>
40 #include <SketchPlugin_Sketch.h>
41 #include <SketchPlugin_Line.h>
42 //#include <SketchPlugin_Arc.h>
43 //#include <SketchPlugin_Circle.h>
44 #include <SketchPlugin_ConstraintLength.h>
45 #include <SketchPlugin_ConstraintDistance.h>
46 #include <SketchPlugin_ConstraintParallel.h>
47 #include <SketchPlugin_ConstraintPerpendicular.h>
48 #include <SketchPlugin_ConstraintRadius.h>
49 //#include <SketchPlugin_ConstraintRigid.h>
51 #include <Events_Loop.h>
53 #include <StdSelect_TypeOfFace.hxx>
54 #include <TopoDS_Vertex.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <BRep_Tool.hxx>
60 #include <QMouseEvent>
63 #include <QApplication>
65 #include <GeomAlgoAPI_FaceBuilder.h>
66 #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 myRestartingMode(RM_None)
82 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
83 mySketchMgr = new PartSet_SketcherMgr(this);
85 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
86 XGUI_Workshop* aWorkshop = aConnector->workshop();
88 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
89 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
90 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
91 this, SLOT(onOperationActivatedByPreselection()));
93 ModuleBase_IViewer* aViewer = theWshop->viewer();
94 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
95 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
98 PartSet_Module::~PartSet_Module()
100 if (!myDocumentShapeFilter.IsNull())
101 myDocumentShapeFilter.Nullify();
104 void PartSet_Module::registerValidators()
106 //Registering of validators
107 SessionPtr aMgr = ModelAPI_Session::get();
108 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
109 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
110 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
111 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
112 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
113 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
114 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
115 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
116 aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
119 void PartSet_Module::registerFilters()
121 //Registering of selection filters
122 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
123 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
125 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
126 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
129 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
131 if (theOperation->isEditOperation())
133 // the selection is cleared after commit the create operation
134 // in order to do not use the same selected objects in the restarted operation
135 // for common behaviour, the selection is cleared even if the operation is not restarted
136 myWorkshop->viewer()->AISContext()->ClearSelected();
138 /// Restart sketcher operations automatically
139 FeaturePtr aFeature = theOperation->feature();
140 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
141 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
142 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
143 myRestartingMode == RM_EmptyFeatureUsed)) {
144 myLastOperationId = theOperation->id();
145 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
147 launchOperation(myLastOperationId);
149 breakOperationSequence();
152 void PartSet_Module::breakOperationSequence()
154 myLastOperationId = "";
155 myLastFeature = FeaturePtr();
156 myRestartingMode = RM_None;
159 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
161 breakOperationSequence();
164 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
166 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
167 mySketchMgr->startSketch(theOperation);
169 if (myDocumentShapeFilter.IsNull())
170 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
171 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
174 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
176 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
177 mySketchMgr->stopSketch(theOperation);
179 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
183 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
185 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
186 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
187 (theOperation->isEditOperation())) {
188 // we have to manually activate the sketch label in edit mode
189 aPanel->activateWidget(aPanel->modelWidgets().first());
193 // Restart last operation type
194 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
195 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
196 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
197 // Initialise new line with first point equal to end of previous
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(mySketchMgr->activeSketch(), theOperation->feature(),
206 aWgt->attributeID(), aPoint->x(), aPoint->y());
207 theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
212 // Start editing constraint
213 if (theOperation->isEditOperation()) {
214 std::string aId = theOperation->id().toStdString();
215 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
216 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
217 (aId == SketchPlugin_ConstraintLength::ID()) ||
218 (aId == SketchPlugin_ConstraintDistance::ID())) {
219 // Find and activate widget for management of point for dimension line position
220 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
221 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
222 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
224 aPanel->activateWidget(aPntWgt);
235 void PartSet_Module::onSelectionChanged()
237 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
241 bool isSketcherOp = false;
242 // An edit operation is enable only if the current opeation is the sketch operation
243 if (mySketchMgr->activeSketch()) {
244 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
245 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
248 // Editing of constraints can be done on selection
249 ModuleBase_ISelection* aSelect = myWorkshop->selection();
250 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
251 if (aSelected.size() == 1) {
252 ModuleBase_ViewerPrs aPrs = aSelected.first();
253 ObjectPtr aObject = aPrs.object();
254 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
256 std::string aId = aFeature->getKind();
257 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
258 (aId == SketchPlugin_ConstraintLength::ID()) ||
259 (aId == SketchPlugin_ConstraintDistance::ID())) {
260 editFeature(aFeature);
267 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
269 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
270 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
271 anOpMgr->onKeyReleased(theEvent);
274 void PartSet_Module::onEnterReleased()
276 myRestartingMode = RM_EmptyFeatureUsed;
279 void PartSet_Module::onOperationActivatedByPreselection()
281 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
285 // Set final definitions if they are necessary
286 //propertyPanelDefined(aOperation);
288 /// Commit sketcher operations automatically
289 FeaturePtr aFeature = aOperation->feature();
290 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
291 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
293 aOperation->commit();
297 void PartSet_Module::onNoMoreWidgets()
299 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
301 /// Restart sketcher operations automatically
302 FeaturePtr aFeature = aOperation->feature();
303 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
304 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
306 if (myRestartingMode != RM_Forbided)
307 myRestartingMode = RM_LastFeatureUsed;
308 aOperation->commit();
313 void PartSet_Module::onVertexSelected()
315 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
316 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
317 /// If last line finished on vertex the lines creation sequence has to be break
318 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
319 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
320 if (aWidgets.last() == aPanel->activeWidget()) {
321 myRestartingMode = RM_Forbided;
326 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
327 Config_WidgetAPI* theWidgetApi, std::string theParentId,
328 QList<ModuleBase_ModelWidget*>& theModelWidgets)
330 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
331 XGUI_Workshop* aWorkshop = aConnector->workshop();
332 if (theType == "sketch-start-label") {
333 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
334 aWgt->setWorkshop(aWorkshop);
335 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
336 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
337 theModelWidgets.append(aWgt);
338 return aWgt->getControl();
340 } else if (theType == "sketch-2dpoint_selector") {
341 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
342 aWgt->setWorkshop(aWorkshop);
343 aWgt->setSketch(mySketchMgr->activeSketch());
345 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
347 theModelWidgets.append(aWgt);
348 return aWgt->getControl();
350 } if (theType == "point2ddistance") {
351 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
352 aWgt->setWorkshop(aWorkshop);
353 aWgt->setSketch(mySketchMgr->activeSketch());
355 theModelWidgets.append(aWgt);
356 return aWgt->getControl();
358 } if (theType == "sketch_shape_selector") {
359 PartSet_WidgetShapeSelector* aWgt =
360 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
361 aWgt->setSketcher(mySketchMgr->activeSketch());
363 theModelWidgets.append(aWgt);
364 return aWgt->getControl();
366 } if (theType == "sketch_constraint_shape_selector") {
367 PartSet_WidgetConstraintShapeSelector* aWgt =
368 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
369 aWgt->setSketcher(mySketchMgr->activeSketch());
371 theModelWidgets.append(aWgt);
372 return aWgt->getControl();