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);
65 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
69 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
71 // feature cannot be moved
74 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
76 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
78 computeProjection(theID);
79 myIsComputing = false;
83 void SketchPlugin_Projection::computeProjection(const std::string& theID)
85 AttributeSelectionPtr aExtFeature =
86 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
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()));
97 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
98 FeaturePtr aProjection;
99 if (aRefAttr && aRefAttr->isInitialized())
100 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
102 // if the type of feature differs with already selected, remove it and create once again
103 bool hasPrevProj = aProjection.get() != 0;
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);
115 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
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());
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
127 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
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);
137 else if (anEdge->isCircle()) {
138 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
139 double aRadius = aCircle->radius();
141 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
142 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
145 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
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);
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);
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);
164 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
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);
178 aProjection->execute();
179 aRefAttr->setObject(aProjection);
182 FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
183 aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
187 if (theID == EXTERNAL_FEATURE_ID())
188 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->context()->shape());