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