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>
29 #include <ModelAPI_ShapeValidator.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Dir.h>
35 #include <XGUI_Displayer.h>
36 #include <XGUI_Workshop.h>
37 #include <XGUI_OperationMgr.h>
38 #include <XGUI_PropertyPanel.h>
39 #include <XGUI_ModuleConnector.h>
40 #include <XGUI_Tools.h>
42 #include <SketchPlugin_Feature.h>
43 #include <SketchPlugin_Sketch.h>
44 #include <SketchPlugin_Line.h>
45 //#include <SketchPlugin_Arc.h>
46 //#include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_ConstraintLength.h>
48 #include <SketchPlugin_ConstraintDistance.h>
49 #include <SketchPlugin_ConstraintParallel.h>
50 #include <SketchPlugin_ConstraintPerpendicular.h>
51 #include <SketchPlugin_ConstraintRadius.h>
52 //#include <SketchPlugin_ConstraintRigid.h>
54 #include <Events_Loop.h>
55 #include <Config_PropManager.h>
57 #include <StdSelect_TypeOfFace.hxx>
58 #include <TopoDS_Vertex.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <BRep_Tool.hxx>
64 #include <QMouseEvent>
67 #include <QApplication>
69 #include <GeomAlgoAPI_FaceBuilder.h>
70 #include <GeomDataAPI_Dir.h>
76 /*!Create and return new instance of XGUI_Module*/
77 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
79 return new PartSet_Module(theWshop);
82 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
83 : ModuleBase_IModule(theWshop),
84 myRestartingMode(RM_None)
86 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
87 mySketchMgr = new PartSet_SketcherMgr(this);
89 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
90 XGUI_Workshop* aWorkshop = aConnector->workshop();
92 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
93 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
94 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
95 this, SLOT(onOperationActivatedByPreselection()));
97 ModuleBase_IViewer* aViewer = theWshop->viewer();
98 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
99 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
102 PartSet_Module::~PartSet_Module()
104 if (!myDocumentShapeFilter.IsNull())
105 myDocumentShapeFilter.Nullify();
108 void PartSet_Module::registerValidators()
110 //Registering of validators
111 SessionPtr aMgr = ModelAPI_Session::get();
112 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
113 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
114 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
115 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
116 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
117 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
118 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
119 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
120 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
121 aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
124 void PartSet_Module::registerFilters()
126 //Registering of selection filters
127 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
128 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
130 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
131 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
132 aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
133 Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
134 aFactory->registerFilter("NoConstructionSubShapesFilter",
135 new ModuleBase_FilterCustom(aSelectFilter));
138 void PartSet_Module::registerProperties()
140 Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
142 Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
143 Config_Prop::Integer, SKETCH_WIDTH);
146 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
148 if (theOperation->isEditOperation())
150 // the selection is cleared after commit the create operation
151 // in order to do not use the same selected objects in the restarted operation
152 // for common behaviour, the selection is cleared even if the operation is not restarted
153 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
154 if (!aContext.IsNull())
155 aContext->ClearSelected();
157 /// Restart sketcher operations automatically
158 FeaturePtr aFeature = theOperation->feature();
159 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
160 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
161 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
162 myRestartingMode == RM_EmptyFeatureUsed)) {
163 myLastOperationId = theOperation->id();
164 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
166 launchOperation(myLastOperationId);
168 breakOperationSequence();
171 void PartSet_Module::breakOperationSequence()
173 myLastOperationId = "";
174 myLastFeature = FeaturePtr();
175 myRestartingMode = RM_None;
178 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
180 breakOperationSequence();
183 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
185 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
186 mySketchMgr->startSketch(theOperation);
188 if (myDocumentShapeFilter.IsNull())
189 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
190 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
193 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
195 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
196 mySketchMgr->stopSketch(theOperation);
198 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
201 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
203 bool aCanDisplay = false;
204 CompositeFeaturePtr aSketchFeature = mySketchMgr->activeSketch();
205 if (aSketchFeature.get() != NULL) {
206 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
208 // MPV: the second and third conditions to avoid crash on exit for application
209 if (aFeature.get() != NULL && aFeature->data().get() && aFeature->data()->isValid()) {
210 if (aFeature == aSketchFeature) {
213 else if (aSketchFeature.get() && aSketchFeature->data().get() &&
214 aSketchFeature->data()->isValid()) {
215 for (int i = 0; i < aSketchFeature->numberOfSubs() && !aCanDisplay; i++) {
216 FeaturePtr aSubFeature = aSketchFeature->subFeature(i);
217 std::list<ResultPtr> aResults = aSubFeature->results();
218 std::list<ResultPtr>::const_iterator aIt;
219 for (aIt = aResults.begin(); aIt != aResults.end() && !aCanDisplay; ++aIt) {
220 if (theObject == (*aIt))
223 if (aSubFeature == theObject)
230 aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
235 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
237 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
238 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
239 (theOperation->isEditOperation())) {
240 // we have to manually activate the sketch label in edit mode
241 aPanel->activateWidget(aPanel->modelWidgets().first());
245 // Restart last operation type
246 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
247 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
248 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
249 // Initialise new line with first point equal to end of previous
250 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
252 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
253 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
254 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
256 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
257 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
258 aWgt->attributeID(), aPoint->x(), aPoint->y());
259 aPanel->activateNextWidget(aPnt2dWgt);
264 // Start editing constraint
265 if (theOperation->isEditOperation()) {
266 // TODO: #391 - to be removed
267 std::string aId = theOperation->id().toStdString();
268 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
269 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
270 (aId == SketchPlugin_ConstraintLength::ID()) ||
271 (aId == SketchPlugin_ConstraintDistance::ID())) {
272 // Find and activate widget for management of point for dimension line position
273 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
274 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
275 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
277 aPanel->activateWidget(aPntWgt);
288 void PartSet_Module::onSelectionChanged()
290 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
294 bool isSketcherOp = false;
295 // An edit operation is enable only if the current opeation is the sketch operation
296 if (mySketchMgr->activeSketch()) {
297 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
298 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
301 // Editing of constraints can be done on selection
302 ModuleBase_ISelection* aSelect = myWorkshop->selection();
303 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
304 if (aSelected.size() == 1) {
305 ModuleBase_ViewerPrs aPrs = aSelected.first();
306 ObjectPtr aObject = aPrs.object();
307 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
309 std::string aId = aFeature->getKind();
310 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
311 (aId == SketchPlugin_ConstraintLength::ID()) ||
312 (aId == SketchPlugin_ConstraintDistance::ID())) {
313 editFeature(aFeature);
320 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
322 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
323 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
324 anOpMgr->onKeyReleased(theEvent);
327 void PartSet_Module::onEnterReleased()
329 myRestartingMode = RM_EmptyFeatureUsed;
332 void PartSet_Module::onOperationActivatedByPreselection()
334 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
338 // Set final definitions if they are necessary
339 //propertyPanelDefined(aOperation);
341 /// Commit sketcher operations automatically
342 FeaturePtr aFeature = aOperation->feature();
343 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
344 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
346 aOperation->commit();
350 void PartSet_Module::onNoMoreWidgets()
352 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
354 /// Restart sketcher operations automatically
355 FeaturePtr aFeature = aOperation->feature();
356 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
357 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
359 if (myRestartingMode != RM_Forbided)
360 myRestartingMode = RM_LastFeatureUsed;
361 aOperation->commit();
366 void PartSet_Module::onVertexSelected()
368 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
369 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
370 /// If last line finished on vertex the lines creation sequence has to be break
371 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
372 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
373 if (aWidgets.last() == aPanel->activeWidget()) {
374 myRestartingMode = RM_Forbided;
379 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
380 Config_WidgetAPI* theWidgetApi, std::string theParentId,
381 QList<ModuleBase_ModelWidget*>& theModelWidgets)
383 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
384 XGUI_Workshop* aWorkshop = aConnector->workshop();
385 if (theType == "sketch-start-label") {
386 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
387 aWgt->setWorkshop(aWorkshop);
388 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
389 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
390 theModelWidgets.append(aWgt);
391 return aWgt->getControl();
393 } else if (theType == "sketch-2dpoint_selector") {
394 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
395 aWgt->setWorkshop(aWorkshop);
396 aWgt->setSketch(mySketchMgr->activeSketch());
398 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
400 theModelWidgets.append(aWgt);
401 return aWgt->getControl();
403 } if (theType == "point2ddistance") {
404 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
405 aWgt->setWorkshop(aWorkshop);
406 aWgt->setSketch(mySketchMgr->activeSketch());
408 theModelWidgets.append(aWgt);
409 return aWgt->getControl();
411 } if (theType == "sketch_shape_selector") {
412 PartSet_WidgetShapeSelector* aWgt =
413 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
414 aWgt->setSketcher(mySketchMgr->activeSketch());
416 theModelWidgets.append(aWgt);
417 return aWgt->getControl();
419 } if (theType == "sketch_constraint_shape_selector") {
420 PartSet_WidgetConstraintShapeSelector* aWgt =
421 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
422 aWgt->setSketcher(mySketchMgr->activeSketch());
424 theModelWidgets.append(aWgt);
425 return aWgt->getControl();