Salome HOME
ef83cce1d2db49d8a2b7fc0c3302de40b4908eaa
[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_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 <GeomAlgoAPI_EdgeBuilder.h>
43 #include <GeomDataAPI_Point2D.h>
44
45 #include <cmath>
46
47 static const double tolerance = 1.e-7;
48
49 SketchPlugin_Projection::SketchPlugin_Projection()
50     : SketchPlugin_SketchEntity(),
51       myIsComputing(false)
52 {
53 }
54
55 void SketchPlugin_Projection::initDerivedClassAttributes()
56 {
57   data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
58   data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
59   data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
60
61   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
63
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
65 }
66
67 void SketchPlugin_Projection::execute()
68 {
69   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
70   if (!aRefAttr || !aRefAttr->isInitialized())
71     return;
72   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
73
74   if (!lastResult().get() && aProjection->lastResult().get()) {
75     ResultConstructionPtr aConstr = document()->createConstruction(data());
76     aConstr->setShape(aProjection->lastResult()->shape());
77     aConstr->setIsInHistory(false);
78     aConstr->setDisplayed(false);
79     setResult(aConstr);
80
81     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
82   }
83
84   // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
85   // projection must be fully recomputed
86   computeProjection(EXTERNAL_FEATURE_ID());
87 }
88
89 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
90 {
91   // feature cannot be moved
92 }
93
94 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
95 {
96   if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
97     myIsComputing = true;
98     computeProjection(theID);
99     myIsComputing = false;
100   }
101 }
102
103 void SketchPlugin_Projection::computeProjection(const std::string& theID)
104 {
105   AttributeSelectionPtr aExtFeature =
106       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
107
108   std::shared_ptr<GeomAPI_Edge> anEdge;
109   if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
110     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
111   } else if (aExtFeature->context() && aExtFeature->context()->shape() &&
112              aExtFeature->context()->shape()->isEdge()) {
113     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
114   }
115   if (!anEdge.get())
116     return;
117
118   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
119   FeaturePtr aProjection;
120   if (aRefAttr && aRefAttr->isInitialized())
121     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
122
123   // if the type of feature differs with already selected, remove it and create once again
124   bool hasPrevProj = aProjection.get() != 0;
125   if (hasPrevProj) {
126     if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
127         (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
128         (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
129       DocumentPtr aDoc = sketch()->document();
130
131       std::set<FeaturePtr> aFeaturesToBeRemoved;
132       aFeaturesToBeRemoved.insert(aProjection);
133       ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
134       aProjection = FeaturePtr();
135       aRefAttr->setObject(aProjection);
136     }
137   }
138
139   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
140
141   ResultConstructionPtr aResult =
142       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
143   if (aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
144     aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
145     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
146   }
147
148   if (anEdge->isLine()) {
149     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
150     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
151
152     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
153     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
154     if (aFirstInSketch->distance(aLastInSketch) < tolerance)
155       return; // line is semi-orthogonal to the sketch plane
156
157     if (!hasPrevProj)
158       aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
159
160     // update attributes of projection
161     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162         aProjection->attribute(SketchPlugin_Line::START_ID()));
163     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164         aProjection->attribute(SketchPlugin_Line::END_ID()));
165     aStartPnt->setValue(aFirstInSketch);
166     aEndPnt->setValue(aLastInSketch);
167   }
168   else if (anEdge->isCircle()) {
169     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
170     double aRadius = aCircle->radius();
171
172     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
173     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
174
175     if (!hasPrevProj)
176       aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
177
178     // update attributes of projection
179     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
180       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181         aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
182     aCenterPnt->setValue(aCenterInSketch);
183     aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
184   }
185   else if (anEdge->isArc()) {
186     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
187     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
188     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
189     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
190
191     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
192     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
193     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
194
195     if (!hasPrevProj)
196       aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
197
198     // update attributes of projection
199     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
200       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
201         aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
202     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
203       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204         aProjection->attribute(SketchPlugin_Arc::START_ID()));
205     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
206       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
207         aProjection->attribute(SketchPlugin_Arc::END_ID()));
208     aStartPnt->setValue(aFirstInSketch);
209     aEndPnt->setValue(aLastInSketch);
210     aCenterPnt->setValue(aCenterInSketch);
211   }
212
213   aProjection->boolean(COPY_ID())->setValue(true);
214   aProjection->execute();
215   aRefAttr->setObject(aProjection);
216
217   if (theID == EXTERNAL_FEATURE_ID()) {
218     selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
219
220     if (aResult) {
221       aResult->setShape(aProjection->lastResult()->shape());
222       setResult(aResult);
223       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
224     }
225   }
226 }