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>
29 #include <SketchPlugin_Line.h>
31 #include <V3d_View.hxx>
32 #include <TopoDS_Vertex.hxx>
34 #include <BRep_Tool.hxx>
40 #include <QMouseEvent>
44 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
46 boost::shared_ptr<ModelAPI_Feature> theFeature)
47 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
48 myPointSelectionMode(SM_FirstPoint)
52 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
56 bool PartSet_OperationSketchLine::isGranted(ModuleBase_IOperation* theOperation) const
58 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
61 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
63 std::list<int> aModes;
64 if (theFeature != feature())
65 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
69 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
70 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
71 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
73 if (!theFeature || theFeature->getKind() != "SketchLine")
75 // use the last point of the previous feature as the first of the new one
76 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
77 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
80 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::sketch() const
85 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
86 const std::list<XGUI_ViewerPrs>& theSelected,
87 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
91 bool isFoundPoint = false;
92 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
93 if (theSelected.empty()) {
94 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
98 XGUI_ViewerPrs aPrs = theSelected.front();
99 const TopoDS_Shape& aShape = aPrs.shape();
100 if (!aShape.IsNull()) // the point is selected
102 if (aShape.ShapeType() == TopAbs_VERTEX) {
103 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
104 if (!aVertex.IsNull()) {
105 aPoint = BRep_Tool::Pnt(aVertex);
106 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
109 setConstraints(aX, anY);
112 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
114 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
116 double X0, X1, X2, X3;
117 double Y0, Y1, Y2, Y3;
118 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
119 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
120 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
122 switch (myPointSelectionMode) {
124 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
126 case SM_SecondPoint: {
127 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
128 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
142 switch (myPointSelectionMode)
144 case SM_FirstPoint: {
145 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
146 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
147 myPointSelectionMode = SM_SecondPoint;
150 case SM_SecondPoint: {
151 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
152 myPointSelectionMode = SM_DonePoint;
160 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
162 switch (myPointSelectionMode)
164 case SM_FirstPoint: {
166 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
167 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
168 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
169 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
174 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
175 setLinePoint(aPoint, theView, LINE_ATTR_END);
181 emit featureConstructed(feature(), FM_Deactivation);
182 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
189 void PartSet_OperationSketchLine::keyReleased(const int theKey)
192 case Qt::Key_Return: {
193 if (myPointSelectionMode == SM_DonePoint)
196 emit featureConstructed(feature(), FM_Deactivation);
200 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
203 case Qt::Key_Escape: {
204 if (myPointSelectionMode == SM_DonePoint)
207 emit featureConstructed(feature(), FM_Deactivation);
217 void PartSet_OperationSketchLine::startOperation()
219 PartSet_OperationSketchBase::startOperation();
220 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
221 emit multiSelectionEnabled(false);
224 void PartSet_OperationSketchLine::abortOperation()
226 emit featureConstructed(feature(), FM_Hide);
227 PartSet_OperationSketchBase::abortOperation();
230 void PartSet_OperationSketchLine::stopOperation()
232 PartSet_OperationSketchBase::stopOperation();
233 emit multiSelectionEnabled(true);
236 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
238 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
240 boost::shared_ptr<SketchPlugin_Feature> aFeature =
241 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
243 aFeature->addSub(aNewFeature);
246 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
247 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
249 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
250 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
251 (aData->attribute(LINE_ATTR_START));
252 createConstraint(myInitPoint, aPoint);
255 emit featureConstructed(aNewFeature, FM_Activation);
259 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
260 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
262 boost::shared_ptr<ModelAPI_Document> aDoc = document();
263 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
266 boost::shared_ptr<SketchPlugin_Feature> aSketch =
267 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
268 aSketch->addSub(aFeature);
271 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
273 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
274 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
275 aRef1->setAttr(thePoint1);
277 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
278 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
279 aRef2->setAttr(thePoint2);
281 if (aFeature) // TODO: generate an error if feature was not created
285 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
287 std::string aPointArg;
288 switch (myPointSelectionMode)
291 aPointArg = LINE_ATTR_START;
294 aPointArg = LINE_ATTR_END;
298 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
299 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
300 (aData->attribute(aPointArg));
301 aData = sketch()->data();
302 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
303 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
305 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
306 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
307 aLast = aFeatures.end();
308 for (; anIt != aLast; anIt++) {
309 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
310 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
312 createConstraint(aFPoint, aPoint);
316 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
317 const std::string& theAttribute,
318 double& theX, double& theY)
320 if (!theFeature || theFeature->getKind() != "SketchLine")
322 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
323 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
324 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
329 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
330 boost::shared_ptr<ModelAPI_Feature> theFeature,
331 double theX, double theY)
333 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
334 if (!theFeature || theFeature->getKind() != "SketchLine")
336 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
338 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
339 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
340 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
343 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
344 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
350 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
351 double theX, double theY,
352 const std::string& theAttribute)
356 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
357 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
358 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
359 aPoint->setValue(theX, theY);
362 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
363 Handle(V3d_View) theView,
364 const std::string& theAttribute)
367 PartSet_Tools::ConvertTo2D(thePoint, sketch(), theView, aX, anY);
368 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
369 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
370 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
371 aPoint->setValue(aX, anY);