Salome HOME
a39d90d7c4337a2e1ca4214b8f6c63c4e410172b
[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() &&
98              aExtFeature->context()->shape()->isEdge()) {
99     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
100   }
101   if (!anEdge.get())
102     return;
103
104   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
105   FeaturePtr aProjection;
106   if (aRefAttr && aRefAttr->isInitialized())
107     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
108
109   // if the type of feature differs with already selected, remove it and create once again
110   bool hasPrevProj = aProjection.get() != 0;
111   if (hasPrevProj) {
112     if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
113         (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
114         (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
115       DocumentPtr aDoc = sketch()->document();
116
117       std::set<FeaturePtr> aFeaturesToBeRemoved;
118       aFeaturesToBeRemoved.insert(aProjection);
119       ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
120       aProjection = FeaturePtr();
121       aRefAttr->setObject(aProjection);
122     }
123   }
124
125   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
126
127   ResultConstructionPtr aResult =
128       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
129   if (aResult && aResult->shape()) {
130     aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
131     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
132   }
133
134   if (anEdge->isLine()) {
135     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
136     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
137
138     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
139     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
140     if (aFirstInSketch->distance(aLastInSketch) < tolerance)
141       return; // line is semi-orthogonal to the sketch plane
142
143     if (!hasPrevProj)
144       aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
145
146     // update attributes of projection
147     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148         aProjection->attribute(SketchPlugin_Line::START_ID()));
149     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150         aProjection->attribute(SketchPlugin_Line::END_ID()));
151     aStartPnt->setValue(aFirstInSketch);
152     aEndPnt->setValue(aLastInSketch);
153   }
154   else if (anEdge->isCircle()) {
155     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
156     double aRadius = aCircle->radius();
157
158     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
159     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
160
161     if (!hasPrevProj)
162       aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
163
164     // update attributes of projection
165     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
166       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
167         aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
168     aCenterPnt->setValue(aCenterInSketch);
169     aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
170   }
171   else if (anEdge->isArc()) {
172     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
173     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
174     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
175     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
176
177     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
178     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
179     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
180
181     if (!hasPrevProj)
182       aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
183
184     // update attributes of projection
185     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
186       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
187         aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
188     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
189       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190         aProjection->attribute(SketchPlugin_Arc::START_ID()));
191     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
192       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
193         aProjection->attribute(SketchPlugin_Arc::END_ID()));
194     aStartPnt->setValue(aFirstInSketch);
195     aEndPnt->setValue(aLastInSketch);
196     aCenterPnt->setValue(aCenterInSketch);
197   }
198
199   aProjection->boolean(COPY_ID())->setValue(true);
200   aProjection->execute();
201   aRefAttr->setObject(aProjection);
202
203   if (!hasPrevProj) {
204     FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
205     aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
206     aFixed->execute();
207   }
208
209   if (theID == EXTERNAL_FEATURE_ID()) {
210     selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
211
212     if (aResult) {
213       aResult->setShape(aProjection->lastResult()->shape());
214       setResult(aResult);
215       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
216     }
217   }
218 }