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>
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>
32 static const double tolerance = 1.e-7;
34 SketchPlugin_Projection::SketchPlugin_Projection()
35 : SketchPlugin_SketchEntity(),
40 void SketchPlugin_Projection::initDerivedClassAttributes()
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);
46 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
47 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
49 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
52 void SketchPlugin_Projection::execute()
54 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
55 if (!aRefAttr || !aRefAttr->isInitialized())
57 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
59 if (!lastResult().get()) {
60 ResultConstructionPtr aConstr = document()->createConstruction(data());
61 aConstr->setShape(aProjection->lastResult()->shape());
62 aConstr->setIsInHistory(false);
63 aConstr->setDisplayed(false);
66 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
70 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
72 // feature cannot be moved
75 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
77 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
79 computeProjection(theID);
80 myIsComputing = false;
84 void SketchPlugin_Projection::computeProjection(const std::string& theID)
86 AttributeSelectionPtr aExtFeature =
87 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
89 std::shared_ptr<GeomAPI_Edge> anEdge;
90 if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
91 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
92 } else if (aExtFeature->context() && aExtFeature->context()->shape() && aExtFeature->context()->shape()->isEdge()) {
93 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
98 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
99 FeaturePtr aProjection;
100 if (aRefAttr && aRefAttr->isInitialized())
101 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
103 // if the type of feature differs with already selected, remove it and create once again
104 bool hasPrevProj = aProjection.get() != 0;
106 if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
107 (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
108 (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
109 DocumentPtr aDoc = sketch()->document();
110 aDoc->removeFeature(aProjection);
111 aProjection = FeaturePtr();
112 aRefAttr->setObject(aProjection);
116 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
118 ResultConstructionPtr aResult =
119 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
120 if (aResult && aResult->shape()) {
121 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
122 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
125 if (anEdge->isLine()) {
126 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
127 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
129 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
130 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
131 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
132 return; // line is semi-orthogonal to the sketch plane
135 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
137 // update attributes of projection
138 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
139 aProjection->attribute(SketchPlugin_Line::START_ID()));
140 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141 aProjection->attribute(SketchPlugin_Line::END_ID()));
142 aStartPnt->setValue(aFirstInSketch);
143 aEndPnt->setValue(aLastInSketch);
145 else if (anEdge->isCircle()) {
146 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
147 double aRadius = aCircle->radius();
149 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
150 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
153 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
155 // update attributes of projection
156 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
157 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
158 aCenterPnt->setValue(aCenterInSketch);
159 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
161 else if (anEdge->isArc()) {
162 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
163 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
164 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
165 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
167 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
168 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
169 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
172 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
174 // update attributes of projection
175 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
177 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 aProjection->attribute(SketchPlugin_Arc::START_ID()));
179 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180 aProjection->attribute(SketchPlugin_Arc::END_ID()));
181 aStartPnt->setValue(aFirstInSketch);
182 aEndPnt->setValue(aLastInSketch);
183 aCenterPnt->setValue(aCenterInSketch);
186 aProjection->execute();
187 aRefAttr->setObject(aProjection);
190 FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
191 aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
195 if (theID == EXTERNAL_FEATURE_ID()) {
196 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->context()->shape());
199 aResult->setShape(aProjection->lastResult()->shape());
201 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());