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());
71 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
73 // feature cannot be moved
76 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
78 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
80 computeProjection(theID);
81 myIsComputing = false;
85 void SketchPlugin_Projection::computeProjection(const std::string& theID)
87 AttributeSelectionPtr aExtFeature =
88 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
90 std::shared_ptr<GeomAPI_Edge> anEdge;
91 if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
92 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
93 } else if (aExtFeature->context() && aExtFeature->context()->shape() && aExtFeature->context()->shape()->isEdge()) {
94 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
99 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
100 FeaturePtr aProjection;
101 if (aRefAttr && aRefAttr->isInitialized())
102 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
104 // if the type of feature differs with already selected, remove it and create once again
105 bool hasPrevProj = aProjection.get() != 0;
107 if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
108 (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
109 (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
110 DocumentPtr aDoc = sketch()->document();
112 std::set<FeaturePtr> aFeaturesToBeRemoved;
113 aFeaturesToBeRemoved.insert(aProjection);
114 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
115 aProjection = FeaturePtr();
116 aRefAttr->setObject(aProjection);
120 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
122 ResultConstructionPtr aResult =
123 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
124 if (aResult && aResult->shape()) {
125 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
126 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
129 if (anEdge->isLine()) {
130 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
131 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
133 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
134 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
135 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
136 return; // line is semi-orthogonal to the sketch plane
139 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
141 // update attributes of projection
142 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143 aProjection->attribute(SketchPlugin_Line::START_ID()));
144 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145 aProjection->attribute(SketchPlugin_Line::END_ID()));
146 aStartPnt->setValue(aFirstInSketch);
147 aEndPnt->setValue(aLastInSketch);
149 else if (anEdge->isCircle()) {
150 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
151 double aRadius = aCircle->radius();
153 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
154 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
157 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
159 // update attributes of projection
160 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
162 aCenterPnt->setValue(aCenterInSketch);
163 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
165 else if (anEdge->isArc()) {
166 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
167 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
168 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
169 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
171 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
172 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
173 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
176 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
178 // update attributes of projection
179 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
181 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 aProjection->attribute(SketchPlugin_Arc::START_ID()));
183 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184 aProjection->attribute(SketchPlugin_Arc::END_ID()));
185 aStartPnt->setValue(aFirstInSketch);
186 aEndPnt->setValue(aLastInSketch);
187 aCenterPnt->setValue(aCenterInSketch);
190 aProjection->execute();
191 aRefAttr->setObject(aProjection);
194 FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
195 aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
199 if (theID == EXTERNAL_FEATURE_ID()) {
200 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
203 aResult->setShape(aProjection->lastResult()->shape());
205 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());