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 bool isFoundPoint = false;
83 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
84 if (theSelected.empty()) {
85 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
89 XGUI_ViewerPrs aPrs = theSelected.front();
90 const TopoDS_Shape& aShape = aPrs.shape();
91 if (!aShape.IsNull()) // the point is selected
93 if (aShape.ShapeType() == TopAbs_VERTEX) {
94 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
95 if (!aVertex.IsNull()) {
96 aPoint = BRep_Tool::Pnt(aVertex);
97 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
100 setConstraints(aX, anY);
103 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
105 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
107 double X0, X1, X2, X3;
108 double Y0, Y1, Y2, Y3;
109 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
110 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
111 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
113 switch (myPointSelectionMode) {
115 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
117 case SM_SecondPoint: {
118 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
119 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
133 switch (myPointSelectionMode)
135 case SM_FirstPoint: {
136 setLinePoint(aX, anY, LINE_ATTR_START);
137 myPointSelectionMode = SM_SecondPoint;
140 case SM_SecondPoint: {
141 setLinePoint(aX, anY, LINE_ATTR_END);
143 emit featureConstructed(feature(), FM_Deactivation);
144 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
152 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView,
153 const std::list<XGUI_ViewerPrs>& /*theSelected*/)
155 switch (myPointSelectionMode)
159 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
160 setLinePoint(aPoint, theView, LINE_ATTR_END);
168 void PartSet_OperationSketchLine::keyReleased(const int theKey)
171 case Qt::Key_Escape: {
175 case Qt::Key_Return: {
177 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
185 void PartSet_OperationSketchLine::startOperation()
187 PartSet_OperationSketchBase::startOperation();
188 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
189 emit multiSelectionEnabled(false);
192 void PartSet_OperationSketchLine::abortOperation()
194 emit featureConstructed(feature(), FM_Abort);
195 PartSet_OperationSketchBase::abortOperation();
198 void PartSet_OperationSketchLine::stopOperation()
200 PartSet_OperationSketchBase::stopOperation();
201 emit multiSelectionEnabled(true);
204 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
206 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
208 boost::shared_ptr<SketchPlugin_Feature> aFeature =
209 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
211 aFeature->addSub(aNewFeature);
214 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
215 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
216 (aData->attribute(LINE_ATTR_START));
217 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
219 createConstraint(myInitPoint, aPoint);
222 emit featureConstructed(aNewFeature, FM_Activation);
226 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
227 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
229 boost::shared_ptr<ModelAPI_Document> aDoc = document();
230 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
232 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
234 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
235 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
236 aRef1->setAttr(thePoint1);
238 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
239 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
240 aRef2->setAttr(thePoint2);
242 if (aFeature) // TODO: generate an error if feature was not created
246 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
248 std::string aPointArg;
249 switch (myPointSelectionMode)
252 aPointArg = LINE_ATTR_START;
255 aPointArg = LINE_ATTR_END;
259 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
260 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
261 (aData->attribute(aPointArg));
262 aData = mySketch->data();
263 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
264 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
266 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
267 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
268 aLast = aFeatures.end();
269 for (; anIt != aLast; anIt++) {
270 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
271 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
273 createConstraint(aFPoint, aPoint);
277 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
278 const std::string& theAttribute,
279 double& theX, double& theY)
283 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
284 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
285 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
290 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
291 boost::shared_ptr<ModelAPI_Feature> theFeature,
292 double theX, double theY)
294 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
297 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
299 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
300 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
301 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
304 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
305 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
311 void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
312 const std::string& theAttribute)
314 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
315 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
316 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
317 aPoint->setValue(theX, theY);
320 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
321 Handle(V3d_View) theView,
322 const std::string& theAttribute)
325 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
326 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
327 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
328 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
329 aPoint->setValue(aX, anY);