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 <SketchPlugin_Sketch.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_AttributeRefAttr.h>
16 #include <ModelAPI_AttributeRefList.h>
18 #include <SketchPlugin_Constraint.h>
20 #include <Geom_Line.hxx>
23 #include <XGUI_ViewerPrs.h>
25 #include <SketchPlugin_Line.h>
27 #include <V3d_View.hxx>
28 #include <TopoDS_Vertex.hxx>
30 #include <BRep_Tool.hxx>
36 #include <QMouseEvent>
40 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
42 boost::shared_ptr<ModelAPI_Feature> theFeature)
43 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
44 myPointSelectionMode(SM_FirstPoint)
48 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
52 bool PartSet_OperationSketchLine::isGranted() const
57 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
59 std::list<int> aModes;
60 if (theFeature != feature())
62 aModes.push_back(TopAbs_VERTEX);
63 aModes.push_back(TopAbs_EDGE);
68 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
72 // use the last point of the previous feature as the first of the new one
73 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
74 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
77 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
78 const std::list<XGUI_ViewerPrs>& theSelected)
82 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
83 if (theSelected.empty()) {
84 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
87 XGUI_ViewerPrs aPrs = theSelected.front();
88 const TopoDS_Shape& aShape = aPrs.shape();
89 if (!aShape.IsNull()) // the point is selected
91 if (aShape.ShapeType() == TopAbs_VERTEX) {
92 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
93 if (!aVertex.IsNull()) {
94 aPoint = BRep_Tool::Pnt(aVertex);
95 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
97 setConstraints(aX, anY);
100 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
102 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
104 double X0, X1, X2, X3;
105 double Y0, Y1, Y2, Y3;
106 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
107 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
108 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
110 switch (myPointSelectionMode) {
112 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
114 case SM_SecondPoint: {
115 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
116 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
126 switch (myPointSelectionMode)
128 case SM_FirstPoint: {
129 setLinePoint(aX, anY, LINE_ATTR_START);
130 myPointSelectionMode = SM_SecondPoint;
133 case SM_SecondPoint: {
134 setLinePoint(aX, anY, LINE_ATTR_END);
136 emit featureConstructed(feature(), FM_Deactivation);
137 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
145 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
147 switch (myPointSelectionMode)
151 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
152 setLinePoint(aPoint, theView, LINE_ATTR_END);
160 void PartSet_OperationSketchLine::keyReleased(const int theKey)
163 case Qt::Key_Escape: {
167 case Qt::Key_Return: {
169 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
177 void PartSet_OperationSketchLine::startOperation()
179 PartSet_OperationSketchBase::startOperation();
180 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
181 emit multiSelectionEnabled(false);
184 void PartSet_OperationSketchLine::abortOperation()
186 emit featureConstructed(feature(), FM_Abort);
187 PartSet_OperationSketchBase::abortOperation();
190 void PartSet_OperationSketchLine::stopOperation()
192 PartSet_OperationSketchBase::stopOperation();
193 emit multiSelectionEnabled(true);
196 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
198 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
200 boost::shared_ptr<SketchPlugin_Feature> aFeature =
201 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
203 aFeature->addSub(aNewFeature);
206 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
207 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
208 (aData->attribute(LINE_ATTR_START));
209 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
211 createConstraint(myInitPoint, aPoint);
214 emit featureConstructed(aNewFeature, FM_Activation);
218 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
219 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
221 boost::shared_ptr<ModelAPI_Document> aDoc = document();
222 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
224 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
226 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
227 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
228 aRef1->setAttr(thePoint1);
230 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
231 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
232 aRef2->setAttr(thePoint2);
234 if (aFeature) // TODO: generate an error if feature was not created
238 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
240 std::string aPointArg;
241 switch (myPointSelectionMode)
244 aPointArg = LINE_ATTR_START;
247 aPointArg = LINE_ATTR_END;
251 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
252 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
253 (aData->attribute(aPointArg));
254 aData = mySketch->data();
255 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
256 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
258 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
259 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
260 aLast = aFeatures.end();
261 for (; anIt != aLast; anIt++) {
262 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
263 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
265 createConstraint(aFPoint, aPoint);
269 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
270 const std::string& theAttribute,
271 double& theX, double& theY)
275 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
276 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
277 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
282 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
283 boost::shared_ptr<ModelAPI_Feature> theFeature,
284 double theX, double theY)
286 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
289 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
291 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
292 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
293 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
296 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
297 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
303 void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
304 const std::string& theAttribute)
306 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
307 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
308 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
309 aPoint->setValue(theX, theY);
312 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
313 Handle(V3d_View) theView,
314 const std::string& theAttribute)
317 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
318 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
319 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
320 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
321 aPoint->setValue(aX, anY);