Salome HOME
Fix the naming problems for the sub-solids sub elements selection
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Projection.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SketchPlugin_Projection.h>
22
23 #include <SketchPlugin_Arc.h>
24 #include <SketchPlugin_Circle.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_Lin.h>
41 #include <GeomAPI_Pnt.h>
42 #include <GeomAPI_Pnt2d.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomAlgoAPI_EdgeBuilder.h>
45 #include <GeomDataAPI_Point2D.h>
46
47 #include <cmath>
48
49 static const double tolerance = 1.e-7;
50
51 SketchPlugin_Projection::SketchPlugin_Projection()
52     : SketchPlugin_SketchEntity(),
53       myIsComputing(false)
54 {
55 }
56
57 void SketchPlugin_Projection::initDerivedClassAttributes()
58 {
59   data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
60   data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
61   data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
62
63   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
65
66   data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
67
68   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
69 }
70
71 void SketchPlugin_Projection::execute()
72 {
73   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
74   if (!aRefAttr || !aRefAttr->isInitialized())
75     return;
76   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
77
78   if (!lastResult().get()) {
79     bool hasProjResult = aProjection->lastResult().get() != NULL;
80     ResultConstructionPtr aConstr = document()->createConstruction(data());
81     if (hasProjResult)
82       aConstr->setShape(aProjection->lastResult()->shape());
83     aConstr->setIsInHistory(false);
84     aConstr->setDisplayed(false);
85     setResult(aConstr);
86
87     if (hasProjResult)
88       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
89   }
90
91   // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
92   // projection must be fully recomputed
93   computeProjection(EXTERNAL_FEATURE_ID());
94 }
95
96 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
97 {
98   if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
99     myIsComputing = true;
100     computeProjection(theID);
101     myIsComputing = false;
102   }
103 }
104
105 static bool isValidProjectionType(FeaturePtr theProjection,
106                                   GeomEdgePtr theEdge,
107                                   GeomVertexPtr theVertex)
108 {
109   if (theVertex && theProjection->getKind() == SketchPlugin_Point::ID())
110     return true;
111   if (theEdge) {
112     if (theEdge->isLine() && theProjection->getKind() == SketchPlugin_Line::ID())
113       return true;
114     else if (theEdge->isCircle() && theProjection->getKind() == SketchPlugin_Circle::ID())
115       return true;
116     else if (theEdge->isArc() && theProjection->getKind() == SketchPlugin_Arc::ID())
117       return true;
118   }
119   return false;
120 }
121
122 void SketchPlugin_Projection::computeProjection(const std::string& theID)
123 {
124   AttributeSelectionPtr aExtFeature =
125       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
126
127   GeomShapePtr aShape;
128   GeomEdgePtr anEdge;
129   GeomVertexPtr aVertex;
130   if (aExtFeature)
131     aShape = aExtFeature->value();
132   if (!aShape && aExtFeature->context())
133     aShape = aExtFeature->context()->shape();
134   if (aShape) {
135     if (aShape->isEdge())
136       anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
137     else if (aShape->isVertex())
138       aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
139   }
140   if (!anEdge && !aVertex)
141     return;
142
143   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
144   FeaturePtr aProjection;
145   if (aRefAttr && aRefAttr->isInitialized())
146     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
147
148   // if the type of feature differs with already selected, remove it and create once again
149   bool hasPrevProj = aProjection.get() != 0;
150   if (hasPrevProj && !isValidProjectionType(aProjection, anEdge, aVertex)) {
151     DocumentPtr aDoc = sketch()->document();
152
153     aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
154     std::set<FeaturePtr> aFeaturesToBeRemoved;
155     aFeaturesToBeRemoved.insert(aProjection);
156     ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
157     aProjection = FeaturePtr();
158     aRefAttr->setObject(aProjection);
159     hasPrevProj = false;
160   }
161
162   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
163
164   ResultConstructionPtr aResult =
165       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
166   if (aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
167     aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
168     if (aProjection)
169       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
170   }
171
172   if (aVertex) {
173     std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
174     std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
175
176     if (!hasPrevProj)
177       aProjection = sketch()->addFeature(SketchPlugin_Point::ID());
178
179     // update coordinates of projection
180     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181         aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
182   }
183   else if (anEdge->isLine()) {
184     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
185     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
186
187     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
188     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
189     if (aFirstInSketch->distance(aLastInSketch) < tolerance)
190       return; // line is semi-orthogonal to the sketch plane
191
192     if (!hasPrevProj)
193       aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
194
195     // update attributes of projection
196     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
197         aProjection->attribute(SketchPlugin_Line::START_ID()));
198     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
199         aProjection->attribute(SketchPlugin_Line::END_ID()));
200     aStartPnt->setValue(aFirstInSketch);
201     aEndPnt->setValue(aLastInSketch);
202   }
203   else if (anEdge->isCircle()) {
204     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
205     double aRadius = aCircle->radius();
206
207     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
208     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
209
210     if (!hasPrevProj)
211       aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
212
213     // update attributes of projection
214     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
215       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
216         aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
217     aCenterPnt->setValue(aCenterInSketch);
218     aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
219   }
220   else if (anEdge->isArc()) {
221     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
222     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
223     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
224     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
225
226     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
227     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
228     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
229
230     bool isInversed = aCircle->normal()->dot(aSketchPlane->direction()) < 0.;
231
232     if (!hasPrevProj)
233       aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
234
235     bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
236
237     // update attributes of projection
238     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
239       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
240         aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
241     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
242       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
243         aProjection->attribute(SketchPlugin_Arc::START_ID()));
244     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
245       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
246         aProjection->attribute(SketchPlugin_Arc::END_ID()));
247     aStartPnt->setValue(aFirstInSketch);
248     aEndPnt->setValue(aLastInSketch);
249     aCenterPnt->setValue(aCenterInSketch);
250     aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
251
252     aProjection->data()->blockSendAttributeUpdated(aWasBlocked, false);
253   } else
254     return;
255
256   aProjection->boolean(COPY_ID())->setValue(true);
257   aProjection->execute();
258   aRefAttr->setObject(aProjection);
259
260   if (theID == EXTERNAL_FEATURE_ID()) {
261     selection(EXTERNAL_ID())->selectValue(aExtFeature);
262
263     if (aResult) {
264       aResult->setShape(aProjection->lastResult()->shape());
265       setResult(aResult);
266       GeomShapePtr anEmptyVal;
267       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
268     }
269   }
270 }