Salome HOME
Renamed SHAPER_CONFIG_FILE to PLUGINS_CONFIG_FILE
[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
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Lin.h>
13 #include <GeomDataAPI_Point2D.h>
14
15 SketchPlugin_IntersectionPoint::SketchPlugin_IntersectionPoint()
16     : SketchPlugin_Point()
17 {
18 }
19
20 void SketchPlugin_IntersectionPoint::initDerivedClassAttributes()
21 {
22   data()->addAttribute(EXTERNAL_LINE_ID(), ModelAPI_AttributeSelection::typeId());
23
24   SketchPlugin_Point::initDerivedClassAttributes();
25 }
26
27 void SketchPlugin_IntersectionPoint::execute()
28 {
29   SketchPlugin_Sketch* aSketch = sketch();
30   if (aSketch) {
31     computePoint();
32     SketchPlugin_Point::execute();
33
34     // set this feature as external
35     data()->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
36   }
37 }
38
39 void SketchPlugin_IntersectionPoint::move(double theDeltaX, double theDeltaY)
40 {
41 }
42
43 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
44 {
45   if (theID == EXTERNAL_LINE_ID()) {
46     // compute intersection between line and sketch plane
47     computePoint();
48   }
49 }
50
51 void SketchPlugin_IntersectionPoint::computePoint()
52 {
53   AttributeSelectionPtr aLineAttr =
54       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_LINE_ID()));
55
56   std::shared_ptr<GeomAPI_Edge> anEdge;
57   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
58     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
59   } else if(aLineAttr->context() && aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
60     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
61   }
62   if(!anEdge.get())
63     return;
64
65   std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
66   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
67
68   std::shared_ptr<GeomAPI_Pnt> anIntersection = aSketchPlane->intersect(aLine);
69   if (!anIntersection)
70     return;
71
72   std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr = 
73       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
74   aCoordAttr->setValue(sketch()->to2D(anIntersection));
75 }