]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_IntersectionPoint.cpp
Salome HOME
ac2220fa812881a3b3eb560659ca54e16932e567
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_IntersectionPoint.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "SketchPlugin_IntersectionPoint.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_Session.h>
24 #include <ModelAPI_Validator.h>
25
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomDataAPI_Point2D.h>
29
30 SketchPlugin_IntersectionPoint::SketchPlugin_IntersectionPoint()
31     : SketchPlugin_Point()
32 {
33 }
34
35 void SketchPlugin_IntersectionPoint::initDerivedClassAttributes()
36 {
37   data()->addAttribute(EXTERNAL_LINE_ID(), ModelAPI_AttributeSelection::typeId());
38
39   SketchPlugin_Point::initDerivedClassAttributes();
40 }
41
42 void SketchPlugin_IntersectionPoint::execute()
43 {
44   SketchPlugin_Sketch* aSketch = sketch();
45   if (aSketch) {
46     computePoint();
47     SketchPlugin_Point::execute();
48
49     // set this feature as external
50     data()->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
51   }
52 }
53
54 void SketchPlugin_IntersectionPoint::move(double theDeltaX, double theDeltaY)
55 {
56 }
57
58 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
59 {
60   if (theID == EXTERNAL_LINE_ID()) {
61     // compute intersection between line and sketch plane
62     computePoint();
63   }
64 }
65
66 void SketchPlugin_IntersectionPoint::computePoint()
67 {
68   AttributeSelectionPtr aLineAttr =
69       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_LINE_ID()));
70
71   std::shared_ptr<GeomAPI_Edge> anEdge;
72   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
73     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
74   } else if(aLineAttr->context() && aLineAttr->context()->shape() &&
75             aLineAttr->context()->shape()->isEdge()) {
76     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
77   }
78   if(!anEdge.get())
79     return;
80
81   std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
82   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
83
84   std::shared_ptr<GeomAPI_Pnt> anIntersection = aSketchPlane->intersect(aLine);
85   if (!anIntersection)
86     return;
87
88   std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr =
89       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
90   aCoordAttr->setValue(sketch()->to2D(anIntersection));
91 }