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>
13 #include <ModelAPI_AttributeRefAttr.h>
15 #include <SketchPlugin_Constraint.h>
17 #include <Geom_Line.hxx>
20 #include <XGUI_ViewerPrs.h>
22 #include <SketchPlugin_Line.h>
24 #include <V3d_View.hxx>
25 #include <TopoDS_Vertex.hxx>
27 #include <BRep_Tool.hxx>
33 #include <QMouseEvent>
37 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
39 boost::shared_ptr<ModelAPI_Feature> theFeature)
40 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
41 myPointSelectionMode(SM_FirstPoint)
45 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
49 bool PartSet_OperationSketchLine::isGranted() const
54 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
56 return std::list<int>();
59 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
63 // use the last point of the previous feature as the first of the new one
64 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
65 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
68 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
69 const std::list<XGUI_ViewerPrs>& theSelected)
73 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
74 if (theSelected.empty()) {
75 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
78 XGUI_ViewerPrs aPrs = theSelected.front();
79 const TopoDS_Shape& aShape = aPrs.shape();
80 if (!aShape.IsNull()) // the point is selected
82 if (aShape.ShapeType() == TopAbs_VERTEX) {
83 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
84 if (!aVertex.IsNull()) {
85 aPoint = BRep_Tool::Pnt(aVertex);
86 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
89 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
91 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
93 double X0, X1, X2, X3;
94 double Y0, Y1, Y2, Y3;
95 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
96 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
97 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
99 switch (myPointSelectionMode) {
101 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
103 case SM_SecondPoint: {
104 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
105 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
115 switch (myPointSelectionMode)
117 case SM_FirstPoint: {
118 setLinePoint(aX, anY, LINE_ATTR_START);
119 myPointSelectionMode = SM_SecondPoint;
122 case SM_SecondPoint: {
123 setLinePoint(aX, anY, LINE_ATTR_END);
125 emit featureConstructed(feature(), FM_Deactivation);
126 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
134 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
136 switch (myPointSelectionMode)
140 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
141 setLinePoint(aPoint, theView, LINE_ATTR_END);
149 void PartSet_OperationSketchLine::keyReleased(const int theKey)
152 case Qt::Key_Escape: {
156 case Qt::Key_Return: {
158 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
166 void PartSet_OperationSketchLine::startOperation()
168 PartSet_OperationSketchBase::startOperation();
169 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
170 emit multiSelectionEnabled(false);
173 void PartSet_OperationSketchLine::abortOperation()
175 emit featureConstructed(feature(), FM_Abort);
176 PartSet_OperationSketchBase::abortOperation();
179 void PartSet_OperationSketchLine::stopOperation()
181 PartSet_OperationSketchBase::stopOperation();
182 emit multiSelectionEnabled(true);
185 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
187 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
189 boost::shared_ptr<SketchPlugin_Feature> aFeature =
190 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
192 aFeature->addSub(aNewFeature);
195 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
196 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
197 (aData->attribute(LINE_ATTR_START));
198 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
200 //createConstraint(myInitPoint, aPoint);
203 emit featureConstructed(aNewFeature, FM_Activation);
207 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
208 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
210 boost::shared_ptr<ModelAPI_Document> aDoc = document();
211 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
213 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
215 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
216 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
217 aRef1->setAttr(thePoint1);
219 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
220 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
221 aRef2->setAttr(thePoint2);
223 if (aFeature) // TODO: generate an error if feature was not created
227 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
228 const std::string& theAttribute,
229 double& theX, double& theY)
233 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
234 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
235 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
240 void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
241 const std::string& theAttribute)
243 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
244 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
245 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
246 aPoint->setValue(theX, theY);
249 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
250 Handle(V3d_View) theView,
251 const std::string& theAttribute)
254 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
255 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
256 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
257 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
258 aPoint->setValue(aX, anY);