Salome HOME
Avoid crash in the Projection feature
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Projection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_Projection.cpp
4 // Created: 07 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include <SketchPlugin_Projection.h>
8
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Sketch.h>
13 #include <SketchPlugin_ConstraintRigid.h>
14
15 #include <ModelAPI_AttributeRefAttr.h>
16 #include <ModelAPI_AttributeSelection.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_ResultConstruction.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <GeomAPI_Circ.h>
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Lin.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Pnt2d.h>
27 #include <GeomAlgoAPI_EdgeBuilder.h>
28 #include <GeomDataAPI_Point2D.h>
29
30 #include <cmath>
31
32 static const double tolerance = 1.e-7;
33
34 SketchPlugin_Projection::SketchPlugin_Projection()
35     : SketchPlugin_SketchEntity(),
36       myIsComputing(false)
37 {
38 }
39
40 void SketchPlugin_Projection::initDerivedClassAttributes()
41 {
42   data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
43   data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
44   data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
45
46   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
47   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
48
49   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
50 }
51
52 void SketchPlugin_Projection::execute()
53 {
54   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
55   if (!aRefAttr || !aRefAttr->isInitialized())
56     return;
57   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
58
59   if (!lastResult().get() && aProjection->lastResult().get()) {
60     ResultConstructionPtr aConstr = document()->createConstruction(data());
61     aConstr->setShape(aProjection->lastResult()->shape());
62     aConstr->setIsInHistory(false);
63     aConstr->setDisplayed(false);
64     setResult(aConstr);
65
66     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
67   }
68 }
69
70 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
71 {
72   // feature cannot be moved
73 }
74
75 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
76 {
77   if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
78     myIsComputing = true;
79     computeProjection(theID);
80     myIsComputing = false;
81   }
82 }
83
84 void SketchPlugin_Projection::computeProjection(const std::string& theID)
85 {
86   AttributeSelectionPtr aExtFeature =
87       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
88
89   std::shared_ptr<GeomAPI_Edge> anEdge;
90   if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
91     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
92   } else if (aExtFeature->context() && aExtFeature->context()->shape() && aExtFeature->context()->shape()->isEdge()) {
93     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
94   }
95   if (!anEdge.get())
96     return;
97
98   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
99   FeaturePtr aProjection;
100   if (aRefAttr && aRefAttr->isInitialized())
101     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
102
103   // if the type of feature differs with already selected, remove it and create once again
104   bool hasPrevProj = aProjection.get() != 0;
105   if (hasPrevProj) {
106     if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
107         (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
108         (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
109       DocumentPtr aDoc = sketch()->document();
110       aDoc->removeFeature(aProjection);
111       aProjection = FeaturePtr();
112       aRefAttr->setObject(aProjection);
113     }
114   }
115
116   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
117
118   ResultConstructionPtr aResult =
119       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
120   if (aResult && aResult->shape()) {
121     aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
122     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
123   }
124
125   if (anEdge->isLine()) {
126     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
127     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
128
129     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
130     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
131     if (aFirstInSketch->distance(aLastInSketch) < tolerance)
132       return; // line is semi-orthogonal to the sketch plane
133
134     if (!hasPrevProj)
135       aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
136
137     // update attributes of projection
138     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
139         aProjection->attribute(SketchPlugin_Line::START_ID()));
140     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141         aProjection->attribute(SketchPlugin_Line::END_ID()));
142     aStartPnt->setValue(aFirstInSketch);
143     aEndPnt->setValue(aLastInSketch);
144   }
145   else if (anEdge->isCircle()) {
146     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
147     double aRadius = aCircle->radius();
148
149     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
150     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
151
152     if (!hasPrevProj)
153       aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
154
155     // update attributes of projection
156     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
157         aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
158     aCenterPnt->setValue(aCenterInSketch);
159     aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
160   }
161   else if (anEdge->isArc()) {
162     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
163     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
164     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
165     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
166
167     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
168     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
169     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
170
171     if (!hasPrevProj)
172       aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
173
174     // update attributes of projection
175     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176         aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
177     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178         aProjection->attribute(SketchPlugin_Arc::START_ID()));
179     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180         aProjection->attribute(SketchPlugin_Arc::END_ID()));
181     aStartPnt->setValue(aFirstInSketch);
182     aEndPnt->setValue(aLastInSketch);
183     aCenterPnt->setValue(aCenterInSketch);
184   }
185
186   aProjection->execute();
187   aRefAttr->setObject(aProjection);
188
189   if (!hasPrevProj) {
190     FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
191     aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
192     aFixed->execute();
193   }
194
195   if (theID == EXTERNAL_FEATURE_ID()) {
196     selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->context()->shape());
197
198     if (aResult) {
199       aResult->setShape(aProjection->lastResult()->shape());
200       setResult(aResult);
201       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
202     }
203   }
204 }