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