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>
56 #include <StdSelect_TypeOfFace.hxx>
57 #include <TopoDS_Vertex.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <BRep_Tool.hxx>
63 #include <QMouseEvent>
66 #include <QApplication>
68 #include <GeomAlgoAPI_FaceBuilder.h>
69 #include <GeomDataAPI_Dir.h>
75 /*!Create and return new instance of XGUI_Module*/
76 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
78 return new PartSet_Module(theWshop);
81 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
82 : ModuleBase_IModule(theWshop),
83 myRestartingMode(RM_None)
85 //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
86 mySketchMgr = new PartSet_SketcherMgr(this);
88 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
89 XGUI_Workshop* aWorkshop = aConnector->workshop();
91 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
92 connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
93 connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
94 this, SLOT(onOperationActivatedByPreselection()));
96 ModuleBase_IViewer* aViewer = theWshop->viewer();
97 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
98 this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
101 PartSet_Module::~PartSet_Module()
103 if (!myDocumentShapeFilter.IsNull())
104 myDocumentShapeFilter.Nullify();
107 void PartSet_Module::registerValidators()
109 //Registering of validators
110 SessionPtr aMgr = ModelAPI_Session::get();
111 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
112 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
113 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
114 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
115 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
116 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
117 aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
118 aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
119 aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
120 aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
123 void PartSet_Module::registerFilters()
125 //Registering of selection filters
126 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
127 ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
129 aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
130 aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
131 aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
132 Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
133 aFactory->registerFilter("NoConstructionSubShapesFilter",
134 new ModuleBase_FilterCustom(aSelectFilter));
137 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
139 if (theOperation->isEditOperation())
141 // the selection is cleared after commit the create operation
142 // in order to do not use the same selected objects in the restarted operation
143 // for common behaviour, the selection is cleared even if the operation is not restarted
144 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
145 if (!aContext.IsNull())
146 aContext->ClearSelected();
148 /// Restart sketcher operations automatically
149 FeaturePtr aFeature = theOperation->feature();
150 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
151 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
152 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
153 myRestartingMode == RM_EmptyFeatureUsed)) {
154 myLastOperationId = theOperation->id();
155 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
157 launchOperation(myLastOperationId);
159 breakOperationSequence();
162 void PartSet_Module::breakOperationSequence()
164 myLastOperationId = "";
165 myLastFeature = FeaturePtr();
166 myRestartingMode = RM_None;
169 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
171 breakOperationSequence();
174 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
176 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
177 mySketchMgr->startSketch(theOperation);
179 if (myDocumentShapeFilter.IsNull())
180 myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
181 myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
184 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
186 if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
187 mySketchMgr->stopSketch(theOperation);
189 myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
192 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
194 bool aCanDisplay = false;
195 CompositeFeaturePtr aSketchFeature = mySketchMgr->activeSketch();
196 if (aSketchFeature.get() != NULL) {
197 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
199 if (aFeature.get() != NULL) {
200 if (aFeature == aSketchFeature) {
204 for (int i = 0; i < aSketchFeature->numberOfSubs() && !aCanDisplay; i++) {
205 FeaturePtr aSubFeature = aSketchFeature->subFeature(i);
206 std::list<ResultPtr> aResults = aSubFeature->results();
207 std::list<ResultPtr>::const_iterator aIt;
208 for (aIt = aResults.begin(); aIt != aResults.end() && !aCanDisplay; ++aIt) {
209 if (theObject == (*aIt))
212 if (aSubFeature == theObject)
219 aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
224 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
226 ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
227 if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) &&
228 (theOperation->isEditOperation())) {
229 // we have to manually activate the sketch label in edit mode
230 aPanel->activateWidget(aPanel->modelWidgets().first());
234 // Restart last operation type
235 if ((theOperation->id() == myLastOperationId) && myLastFeature) {
236 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
237 if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
238 // Initialise new line with first point equal to end of previous
239 PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
241 std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
242 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
243 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
245 aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
246 PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(),
247 aWgt->attributeID(), aPoint->x(), aPoint->y());
248 aPanel->activateNextWidget(aPnt2dWgt);
253 // Start editing constraint
254 if (theOperation->isEditOperation()) {
255 // TODO: #391 - to be removed
256 std::string aId = theOperation->id().toStdString();
257 if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
258 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
259 (aId == SketchPlugin_ConstraintLength::ID()) ||
260 (aId == SketchPlugin_ConstraintDistance::ID())) {
261 // Find and activate widget for management of point for dimension line position
262 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
263 foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
264 PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
266 aPanel->activateWidget(aPntWgt);
277 void PartSet_Module::onSelectionChanged()
279 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
283 bool isSketcherOp = false;
284 // An edit operation is enable only if the current opeation is the sketch operation
285 if (mySketchMgr->activeSketch()) {
286 if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
287 isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
290 // Editing of constraints can be done on selection
291 ModuleBase_ISelection* aSelect = myWorkshop->selection();
292 QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
293 if (aSelected.size() == 1) {
294 ModuleBase_ViewerPrs aPrs = aSelected.first();
295 ObjectPtr aObject = aPrs.object();
296 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
298 std::string aId = aFeature->getKind();
299 if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
300 (aId == SketchPlugin_ConstraintLength::ID()) ||
301 (aId == SketchPlugin_ConstraintDistance::ID())) {
302 editFeature(aFeature);
309 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
311 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
312 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
313 anOpMgr->onKeyReleased(theEvent);
316 void PartSet_Module::onEnterReleased()
318 myRestartingMode = RM_EmptyFeatureUsed;
321 void PartSet_Module::onOperationActivatedByPreselection()
323 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
327 // Set final definitions if they are necessary
328 //propertyPanelDefined(aOperation);
330 /// Commit sketcher operations automatically
331 FeaturePtr aFeature = aOperation->feature();
332 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
333 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
335 aOperation->commit();
339 void PartSet_Module::onNoMoreWidgets()
341 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
343 /// Restart sketcher operations automatically
344 FeaturePtr aFeature = aOperation->feature();
345 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
346 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
348 if (myRestartingMode != RM_Forbided)
349 myRestartingMode = RM_LastFeatureUsed;
350 aOperation->commit();
355 void PartSet_Module::onVertexSelected()
357 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
358 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
359 /// If last line finished on vertex the lines creation sequence has to be break
360 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
361 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
362 if (aWidgets.last() == aPanel->activeWidget()) {
363 myRestartingMode = RM_Forbided;
368 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
369 Config_WidgetAPI* theWidgetApi, std::string theParentId,
370 QList<ModuleBase_ModelWidget*>& theModelWidgets)
372 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
373 XGUI_Workshop* aWorkshop = aConnector->workshop();
374 if (theType == "sketch-start-label") {
375 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
376 aWgt->setWorkshop(aWorkshop);
377 connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
378 mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
379 theModelWidgets.append(aWgt);
380 return aWgt->getControl();
382 } else if (theType == "sketch-2dpoint_selector") {
383 PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
384 aWgt->setWorkshop(aWorkshop);
385 aWgt->setSketch(mySketchMgr->activeSketch());
387 connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
389 theModelWidgets.append(aWgt);
390 return aWgt->getControl();
392 } if (theType == "point2ddistance") {
393 PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
394 aWgt->setWorkshop(aWorkshop);
395 aWgt->setSketch(mySketchMgr->activeSketch());
397 theModelWidgets.append(aWgt);
398 return aWgt->getControl();
400 } if (theType == "sketch_shape_selector") {
401 PartSet_WidgetShapeSelector* aWgt =
402 new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
403 aWgt->setSketcher(mySketchMgr->activeSketch());
405 theModelWidgets.append(aWgt);
406 return aWgt->getControl();
408 } if (theType == "sketch_constraint_shape_selector") {
409 PartSet_WidgetConstraintShapeSelector* aWgt =
410 new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
411 aWgt->setSketcher(mySketchMgr->activeSketch());
413 theModelWidgets.append(aWgt);
414 return aWgt->getControl();