1 // File: PartSet_OperationSketchLine.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationSketchLine.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_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
25 boost::shared_ptr<ModelAPI_Feature> theFeature)
26 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
27 myPointSelectionMode(SM_FirstPoint)
31 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
35 bool PartSet_OperationSketchLine::isGranted() const
40 int PartSet_OperationSketchLine::getSelectionMode(boost::shared_ptr<ModelAPI_Feature> theFeature) const
43 if (theFeature != feature())
44 aMode = TopAbs_VERTEX;
48 void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint)
50 switch (myPointSelectionMode)
53 setLinePoint(thePoint, LINE_ATTR_START);
54 myPointSelectionMode = SM_SecondPoint;
57 case SM_SecondPoint: {
58 setLinePoint(thePoint, LINE_ATTR_END);
59 myPointSelectionMode = SM_None;
71 void PartSet_OperationSketchLine::mouseMoved(const gp_Pnt& thePoint)
73 switch (myPointSelectionMode)
76 setLinePoint(thePoint, LINE_ATTR_END);
79 boost::shared_ptr<ModelAPI_Feature> aPrevFeature = feature();
80 // stop the last operation
82 document()->finishOperation();
83 //emit changeSelectionMode(aPrevFeature, TopAbs_VERTEX);
84 // start a new operation
85 document()->startOperation();
87 // use the last point of the previous feature as the first of the new one
88 setLinePoint(aPrevFeature, LINE_ATTR_END, LINE_ATTR_START);
89 myPointSelectionMode = SM_SecondPoint;
91 emit featureConstructed(aPrevFeature);
99 void PartSet_OperationSketchLine::startOperation()
101 PartSet_OperationSketchBase::startOperation();
104 boost::shared_ptr<SketchPlugin_Feature> aFeature =
105 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
107 aFeature->addSub(feature());
109 myPointSelectionMode = SM_FirstPoint;
112 void PartSet_OperationSketchLine::stopOperation()
114 PartSet_OperationSketchBase::stopOperation();
115 myPointSelectionMode = SM_None;
118 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
119 const std::string& theAttribute)
121 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
122 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
123 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
127 convertTo2D(thePoint, aX, anY);
128 aPoint->setValue(aX, anY);
131 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
132 const std::string& theSourceAttribute,
133 const std::string& theAttribute)
135 boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
136 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
137 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theSourceAttribute));
138 double aX = aPoint->x();
139 double anY = aPoint->y();
141 aData = feature()->data();
142 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
143 aPoint->setValue(aX, anY);
146 void PartSet_OperationSketchLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
151 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
152 boost::shared_ptr<ModelAPI_Data> aData = mySketch->data();
154 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
155 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
157 boost::shared_ptr<GeomDataAPI_Dir> aX =
158 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
159 boost::shared_ptr<GeomDataAPI_Dir> anY =
160 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
162 gp_Pnt aVec(thePoint.X() - anOrigin->x(), thePoint.Y() - anOrigin->y(), thePoint.Z() - anOrigin->z());
163 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
164 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();