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() && aExtFeature->context()->shape()->isEdge()) {
98 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
103 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
104 FeaturePtr aProjection;
105 if (aRefAttr && aRefAttr->isInitialized())
106 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
108 // if the type of feature differs with already selected, remove it and create once again
109 bool hasPrevProj = aProjection.get() != 0;
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();
116 std::set<FeaturePtr> aFeaturesToBeRemoved;
117 aFeaturesToBeRemoved.insert(aProjection);
118 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
119 aProjection = FeaturePtr();
120 aRefAttr->setObject(aProjection);
124 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
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());
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());
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
143 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
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);
153 else if (anEdge->isCircle()) {
154 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
155 double aRadius = aCircle->radius();
157 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
158 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
161 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
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);
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);
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);
180 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
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);
194 aProjection->boolean(COPY_ID())->setValue(true);
195 aProjection->execute();
196 aRefAttr->setObject(aProjection);
199 FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
200 aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
204 if (theID == EXTERNAL_FEATURE_ID()) {
205 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
208 aResult->setShape(aProjection->lastResult()->shape());
210 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());