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>
20 #include <ModuleBase_FilterMulti.h>
21 #include <ModuleBase_FilterCustom.h>
22 #include <ModuleBase_FilterNoConsructionSubShapes.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Session.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Dir.h>
34 #include <XGUI_Displayer.h>
35 #include <XGUI_Workshop.h>
36 #include <XGUI_OperationMgr.h>
37 #include <XGUI_PropertyPanel.h>
38 #include <XGUI_ModuleConnector.h>
39 #include <XGUI_Tools.h>
41 #include <SketchPlugin_Feature.h>
42 #include <SketchPlugin_Sketch.h>
43 #include <SketchPlugin_Line.h>
44 //#include <SketchPlugin_Arc.h>
45 //#include <SketchPlugin_Circle.h>
46 #include <SketchPlugin_ConstraintLength.h>
47 #include <SketchPlugin_ConstraintDistance.h>
48 #include <SketchPlugin_ConstraintParallel.h>
49 #include <SketchPlugin_ConstraintPerpendicular.h>
50 #include <SketchPlugin_ConstraintRadius.h>
51 //#include <SketchPlugin_ConstraintRigid.h>
53 #include <Events_Loop.h>
55 #include <StdSelect_TypeOfFace.hxx>
56 #include <TopoDS_Vertex.hxx>
58 #include <TopoDS_Shape.hxx>
59 #include <BRep_Tool.hxx>
62 #include <QMouseEvent>
65 #include <QApplication>
67 #include <GeomAlgoAPI_FaceBuilder.h>
68 #include <GeomDataAPI_Dir.h>
74 /*!Create and return new instance of XGUI_Module*/
75 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
77 return new PartSet_Module(theWshop);
80 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
81 : ModuleBase_IModule(theWshop),
82 myRestartingMode(RM_None)
84 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
85 mySketchMgr = new PartSet_SketcherMgr(this);
87 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
88 XGUI_Workshop* aWorkshop = aConnector->workshop();
90 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
91 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
92 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
93 this, SLOT(onOperationActivatedByPreselection()));
95 ModuleBase_IViewer* aViewer = theWshop->viewer();
96 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
97 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
100 PartSet_Module::~PartSet_Module()
102 if (!myDocumentShapeFilter.IsNull())
103 myDocumentShapeFilter.Nullify();
106 void PartSet_Module::registerValidators()
108 //Registering of validators
109 SessionPtr aMgr = ModelAPI_Session::get();
110 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
111 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
112 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
113 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
114 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
115 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
116 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
117 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
118 aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
121 void PartSet_Module::registerFilters()
123 //Registering of selection filters
124 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
125 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
127 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
128 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
129 aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
130 Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
131 aFactory->registerFilter("NoConstructionSubShapesFilter",
132 new ModuleBase_FilterCustom(aSelectFilter));
135 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
137 if (theOperation->isEditOperation())
139 // the selection is cleared after commit the create operation
140 // in order to do not use the same selected objects in the restarted operation
141 // for common behaviour, the selection is cleared even if the operation is not restarted
142 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
143 if (!aContext.IsNull())
144 aContext->ClearSelected();
146 /// Restart sketcher operations automatically
147 FeaturePtr aFeature = theOperation->feature();
148 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
149 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
150 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
151 myRestartingMode == RM_EmptyFeatureUsed)) {
152 myLastOperationId = theOperation->id();
153 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
155 launchOperation(myLastOperationId);
157 breakOperationSequence();
160 void PartSet_Module::breakOperationSequence()
162 myLastOperationId = "";
163 myLastFeature = FeaturePtr();
164 myRestartingMode = RM_None;
167 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
169 breakOperationSequence();
172 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
174 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
175 mySketchMgr->startSketch(theOperation);
177 if (myDocumentShapeFilter.IsNull())
178 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
179 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
182 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
184 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
185 mySketchMgr->stopSketch(theOperation);
187 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
190 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
192 bool aCanDisplay = false;
193 CompositeFeaturePtr aSketchFeature = mySketchMgr->activeSketch();
194 if (aSketchFeature.get() != NULL) {
195 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
197 if (aFeature.get() != NULL) {
198 if (aFeature == aSketchFeature) {
202 for (int i = 0; i < aSketchFeature->numberOfSubs() && !aCanDisplay; i++) {
203 FeaturePtr aSubFeature = aSketchFeature->subFeature(i);
204 std::list<ResultPtr> aResults = aSubFeature->results();
205 std::list<ResultPtr>::const_iterator aIt;
206 for (aIt = aResults.begin(); aIt != aResults.end() && !aCanDisplay; ++aIt) {
207 if (theObject == (*aIt))
210 if (aSubFeature == theObject)
217 aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
222 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
224 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
225 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
226 (theOperation->isEditOperation())) {
227 // we have to manually activate the sketch label in edit mode
228 aPanel->activateWidget(aPanel->modelWidgets().first());
232 // Restart last operation type
233 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
234 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
235 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
236 // Initialise new line with first point equal to end of previous
237 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
239 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
240 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
241 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
243 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
244 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
245 aWgt->attributeID(), aPoint->x(), aPoint->y());
246 aPanel->activateNextWidget(aPnt2dWgt);
251 // Start editing constraint
252 if (theOperation->isEditOperation()) {
253 // TODO: #391 - to be removed
254 std::string aId = theOperation->id().toStdString();
255 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
256 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
257 (aId == SketchPlugin_ConstraintLength::ID()) ||
258 (aId == SketchPlugin_ConstraintDistance::ID())) {
259 // Find and activate widget for management of point for dimension line position
260 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
261 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
262 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
264 aPanel->activateWidget(aPntWgt);
275 void PartSet_Module::onSelectionChanged()
277 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
281 bool isSketcherOp = false;
282 // An edit operation is enable only if the current opeation is the sketch operation
283 if (mySketchMgr->activeSketch()) {
284 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
285 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
288 // Editing of constraints can be done on selection
289 ModuleBase_ISelection* aSelect = myWorkshop->selection();
290 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
291 if (aSelected.size() == 1) {
292 ModuleBase_ViewerPrs aPrs = aSelected.first();
293 ObjectPtr aObject = aPrs.object();
294 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
296 std::string aId = aFeature->getKind();
297 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
298 (aId == SketchPlugin_ConstraintLength::ID()) ||
299 (aId == SketchPlugin_ConstraintDistance::ID())) {
300 editFeature(aFeature);
307 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
309 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
310 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
311 anOpMgr->onKeyReleased(theEvent);
314 void PartSet_Module::onEnterReleased()
316 myRestartingMode = RM_EmptyFeatureUsed;
319 void PartSet_Module::onOperationActivatedByPreselection()
321 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
325 // Set final definitions if they are necessary
326 //propertyPanelDefined(aOperation);
328 /// Commit sketcher operations automatically
329 FeaturePtr aFeature = aOperation->feature();
330 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
331 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
333 aOperation->commit();
337 void PartSet_Module::onNoMoreWidgets()
339 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
341 /// Restart sketcher operations automatically
342 FeaturePtr aFeature = aOperation->feature();
343 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
344 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
346 if (myRestartingMode != RM_Forbided)
347 myRestartingMode = RM_LastFeatureUsed;
348 aOperation->commit();
353 void PartSet_Module::onVertexSelected()
355 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
356 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
357 /// If last line finished on vertex the lines creation sequence has to be break
358 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
359 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
360 if (aWidgets.last() == aPanel->activeWidget()) {
361 myRestartingMode = RM_Forbided;
366 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
367 Config_WidgetAPI* theWidgetApi, std::string theParentId,
368 QList<ModuleBase_ModelWidget*>& theModelWidgets)
370 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
371 XGUI_Workshop* aWorkshop = aConnector->workshop();
372 if (theType == "sketch-start-label") {
373 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
374 aWgt->setWorkshop(aWorkshop);
375 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
376 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
377 theModelWidgets.append(aWgt);
378 return aWgt->getControl();
380 } else if (theType == "sketch-2dpoint_selector") {
381 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
382 aWgt->setWorkshop(aWorkshop);
383 aWgt->setSketch(mySketchMgr->activeSketch());
385 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
387 theModelWidgets.append(aWgt);
388 return aWgt->getControl();
390 } if (theType == "point2ddistance") {
391 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
392 aWgt->setWorkshop(aWorkshop);
393 aWgt->setSketch(mySketchMgr->activeSketch());
395 theModelWidgets.append(aWgt);
396 return aWgt->getControl();
398 } if (theType == "sketch_shape_selector") {
399 PartSet_WidgetShapeSelector* aWgt =
400 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
401 aWgt->setSketcher(mySketchMgr->activeSketch());
403 theModelWidgets.append(aWgt);
404 return aWgt->getControl();
406 } if (theType == "sketch_constraint_shape_selector") {
407 PartSet_WidgetConstraintShapeSelector* aWgt =
408 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
409 aWgt->setSketcher(mySketchMgr->activeSketch());
411 theModelWidgets.append(aWgt);
412 return aWgt->getControl();