Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[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_Line.h>
26 #include <SketchPlugin_Point.h>
27 #include <SketchPlugin_Sketch.h>
28 #include <SketchPlugin_ConstraintRigid.h>
29
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Tools.h>
37
38 #include <GeomAPI_Circ.h>
39 #include <GeomAPI_Edge.h>
40 #include <GeomAPI_Ellipse.h>
41 #include <GeomAPI_Lin.h>
42 #include <GeomAPI_Pnt.h>
43 #include <GeomAPI_Pnt2d.h>
44 #include <GeomAPI_Vertex.h>
45 #include <GeomAlgoAPI_EdgeBuilder.h>
46 #include <GeomAlgoAPI_Projection.h>
47 #include <GeomDataAPI_Point2D.h>
48
49 #include <cmath>
50
51 static const double tolerance = 1.e-7;
52
53 SketchPlugin_Projection::SketchPlugin_Projection()
54     : SketchPlugin_SketchEntity(),
55       myIsComputing(false)
56 {
57 }
58
59 void SketchPlugin_Projection::initDerivedClassAttributes()
60 {
61   data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
62   data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
63   data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
64
65   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
66   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
67
68   data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
69
70   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
71 }
72
73 void SketchPlugin_Projection::execute()
74 {
75   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
76   if (!aRefAttr || !aRefAttr->isInitialized())
77     return;
78   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
79
80   if (!lastResult().get()) {
81     bool hasProjResult = aProjection->lastResult().get() != NULL;
82     ResultConstructionPtr aConstr = document()->createConstruction(data());
83     if (hasProjResult)
84       aConstr->setShape(aProjection->lastResult()->shape());
85     aConstr->setIsInHistory(false);
86     aConstr->setDisplayed(false);
87     setResult(aConstr);
88
89     if (hasProjResult)
90       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
91   }
92
93   // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
94   // projection must be fully recomputed
95   computeProjection(EXTERNAL_FEATURE_ID());
96 }
97
98 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
99 {
100   if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
101     myIsComputing = true;
102     computeProjection(theID);
103     myIsComputing = false;
104   }
105 }
106
107 static const std::string& projectionType(GeomEdgePtr theEdge,
108                                          GeomVertexPtr theVertex)
109 {
110   if (theVertex)
111     return SketchPlugin_Point::ID();
112   if (theEdge) {
113     if (theEdge->isLine())
114       return SketchPlugin_Line::ID();
115     else if (theEdge->isCircle())
116       return SketchPlugin_Circle::ID();
117     else if (theEdge->isArc())
118       return SketchPlugin_Arc::ID();
119     else if (theEdge->isEllipse())
120       return SketchPlugin_Ellipse::ID();
121   }
122   static const std::string DUMMY;
123   return DUMMY;
124 }
125
126 void SketchPlugin_Projection::computeProjection(const std::string& theID)
127 {
128   AttributeSelectionPtr aExtFeature =
129       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
130
131   GeomShapePtr aShape;
132   GeomEdgePtr anEdge;
133   GeomVertexPtr aVertex;
134   if (aExtFeature)
135     aShape = aExtFeature->value();
136   if (!aShape && aExtFeature->context())
137     aShape = aExtFeature->context()->shape();
138   if (aShape) {
139     if (aShape->isEdge())
140       anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
141     else if (aShape->isVertex())
142       aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
143   }
144   if (!anEdge && !aVertex)
145     return;
146
147   const std::string& aProjType = projectionType(anEdge, aVertex);
148
149   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
150   FeaturePtr aProjection;
151   if (aRefAttr && aRefAttr->isInitialized())
152     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
153
154   // if the type of feature differs with already selected, remove it and create once again
155   bool isRebuild = rebuildProjectedFeature(aProjection, aProjType, true);
156
157   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
158
159   ResultConstructionPtr aResult =
160       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
161   if (!isRebuild && aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
162     aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
163     if (aProjection)
164       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
165   }
166
167   keepCurrentFeature();
168
169   if (aVertex) {
170     std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
171     std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
172
173     rebuildProjectedFeature(aProjection, SketchPlugin_Point::ID());
174
175     // update coordinates of projection
176     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
177         aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
178   }
179   else if (anEdge->isLine()) {
180     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
181     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
182
183     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
184     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
185     if (aFirstInSketch->distance(aLastInSketch) < tolerance)
186       return; // line is semi-orthogonal to the sketch plane
187
188     rebuildProjectedFeature(aProjection, SketchPlugin_Line::ID());
189
190     // update attributes of projection
191     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192         aProjection->attribute(SketchPlugin_Line::START_ID()));
193     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194         aProjection->attribute(SketchPlugin_Line::END_ID()));
195     aStartPnt->setValue(aFirstInSketch);
196     aEndPnt->setValue(aLastInSketch);
197   }
198   else if (anEdge->isCircle() || anEdge->isEllipse()) {
199     GeomAlgoAPI_Projection aProjAlgo(aSketchPlane);
200     GeomCurvePtr aProjectedCurve = aProjAlgo.project(anEdge);
201
202     if (aProjectedCurve->isCircle()) {
203       rebuildProjectedFeature(aProjection, SketchPlugin_Circle::ID());
204
205       GeomAPI_Circ aCircle(aProjectedCurve);
206       std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle.center());
207       std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
208
209       // update attributes of projection
210       std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
211           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
212           aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
213       aCenterPnt->setValue(aCenterInSketch);
214       aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aCircle.radius());
215     }
216     else if (aProjectedCurve->isEllipse()) {
217       rebuildProjectedFeature(aProjection, SketchPlugin_Ellipse::ID());
218
219       GeomAPI_Ellipse anEllipse(aProjectedCurve);
220       std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(anEllipse.center());
221       std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
222       std::shared_ptr<GeomAPI_Pnt> aFocus = aSketchPlane->project(anEllipse.firstFocus());
223       std::shared_ptr<GeomAPI_Pnt2d> aFocusInSketch = sketch()->to2D(aFocus);
224
225       // update attributes of projection
226       std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
227           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
228           aProjection->attribute(SketchPlugin_Ellipse::CENTER_ID()));
229       aCenterPnt->setValue(aCenterInSketch);
230       std::shared_ptr<GeomDataAPI_Point2D> aFocusPnt =
231           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
232           aProjection->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
233       aFocusPnt->setValue(aFocusInSketch);
234       aProjection->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(
235           anEllipse.minorRadius());
236     }
237     else
238       return;
239   }
240   else if (anEdge->isArc()) {
241     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
242     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
243     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
244     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
245
246     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
247     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
248     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
249
250     double aNormalsDot = aCircle->normal()->dot(aSketchPlane->direction());
251     if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
252       return; // arc is not in the plane, parallel to the sketch plane
253
254     bool isInversed = aNormalsDot < 0.;
255
256     rebuildProjectedFeature(aProjection, SketchPlugin_Arc::ID());
257
258     bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
259
260     // update attributes of projection
261     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
262       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
263         aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
264     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
265       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
266         aProjection->attribute(SketchPlugin_Arc::START_ID()));
267     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
268       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
269         aProjection->attribute(SketchPlugin_Arc::END_ID()));
270     aStartPnt->setValue(aFirstInSketch);
271     aEndPnt->setValue(aLastInSketch);
272     aCenterPnt->setValue(aCenterInSketch);
273     aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
274
275     aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
276   } else
277     return;
278
279   aProjection->boolean(COPY_ID())->setValue(true);
280   aProjection->execute();
281   aRefAttr->setObject(aProjection);
282
283   restoreCurrentFeature();
284
285   if (theID == EXTERNAL_FEATURE_ID()) {
286     selection(EXTERNAL_ID())->selectValue(aExtFeature);
287
288     if (aResult) {
289       aResult->setShape(aProjection->lastResult()->shape());
290       setResult(aResult);
291       GeomShapePtr anEmptyVal;
292       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
293     }
294   }
295 }
296
297 bool SketchPlugin_Projection::rebuildProjectedFeature(FeaturePtr& theProjection,
298                                                       const std::string& theResultType,
299                                                       bool theRemoveOnly)
300 {
301   bool isRebuild = false;
302   if (theProjection && theProjection->getKind() != theResultType) {
303     DocumentPtr aDoc = sketch()->document();
304
305     AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
306     aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
307     std::set<FeaturePtr> aFeaturesToBeRemoved;
308     aFeaturesToBeRemoved.insert(theProjection);
309     ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
310     theProjection = FeaturePtr();
311     aRefAttr->setObject(theProjection);
312     isRebuild = true;
313   }
314
315   if (!theProjection && !theRemoveOnly)
316     theProjection = sketch()->addFeature(theResultType);
317   return isRebuild;
318 }