1 // Copyright (C) 2014-2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_IntersectionPoint.h"
21 #include "SketchPlugin_Point.h"
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_Pnt2d.h>
33 #include <GeomDataAPI_Point2D.h>
35 SketchPlugin_IntersectionPoint::SketchPlugin_IntersectionPoint()
36 : SketchPlugin_SketchEntity(),
41 void SketchPlugin_IntersectionPoint::initDerivedClassAttributes()
43 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
44 data()->addAttribute(INTERSECTION_POINTS_ID(), ModelAPI_AttributeRefList::typeId());
45 data()->attribute(INTERSECTION_POINTS_ID())->setIsArgument(false);
47 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
49 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
50 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
55 void SketchPlugin_IntersectionPoint::execute()
57 AttributeRefListPtr anIntersectionsList = reflist(INTERSECTION_POINTS_ID());
58 if (!anIntersectionsList || !anIntersectionsList->isInitialized())
59 return; // no intersections
61 computePoint(EXTERNAL_FEATURE_ID());
64 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
66 // compute intersection between line and sketch plane
70 void SketchPlugin_IntersectionPoint::computePoint(const std::string& theID)
72 if (theID != EXTERNAL_FEATURE_ID() && theID != EXTERNAL_ID())
79 AttributeSelectionPtr anExternalFeature = selection(EXTERNAL_FEATURE_ID());
83 if (anExternalFeature)
84 aShape = anExternalFeature->value();
85 if (!aShape && anExternalFeature->context())
86 aShape = anExternalFeature->context()->shape();
87 if (aShape && aShape->isEdge())
88 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
91 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
93 std::list<GeomPointPtr> anIntersectionsPoints;
94 anEdge->intersectWithPlane(aSketchPlane, anIntersectionsPoints);
96 AttributeRefListPtr anIntersectionsList = reflist(INTERSECTION_POINTS_ID());
97 std::list<ObjectPtr> anExistentIntersections = anIntersectionsList->list();
98 std::list<ObjectPtr>::const_iterator aExistInterIt = anExistentIntersections.begin();
100 const std::list<ResultPtr>& aResults = results();
101 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
103 int aResultIndex = 0;
104 for (std::list<GeomPointPtr>::iterator aPntIt = anIntersectionsPoints.begin();
105 aPntIt != anIntersectionsPoints.end(); ++aPntIt, ++aResultIndex) {
106 std::shared_ptr<SketchPlugin_Point> aCurSketchPoint;
107 if (aExistInterIt == anExistentIntersections.end()) {
108 keepCurrentFeature();
111 aCurSketchPoint = std::dynamic_pointer_cast<SketchPlugin_Point>(
112 sketch()->addFeature(SketchPlugin_Point::ID()));
113 aCurSketchPoint->boolean(COPY_ID())->setValue(true);
114 anIntersectionsList->append(aCurSketchPoint);
116 restoreCurrentFeature();
118 // update existent point
119 aCurSketchPoint = std::dynamic_pointer_cast<SketchPlugin_Point>(*aExistInterIt);
123 ResultConstructionPtr aCurResult;
124 if (aResIt == aResults.end()) {
126 aCurResult = document()->createConstruction(data(), aResultIndex);
127 aCurResult->setIsInHistory(false);
128 aCurResult->setDisplayed(false);
130 // update existent result
131 aCurResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIt);
132 aCurResult->setShape(std::shared_ptr<GeomAPI_Edge>());
136 // update coordinates of intersection
137 GeomPnt2dPtr aPointInSketch = sketch()->to2D(*aPntIt);
138 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
139 aCurSketchPoint->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPointInSketch);
140 aCurSketchPoint->execute();
143 aCurResult->setShape(aCurSketchPoint->lastResult()->shape());
144 setResult(aCurResult, aResultIndex);
146 // make intersection point external
147 GeomShapePtr anEmptyVal;
148 aCurSketchPoint->selection(EXTERNAL_ID())->setValue(aCurResult, anEmptyVal);
151 // remove rest results from previous pass
152 removeResults(aResultIndex);
153 std::set<FeaturePtr> aFeaturesToBeRemoved;
154 for (; aExistInterIt != anExistentIntersections.end(); ++aExistInterIt) {
155 aFeaturesToBeRemoved.insert(ModelAPI_Feature::feature(*aExistInterIt));
156 anIntersectionsList->removeLast();
158 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
160 if (theID != EXTERNAL_ID())
161 selection(EXTERNAL_ID())->selectValue(anExternalFeature);
163 myIsComputing = false;