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 <Events_Loop.h>
14 #include <Model_Events.h>
16 #include <GeomDataAPI_Point2D.h>
18 #include <ModuleBase_OperationDescription.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <ModelAPI_AttributeRefList.h>
25 #include <SketchPlugin_Constraint.h>
27 #include <Geom_Line.hxx>
30 #include <XGUI_ViewerPrs.h>
32 #include <SketchPlugin_Line.h>
34 #include <V3d_View.hxx>
35 #include <TopoDS_Vertex.hxx>
37 #include <BRep_Tool.hxx>
43 #include <QMouseEvent>
47 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
49 boost::shared_ptr<ModelAPI_Feature> theFeature)
50 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
51 myPointSelectionMode(SM_FirstPoint)
55 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
59 bool PartSet_OperationSketchLine::isGranted(ModuleBase_IOperation* theOperation) const
61 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
64 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
66 std::list<int> aModes;
67 if (theFeature != feature())
68 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
72 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
73 const std::list<XGUI_ViewerPrs>& /*thePresentations*/)
75 if (!theFeature || theFeature->getKind() != "SketchLine")
77 // use the last point of the previous feature as the first of the new one
78 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
79 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
82 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::sketch() const
87 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
88 const std::list<XGUI_ViewerPrs>& theSelected)
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);
148 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
150 myPointSelectionMode = SM_SecondPoint;
153 case SM_SecondPoint: {
154 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
155 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
157 myPointSelectionMode = 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);
175 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
180 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
181 setLinePoint(aPoint, theView, LINE_ATTR_END);
182 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
188 emit featureConstructed(feature(), FM_Deactivation);
189 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
196 void PartSet_OperationSketchLine::keyReleased(const int theKey)
199 case Qt::Key_Return: {
200 if (myPointSelectionMode == SM_DonePoint)
203 emit featureConstructed(feature(), FM_Deactivation);
207 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
211 PartSet_OperationSketchBase::keyReleased(theKey);
216 void PartSet_OperationSketchLine::startOperation()
218 PartSet_OperationSketchBase::startOperation();
219 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
220 emit multiSelectionEnabled(false);
223 void PartSet_OperationSketchLine::abortOperation()
225 emit featureConstructed(feature(), FM_Hide);
226 PartSet_OperationSketchBase::abortOperation();
229 void PartSet_OperationSketchLine::stopOperation()
231 PartSet_OperationSketchBase::stopOperation();
232 emit multiSelectionEnabled(true);
235 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature(const bool theFlushMessage)
237 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature(false);
239 boost::shared_ptr<SketchPlugin_Feature> aFeature =
240 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
242 aFeature->addSub(aNewFeature);
245 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
246 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
248 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
249 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
250 (aData->attribute(LINE_ATTR_START));
251 createConstraint(myInitPoint, aPoint);
254 emit featureConstructed(aNewFeature, FM_Activation);
256 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_CREATED));
260 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
261 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
263 boost::shared_ptr<ModelAPI_Document> aDoc = document();
264 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
267 boost::shared_ptr<SketchPlugin_Feature> aSketch =
268 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
269 aSketch->addSub(aFeature);
272 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
274 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
275 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
276 aRef1->setAttr(thePoint1);
278 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
279 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
280 aRef2->setAttr(thePoint2);
282 if (aFeature) // TODO: generate an error if feature was not created
286 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
288 std::string aPointArg;
289 switch (myPointSelectionMode)
292 aPointArg = LINE_ATTR_START;
295 aPointArg = LINE_ATTR_END;
299 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
300 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
301 (aData->attribute(aPointArg));
302 aData = sketch()->data();
303 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
304 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
306 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
307 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
308 aLast = aFeatures.end();
309 for (; anIt != aLast; anIt++) {
310 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
311 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
313 createConstraint(aFPoint, aPoint);
317 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
318 const std::string& theAttribute,
319 double& theX, double& theY)
321 if (!theFeature || theFeature->getKind() != "SketchLine")
323 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
324 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
325 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
330 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
331 boost::shared_ptr<ModelAPI_Feature> theFeature,
332 double theX, double theY)
334 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
335 if (!theFeature || theFeature->getKind() != "SketchLine")
337 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
339 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
340 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
341 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
344 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
345 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
351 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
352 double theX, double theY,
353 const std::string& theAttribute)
357 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
358 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
359 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
360 aPoint->setValue(theX, theY);
363 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
364 Handle(V3d_View) theView,
365 const std::string& theAttribute)
368 PartSet_Tools::ConvertTo2D(thePoint, sketch(), theView, aX, anY);
369 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
370 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
371 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
372 aPoint->setValue(aX, anY);