Salome HOME
Auxiliary state for Intersection point
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_IntersectionPoint.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_IntersectionPoint.cpp
4 // Created: 07 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_IntersectionPoint.h"
8
9 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12
13 #include <GeomAPI_Edge.h>
14 #include <GeomAPI_Lin.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 SketchPlugin_IntersectionPoint::SketchPlugin_IntersectionPoint()
18     : SketchPlugin_Point()
19 {
20 }
21
22 void SketchPlugin_IntersectionPoint::initDerivedClassAttributes()
23 {
24   data()->addAttribute(EXTERNAL_LINE_ID(), ModelAPI_AttributeSelection::typeId());
25
26   SketchPlugin_Point::initDerivedClassAttributes();
27 }
28
29 void SketchPlugin_IntersectionPoint::execute()
30 {
31   SketchPlugin_Sketch* aSketch = sketch();
32   if (aSketch) {
33     computePoint();
34     SketchPlugin_Point::execute();
35
36     // set this feature as external
37     data()->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
38   }
39 }
40
41 void SketchPlugin_IntersectionPoint::move(double theDeltaX, double theDeltaY)
42 {
43 }
44
45 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
46 {
47   if (theID == EXTERNAL_LINE_ID()) {
48     // compute intersection between line and sketch plane
49     computePoint();
50   }
51 }
52
53 void SketchPlugin_IntersectionPoint::computePoint()
54 {
55   AttributeSelectionPtr aLineAttr =
56       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_LINE_ID()));
57
58   std::shared_ptr<GeomAPI_Edge> anEdge;
59   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
60     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
61   } else if(aLineAttr->context() && aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
62     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
63   }
64   if(!anEdge.get())
65     return;
66
67   std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
68   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
69
70   std::shared_ptr<GeomAPI_Pnt> anIntersection = aSketchPlane->intersect(aLine);
71   if (!anIntersection)
72     return;
73
74   std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr = 
75       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
76   aCoordAttr->setValue(sketch()->to2D(anIntersection));
77 }