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