1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Projection.cpp
4 // Created: 07 May 2014
5 // Author: Artem ZHIDKOV
7 #include <SketchPlugin_Projection.h>
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>
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>
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>
33 static const double tolerance = 1.e-7;
35 SketchPlugin_Projection::SketchPlugin_Projection()
36 : SketchPlugin_SketchEntity(),
41 void SketchPlugin_Projection::initDerivedClassAttributes()
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);
47 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
48 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
50 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
53 void SketchPlugin_Projection::execute()
55 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
56 if (!aRefAttr || !aRefAttr->isInitialized())
58 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
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);
67 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
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());
75 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
77 // feature cannot be moved
80 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
82 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
84 computeProjection(theID);
85 myIsComputing = false;
89 void SketchPlugin_Projection::computeProjection(const std::string& theID)
91 AttributeSelectionPtr aExtFeature =
92 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
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()));
104 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
105 FeaturePtr aProjection;
106 if (aRefAttr && aRefAttr->isInitialized())
107 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
109 // if the type of feature differs with already selected, remove it and create once again
110 bool hasPrevProj = aProjection.get() != 0;
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();
117 std::set<FeaturePtr> aFeaturesToBeRemoved;
118 aFeaturesToBeRemoved.insert(aProjection);
119 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
120 aProjection = FeaturePtr();
121 aRefAttr->setObject(aProjection);
125 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
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());
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());
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
144 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
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);
154 else if (anEdge->isCircle()) {
155 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
156 double aRadius = aCircle->radius();
158 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
159 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
162 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
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);
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);
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);
182 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
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);
199 aProjection->boolean(COPY_ID())->setValue(true);
200 aProjection->execute();
201 aRefAttr->setObject(aProjection);
203 if (theID == EXTERNAL_FEATURE_ID()) {
204 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
207 aResult->setShape(aProjection->lastResult()->shape());
209 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());