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