1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_IntersectionPoint.h"
22 #include "SketchPlugin_Point.h"
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>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_Pnt2d.h>
34 #include <GeomDataAPI_Point2D.h>
36 SketchPlugin_IntersectionPoint::SketchPlugin_IntersectionPoint()
37 : SketchPlugin_SketchEntity(),
42 void SketchPlugin_IntersectionPoint::initDerivedClassAttributes()
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);
48 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
50 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
51 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
56 void SketchPlugin_IntersectionPoint::execute()
58 AttributeRefListPtr anIntersectionsList = reflist(INTERSECTION_POINTS_ID());
59 if (!anIntersectionsList || !anIntersectionsList->isInitialized())
60 return; // no intersections
62 computePoint(EXTERNAL_FEATURE_ID());
65 void SketchPlugin_IntersectionPoint::attributeChanged(const std::string& theID)
67 // compute intersection between line and sketch plane
71 void SketchPlugin_IntersectionPoint::computePoint(const std::string& theID)
73 if (theID != EXTERNAL_FEATURE_ID() && theID != EXTERNAL_ID())
80 AttributeSelectionPtr anExternalFeature = selection(EXTERNAL_FEATURE_ID());
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));
92 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
94 std::list<GeomPointPtr> anIntersectionsPoints;
95 anEdge->intersectWithPlane(aSketchPlane, anIntersectionsPoints);
97 AttributeRefListPtr anIntersectionsList = reflist(INTERSECTION_POINTS_ID());
98 std::list<ObjectPtr> anExistentIntersections = anIntersectionsList->list();
99 std::list<ObjectPtr>::const_iterator aExistInterIt = anExistentIntersections.begin();
101 const std::list<ResultPtr>& aResults = results();
102 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
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()) {
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);
115 // update existent point
116 aCurSketchPoint = std::dynamic_pointer_cast<SketchPlugin_Point>(*aExistInterIt);
120 ResultConstructionPtr aCurResult;
121 if (aResIt == aResults.end()) {
123 aCurResult = document()->createConstruction(data(), aResultIndex);
124 aCurResult->setIsInHistory(false);
125 aCurResult->setDisplayed(false);
127 // update existent result
128 aCurResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIt);
129 aCurResult->setShape(std::shared_ptr<GeomAPI_Edge>());
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();
140 aCurResult->setShape(aCurSketchPoint->lastResult()->shape());
141 setResult(aCurResult, aResultIndex);
143 // make intersection point external
144 GeomShapePtr anEmptyVal;
145 aCurSketchPoint->selection(EXTERNAL_ID())->setValue(aCurResult, anEmptyVal);
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();
155 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
157 if (theID != EXTERNAL_ID())
158 selection(EXTERNAL_ID())->selectValue(anExternalFeature);
160 myIsComputing = false;