1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include <SketchPlugin_Projection.h>
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Ellipse.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Point.h>
27 #include <SketchPlugin_Sketch.h>
28 #include <SketchPlugin_ConstraintRigid.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Tools.h>
38 #include <GeomAPI_Circ.h>
39 #include <GeomAPI_Edge.h>
40 #include <GeomAPI_Ellipse.h>
41 #include <GeomAPI_Lin.h>
42 #include <GeomAPI_Pnt.h>
43 #include <GeomAPI_Pnt2d.h>
44 #include <GeomAPI_Vertex.h>
45 #include <GeomAlgoAPI_EdgeBuilder.h>
46 #include <GeomAlgoAPI_Projection.h>
47 #include <GeomDataAPI_Point2D.h>
51 static const double tolerance = 1.e-7;
53 SketchPlugin_Projection::SketchPlugin_Projection()
54 : SketchPlugin_SketchEntity(),
59 void SketchPlugin_Projection::initDerivedClassAttributes()
61 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
63 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
65 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
66 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
68 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
70 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
73 void SketchPlugin_Projection::execute()
75 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
76 if (!aRefAttr || !aRefAttr->isInitialized())
78 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
80 if (!lastResult().get()) {
81 bool hasProjResult = aProjection->lastResult().get() != NULL;
82 ResultConstructionPtr aConstr = document()->createConstruction(data());
84 aConstr->setShape(aProjection->lastResult()->shape());
85 aConstr->setIsInHistory(false);
86 aConstr->setDisplayed(false);
90 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
93 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
94 // projection must be fully recomputed
95 computeProjection(EXTERNAL_FEATURE_ID());
98 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
100 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
101 myIsComputing = true;
102 computeProjection(theID);
103 myIsComputing = false;
107 static const std::string& projectionType(GeomEdgePtr theEdge,
108 GeomVertexPtr theVertex)
111 return SketchPlugin_Point::ID();
113 if (theEdge->isLine())
114 return SketchPlugin_Line::ID();
115 else if (theEdge->isCircle())
116 return SketchPlugin_Circle::ID();
117 else if (theEdge->isArc())
118 return SketchPlugin_Arc::ID();
119 else if (theEdge->isEllipse())
120 return SketchPlugin_Ellipse::ID();
122 static const std::string DUMMY;
126 void SketchPlugin_Projection::computeProjection(const std::string& theID)
128 AttributeSelectionPtr aExtFeature =
129 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
133 GeomVertexPtr aVertex;
135 aShape = aExtFeature->value();
136 if (!aShape && aExtFeature->context())
137 aShape = aExtFeature->context()->shape();
139 if (aShape->isEdge())
140 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
141 else if (aShape->isVertex())
142 aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
144 if (!anEdge && !aVertex)
147 const std::string& aProjType = projectionType(anEdge, aVertex);
149 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
150 FeaturePtr aProjection;
151 if (aRefAttr && aRefAttr->isInitialized())
152 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
154 // if the type of feature differs with already selected, remove it and create once again
155 bool isRebuild = rebuildProjectedFeature(aProjection, aProjType, true);
157 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
159 ResultConstructionPtr aResult =
160 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
161 if (!isRebuild && aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
162 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
164 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
167 keepCurrentFeature();
170 std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
171 std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
173 rebuildProjectedFeature(aProjection, SketchPlugin_Point::ID());
175 // update coordinates of projection
176 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
177 aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
179 else if (anEdge->isLine()) {
180 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
181 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
183 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
184 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
185 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
186 return; // line is semi-orthogonal to the sketch plane
188 rebuildProjectedFeature(aProjection, SketchPlugin_Line::ID());
190 // update attributes of projection
191 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aProjection->attribute(SketchPlugin_Line::START_ID()));
193 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194 aProjection->attribute(SketchPlugin_Line::END_ID()));
195 aStartPnt->setValue(aFirstInSketch);
196 aEndPnt->setValue(aLastInSketch);
198 else if (anEdge->isCircle() || anEdge->isEllipse()) {
199 GeomAlgoAPI_Projection aProjAlgo(aSketchPlane);
200 GeomCurvePtr aProjectedCurve = aProjAlgo.project(anEdge);
202 if (aProjectedCurve->isCircle()) {
203 rebuildProjectedFeature(aProjection, SketchPlugin_Circle::ID());
205 GeomAPI_Circ aCircle(aProjectedCurve);
206 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle.center());
207 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
209 // update attributes of projection
210 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
211 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
212 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
213 aCenterPnt->setValue(aCenterInSketch);
214 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aCircle.radius());
216 else if (aProjectedCurve->isEllipse()) {
217 rebuildProjectedFeature(aProjection, SketchPlugin_Ellipse::ID());
219 GeomAPI_Ellipse anEllipse(aProjectedCurve);
220 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(anEllipse.center());
221 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
222 std::shared_ptr<GeomAPI_Pnt> aFocus = aSketchPlane->project(anEllipse.firstFocus());
223 std::shared_ptr<GeomAPI_Pnt2d> aFocusInSketch = sketch()->to2D(aFocus);
225 // update attributes of projection
226 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
227 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
228 aProjection->attribute(SketchPlugin_Ellipse::CENTER_ID()));
229 aCenterPnt->setValue(aCenterInSketch);
230 std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
231 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
232 aProjection->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
233 aFocusPnt->setValue(aFocusInSketch);
234 aProjection->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(
235 anEllipse.minorRadius());
240 else if (anEdge->isArc()) {
241 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
242 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
243 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
244 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
246 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
247 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
248 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
250 double aNormalsDot = aCircle->normal()->dot(aSketchPlane->direction());
251 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
252 return; // arc is not in the plane, parallel to the sketch plane
254 bool isInversed = aNormalsDot < 0.;
256 rebuildProjectedFeature(aProjection, SketchPlugin_Arc::ID());
258 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
260 // update attributes of projection
261 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
262 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
263 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
264 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
265 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
266 aProjection->attribute(SketchPlugin_Arc::START_ID()));
267 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
268 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
269 aProjection->attribute(SketchPlugin_Arc::END_ID()));
270 aStartPnt->setValue(aFirstInSketch);
271 aEndPnt->setValue(aLastInSketch);
272 aCenterPnt->setValue(aCenterInSketch);
273 aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
275 aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
279 aProjection->boolean(COPY_ID())->setValue(true);
280 aProjection->execute();
281 aRefAttr->setObject(aProjection);
283 restoreCurrentFeature();
285 if (theID == EXTERNAL_FEATURE_ID()) {
286 selection(EXTERNAL_ID())->selectValue(aExtFeature);
289 aResult->setShape(aProjection->lastResult()->shape());
291 GeomShapePtr anEmptyVal;
292 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
297 bool SketchPlugin_Projection::rebuildProjectedFeature(FeaturePtr& theProjection,
298 const std::string& theResultType,
301 bool isRebuild = false;
302 if (theProjection && theProjection->getKind() != theResultType) {
303 DocumentPtr aDoc = sketch()->document();
305 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
306 aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
307 std::set<FeaturePtr> aFeaturesToBeRemoved;
308 aFeaturesToBeRemoved.insert(theProjection);
309 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
310 theProjection = FeaturePtr();
311 aRefAttr->setObject(theProjection);
315 if (!theProjection && !theRemoveOnly)
316 theProjection = sketch()->addFeature(theResultType);