Salome HOME
79eb675ca240ca273480dd8d6d4c92ed3b8f08d9
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Projection.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchPlugin_Projection.h>
21
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>
30
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
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>
49
50 #include <cmath>
51
52 static const double tolerance = 1.e-7;
53
54 SketchPlugin_Projection::SketchPlugin_Projection()
55     : SketchPlugin_SketchEntity(),
56       myIsComputing(false)
57 {
58 }
59
60 void SketchPlugin_Projection::initDerivedClassAttributes()
61 {
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);
65
66   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
67   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
68
69   data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
70
71   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
72 }
73
74 void SketchPlugin_Projection::execute()
75 {
76   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
77   if (!aRefAttr || !aRefAttr->isInitialized())
78     return;
79   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
80
81   if (!lastResult().get()) {
82     bool hasProjResult = aProjection->lastResult().get() != NULL;
83     ResultConstructionPtr aConstr = document()->createConstruction(data());
84     if (hasProjResult)
85       aConstr->setShape(aProjection->lastResult()->shape());
86     aConstr->setIsInHistory(false);
87     aConstr->setDisplayed(false);
88     setResult(aConstr);
89
90     if (hasProjResult)
91       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
92   }
93
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());
97 }
98
99 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
100 {
101   if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
102     myIsComputing = true;
103     computeProjection(theID);
104     myIsComputing = false;
105   }
106 }
107
108 static const std::set<std::string>& POINT_PROJECTION()
109 {
110   static std::set<std::string> aProj;
111   if (aProj.empty())
112     aProj.insert(SketchPlugin_Point::ID());
113   return aProj;
114 }
115
116 static const std::set<std::string>& LINE_PROJECTION()
117 {
118   static std::set<std::string> aProj;
119   if (aProj.empty())
120     aProj.insert(SketchPlugin_Line::ID());
121   return aProj;
122 }
123
124 static const std::set<std::string>& CIRCLE_ELLIPSE_PROJECTION()
125 {
126   static std::set<std::string> aProj;
127   if (aProj.empty()) {
128     aProj.insert(SketchPlugin_Circle::ID());
129     aProj.insert(SketchPlugin_Ellipse::ID());
130   }
131   return aProj;
132 }
133
134 static const std::set<std::string>& ARC_PROJECTION()
135 {
136   static std::set<std::string> aProj;
137   if (aProj.empty()) {
138     aProj.insert(SketchPlugin_Arc::ID());
139     aProj.insert(SketchPlugin_EllipticArc::ID());
140   }
141   return aProj;
142 }
143
144
145 static const std::set<std::string>& possibleProjectionTypes(GeomEdgePtr theEdge,
146                                                             GeomVertexPtr theVertex)
147 {
148   if (theVertex)
149     return POINT_PROJECTION();
150   if (theEdge) {
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();
156       else
157         return ARC_PROJECTION();
158     }
159   }
160   static const std::set<std::string> DUMMY;
161   return DUMMY;
162 }
163
164 void SketchPlugin_Projection::computeProjection(const std::string& theID)
165 {
166   AttributeSelectionPtr aExtFeature =
167       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
168
169   GeomShapePtr aShape;
170   GeomEdgePtr anEdge;
171   GeomVertexPtr aVertex;
172   if (aExtFeature)
173     aShape = aExtFeature->value();
174   if (!aShape && aExtFeature->context())
175     aShape = aExtFeature->context()->shape();
176   if (aShape) {
177     if (aShape->isEdge())
178       anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
179     else if (aShape->isVertex())
180       aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
181   }
182   if (!anEdge && !aVertex)
183     return;
184
185   const std::set<std::string>& aProjType = possibleProjectionTypes(anEdge, aVertex);
186
187   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
188   FeaturePtr aProjection;
189   if (aRefAttr && aRefAttr->isInitialized())
190     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
191
192   // if the type of feature differs with already selected, remove it and create once again
193   bool isRebuild = rebuildProjectedFeature(aProjection, aProjType);
194
195   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
196
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>());
201     if (aProjection)
202       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
203   }
204
205   keepCurrentFeature();
206
207   if (aVertex) {
208     std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
209     std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
210
211     rebuildProjectedFeature(aProjection, POINT_PROJECTION(), SketchPlugin_Point::ID());
212
213     // update coordinates of projection
214     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215         aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
216   }
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());
220
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
225
226     rebuildProjectedFeature(aProjection, LINE_PROJECTION(), SketchPlugin_Line::ID());
227
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);
235   }
236   else if (anEdge->isCircle() || anEdge->isArc() || anEdge->isEllipse()) {
237     GeomAlgoAPI_Projection aProjAlgo(aSketchPlane);
238     GeomCurvePtr aProjectedCurve = aProjAlgo.project(anEdge);
239
240     if (aProjectedCurve->isCircle()) {
241       GeomAPI_Circ aCircle(aProjectedCurve);
242       GeomPointPtr aCenter = aSketchPlane->project(aCircle.center());
243       GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
244
245       if (aProjectedCurve->isTrimmed()) {
246         // ARC is a projection
247         rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_Arc::ID());
248
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));
253
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
257
258         bool isInversed = aNormalsDot < 0.;
259
260         bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
261
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);
276
277         aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
278       }
279       else {
280         // CIRCLE is a projection
281         rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
282                                 SketchPlugin_Circle::ID());
283
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());
290       }
291     }
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);
298
299       if (aProjectedCurve->isTrimmed()) {
300         // ELLIPTIC ARC is a projection
301         rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_EllipticArc::ID());
302
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));
307
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
311
312         bool isInversed = aNormalsDot < 0.;
313
314         bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
315
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);
334
335         aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
336       }
337       else {
338         // ELLIPSE is a projection
339         rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
340                                 SketchPlugin_Ellipse::ID());
341
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());
353       }
354     }
355     else
356       return;
357   } else
358     return;
359
360   aProjection->boolean(COPY_ID())->setValue(true);
361   aProjection->execute();
362   aRefAttr->setObject(aProjection);
363
364   restoreCurrentFeature();
365
366   if (theID == EXTERNAL_FEATURE_ID()) {
367     selection(EXTERNAL_ID())->selectValue(aExtFeature);
368
369     if (aResult) {
370       aResult->setShape(aProjection->lastResult()->shape());
371       setResult(aResult);
372       GeomShapePtr anEmptyVal;
373       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
374     }
375   }
376 }
377
378 bool SketchPlugin_Projection::rebuildProjectedFeature(
379     FeaturePtr& theProjection,
380     const std::set<std::string>& theSupportedTypes,
381     const std::string& theRequestedFeature)
382 {
383   bool isRebuild = false;
384   if (theProjection &&
385       (theSupportedTypes.find(theProjection->getKind()) == theSupportedTypes.end() ||
386       (!theRequestedFeature.empty() && theProjection->getKind() != theRequestedFeature))) {
387     DocumentPtr aDoc = sketch()->document();
388
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);
396     isRebuild = true;
397   }
398
399   if (!theProjection && !theRequestedFeature.empty())
400     theProjection = sketch()->addFeature(theRequestedFeature);
401   return isRebuild;
402 }