1 // File: PartSet_OperationSketchLine.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationSketchLine.h>
7 #include <PartSet_Tools.h>
9 #include <SketchPlugin_Feature.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Document.h>
14 #include <Geom_Line.hxx>
17 #include <XGUI_ViewerPrs.h>
19 #include <SketchPlugin_Line.h>
21 #include <V3d_View.hxx>
22 #include <TopoDS_Vertex.hxx>
24 #include <BRep_Tool.hxx>
30 #include <QMouseEvent>
34 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
36 boost::shared_ptr<ModelAPI_Feature> theFeature)
37 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
38 myPointSelectionMode(SM_FirstPoint)
42 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
46 bool PartSet_OperationSketchLine::isGranted() const
51 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
53 return std::list<int>();
56 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
60 // use the last point of the previous feature as the first of the new one
61 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
62 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
65 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
66 const std::list<XGUI_ViewerPrs>& theSelected)
70 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
71 if (theSelected.empty()) {
72 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
75 XGUI_ViewerPrs aPrs = theSelected.front();
76 const TopoDS_Shape& aShape = aPrs.shape();
77 if (!aShape.IsNull()) // the point is selected
79 if (aShape.ShapeType() == TopAbs_VERTEX) {
80 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
81 if (!aVertex.IsNull()) {
82 aPoint = BRep_Tool::Pnt(aVertex);
83 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
86 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
88 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
90 double X0, X1, X2, X3;
91 double Y0, Y1, Y2, Y3;
92 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
93 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
94 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
96 switch (myPointSelectionMode) {
98 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
100 case SM_SecondPoint: {
101 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
102 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
112 switch (myPointSelectionMode)
114 case SM_FirstPoint: {
115 setLinePoint(aX, anY, LINE_ATTR_START);
116 myPointSelectionMode = SM_SecondPoint;
119 case SM_SecondPoint: {
120 setLinePoint(aX, anY, LINE_ATTR_END);
122 emit featureConstructed(feature(), FM_Deactivation);
123 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
131 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
133 switch (myPointSelectionMode)
137 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
138 setLinePoint(aPoint, theView, LINE_ATTR_END);
146 void PartSet_OperationSketchLine::keyReleased(const int theKey)
149 case Qt::Key_Escape: {
153 case Qt::Key_Return: {
155 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
163 void PartSet_OperationSketchLine::startOperation()
165 PartSet_OperationSketchBase::startOperation();
166 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
167 emit multiSelectionEnabled(false);
170 void PartSet_OperationSketchLine::abortOperation()
172 emit featureConstructed(feature(), FM_Abort);
173 PartSet_OperationSketchBase::abortOperation();
176 void PartSet_OperationSketchLine::stopOperation()
178 PartSet_OperationSketchBase::stopOperation();
179 emit multiSelectionEnabled(true);
182 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
184 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
186 boost::shared_ptr<SketchPlugin_Feature> aFeature =
187 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
189 aFeature->addSub(aNewFeature);
192 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
193 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
194 (aData->attribute(LINE_ATTR_START));
195 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
198 emit featureConstructed(aNewFeature, FM_Activation);
202 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
203 const std::string& theAttribute,
204 double& theX, double& theY)
208 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
209 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
210 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
215 void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
216 const std::string& theAttribute)
218 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
219 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
220 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
221 aPoint->setValue(theX, theY);
224 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
225 Handle(V3d_View) theView,
226 const std::string& theAttribute)
229 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
230 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
231 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
232 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
233 aPoint->setValue(aX, anY);