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())
61 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
65 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
66 const std::list<XGUI_ViewerPrs>& /*thePresentations*/)
68 if (!theFeature || theFeature->getKind() != "SketchLine")
70 // use the last point of the previous feature as the first of the new one
71 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
72 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
75 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
76 const std::list<XGUI_ViewerPrs>& theSelected)
80 bool isFoundPoint = false;
81 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
82 if (theSelected.empty()) {
83 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
87 XGUI_ViewerPrs aPrs = theSelected.front();
88 const TopoDS_Shape& aShape = aPrs.shape();
89 if (!aShape.IsNull()) // the point is selected
91 if (aShape.ShapeType() == TopAbs_VERTEX) {
92 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
93 if (!aVertex.IsNull()) {
94 aPoint = BRep_Tool::Pnt(aVertex);
95 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
98 setConstraints(aX, anY);
101 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
103 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
105 double X0, X1, X2, X3;
106 double Y0, Y1, Y2, Y3;
107 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
108 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
109 PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
111 switch (myPointSelectionMode) {
113 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
115 case SM_SecondPoint: {
116 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
117 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
131 switch (myPointSelectionMode)
133 case SM_FirstPoint: {
134 setLinePoint(aX, anY, LINE_ATTR_START);
135 myPointSelectionMode = SM_SecondPoint;
138 case SM_SecondPoint: {
139 setLinePoint(aX, anY, LINE_ATTR_END);
141 emit featureConstructed(feature(), FM_Deactivation);
142 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
150 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
152 switch (myPointSelectionMode)
156 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
157 setLinePoint(aPoint, theView, LINE_ATTR_END);
165 void PartSet_OperationSketchLine::keyReleased(const int theKey)
168 case Qt::Key_Escape: {
172 case Qt::Key_Return: {
174 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
182 void PartSet_OperationSketchLine::startOperation()
184 PartSet_OperationSketchBase::startOperation();
185 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
186 emit multiSelectionEnabled(false);
189 void PartSet_OperationSketchLine::abortOperation()
191 emit featureConstructed(feature(), FM_Abort);
192 PartSet_OperationSketchBase::abortOperation();
195 void PartSet_OperationSketchLine::stopOperation()
197 PartSet_OperationSketchBase::stopOperation();
198 emit multiSelectionEnabled(true);
201 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
203 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
205 boost::shared_ptr<SketchPlugin_Feature> aFeature =
206 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
208 aFeature->addSub(aNewFeature);
211 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
212 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
213 (aData->attribute(LINE_ATTR_START));
214 aPoint->setValue(myInitPoint->x(), myInitPoint->y());
216 createConstraint(myInitPoint, aPoint);
219 emit featureConstructed(aNewFeature, FM_Activation);
223 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
224 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
226 boost::shared_ptr<ModelAPI_Document> aDoc = document();
227 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
230 boost::shared_ptr<SketchPlugin_Feature> aSketch =
231 boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
232 aSketch->addSub(aFeature);
235 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
237 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
238 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
239 aRef1->setAttr(thePoint1);
241 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
242 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
243 aRef2->setAttr(thePoint2);
245 if (aFeature) // TODO: generate an error if feature was not created
249 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
251 std::string aPointArg;
252 switch (myPointSelectionMode)
255 aPointArg = LINE_ATTR_START;
258 aPointArg = LINE_ATTR_END;
262 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
263 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
264 (aData->attribute(aPointArg));
265 aData = mySketch->data();
266 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
267 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
269 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
270 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
271 aLast = aFeatures.end();
272 for (; anIt != aLast; anIt++) {
273 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
274 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
276 createConstraint(aFPoint, aPoint);
280 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
281 const std::string& theAttribute,
282 double& theX, double& theY)
284 if (!theFeature || theFeature->getKind() != "SketchLine")
286 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
287 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
288 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
293 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
294 boost::shared_ptr<ModelAPI_Feature> theFeature,
295 double theX, double theY)
297 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
298 if (!theFeature || theFeature->getKind() != "SketchLine")
300 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
302 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
303 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
304 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
307 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
308 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
314 void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
315 const std::string& theAttribute)
317 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
318 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
319 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
320 aPoint->setValue(theX, theY);
323 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
324 Handle(V3d_View) theView,
325 const std::string& theAttribute)
328 PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
329 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
330 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
331 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
332 aPoint->setValue(aX, anY);