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);
149 myPointSelectionMode = SM_SecondPoint;
152 case SM_SecondPoint: {
153 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
156 myPointSelectionMode = SM_DonePoint;
164 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
166 switch (myPointSelectionMode)
168 case SM_FirstPoint: {
170 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
171 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
172 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
173 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
179 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
180 setLinePoint(aPoint, theView, LINE_ATTR_END);
187 emit featureConstructed(feature(), FM_Deactivation);
188 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
195 void PartSet_OperationSketchLine::keyReleased(const int theKey)
198 case Qt::Key_Return: {
199 if (myPointSelectionMode == SM_DonePoint)
202 emit featureConstructed(feature(), FM_Deactivation);
206 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
209 case Qt::Key_Escape: {
210 if (myPointSelectionMode == SM_DonePoint)
213 emit featureConstructed(feature(), FM_Deactivation);
223 void PartSet_OperationSketchLine::startOperation()
225 PartSet_OperationSketchBase::startOperation();
226 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
227 emit multiSelectionEnabled(false);
230 void PartSet_OperationSketchLine::abortOperation()
232 emit featureConstructed(feature(), FM_Hide);
233 PartSet_OperationSketchBase::abortOperation();
236 void PartSet_OperationSketchLine::stopOperation()
238 PartSet_OperationSketchBase::stopOperation();
239 emit multiSelectionEnabled(true);
242 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature(const bool theFlushMessage)
244 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature(false);
246 boost::shared_ptr<SketchPlugin_Feature> aFeature =
247 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
249 aFeature->addSub(aNewFeature);
252 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
253 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
255 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
256 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
257 (aData->attribute(LINE_ATTR_START));
258 createConstraint(myInitPoint, aPoint);
261 emit featureConstructed(aNewFeature, FM_Activation);
267 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
268 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
270 boost::shared_ptr<ModelAPI_Document> aDoc = document();
271 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
274 boost::shared_ptr<SketchPlugin_Feature> aSketch =
275 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
276 aSketch->addSub(aFeature);
279 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
281 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
282 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
283 aRef1->setAttr(thePoint1);
285 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
286 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
287 aRef2->setAttr(thePoint2);
289 if (aFeature) // TODO: generate an error if feature was not created
293 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
295 std::string aPointArg;
296 switch (myPointSelectionMode)
299 aPointArg = LINE_ATTR_START;
302 aPointArg = LINE_ATTR_END;
306 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
307 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
308 (aData->attribute(aPointArg));
309 aData = sketch()->data();
310 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
311 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
313 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
314 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
315 aLast = aFeatures.end();
316 for (; anIt != aLast; anIt++) {
317 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
318 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
320 createConstraint(aFPoint, aPoint);
324 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
325 const std::string& theAttribute,
326 double& theX, double& theY)
328 if (!theFeature || theFeature->getKind() != "SketchLine")
330 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
331 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
332 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
337 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
338 boost::shared_ptr<ModelAPI_Feature> theFeature,
339 double theX, double theY)
341 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
342 if (!theFeature || theFeature->getKind() != "SketchLine")
344 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
346 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
347 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
348 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
351 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
352 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
358 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
359 double theX, double theY,
360 const std::string& theAttribute)
364 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
365 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
366 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
367 aPoint->setValue(theX, theY);
370 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
371 Handle(V3d_View) theView,
372 const std::string& theAttribute)
375 PartSet_Tools::ConvertTo2D(thePoint, sketch(), theView, aX, anY);
376 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
377 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
378 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
379 aPoint->setValue(aX, anY);