1 // File: SketchPlugin_Line.cxx
2 // Created: 27 Mar 2014
3 // Author: Mikhail PONIKAROV
5 #include "SketchPlugin_Line.h"
6 #include "SketchPlugin_Sketch.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_ResultConstruction.h>
9 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_Session.h>
13 #include <GeomAPI_Pnt.h>
14 #include <GeomAPI_Lin2d.h>
15 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAlgoAPI_EdgeBuilder.h>
18 #include <GeomDataAPI_Point2D.h>
22 SketchPlugin_Line::SketchPlugin_Line()
23 : SketchPlugin_Feature()
27 void SketchPlugin_Line::initAttributes()
29 data()->addAttribute(SketchPlugin_Line::START_ID(), GeomDataAPI_Point2D::type());
30 data()->addAttribute(SketchPlugin_Line::END_ID(), GeomDataAPI_Point2D::type());
31 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
32 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
35 void SketchPlugin_Line::execute()
37 SketchPlugin_Sketch* aSketch = sketch();
39 // compute a start point in 3D view
40 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
41 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Line::START_ID()));
42 // compute an end point in 3D view
43 boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
44 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Line::END_ID()));
45 if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
46 boost::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
47 boost::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
49 boost::shared_ptr<GeomAPI_Shape> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
51 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(
53 aConstr->setShape(anEdge);
54 aConstr->setIsInHistory(false);
60 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
62 boost::shared_ptr<ModelAPI_Data> aData = data();
63 if (!aData->isValid())
66 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
67 aData->attribute(SketchPlugin_Line::START_ID()));
68 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
70 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
71 aData->attribute(SketchPlugin_Line::END_ID()));
72 aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
75 double SketchPlugin_Line::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
79 boost::shared_ptr<ModelAPI_Data> aData = data();
80 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
81 aData->attribute(SketchPlugin_Line::START_ID()));
82 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
83 aData->attribute(SketchPlugin_Line::END_ID()));
85 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
87 if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
88 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
89 aDelta = aResult->distance(thePoint);
91 aDelta = aLin2d.distance(thePoint);
97 bool SketchPlugin_Line::isFixed() {
98 return data()->selection(EXTERNAL_ID())->context();