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)
68 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
70 if (!theSelected.empty()) {
71 XGUI_ViewerPrs aPrs = theSelected.front();
72 const TopoDS_Shape& aShape = aPrs.shape();
73 if (!aShape.IsNull()) {
74 if (aShape.ShapeType() == TopAbs_VERTEX) {
75 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
76 if (!aVertex.IsNull())
77 aPoint = BRep_Tool::Pnt(aVertex);
79 else if (aShape.ShapeType() == TopAbs_EDGE) {
80 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
83 double X0, X1, X2, X3;
84 double Y0, Y1, Y2, Y3;
85 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
86 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
88 if (myPointSelectionMode == SM_SecondPoint) {
89 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
90 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
93 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
95 setLinePoint(aX, anY, LINE_ATTR_END);
97 emit featureConstructed(feature(), FM_Deactivation);
98 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
106 switch (myPointSelectionMode)
108 case SM_FirstPoint: {
109 setLinePoint(aPoint, theView, LINE_ATTR_START);
110 myPointSelectionMode = SM_SecondPoint;
113 case SM_SecondPoint: {
114 setLinePoint(aPoint, theView, LINE_ATTR_END);
116 emit featureConstructed(feature(), FM_Deactivation);
117 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
125 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
127 switch (myPointSelectionMode)
131 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
132 setLinePoint(aPoint, theView, LINE_ATTR_END);
140 void PartSet_OperationSketchLine::keyReleased(const int theKey)
143 case Qt::Key_Escape: {
147 case Qt::Key_Return: {
149 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
157 void PartSet_OperationSketchLine::startOperation()
159 PartSet_OperationSketchBase::startOperation();
160 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
161 emit multiSelectionEnabled(false);
164 void PartSet_OperationSketchLine::abortOperation()
166 emit featureConstructed(feature(), FM_Abort);
167 PartSet_OperationSketchBase::abortOperation();
170 void PartSet_OperationSketchLine::stopOperation()
172 PartSet_OperationSketchBase::stopOperation();
173 emit multiSelectionEnabled(true);
176 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
178 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
180 boost::shared_ptr<SketchPlugin_Feature> aFeature =
181 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
183 aFeature->addSub(aNewFeature);
186 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
187 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
188 (aData->attribute(LINE_ATTR_START));
189 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
192 emit featureConstructed(aNewFeature, FM_Activation);
196 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
197 const std::string& theAttribute,
198 double& theX, double& theY)
202 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
203 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
204 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
209 void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
210 const std::string& theAttribute)
212 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
213 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
214 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
215 aPoint->setValue(theX, theY);
218 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
219 Handle(V3d_View) theView,
220 const std::string& theAttribute)
223 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
224 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
225 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
226 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
227 aPoint->setValue(aX, anY);