1 // File: PartSet_OperationSketchLine.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationSketchLine.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
13 #include <GeomDataAPI_Point2D.h>
15 #include <ModuleBase_OperationDescription.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeRefList.h>
22 #include <SketchPlugin_Constraint.h>
24 #include <Geom_Line.hxx>
27 #include <XGUI_ViewerPrs.h>
28 #include <XGUI_Constants.h>
30 #include <SketchPlugin_Line.h>
32 #include <V3d_View.hxx>
33 #include <TopoDS_Vertex.hxx>
35 #include <BRep_Tool.hxx>
41 #include <QMouseEvent>
45 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
47 boost::shared_ptr<ModelAPI_Feature> theFeature)
48 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
49 myPointSelectionMode(SM_FirstPoint)
53 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
57 bool PartSet_OperationSketchLine::isGranted(ModuleBase_IOperation* theOperation) const
59 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
62 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
64 std::list<int> aModes;
65 if (theFeature != feature())
66 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
70 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
71 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
72 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
74 if (!theFeature || theFeature->getKind() != "SketchLine")
76 // use the last point of the previous feature as the first of the new one
77 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
78 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
81 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::sketch() const
86 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
87 const std::list<XGUI_ViewerPrs>& theSelected,
88 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
92 bool isFoundPoint = false;
93 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
94 if (theSelected.empty()) {
95 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
99 XGUI_ViewerPrs aPrs = theSelected.front();
100 const TopoDS_Shape& aShape = aPrs.shape();
101 if (!aShape.IsNull()) // the point is selected
103 if (aShape.ShapeType() == TopAbs_VERTEX) {
104 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
105 if (!aVertex.IsNull()) {
106 aPoint = BRep_Tool::Pnt(aVertex);
107 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
110 setConstraints(aX, anY);
113 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
115 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
117 double X0, X1, X2, X3;
118 double Y0, Y1, Y2, Y3;
119 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
120 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
121 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
123 switch (myPointSelectionMode) {
125 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
127 case SM_SecondPoint: {
128 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
129 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
143 switch (myPointSelectionMode)
145 case SM_FirstPoint: {
146 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
147 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
150 setPointSelectionMode(SM_SecondPoint);
153 case SM_SecondPoint: {
154 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
157 setPointSelectionMode(SM_DonePoint);
165 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
167 switch (myPointSelectionMode)
169 case SM_FirstPoint: {
171 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
172 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
173 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
174 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
176 emit focusActivated(LINE_ATTR_START);
181 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
182 setLinePoint(aPoint, theView, LINE_ATTR_END);
184 emit focusActivated(LINE_ATTR_END);
190 emit featureConstructed(feature(), FM_Deactivation);
191 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
198 void PartSet_OperationSketchLine::keyReleased(std::string theName, QKeyEvent* theEvent)
200 int aKeyType = theEvent->key();
201 if (!theName.empty() && aKeyType == Qt::Key_Return) {
202 if (theName == LINE_ATTR_START) {
203 setPointSelectionMode(SM_SecondPoint, false);
205 else if (theName == LINE_ATTR_END) {
206 setPointSelectionMode(SM_DonePoint, false);
209 keyReleased(theEvent->key());
212 void PartSet_OperationSketchLine::keyReleased(const int theKey)
215 case Qt::Key_Return: {
216 if (myPointSelectionMode == SM_DonePoint)
219 emit featureConstructed(feature(), FM_Deactivation);
220 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
224 //emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
227 case Qt::Key_Escape: {
228 if (myPointSelectionMode == SM_DonePoint)
231 emit featureConstructed(feature(), FM_Deactivation);
241 void PartSet_OperationSketchLine::startOperation()
243 PartSet_OperationSketchBase::startOperation();
244 setPointSelectionMode(!myInitPoint ? SM_FirstPoint : SM_SecondPoint);
246 emit multiSelectionEnabled(false);
249 void PartSet_OperationSketchLine::abortOperation()
251 emit featureConstructed(feature(), FM_Hide);
252 PartSet_OperationSketchBase::abortOperation();
255 void PartSet_OperationSketchLine::stopOperation()
257 PartSet_OperationSketchBase::stopOperation();
258 emit multiSelectionEnabled(true);
261 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature(const bool theFlushMessage)
263 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature(false);
265 boost::shared_ptr<SketchPlugin_Feature> aFeature =
266 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
268 aFeature->addSub(aNewFeature);
271 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
272 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
274 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
275 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
276 (aData->attribute(LINE_ATTR_START));
277 createConstraint(myInitPoint, aPoint);
280 emit featureConstructed(aNewFeature, FM_Activation);
286 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
287 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
289 boost::shared_ptr<ModelAPI_Document> aDoc = document();
290 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
293 boost::shared_ptr<SketchPlugin_Feature> aSketch =
294 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
295 aSketch->addSub(aFeature);
298 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
300 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
301 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
302 aRef1->setAttr(thePoint1);
304 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
305 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
306 aRef2->setAttr(thePoint2);
308 if (aFeature) // TODO: generate an error if feature was not created
312 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
314 std::string aPointArg;
315 switch (myPointSelectionMode)
318 aPointArg = LINE_ATTR_START;
321 aPointArg = LINE_ATTR_END;
325 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
326 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
327 (aData->attribute(aPointArg));
328 aData = sketch()->data();
329 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
330 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
332 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
333 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
334 aLast = aFeatures.end();
335 for (; anIt != aLast; anIt++) {
336 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
337 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
339 createConstraint(aFPoint, aPoint);
343 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
344 const std::string& theAttribute,
345 double& theX, double& theY)
347 if (!theFeature || theFeature->getKind() != "SketchLine")
349 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
350 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
351 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
356 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
357 boost::shared_ptr<ModelAPI_Feature> theFeature,
358 double theX, double theY)
360 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
361 if (!theFeature || theFeature->getKind() != "SketchLine")
363 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
365 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
366 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
367 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
370 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
371 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
377 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
378 double theX, double theY,
379 const std::string& theAttribute)
383 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
384 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
385 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
386 aPoint->setValue(theX, theY);
389 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
390 Handle(V3d_View) theView,
391 const std::string& theAttribute)
394 PartSet_Tools::ConvertTo2D(thePoint, sketch(), theView, aX, anY);
395 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
396 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
397 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
398 aPoint->setValue(aX, anY);
401 void PartSet_OperationSketchLine::setPointSelectionMode(const PointSelectionMode& theMode,
402 const bool isToEmitSignal)
404 myPointSelectionMode = theMode;
405 if (isToEmitSignal) {
409 aName = LINE_ATTR_START;
412 aName = LINE_ATTR_END;
415 aName = XGUI::PROP_PANEL_OK;
420 emit focusActivated(aName);