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_EllipticArc.h>
26 #include <SketchPlugin_Line.h>
27 #include <SketchPlugin_Point.h>
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_ConstraintRigid.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeSelection.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Validator.h>
37 #include <ModelAPI_Tools.h>
39 #include <GeomAPI_Circ.h>
40 #include <GeomAPI_Edge.h>
41 #include <GeomAPI_Ellipse.h>
42 #include <GeomAPI_Lin.h>
43 #include <GeomAPI_Pnt.h>
44 #include <GeomAPI_Pnt2d.h>
45 #include <GeomAPI_Vertex.h>
46 #include <GeomAlgoAPI_EdgeBuilder.h>
47 #include <GeomAlgoAPI_Projection.h>
48 #include <GeomDataAPI_Point2D.h>
52 static const double tolerance = 1.e-7;
54 SketchPlugin_Projection::SketchPlugin_Projection()
55 : SketchPlugin_SketchEntity(),
60 void SketchPlugin_Projection::initDerivedClassAttributes()
62 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
64 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
66 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
67 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
69 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
71 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
74 void SketchPlugin_Projection::execute()
76 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
77 if (!aRefAttr || !aRefAttr->isInitialized())
79 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
81 if (!lastResult().get()) {
82 bool hasProjResult = aProjection->lastResult().get() != NULL;
83 ResultConstructionPtr aConstr = document()->createConstruction(data());
85 aConstr->setShape(aProjection->lastResult()->shape());
86 aConstr->setIsInHistory(false);
87 aConstr->setDisplayed(false);
91 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
94 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
95 // projection must be fully recomputed
96 computeProjection(EXTERNAL_FEATURE_ID());
99 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
101 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
102 myIsComputing = true;
103 computeProjection(theID);
104 myIsComputing = false;
108 static const std::set<std::string>& POINT_PROJECTION()
110 static std::set<std::string> aProj;
112 aProj.insert(SketchPlugin_Point::ID());
116 static const std::set<std::string>& LINE_PROJECTION()
118 static std::set<std::string> aProj;
120 aProj.insert(SketchPlugin_Line::ID());
124 static const std::set<std::string>& CIRCLE_ELLIPSE_PROJECTION()
126 static std::set<std::string> aProj;
128 aProj.insert(SketchPlugin_Circle::ID());
129 aProj.insert(SketchPlugin_Ellipse::ID());
134 static const std::set<std::string>& ARC_PROJECTION()
136 static std::set<std::string> aProj;
138 aProj.insert(SketchPlugin_Arc::ID());
139 aProj.insert(SketchPlugin_EllipticArc::ID());
145 static const std::set<std::string>& possibleProjectionTypes(GeomEdgePtr theEdge,
146 GeomVertexPtr theVertex)
149 return POINT_PROJECTION();
151 if (theEdge->isLine())
152 return LINE_PROJECTION();
153 else if (theEdge->isCircle() || theEdge->isArc() || theEdge->isEllipse()) {
154 if (theEdge->isClosed())
155 return CIRCLE_ELLIPSE_PROJECTION();
157 return ARC_PROJECTION();
160 static const std::set<std::string> DUMMY;
164 void SketchPlugin_Projection::computeProjection(const std::string& theID)
166 AttributeSelectionPtr aExtFeature =
167 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
171 GeomVertexPtr aVertex;
173 aShape = aExtFeature->value();
174 if (!aShape && aExtFeature->context())
175 aShape = aExtFeature->context()->shape();
177 if (aShape->isEdge())
178 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
179 else if (aShape->isVertex())
180 aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
182 if (!anEdge && !aVertex)
185 const std::set<std::string>& aProjType = possibleProjectionTypes(anEdge, aVertex);
187 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
188 FeaturePtr aProjection;
189 if (aRefAttr && aRefAttr->isInitialized())
190 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
192 // if the type of feature differs with already selected, remove it and create once again
193 bool isRebuild = rebuildProjectedFeature(aProjection, aProjType);
195 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
197 ResultConstructionPtr aResult =
198 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
199 if (!isRebuild && aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
200 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
202 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
205 keepCurrentFeature();
208 std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
209 std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
211 rebuildProjectedFeature(aProjection, POINT_PROJECTION(), SketchPlugin_Point::ID());
213 // update coordinates of projection
214 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215 aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
217 else if (anEdge->isLine()) {
218 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
219 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
221 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
222 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
223 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
224 return; // line is semi-orthogonal to the sketch plane
226 rebuildProjectedFeature(aProjection, LINE_PROJECTION(), SketchPlugin_Line::ID());
228 // update attributes of projection
229 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
230 aProjection->attribute(SketchPlugin_Line::START_ID()));
231 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
232 aProjection->attribute(SketchPlugin_Line::END_ID()));
233 aStartPnt->setValue(aFirstInSketch);
234 aEndPnt->setValue(aLastInSketch);
236 else if (anEdge->isCircle() || anEdge->isArc() || anEdge->isEllipse()) {
237 GeomAlgoAPI_Projection aProjAlgo(aSketchPlane);
238 GeomCurvePtr aProjectedCurve = aProjAlgo.project(anEdge);
240 if (aProjectedCurve->isCircle()) {
241 GeomAPI_Circ aCircle(aProjectedCurve);
242 GeomPointPtr aCenter = aSketchPlane->project(aCircle.center());
243 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
245 if (aProjectedCurve->isTrimmed()) {
246 // ARC is a projection
247 rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_Arc::ID());
249 GeomPointPtr aFirst = aProjectedCurve->getPoint(aProjectedCurve->startParam());
250 GeomPointPtr aLast = aProjectedCurve->getPoint(aProjectedCurve->endParam());
251 GeomPnt2dPtr aFirstInSketch = sketch()->to2D(aSketchPlane->project(aFirst));
252 GeomPnt2dPtr aLastInSketch = sketch()->to2D(aSketchPlane->project(aLast));
254 double aNormalsDot = aCircle.normal()->dot(aSketchPlane->direction());
255 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
256 return; // arc is not in the plane, parallel to the sketch plane
258 bool isInversed = aNormalsDot < 0.;
260 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
262 // update attributes of projection
263 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
264 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
265 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
266 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
267 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
268 aProjection->attribute(SketchPlugin_Arc::START_ID()));
269 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
270 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
271 aProjection->attribute(SketchPlugin_Arc::END_ID()));
272 aStartPnt->setValue(aFirstInSketch);
273 aEndPnt->setValue(aLastInSketch);
274 aCenterPnt->setValue(aCenterInSketch);
275 aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
277 aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
280 // CIRCLE is a projection
281 rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
282 SketchPlugin_Circle::ID());
284 // update attributes of projection
285 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
286 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
287 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
288 aCenterPnt->setValue(aCenterInSketch);
289 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aCircle.radius());
292 else if (aProjectedCurve->isEllipse()) {
293 GeomAPI_Ellipse anEllipse(aProjectedCurve);
294 GeomPointPtr aCenter = aSketchPlane->project(anEllipse.center());
295 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
296 GeomPointPtr aFocus = aSketchPlane->project(anEllipse.firstFocus());
297 GeomPnt2dPtr aFocusInSketch = sketch()->to2D(aFocus);
299 if (aProjectedCurve->isTrimmed()) {
300 // ELLIPTIC ARC is a projection
301 rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_EllipticArc::ID());
303 GeomPointPtr aFirst = aProjectedCurve->getPoint(aProjectedCurve->startParam());
304 GeomPointPtr aLast = aProjectedCurve->getPoint(aProjectedCurve->endParam());
305 GeomPnt2dPtr aFirstInSketch = sketch()->to2D(aSketchPlane->project(aFirst));
306 GeomPnt2dPtr aLastInSketch = sketch()->to2D(aSketchPlane->project(aLast));
308 double aNormalsDot = anEllipse.normal()->dot(aSketchPlane->direction());
309 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
310 return; // arc is not in the plane, parallel to the sketch plane
312 bool isInversed = aNormalsDot < 0.;
314 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
316 // update attributes of projection
317 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
318 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
319 aProjection->attribute(SketchPlugin_EllipticArc::CENTER_ID()));
320 std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
321 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
322 aProjection->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()));
323 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
324 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
325 aProjection->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
326 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
327 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
328 aProjection->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
329 aStartPnt->setValue(aFirstInSketch);
330 aEndPnt->setValue(aLastInSketch);
331 aCenterPnt->setValue(aCenterInSketch);
332 aFocusPnt->setValue(aFocusInSketch);
333 aProjection->boolean(SketchPlugin_EllipticArc::REVERSED_ID())->setValue(isInversed);
335 aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
338 // ELLIPSE is a projection
339 rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
340 SketchPlugin_Ellipse::ID());
342 // update attributes of projection
343 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
344 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
345 aProjection->attribute(SketchPlugin_Ellipse::CENTER_ID()));
346 aCenterPnt->setValue(aCenterInSketch);
347 std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
348 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
349 aProjection->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
350 aFocusPnt->setValue(aFocusInSketch);
351 aProjection->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(
352 anEllipse.minorRadius());
360 aProjection->boolean(COPY_ID())->setValue(true);
361 aProjection->execute();
362 aRefAttr->setObject(aProjection);
364 restoreCurrentFeature();
366 if (theID == EXTERNAL_FEATURE_ID()) {
367 selection(EXTERNAL_ID())->selectValue(aExtFeature);
370 aResult->setShape(aProjection->lastResult()->shape());
372 GeomShapePtr anEmptyVal;
373 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
378 bool SketchPlugin_Projection::rebuildProjectedFeature(
379 FeaturePtr& theProjection,
380 const std::set<std::string>& theSupportedTypes,
381 const std::string& theRequestedFeature)
383 bool isRebuild = false;
385 (theSupportedTypes.find(theProjection->getKind()) == theSupportedTypes.end() ||
386 (!theRequestedFeature.empty() && theProjection->getKind() != theRequestedFeature))) {
387 DocumentPtr aDoc = sketch()->document();
389 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
390 aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
391 std::set<FeaturePtr> aFeaturesToBeRemoved;
392 aFeaturesToBeRemoved.insert(theProjection);
393 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
394 theProjection = FeaturePtr();
395 aRefAttr->setObject(theProjection);
399 if (!theProjection && !theRequestedFeature.empty())
400 theProjection = sketch()->addFeature(theRequestedFeature);