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_BSpline.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Ellipse.h>
26 #include <SketchPlugin_EllipticArc.h>
27 #include <SketchPlugin_Line.h>
28 #include <SketchPlugin_Point.h>
29 #include <SketchPlugin_Sketch.h>
30 #include <SketchPlugin_ConstraintRigid.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeDoubleArray.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_ResultConstruction.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Validator.h>
40 #include <ModelAPI_Tools.h>
41 #include <ModelAPI_Events.h>
43 #include <Events_Loop.h>
45 #include <GeomAPI_BSpline.h>
46 #include <GeomAPI_Circ.h>
47 #include <GeomAPI_Edge.h>
48 #include <GeomAPI_Ellipse.h>
49 #include <GeomAPI_Lin.h>
50 #include <GeomAPI_Pnt.h>
51 #include <GeomAPI_Pnt2d.h>
52 #include <GeomAPI_Vertex.h>
53 #include <GeomAlgoAPI_EdgeBuilder.h>
54 #include <GeomAlgoAPI_Projection.h>
55 #include <GeomDataAPI_Point2D.h>
56 #include <GeomDataAPI_Point2DArray.h>
60 static const double tolerance = 1.e-7;
63 SketchPlugin_Projection::SketchPlugin_Projection()
64 : SketchPlugin_SketchEntity(),
69 void SketchPlugin_Projection::initDerivedClassAttributes()
71 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
73 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
75 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
76 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
78 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
83 void SketchPlugin_Projection::execute()
85 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
86 if (!aRefAttr || !aRefAttr->isInitialized())
88 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
90 if (!lastResult().get()) {
91 bool hasProjResult = aProjection->lastResult().get() != NULL;
92 ResultConstructionPtr aConstr = document()->createConstruction(data());
94 aConstr->setShape(aProjection->lastResult()->shape());
95 aConstr->setIsInHistory(false);
96 aConstr->setDisplayed(false);
100 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
103 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
104 // projection must be fully recomputed
105 computeProjection(EXTERNAL_FEATURE_ID());
108 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
110 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
111 myIsComputing = true;
112 computeProjection(theID);
113 myIsComputing = false;
117 static const std::set<std::string>& POINT_PROJECTION()
119 static std::set<std::string> aProj;
121 aProj.insert(SketchPlugin_Point::ID());
125 static const std::set<std::string>& LINE_PROJECTION()
127 static std::set<std::string> aProj;
129 aProj.insert(SketchPlugin_Line::ID());
133 static const std::set<std::string>& CIRCLE_ELLIPSE_PROJECTION()
135 static std::set<std::string> aProj;
137 aProj.insert(SketchPlugin_Circle::ID());
138 aProj.insert(SketchPlugin_Ellipse::ID());
143 static const std::set<std::string>& ARC_PROJECTION()
145 static std::set<std::string> aProj;
147 aProj.insert(SketchPlugin_Arc::ID());
148 aProj.insert(SketchPlugin_EllipticArc::ID());
153 static const std::set<std::string>& BSPLINE_PROJECTION()
155 static std::set<std::string> aProj;
157 aProj.insert(SketchPlugin_BSpline::ID());
162 static const std::set<std::string>& possibleProjectionTypes(GeomEdgePtr theEdge,
163 GeomVertexPtr theVertex)
166 return POINT_PROJECTION();
168 if (theEdge->isLine())
169 return LINE_PROJECTION();
170 else if (theEdge->isCircle() || theEdge->isArc() || theEdge->isEllipse()) {
171 if (theEdge->isClosed())
172 return CIRCLE_ELLIPSE_PROJECTION();
174 return ARC_PROJECTION();
177 return BSPLINE_PROJECTION();
179 static const std::set<std::string> DUMMY;
183 void SketchPlugin_Projection::computeProjection(const std::string& theID)
185 AttributeSelectionPtr aExtFeature =
186 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
190 GeomVertexPtr aVertex;
192 aShape = aExtFeature->value();
193 if (!aShape && aExtFeature->context())
194 aShape = aExtFeature->context()->shape();
196 if (aShape->isEdge())
197 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
198 else if (aShape->isVertex())
199 aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
201 if (!anEdge && !aVertex)
204 const std::set<std::string>& aProjType = possibleProjectionTypes(anEdge, aVertex);
206 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
207 FeaturePtr aProjection;
208 if (aRefAttr && aRefAttr->isInitialized())
209 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
211 // if the type of feature differs with already selected, remove it and create once again
212 bool isRebuild = rebuildProjectedFeature(aProjection, aProjType);
214 ResultConstructionPtr aResult =
215 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
216 if (!isRebuild && aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
217 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
219 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
222 keepCurrentFeature();
224 bool isProjected = false;
226 isProjected = projectPoint(aProjection, aVertex->point());
228 isProjected = projectEdge(aProjection, anEdge);
231 return; // projection is not computed, stop processing
233 aProjection->boolean(COPY_ID())->setValue(true);
234 aProjection->execute();
235 aRefAttr->setObject(aProjection);
237 restoreCurrentFeature();
239 if (theID == EXTERNAL_FEATURE_ID()) {
240 selection(EXTERNAL_ID())->selectValue(aExtFeature);
243 aResult->setShape(aProjection->lastResult()->shape());
245 GeomShapePtr anEmptyVal;
246 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
248 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_VISUAL_ATTRIBUTES);
249 ModelAPI_EventCreator::get()->sendUpdated(aProjection, anEvent, false);
254 bool SketchPlugin_Projection::rebuildProjectedFeature(
255 FeaturePtr& theProjection,
256 const std::set<std::string>& theSupportedTypes,
257 const std::string& theRequestedFeature)
259 bool isRebuild = false;
261 (theSupportedTypes.find(theProjection->getKind()) == theSupportedTypes.end() ||
262 (!theRequestedFeature.empty() && theProjection->getKind() != theRequestedFeature))) {
263 DocumentPtr aDoc = sketch()->document();
265 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
266 aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
267 std::set<FeaturePtr> aFeaturesToBeRemoved;
268 aFeaturesToBeRemoved.insert(theProjection);
269 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
270 theProjection = FeaturePtr();
271 aRefAttr->setObject(theProjection);
275 if (!theProjection && !theRequestedFeature.empty())
276 theProjection = sketch()->addFeature(theRequestedFeature);
280 bool SketchPlugin_Projection::projectPoint(FeaturePtr& theProjection, const GeomPointPtr& thePoint)
282 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
284 std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(thePoint);
285 std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
287 rebuildProjectedFeature(theProjection, POINT_PROJECTION(), SketchPlugin_Point::ID());
289 // update coordinates of projection
290 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
291 theProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
295 bool SketchPlugin_Projection::projectSegment(FeaturePtr& theProjection, const GeomEdgePtr& theEdge)
297 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
299 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(theEdge->firstPoint());
300 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(theEdge->lastPoint());
302 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
303 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
304 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
305 return false; // line is semi-orthogonal to the sketch plane
307 rebuildProjectedFeature(theProjection, LINE_PROJECTION(), SketchPlugin_Line::ID());
309 // update attributes of projection
310 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
311 theProjection->attribute(SketchPlugin_Line::START_ID()));
312 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
313 theProjection->attribute(SketchPlugin_Line::END_ID()));
314 aStartPnt->setValue(aFirstInSketch);
315 aEndPnt->setValue(aLastInSketch);
320 bool SketchPlugin_Projection::projectEdge(FeaturePtr& theProjection, const GeomEdgePtr& theEdge)
322 if (theEdge->isLine())
323 return projectSegment(theProjection, theEdge);
325 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
327 GeomAlgoAPI_Projection aProjAlgo(aSketchPlane);
328 GeomCurvePtr aProjectedCurve = aProjAlgo.project(theEdge);
331 if (aProjectedCurve->isCircle()) {
332 if (aProjectedCurve->isTrimmed()) {
333 // ARC is a projection
334 isOk = fillArc(theProjection, aProjectedCurve, aSketchPlane);
337 // CIRCLE is a projection
338 isOk = fillCircle(theProjection, aProjectedCurve, aSketchPlane);
341 else if (aProjectedCurve->isEllipse()) {
342 if (aProjectedCurve->isTrimmed()) {
343 // ELLIPTIC ARC is a projection
344 isOk = fillEllipticArc(theProjection, aProjectedCurve, aSketchPlane);
347 // ELLIPSE is a projection
348 isOk = fillEllipse(theProjection, aProjectedCurve, aSketchPlane);
352 isOk = fillBSpline(theProjection, aProjectedCurve, aSketchPlane);
357 bool SketchPlugin_Projection::fillArc(FeaturePtr& theProjection,
358 const GeomCurvePtr& theArc,
359 const GeomPlanePtr& thePlane)
361 rebuildProjectedFeature(theProjection, ARC_PROJECTION(), SketchPlugin_Arc::ID());
363 GeomAPI_Circ aCircle(theArc);
365 double aNormalsDot = aCircle.normal()->dot(thePlane->direction());
366 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
367 return false; // arc is not in the plane, parallel to the sketch plane
369 bool isInversed = aNormalsDot < 0.;
371 GeomPointPtr aCenter = thePlane->project(aCircle.center());
372 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
374 GeomPointPtr aFirst = theArc->getPoint(theArc->startParam());
375 GeomPnt2dPtr aFirstInSketch = sketch()->to2D(thePlane->project(aFirst));
377 GeomPointPtr aLast = theArc->getPoint(theArc->endParam());
378 GeomPnt2dPtr aLastInSketch = sketch()->to2D(thePlane->project(aLast));
380 bool aWasBlocked = theProjection->data()->blockSendAttributeUpdated(true);
382 // update attributes of projection
383 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
384 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
385 theProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
386 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
387 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
388 theProjection->attribute(SketchPlugin_Arc::START_ID()));
389 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
390 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
391 theProjection->attribute(SketchPlugin_Arc::END_ID()));
392 aStartPnt->setValue(aFirstInSketch);
393 aEndPnt->setValue(aLastInSketch);
394 aCenterPnt->setValue(aCenterInSketch);
395 theProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
397 theProjection->data()->blockSendAttributeUpdated(aWasBlocked);
401 bool SketchPlugin_Projection::fillCircle(FeaturePtr& theProjection,
402 const GeomCurvePtr& theCircle,
403 const GeomPlanePtr& thePlane)
405 rebuildProjectedFeature(theProjection, CIRCLE_ELLIPSE_PROJECTION(), SketchPlugin_Circle::ID());
407 GeomAPI_Circ aCircle(theCircle);
408 GeomPointPtr aCenter = thePlane->project(aCircle.center());
409 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
411 // update attributes of projection
412 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
413 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
414 theProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
415 aCenterPnt->setValue(aCenterInSketch);
416 theProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aCircle.radius());
420 bool SketchPlugin_Projection::fillEllipse(FeaturePtr& theProjection,
421 const GeomCurvePtr& theEllipse,
422 const GeomPlanePtr& thePlane)
424 rebuildProjectedFeature(theProjection, CIRCLE_ELLIPSE_PROJECTION(), SketchPlugin_Ellipse::ID());
426 GeomAPI_Ellipse anEllipse(theEllipse);
427 GeomPointPtr aCenter = thePlane->project(anEllipse.center());
428 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
429 GeomPointPtr aFocus = thePlane->project(anEllipse.firstFocus());
430 GeomPnt2dPtr aFocusInSketch = sketch()->to2D(aFocus);
432 // update attributes of projection
433 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
434 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
435 theProjection->attribute(SketchPlugin_Ellipse::CENTER_ID()));
436 aCenterPnt->setValue(aCenterInSketch);
437 std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
438 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
439 theProjection->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
440 aFocusPnt->setValue(aFocusInSketch);
441 theProjection->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(anEllipse.minorRadius());
445 bool SketchPlugin_Projection::fillEllipticArc(FeaturePtr& theProjection,
446 const GeomCurvePtr& theEllipticArc,
447 const GeomPlanePtr& thePlane)
449 rebuildProjectedFeature(theProjection, ARC_PROJECTION(), SketchPlugin_EllipticArc::ID());
451 GeomAPI_Ellipse anEllipse(theEllipticArc);
453 double aNormalsDot = anEllipse.normal()->dot(thePlane->direction());
454 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
455 return false; // arc is not in the plane, parallel to the sketch plane
457 bool isInversed = aNormalsDot < 0.;
459 GeomPointPtr aCenter = thePlane->project(anEllipse.center());
460 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
461 GeomPointPtr aFocus = thePlane->project(anEllipse.firstFocus());
462 GeomPnt2dPtr aFocusInSketch = sketch()->to2D(aFocus);
464 GeomPointPtr aFirst = theEllipticArc->getPoint(theEllipticArc->startParam());
465 GeomPnt2dPtr aFirstInSketch = sketch()->to2D(thePlane->project(aFirst));
466 GeomPointPtr aLast = theEllipticArc->getPoint(theEllipticArc->endParam());
467 GeomPnt2dPtr aLastInSketch = sketch()->to2D(thePlane->project(aLast));
469 bool aWasBlocked = theProjection->data()->blockSendAttributeUpdated(true);
471 // update attributes of projection
472 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
473 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
474 theProjection->attribute(SketchPlugin_EllipticArc::CENTER_ID()));
475 std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
476 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
477 theProjection->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()));
478 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
479 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
480 theProjection->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
481 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
482 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
483 theProjection->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
484 aStartPnt->setValue(aFirstInSketch);
485 aEndPnt->setValue(aLastInSketch);
486 aCenterPnt->setValue(aCenterInSketch);
487 aFocusPnt->setValue(aFocusInSketch);
488 theProjection->boolean(SketchPlugin_EllipticArc::REVERSED_ID())->setValue(isInversed);
490 theProjection->data()->blockSendAttributeUpdated(aWasBlocked);
494 bool SketchPlugin_Projection::fillBSpline(FeaturePtr& theProjection,
495 const GeomCurvePtr& theCurve,
496 const GeomPlanePtr& thePlane)
498 rebuildProjectedFeature(theProjection, BSPLINE_PROJECTION(), SketchPlugin_BSpline::ID());
500 GeomAPI_BSpline aBSpline(theCurve);
502 theProjection->integer(SketchPlugin_BSpline::DEGREE_ID())->setValue(aBSpline.degree());
504 AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
505 theProjection->attribute(SketchPlugin_BSpline::POLES_ID()));
506 std::list<GeomPointPtr> aPoles = aBSpline.poles();
507 aPolesAttr->setSize((int)aPoles.size());
508 std::list<GeomPointPtr>::iterator anIt = aPoles.begin();
509 for (int anIndex = 0; anIt != aPoles.end(); ++anIt, ++anIndex) {
510 GeomPnt2dPtr aPoleInSketch = sketch()->to2D(*anIt);
511 aPolesAttr->setPnt(anIndex, aPoleInSketch);
514 AttributeDoubleArrayPtr aWeightsAttr =
515 theProjection->data()->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
516 std::list<double> aWeights = aBSpline.weights();
517 if (aWeights.empty()) { // rational B-spline
518 int aSize = (int)aPoles.size();
519 aWeightsAttr->setSize(aSize);
520 for (int anIndex = 0; anIndex < aSize; ++anIndex)
521 aWeightsAttr->setValue(anIndex, 1.0);
523 else { // non-rational B-spline
524 aWeightsAttr->setSize((int)aWeights.size());
525 std::list<double>::iterator anIt = aWeights.begin();
526 for (int anIndex = 0; anIt != aWeights.end(); ++anIt, ++anIndex)
527 aWeightsAttr->setValue(anIndex, *anIt);
530 AttributeDoubleArrayPtr aKnotsAttr =
531 theProjection->data()->realArray(SketchPlugin_BSpline::KNOTS_ID());
532 std::list<double> aKnots = aBSpline.knots();
533 int aSize = (int)aKnots.size();
534 aKnotsAttr->setSize(aSize);
535 std::list<double>::iterator aKIt = aKnots.begin();
536 for (int index = 0; index < aSize; ++index, ++aKIt)
537 aKnotsAttr->setValue(index, *aKIt);
539 AttributeIntArrayPtr aMultsAttr =
540 theProjection->data()->intArray(SketchPlugin_BSpline::MULTS_ID());
541 std::list<int> aMultiplicities = aBSpline.mults();
542 aSize = (int)aMultiplicities.size();
543 aMultsAttr->setSize(aSize);
544 std::list<int>::iterator aMIt = aMultiplicities.begin();
545 for (int index = 0; index < aSize; ++index, ++aMIt)
546 aMultsAttr->setValue(index, *aMIt);