Salome HOME
be389af74185cdfc8f8d62f926cb8697f3a82a0c
[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 #include "SketchPlugin_Point.h"
23
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Validator.h>
30
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_Pnt2d.h>
34 #include <GeomDataAPI_Point2D.h>
35
36 SketchPlugin_IntersectionPoint::SketchPlugin_IntersectionPoint()
37   : SketchPlugin_SketchEntity(),
38     myIsComputing(false)
39 {
40 }
41
42 void SketchPlugin_IntersectionPoint::initDerivedClassAttributes()
43 {
44   data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
45   data()->addAttribute(INTERSECTION_POINTS_ID(), ModelAPI_AttributeRefList::typeId());
46   data()->attribute(INTERSECTION_POINTS_ID())->setIsArgument(false);
47
48   data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
49
50   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
51   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
52
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
54 }
55
56 void SketchPlugin_IntersectionPoint::execute()
57 {
58   AttributeRefListPtr anIntersectionsList = reflist(INTERSECTION_POINTS_ID());
59   if (!anIntersectionsList || !anIntersectionsList->isInitialized())
60     return; // no intersections
61
62   computePoint(EXTERNAL_FEATURE_ID());
63 }
64
65 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
66 {
67   // compute intersection between line and sketch plane
68   computePoint(theID);
69 }
70
71 void SketchPlugin_IntersectionPoint::computePoint(const std::string& theID)
72 {
73   if (theID != EXTERNAL_FEATURE_ID() && theID != EXTERNAL_ID())
74     return;
75
76   if (myIsComputing)
77     return;
78   myIsComputing = true;
79
80   AttributeSelectionPtr anExternalFeature = selection(EXTERNAL_FEATURE_ID());
81
82   GeomShapePtr aShape;
83   GeomEdgePtr anEdge;
84   if (anExternalFeature)
85     aShape = anExternalFeature->value();
86   if (!aShape && anExternalFeature->context())
87     aShape = anExternalFeature->context()->shape();
88   if (aShape && aShape->isEdge())
89     anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
90
91   if (anEdge) {
92     std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
93
94     std::list<GeomPointPtr> anIntersectionsPoints;
95     anEdge->intersectWithPlane(aSketchPlane, anIntersectionsPoints);
96
97     AttributeRefListPtr anIntersectionsList = reflist(INTERSECTION_POINTS_ID());
98     std::list<ObjectPtr> anExistentIntersections = anIntersectionsList->list();
99     std::list<ObjectPtr>::const_iterator aExistInterIt = anExistentIntersections.begin();
100
101     const std::list<ResultPtr>& aResults = results();
102     std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
103
104     int aResultIndex = 0;
105     for (std::list<GeomPointPtr>::iterator aPntIt = anIntersectionsPoints.begin();
106          aPntIt != anIntersectionsPoints.end(); ++aPntIt, ++aResultIndex) {
107       std::shared_ptr<SketchPlugin_Point> aCurSketchPoint;
108       if (aExistInterIt == anExistentIntersections.end()) {
109         // create new point
110         aCurSketchPoint = std::dynamic_pointer_cast<SketchPlugin_Point>(
111           sketch()->addFeature(SketchPlugin_Point::ID()));
112         aCurSketchPoint->boolean(COPY_ID())->setValue(true);
113         anIntersectionsList->append(aCurSketchPoint);
114       } else {
115         // update existent point
116         aCurSketchPoint = std::dynamic_pointer_cast<SketchPlugin_Point>(*aExistInterIt);
117         ++aExistInterIt;
118       }
119
120       ResultConstructionPtr aCurResult;
121       if (aResIt == aResults.end()) {
122         // create new result
123         aCurResult = document()->createConstruction(data(), aResultIndex);
124         aCurResult->setIsInHistory(false);
125         aCurResult->setDisplayed(false);
126       } else {
127         // update existent result
128         aCurResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIt);
129         aCurResult->setShape(std::shared_ptr<GeomAPI_Edge>());
130         ++aResIt;
131       }
132
133       // update coordinates of intersection
134       GeomPnt2dPtr aPointInSketch = sketch()->to2D(*aPntIt);
135       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
136         aCurSketchPoint->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPointInSketch);
137       aCurSketchPoint->execute();
138
139       // update result
140       aCurResult->setShape(aCurSketchPoint->lastResult()->shape());
141       setResult(aCurResult, aResultIndex);
142
143       // make intersection point external
144       GeomShapePtr anEmptyVal;
145       aCurSketchPoint->selection(EXTERNAL_ID())->setValue(aCurResult, anEmptyVal);
146     }
147
148     // remove rest results from previous pass
149     removeResults(aResultIndex);
150     std::set<FeaturePtr> aFeaturesToBeRemoved;
151     for (; aExistInterIt != anExistentIntersections.end(); ++aExistInterIt) {
152       aFeaturesToBeRemoved.insert(ModelAPI_Feature::feature(*aExistInterIt));
153       anIntersectionsList->removeLast();
154     }
155     ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
156
157     if (theID != EXTERNAL_ID())
158       selection(EXTERNAL_ID())->selectValue(anExternalFeature);
159   }
160   myIsComputing = false;
161 }