Salome HOME
Issue #2215: Wrong cut result between a compsolid and a box
[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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_IntersectionPoint.h"
22
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Validator.h>
26
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Lin.h>
29 #include <GeomDataAPI_Point2D.h>
30
31 SketchPlugin_IntersectionPoint::SketchPlugin_IntersectionPoint()
32     : SketchPlugin_Point()
33 {
34 }
35
36 void SketchPlugin_IntersectionPoint::initDerivedClassAttributes()
37 {
38   data()->addAttribute(EXTERNAL_LINE_ID(), ModelAPI_AttributeSelection::typeId());
39
40   SketchPlugin_Point::initDerivedClassAttributes();
41 }
42
43 void SketchPlugin_IntersectionPoint::execute()
44 {
45   SketchPlugin_Sketch* aSketch = sketch();
46   if (aSketch) {
47     computePoint();
48     SketchPlugin_Point::execute();
49
50     // set this feature as external
51     data()->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
52   }
53 }
54
55 void SketchPlugin_IntersectionPoint::move(double theDeltaX, double theDeltaY)
56 {
57 }
58
59 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
60 {
61   if (theID == EXTERNAL_LINE_ID()) {
62     // compute intersection between line and sketch plane
63     computePoint();
64   }
65 }
66
67 void SketchPlugin_IntersectionPoint::computePoint()
68 {
69   AttributeSelectionPtr aLineAttr =
70       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_LINE_ID()));
71
72   std::shared_ptr<GeomAPI_Edge> anEdge;
73   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
74     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
75   } else if(aLineAttr->context() && aLineAttr->context()->shape() &&
76             aLineAttr->context()->shape()->isEdge()) {
77     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
78   }
79   if(!anEdge.get())
80     return;
81
82   std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
83   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
84
85   std::shared_ptr<GeomAPI_Pnt> anIntersection = aSketchPlane->intersect(aLine);
86   if (!anIntersection)
87     return;
88
89   std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr =
90       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
91   aCoordAttr->setValue(sketch()->to2D(anIntersection));
92 }