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 <SketchPlugin_Line.h>
16 #include <V3d_View.hxx>
22 #include <QMouseEvent>
26 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
28 boost::shared_ptr<ModelAPI_Feature> theFeature)
29 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
30 myPointSelectionMode(SM_FirstPoint)
34 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
38 bool PartSet_OperationSketchLine::isGranted() const
43 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
45 return std::list<int>();
48 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
52 // use the last point of the previous feature as the first of the new one
53 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
54 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
57 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
58 const std::list<XGUI_ViewerPrs>& theSelected)
60 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
61 switch (myPointSelectionMode)
64 setLinePoint(aPoint, theView, LINE_ATTR_START);
65 myPointSelectionMode = SM_SecondPoint;
68 case SM_SecondPoint: {
69 setLinePoint(aPoint, theView, LINE_ATTR_END);
71 emit featureConstructed(feature(), FM_Deactivation);
72 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
80 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
82 switch (myPointSelectionMode)
86 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
87 setLinePoint(aPoint, theView, LINE_ATTR_END);
95 void PartSet_OperationSketchLine::keyReleased(const int theKey)
98 case Qt::Key_Escape: {
102 case Qt::Key_Return: {
104 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
112 void PartSet_OperationSketchLine::startOperation()
114 PartSet_OperationSketchBase::startOperation();
115 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
118 void PartSet_OperationSketchLine::abortOperation()
120 emit featureConstructed(feature(), FM_Abort);
121 PartSet_OperationSketchBase::abortOperation();
124 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
126 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
128 boost::shared_ptr<SketchPlugin_Feature> aFeature =
129 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
131 aFeature->addSub(aNewFeature);
134 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
135 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
136 (aData->attribute(LINE_ATTR_START));
137 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
140 emit featureConstructed(aNewFeature, FM_Activation);
144 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
145 Handle(V3d_View) theView,
146 const std::string& theAttribute)
148 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
149 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
150 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
153 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
154 aPoint->setValue(aX, anY);