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>
38 #include <ModelAPI_Events.h>
40 #include <Events_Loop.h>
42 #include <GeomAPI_Circ.h>
43 #include <GeomAPI_Edge.h>
44 #include <GeomAPI_Ellipse.h>
45 #include <GeomAPI_Lin.h>
46 #include <GeomAPI_Pnt.h>
47 #include <GeomAPI_Pnt2d.h>
48 #include <GeomAPI_Vertex.h>
49 #include <GeomAlgoAPI_EdgeBuilder.h>
50 #include <GeomAlgoAPI_Projection.h>
51 #include <GeomDataAPI_Point2D.h>
55 static const double tolerance = 1.e-7;
58 SketchPlugin_Projection::SketchPlugin_Projection()
59 : SketchPlugin_SketchEntity(),
64 void SketchPlugin_Projection::initDerivedClassAttributes()
66 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
68 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
70 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
71 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
73 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
75 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
78 void SketchPlugin_Projection::execute()
80 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
81 if (!aRefAttr || !aRefAttr->isInitialized())
83 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
85 if (!lastResult().get()) {
86 bool hasProjResult = aProjection->lastResult().get() != NULL;
87 ResultConstructionPtr aConstr = document()->createConstruction(data());
89 aConstr->setShape(aProjection->lastResult()->shape());
90 aConstr->setIsInHistory(false);
91 aConstr->setDisplayed(false);
95 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
98 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
99 // projection must be fully recomputed
100 computeProjection(EXTERNAL_FEATURE_ID());
103 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
105 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
106 myIsComputing = true;
107 computeProjection(theID);
108 myIsComputing = false;
112 static const std::set<std::string>& POINT_PROJECTION()
114 static std::set<std::string> aProj;
116 aProj.insert(SketchPlugin_Point::ID());
120 static const std::set<std::string>& LINE_PROJECTION()
122 static std::set<std::string> aProj;
124 aProj.insert(SketchPlugin_Line::ID());
128 static const std::set<std::string>& CIRCLE_ELLIPSE_PROJECTION()
130 static std::set<std::string> aProj;
132 aProj.insert(SketchPlugin_Circle::ID());
133 aProj.insert(SketchPlugin_Ellipse::ID());
138 static const std::set<std::string>& ARC_PROJECTION()
140 static std::set<std::string> aProj;
142 aProj.insert(SketchPlugin_Arc::ID());
143 aProj.insert(SketchPlugin_EllipticArc::ID());
149 static const std::set<std::string>& possibleProjectionTypes(GeomEdgePtr theEdge,
150 GeomVertexPtr theVertex)
153 return POINT_PROJECTION();
155 if (theEdge->isLine())
156 return LINE_PROJECTION();
157 else if (theEdge->isCircle() || theEdge->isArc() || theEdge->isEllipse()) {
158 if (theEdge->isClosed())
159 return CIRCLE_ELLIPSE_PROJECTION();
161 return ARC_PROJECTION();
164 static const std::set<std::string> DUMMY;
168 void SketchPlugin_Projection::computeProjection(const std::string& theID)
170 AttributeSelectionPtr aExtFeature =
171 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
175 GeomVertexPtr aVertex;
177 aShape = aExtFeature->value();
178 if (!aShape && aExtFeature->context())
179 aShape = aExtFeature->context()->shape();
181 if (aShape->isEdge())
182 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
183 else if (aShape->isVertex())
184 aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
186 if (!anEdge && !aVertex)
189 const std::set<std::string>& aProjType = possibleProjectionTypes(anEdge, aVertex);
191 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
192 FeaturePtr aProjection;
193 if (aRefAttr && aRefAttr->isInitialized())
194 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
196 // if the type of feature differs with already selected, remove it and create once again
197 bool isRebuild = rebuildProjectedFeature(aProjection, aProjType);
199 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
201 ResultConstructionPtr aResult =
202 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
203 if (!isRebuild && aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
204 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
206 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
209 keepCurrentFeature();
212 std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
213 std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
215 rebuildProjectedFeature(aProjection, POINT_PROJECTION(), SketchPlugin_Point::ID());
217 // update coordinates of projection
218 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219 aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
221 else if (anEdge->isLine()) {
222 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
223 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
225 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
226 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
227 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
228 return; // line is semi-orthogonal to the sketch plane
230 rebuildProjectedFeature(aProjection, LINE_PROJECTION(), SketchPlugin_Line::ID());
232 // update attributes of projection
233 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
234 aProjection->attribute(SketchPlugin_Line::START_ID()));
235 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236 aProjection->attribute(SketchPlugin_Line::END_ID()));
237 aStartPnt->setValue(aFirstInSketch);
238 aEndPnt->setValue(aLastInSketch);
240 else if (anEdge->isCircle() || anEdge->isArc() || anEdge->isEllipse()) {
241 GeomAlgoAPI_Projection aProjAlgo(aSketchPlane);
242 GeomCurvePtr aProjectedCurve = aProjAlgo.project(anEdge);
244 if (aProjectedCurve->isCircle()) {
245 GeomAPI_Circ aCircle(aProjectedCurve);
246 GeomPointPtr aCenter = aSketchPlane->project(aCircle.center());
247 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
249 if (aProjectedCurve->isTrimmed()) {
250 // ARC is a projection
251 rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_Arc::ID());
253 GeomPointPtr aFirst = aProjectedCurve->getPoint(aProjectedCurve->startParam());
254 GeomPointPtr aLast = aProjectedCurve->getPoint(aProjectedCurve->endParam());
255 GeomPnt2dPtr aFirstInSketch = sketch()->to2D(aSketchPlane->project(aFirst));
256 GeomPnt2dPtr aLastInSketch = sketch()->to2D(aSketchPlane->project(aLast));
258 double aNormalsDot = aCircle.normal()->dot(aSketchPlane->direction());
259 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
260 return; // arc is not in the plane, parallel to the sketch plane
262 bool isInversed = aNormalsDot < 0.;
264 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
266 // update attributes of projection
267 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
268 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
269 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
270 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
271 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
272 aProjection->attribute(SketchPlugin_Arc::START_ID()));
273 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
274 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
275 aProjection->attribute(SketchPlugin_Arc::END_ID()));
276 aStartPnt->setValue(aFirstInSketch);
277 aEndPnt->setValue(aLastInSketch);
278 aCenterPnt->setValue(aCenterInSketch);
279 aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
281 aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
284 // CIRCLE is a projection
285 rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
286 SketchPlugin_Circle::ID());
288 // update attributes of projection
289 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
290 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
291 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
292 aCenterPnt->setValue(aCenterInSketch);
293 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aCircle.radius());
296 else if (aProjectedCurve->isEllipse()) {
297 GeomAPI_Ellipse anEllipse(aProjectedCurve);
298 GeomPointPtr aCenter = aSketchPlane->project(anEllipse.center());
299 GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
300 GeomPointPtr aFocus = aSketchPlane->project(anEllipse.firstFocus());
301 GeomPnt2dPtr aFocusInSketch = sketch()->to2D(aFocus);
303 if (aProjectedCurve->isTrimmed()) {
304 // ELLIPTIC ARC is a projection
305 rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_EllipticArc::ID());
307 GeomPointPtr aFirst = aProjectedCurve->getPoint(aProjectedCurve->startParam());
308 GeomPointPtr aLast = aProjectedCurve->getPoint(aProjectedCurve->endParam());
309 GeomPnt2dPtr aFirstInSketch = sketch()->to2D(aSketchPlane->project(aFirst));
310 GeomPnt2dPtr aLastInSketch = sketch()->to2D(aSketchPlane->project(aLast));
312 double aNormalsDot = anEllipse.normal()->dot(aSketchPlane->direction());
313 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
314 return; // arc is not in the plane, parallel to the sketch plane
316 bool isInversed = aNormalsDot < 0.;
318 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
320 // update attributes of projection
321 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
322 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
323 aProjection->attribute(SketchPlugin_EllipticArc::CENTER_ID()));
324 std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
325 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
326 aProjection->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()));
327 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
328 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
329 aProjection->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
330 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
331 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
332 aProjection->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
333 aStartPnt->setValue(aFirstInSketch);
334 aEndPnt->setValue(aLastInSketch);
335 aCenterPnt->setValue(aCenterInSketch);
336 aFocusPnt->setValue(aFocusInSketch);
337 aProjection->boolean(SketchPlugin_EllipticArc::REVERSED_ID())->setValue(isInversed);
339 aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
342 // ELLIPSE is a projection
343 rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
344 SketchPlugin_Ellipse::ID());
346 // update attributes of projection
347 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
348 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
349 aProjection->attribute(SketchPlugin_Ellipse::CENTER_ID()));
350 aCenterPnt->setValue(aCenterInSketch);
351 std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
352 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
353 aProjection->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
354 aFocusPnt->setValue(aFocusInSketch);
355 aProjection->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(
356 anEllipse.minorRadius());
364 aProjection->boolean(COPY_ID())->setValue(true);
365 aProjection->execute();
366 aRefAttr->setObject(aProjection);
368 restoreCurrentFeature();
370 if (theID == EXTERNAL_FEATURE_ID()) {
371 selection(EXTERNAL_ID())->selectValue(aExtFeature);
374 aResult->setShape(aProjection->lastResult()->shape());
376 GeomShapePtr anEmptyVal;
377 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
379 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_VISUAL_ATTRIBUTES);
380 ModelAPI_EventCreator::get()->sendUpdated(aProjection, anEvent, false);
385 bool SketchPlugin_Projection::rebuildProjectedFeature(
386 FeaturePtr& theProjection,
387 const std::set<std::string>& theSupportedTypes,
388 const std::string& theRequestedFeature)
390 bool isRebuild = false;
392 (theSupportedTypes.find(theProjection->getKind()) == theSupportedTypes.end() ||
393 (!theRequestedFeature.empty() && theProjection->getKind() != theRequestedFeature))) {
394 DocumentPtr aDoc = sketch()->document();
396 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
397 aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
398 std::set<FeaturePtr> aFeaturesToBeRemoved;
399 aFeaturesToBeRemoved.insert(theProjection);
400 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
401 theProjection = FeaturePtr();
402 aRefAttr->setObject(theProjection);
406 if (!theProjection && !theRequestedFeature.empty())
407 theProjection = sketch()->addFeature(theRequestedFeature);