]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_IntersectionPoint.cpp
Salome HOME
#1340 Filter for edges outside sketch plane
[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   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
26
27   SketchPlugin_Point::initDerivedClassAttributes();
28 }
29
30 void SketchPlugin_IntersectionPoint::execute()
31 {
32   SketchPlugin_Sketch* aSketch = sketch();
33   if (aSketch) {
34     computePoint();
35     SketchPlugin_Point::execute();
36
37     // set this feature as external
38     data()->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
39   }
40 }
41
42 void SketchPlugin_IntersectionPoint::move(double theDeltaX, double theDeltaY)
43 {
44 }
45
46 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
47 {
48   if (theID == EXTERNAL_LINE_ID()) {
49     // compute intersection between line and sketch plane
50     computePoint();
51   }
52 }
53
54 void SketchPlugin_IntersectionPoint::computePoint()
55 {
56   AttributeSelectionPtr aLineAttr =
57       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_LINE_ID()));
58
59   std::shared_ptr<GeomAPI_Edge> anEdge;
60   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
61     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
62   } else if(aLineAttr->context() && aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
63     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
64   }
65   if(!anEdge.get())
66     return;
67
68   std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
69   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
70
71   std::shared_ptr<GeomAPI_Pnt> anIntersection = aSketchPlane->intersect(aLine);
72   if (!anIntersection)
73     return;
74
75   std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr = 
76       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
77   aCoordAttr->setValue(sketch()->to2D(anIntersection));
78 }