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)
59 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
60 switch (myPointSelectionMode)
63 setLinePoint(aPoint, theView, LINE_ATTR_START);
64 myPointSelectionMode = SM_SecondPoint;
67 case SM_SecondPoint: {
68 setLinePoint(aPoint, theView, LINE_ATTR_END);
70 emit featureConstructed(feature(), FM_Deactivation);
71 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
79 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
81 switch (myPointSelectionMode)
85 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
86 setLinePoint(aPoint, theView, LINE_ATTR_END);
94 void PartSet_OperationSketchLine::keyReleased(const int theKey)
97 case Qt::Key_Escape: {
101 case Qt::Key_Return: {
103 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
111 void PartSet_OperationSketchLine::startOperation()
113 PartSet_OperationSketchBase::startOperation();
114 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
117 void PartSet_OperationSketchLine::abortOperation()
119 emit featureConstructed(feature(), FM_Abort);
120 PartSet_OperationSketchBase::abortOperation();
123 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
125 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
127 boost::shared_ptr<SketchPlugin_Feature> aFeature =
128 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
130 aFeature->addSub(aNewFeature);
133 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
134 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
135 (aData->attribute(LINE_ATTR_START));
136 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
139 emit featureConstructed(aNewFeature, FM_Activation);
143 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
144 Handle(V3d_View) theView,
145 const std::string& theAttribute)
147 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
148 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
149 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
152 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
153 aPoint->setValue(aX, anY);