1 // File: PartSet_OperationFeatureCreate.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationFeatureCreate.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Point.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Circle.h>
14 #include <SketchPlugin_Arc.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintLength.h>
17 #include <SketchPlugin_ConstraintRadius.h>
18 #include <SketchPlugin_ConstraintParallel.h>
19 #include <SketchPlugin_ConstraintPerpendicular.h>
20 #include <SketchPlugin_ConstraintCoincidence.h>
22 #include <GeomAPI_Pnt2d.h>
24 #include <ModuleBase_OperationDescription.h>
25 #include <ModuleBase_WidgetPoint2D.h>
26 #include <ModuleBase_WidgetFeature.h>
27 #include <ModuleBase_WidgetPoint2dDistance.h>
29 #include <XGUI_ViewerPrs.h>
30 #include <XGUI_Constants.h>
32 #include <V3d_View.hxx>
33 #include <TopoDS_Vertex.hxx>
35 #include <BRep_Tool.hxx>
41 #include <QMouseEvent>
45 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
47 FeaturePtr theFeature)
48 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
52 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
56 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
58 return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND ||
59 theId == SKETCH_CIRCLE_KIND ||
60 theId == SKETCH_ARC_KIND ||
61 theId == SKETCH_CONSTRAINT_DISTANCE_KIND ||
62 theId == SKETCH_CONSTRAINT_LENGTH_KIND ||
63 theId == SKETCH_CONSTRAINT_RADIUS_KIND ||
64 theId == SKETCH_CONSTRAINT_PARALLEL_KIND ||
65 theId == SKETCH_CONSTRAINT_PERPENDICULAR_KIND ||
66 theId == SKETCH_CONSTRAINT_COINCIDENCE_KIND;
69 bool PartSet_OperationFeatureCreate::canBeCommitted() const
71 return !myActiveWidget;
74 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
76 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
79 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
81 std::list<int> aModes;
82 if (theFeature != feature())
83 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
87 void PartSet_OperationFeatureCreate::init(FeaturePtr theFeature,
88 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
89 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
91 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
93 myInitFeature = theFeature;
96 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
101 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
102 const std::list<XGUI_ViewerPrs>& theSelected,
103 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
105 if (canBeCommitted())
107 // if the point creation is finished, the next mouse release should commit the modification
108 // the next release can happens by double click in the viewer
110 restartOperation(feature()->getKind(), feature());
114 double aX = 0, anY = 0;
116 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
117 if (theSelected.empty()) {
118 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
121 XGUI_ViewerPrs aPrs = theSelected.front();
122 const TopoDS_Shape& aShape = aPrs.shape();
123 if (!aShape.IsNull()) // the point is selected
125 if (aShape.ShapeType() == TopAbs_VERTEX) {
126 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
127 if (!aVertex.IsNull()) {
128 aPoint = BRep_Tool::Pnt(aVertex);
129 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
131 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
135 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
137 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
138 // move to selected line
139 if (feature()->getKind() == SKETCH_LINE_KIND) {
140 //FeaturePtr aFeature = aPrs.feature();
141 //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
146 bool isApplyed = false;
148 isApplyed = setWidgetPoint(aX, anY);
150 if (!theSelected.empty()) {
151 XGUI_ViewerPrs aPrs = theSelected.front();
152 FeaturePtr aFeature = aPrs.feature();
154 isApplyed = setWidgetFeature(aFeature);
159 emit activateNextWidget(myActiveWidget);
163 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
165 if (canBeCommitted()) {
167 restartOperation(feature()->getKind(), feature());
171 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
172 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
173 setWidgetPoint(aX, anY);
178 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
180 int aKeyType = theEvent->key();
181 // the second point should be activated by any modification in the property panel
182 if (!theName.empty())
184 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
186 keyReleased(theEvent->key());
189 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
191 myActiveWidget = theWidget;
193 if (myInitFeature && myActiveWidget) {
194 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
196 aWgt->initFromPrevious(myInitFeature);
197 myInitFeature = FeaturePtr();
198 emit activateNextWidget(myActiveWidget);
202 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
205 case Qt::Key_Return: {
206 if (canBeCommitted())
209 // it start a new line creation at a free point
210 restartOperation(feature()->getKind(), FeaturePtr());
214 case Qt::Key_Escape: {
215 if (canBeCommitted())
229 void PartSet_OperationFeatureCreate::startOperation()
231 PartSet_OperationSketchBase::startOperation();
232 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
234 emit multiSelectionEnabled(false);
237 void PartSet_OperationFeatureCreate::abortOperation()
239 emit featureConstructed(feature(), FM_Hide);
240 PartSet_OperationSketchBase::abortOperation();
243 void PartSet_OperationFeatureCreate::stopOperation()
245 PartSet_OperationSketchBase::stopOperation();
246 emit multiSelectionEnabled(true);
249 void PartSet_OperationFeatureCreate::afterCommitOperation()
251 PartSet_OperationSketchBase::afterCommitOperation();
252 emit featureConstructed(feature(), FM_Deactivation);
255 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
257 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
259 boost::shared_ptr<SketchPlugin_Feature> aFeature =
260 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
262 aFeature->addSub(aNewFeature);
264 //myFeaturePrs->init(aNewFeature);
265 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
267 emit featureConstructed(aNewFeature, FM_Activation);
273 bool PartSet_OperationFeatureCreate::isPointWidget() const
275 return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget) ||
276 dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
279 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
281 boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
282 ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
284 aWidget->setPoint(aPoint);
287 ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
289 aWgt->setPoint(feature(), aPoint);
296 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
298 ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
302 return aWidget->setFeature(theFeature);