1 // File: PartSet_OperationEditLine.h
2 // Created: 05 May 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationEditLine.h>
7 #include <SketchPlugin_Feature.h>
8 #include <GeomDataAPI_Point2D.h>
9 #include <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Document.h>
14 #include <SketchPlugin_Sketch.h>
15 #include <SketchPlugin_Line.h>
23 PartSet_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
25 boost::shared_ptr<ModelAPI_Feature> theFeature)
26 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
30 PartSet_OperationEditLine::~PartSet_OperationEditLine()
34 bool PartSet_OperationEditLine::isGranted() const
39 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
41 std::list<int> aModes;
42 //if (theFeature != feature())
43 // aModes.push_back(TopAbs_VERTEX);
47 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
49 setFeature(theFeature);
52 void PartSet_OperationEditLine::mouseReleased(const gp_Pnt& thePoint)
54 /*switch (myPointSelectionMode)
57 setLinePoint(thePoint, LINE_ATTR_START);
58 myPointSelectionMode = SM_SecondPoint;
61 case SM_SecondPoint: {
62 setLinePoint(thePoint, LINE_ATTR_END);
63 myPointSelectionMode = SM_None;
76 void PartSet_OperationEditLine::mouseMoved(const gp_Pnt& thePoint)
78 /* switch (myPointSelectionMode)
81 setLinePoint(thePoint, LINE_ATTR_END);
84 boost::shared_ptr<ModelAPI_Feature> aPrevFeature = feature();
85 // stop the last operation
87 document()->finishOperation();
88 //emit changeSelectionMode(aPrevFeature, TopAbs_VERTEX);
89 // start a new operation
90 document()->startOperation();
92 // use the last point of the previous feature as the first of the new one
93 setLinePoint(aPrevFeature, LINE_ATTR_END, LINE_ATTR_START);
94 myPointSelectionMode = SM_SecondPoint;
96 emit featureConstructed(aPrevFeature, FM_Deactivation);
105 void PartSet_OperationEditLine::keyReleased(const int theKey)
108 case Qt::Key_Escape: {
109 if (myPointSelectionMode != SM_None)
110 emit featureConstructed(feature(), FM_Abort);
114 case Qt::Key_Return: {
115 if (myPointSelectionMode != SM_None) {
116 emit featureConstructed(feature(), FM_Abort);
117 myPointSelectionMode = SM_FirstPoint;
118 document()->abortOperation();
121 myPointSelectionMode = SM_FirstPoint;
130 void PartSet_OperationEditLine::setSelected(boost::shared_ptr<ModelAPI_Feature> theFeature,
131 const TopoDS_Shape& theShape)
133 if (theFeature == feature())
139 emit launchOperation("EditLine", theFeature);
142 void PartSet_OperationEditLine::startOperation()
144 //PartSet_OperationSketchBase::startOperation();
145 //myPointSelectionMode = SM_FirstPoint;
148 void PartSet_OperationEditLine::stopOperation()
150 PartSet_OperationSketchBase::stopOperation();
151 //myPointSelectionMode = SM_None;
154 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
156 return boost::shared_ptr<ModelAPI_Feature>();
159 void PartSet_OperationEditLine::setLinePoint(const gp_Pnt& thePoint,
160 const std::string& theAttribute)
162 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
163 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
164 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
168 convertTo2D(thePoint, aX, anY);
169 aPoint->setValue(aX, anY);
172 void PartSet_OperationEditLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
173 const std::string& theSourceAttribute,
174 const std::string& theAttribute)
176 boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
177 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
178 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theSourceAttribute));
179 double aX = aPoint->x();
180 double anY = aPoint->y();
182 aData = feature()->data();
183 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
184 aPoint->setValue(aX, anY);
187 void PartSet_OperationEditLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
192 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
193 boost::shared_ptr<ModelAPI_Data> aData = mySketch->data();
195 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
196 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
198 boost::shared_ptr<GeomDataAPI_Dir> aX =
199 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
200 boost::shared_ptr<GeomDataAPI_Dir> anY =
201 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
203 gp_Pnt aVec(thePoint.X() - anOrigin->x(), thePoint.Y() - anOrigin->y(), thePoint.Z() - anOrigin->z());
204 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
205 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();