Salome HOME
Fix errors and adjust unit tests
[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
71 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
72 {
73   // feature cannot be moved
74 }
75
76 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
77 {
78   if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
79     myIsComputing = true;
80     computeProjection(theID);
81     myIsComputing = false;
82   }
83 }
84
85 void SketchPlugin_Projection::computeProjection(const std::string& theID)
86 {
87   AttributeSelectionPtr aExtFeature =
88       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
89
90   std::shared_ptr<GeomAPI_Edge> anEdge;
91   if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
92     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
93   } else if (aExtFeature->context() && aExtFeature->context()->shape() && aExtFeature->context()->shape()->isEdge()) {
94     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
95   }
96   if (!anEdge.get())
97     return;
98
99   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
100   FeaturePtr aProjection;
101   if (aRefAttr && aRefAttr->isInitialized())
102     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
103
104   // if the type of feature differs with already selected, remove it and create once again
105   bool hasPrevProj = aProjection.get() != 0;
106   if (hasPrevProj) {
107     if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
108         (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
109         (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
110       DocumentPtr aDoc = sketch()->document();
111
112       std::set<FeaturePtr> aFeaturesToBeRemoved;
113       aFeaturesToBeRemoved.insert(aProjection);
114       ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
115       aProjection = FeaturePtr();
116       aRefAttr->setObject(aProjection);
117     }
118   }
119
120   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
121
122   ResultConstructionPtr aResult =
123       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
124   if (aResult && aResult->shape()) {
125     aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
126     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
127   }
128
129   if (anEdge->isLine()) {
130     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
131     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
132
133     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
134     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
135     if (aFirstInSketch->distance(aLastInSketch) < tolerance)
136       return; // line is semi-orthogonal to the sketch plane
137
138     if (!hasPrevProj)
139       aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
140
141     // update attributes of projection
142     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143         aProjection->attribute(SketchPlugin_Line::START_ID()));
144     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145         aProjection->attribute(SketchPlugin_Line::END_ID()));
146     aStartPnt->setValue(aFirstInSketch);
147     aEndPnt->setValue(aLastInSketch);
148   }
149   else if (anEdge->isCircle()) {
150     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
151     double aRadius = aCircle->radius();
152
153     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
154     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
155
156     if (!hasPrevProj)
157       aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
158
159     // update attributes of projection
160     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161         aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
162     aCenterPnt->setValue(aCenterInSketch);
163     aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
164   }
165   else if (anEdge->isArc()) {
166     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
167     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
168     std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
169     std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
170
171     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
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_Arc::ID());
177
178     // update attributes of projection
179     std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180         aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
181     std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182         aProjection->attribute(SketchPlugin_Arc::START_ID()));
183     std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184         aProjection->attribute(SketchPlugin_Arc::END_ID()));
185     aStartPnt->setValue(aFirstInSketch);
186     aEndPnt->setValue(aLastInSketch);
187     aCenterPnt->setValue(aCenterInSketch);
188   }
189
190   aProjection->boolean(COPY_ID())->setValue(true);
191   aProjection->execute();
192   aRefAttr->setObject(aProjection);
193
194   if (!hasPrevProj) {
195     FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
196     aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
197     aFixed->execute();
198   }
199
200   if (theID == EXTERNAL_FEATURE_ID()) {
201     selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
202
203     if (aResult) {
204       aResult->setShape(aProjection->lastResult()->shape());
205       setResult(aResult);
206       aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
207     }
208   }
209 }