Salome HOME
e04031fee6db7cee731dd447ce225900018759b9
[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 #include <ModelAPI_Events.h>
39
40 #include <Events_Loop.h>
41
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>
52
53 #include <cmath>
54
55 static const double tolerance = 1.e-7;
56
57
58 SketchPlugin_Projection::SketchPlugin_Projection()
59     : SketchPlugin_SketchEntity(),
60       myIsComputing(false)
61 {
62 }
63
64 void SketchPlugin_Projection::initDerivedClassAttributes()
65 {
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);
69
70   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
71   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
72
73   data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
74
75   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
76 }
77
78 void SketchPlugin_Projection::execute()
79 {
80   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
81   if (!aRefAttr || !aRefAttr->isInitialized())
82     return;
83   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
84
85   if (!lastResult().get()) {
86     bool hasProjResult = aProjection->lastResult().get() != NULL;
87     ResultConstructionPtr aConstr = document()->createConstruction(data());
88     if (hasProjResult)
89       aConstr->setShape(aProjection->lastResult()->shape());
90     aConstr->setIsInHistory(false);
91     aConstr->setDisplayed(false);
92     setResult(aConstr);
93
94     if (hasProjResult)
95       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
96   }
97
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());
101 }
102
103 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
104 {
105   if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
106     myIsComputing = true;
107     computeProjection(theID);
108     myIsComputing = false;
109   }
110 }
111
112 static const std::set<std::string>& POINT_PROJECTION()
113 {
114   static std::set<std::string> aProj;
115   if (aProj.empty())
116     aProj.insert(SketchPlugin_Point::ID());
117   return aProj;
118 }
119
120 static const std::set<std::string>& LINE_PROJECTION()
121 {
122   static std::set<std::string> aProj;
123   if (aProj.empty())
124     aProj.insert(SketchPlugin_Line::ID());
125   return aProj;
126 }
127
128 static const std::set<std::string>& CIRCLE_ELLIPSE_PROJECTION()
129 {
130   static std::set<std::string> aProj;
131   if (aProj.empty()) {
132     aProj.insert(SketchPlugin_Circle::ID());
133     aProj.insert(SketchPlugin_Ellipse::ID());
134   }
135   return aProj;
136 }
137
138 static const std::set<std::string>& ARC_PROJECTION()
139 {
140   static std::set<std::string> aProj;
141   if (aProj.empty()) {
142     aProj.insert(SketchPlugin_Arc::ID());
143     aProj.insert(SketchPlugin_EllipticArc::ID());
144   }
145   return aProj;
146 }
147
148
149 static const std::set<std::string>& possibleProjectionTypes(GeomEdgePtr theEdge,
150                                                             GeomVertexPtr theVertex)
151 {
152   if (theVertex)
153     return POINT_PROJECTION();
154   if (theEdge) {
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();
160       else
161         return ARC_PROJECTION();
162     }
163   }
164   static const std::set<std::string> DUMMY;
165   return DUMMY;
166 }
167
168 void SketchPlugin_Projection::computeProjection(const std::string& theID)
169 {
170   AttributeSelectionPtr aExtFeature =
171       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
172
173   GeomShapePtr aShape;
174   GeomEdgePtr anEdge;
175   GeomVertexPtr aVertex;
176   if (aExtFeature)
177     aShape = aExtFeature->value();
178   if (!aShape && aExtFeature->context())
179     aShape = aExtFeature->context()->shape();
180   if (aShape) {
181     if (aShape->isEdge())
182       anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
183     else if (aShape->isVertex())
184       aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
185   }
186   if (!anEdge && !aVertex)
187     return;
188
189   const std::set<std::string>& aProjType = possibleProjectionTypes(anEdge, aVertex);
190
191   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
192   FeaturePtr aProjection;
193   if (aRefAttr && aRefAttr->isInitialized())
194     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
195
196   // if the type of feature differs with already selected, remove it and create once again
197   bool isRebuild = rebuildProjectedFeature(aProjection, aProjType);
198
199   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
200
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>());
205     if (aProjection)
206       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
207   }
208
209   keepCurrentFeature();
210
211   if (aVertex) {
212     std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
213     std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
214
215     rebuildProjectedFeature(aProjection, POINT_PROJECTION(), SketchPlugin_Point::ID());
216
217     // update coordinates of projection
218     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219         aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
220   }
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());
224
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
229
230     rebuildProjectedFeature(aProjection, LINE_PROJECTION(), SketchPlugin_Line::ID());
231
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);
239   }
240   else if (anEdge->isCircle() || anEdge->isArc() || anEdge->isEllipse()) {
241     GeomAlgoAPI_Projection aProjAlgo(aSketchPlane);
242     GeomCurvePtr aProjectedCurve = aProjAlgo.project(anEdge);
243
244     if (aProjectedCurve->isCircle()) {
245       GeomAPI_Circ aCircle(aProjectedCurve);
246       GeomPointPtr aCenter = aSketchPlane->project(aCircle.center());
247       GeomPnt2dPtr aCenterInSketch = sketch()->to2D(aCenter);
248
249       if (aProjectedCurve->isTrimmed()) {
250         // ARC is a projection
251         rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_Arc::ID());
252
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));
257
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
261
262         bool isInversed = aNormalsDot < 0.;
263
264         bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
265
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);
280
281         aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
282       }
283       else {
284         // CIRCLE is a projection
285         rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
286                                 SketchPlugin_Circle::ID());
287
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());
294       }
295     }
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);
302
303       if (aProjectedCurve->isTrimmed()) {
304         // ELLIPTIC ARC is a projection
305         rebuildProjectedFeature(aProjection, ARC_PROJECTION(), SketchPlugin_EllipticArc::ID());
306
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));
311
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
315
316         bool isInversed = aNormalsDot < 0.;
317
318         bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
319
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);
338
339         aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
340       }
341       else {
342         // ELLIPSE is a projection
343         rebuildProjectedFeature(aProjection, CIRCLE_ELLIPSE_PROJECTION(),
344                                 SketchPlugin_Ellipse::ID());
345
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());
357       }
358     }
359     else
360       return;
361   } else
362     return;
363
364   aProjection->boolean(COPY_ID())->setValue(true);
365   aProjection->execute();
366   aRefAttr->setObject(aProjection);
367
368   restoreCurrentFeature();
369
370   if (theID == EXTERNAL_FEATURE_ID()) {
371     selection(EXTERNAL_ID())->selectValue(aExtFeature);
372
373     if (aResult) {
374       aResult->setShape(aProjection->lastResult()->shape());
375       setResult(aResult);
376       GeomShapePtr anEmptyVal;
377       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
378
379       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_VISUAL_ATTRIBUTES);
380       ModelAPI_EventCreator::get()->sendUpdated(aProjection, anEvent, false);
381     }
382   }
383 }
384
385 bool SketchPlugin_Projection::rebuildProjectedFeature(
386     FeaturePtr& theProjection,
387     const std::set<std::string>& theSupportedTypes,
388     const std::string& theRequestedFeature)
389 {
390   bool isRebuild = false;
391   if (theProjection &&
392       (theSupportedTypes.find(theProjection->getKind()) == theSupportedTypes.end() ||
393       (!theRequestedFeature.empty() && theProjection->getKind() != theRequestedFeature))) {
394     DocumentPtr aDoc = sketch()->document();
395
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);
403     isRebuild = true;
404   }
405
406   if (!theProjection && !theRequestedFeature.empty())
407     theProjection = sketch()->addFeature(theRequestedFeature);
408   return isRebuild;
409 }