1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <SketchPlugin_Projection.h>
23 #include <SketchPlugin_Arc.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Sketch.h>
27 #include <SketchPlugin_ConstraintRigid.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeSelection.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_ResultConstruction.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Tools.h>
37 #include <GeomAPI_Circ.h>
38 #include <GeomAPI_Edge.h>
39 #include <GeomAPI_Lin.h>
40 #include <GeomAPI_Pnt.h>
41 #include <GeomAPI_Pnt2d.h>
42 #include <GeomAlgoAPI_EdgeBuilder.h>
43 #include <GeomDataAPI_Point2D.h>
47 static const double tolerance = 1.e-7;
49 SketchPlugin_Projection::SketchPlugin_Projection()
50 : SketchPlugin_SketchEntity(),
55 void SketchPlugin_Projection::initDerivedClassAttributes()
57 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
58 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
59 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
61 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
67 void SketchPlugin_Projection::execute()
69 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
70 if (!aRefAttr || !aRefAttr->isInitialized())
72 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
74 if (!lastResult().get() && aProjection->lastResult().get()) {
75 ResultConstructionPtr aConstr = document()->createConstruction(data());
76 aConstr->setShape(aProjection->lastResult()->shape());
77 aConstr->setIsInHistory(false);
78 aConstr->setDisplayed(false);
81 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
84 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
85 // projection must be fully recomputed
86 computeProjection(EXTERNAL_FEATURE_ID());
89 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
91 // feature cannot be moved
94 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
96 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
98 computeProjection(theID);
99 myIsComputing = false;
103 void SketchPlugin_Projection::computeProjection(const std::string& theID)
105 AttributeSelectionPtr aExtFeature =
106 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
108 std::shared_ptr<GeomAPI_Edge> anEdge;
109 if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
110 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
111 } else if (aExtFeature->context() && aExtFeature->context()->shape() &&
112 aExtFeature->context()->shape()->isEdge()) {
113 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
118 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
119 FeaturePtr aProjection;
120 if (aRefAttr && aRefAttr->isInitialized())
121 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
123 // if the type of feature differs with already selected, remove it and create once again
124 bool hasPrevProj = aProjection.get() != 0;
126 if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
127 (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
128 (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
129 DocumentPtr aDoc = sketch()->document();
131 std::set<FeaturePtr> aFeaturesToBeRemoved;
132 aFeaturesToBeRemoved.insert(aProjection);
133 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
134 aProjection = FeaturePtr();
135 aRefAttr->setObject(aProjection);
139 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
141 ResultConstructionPtr aResult =
142 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
143 if (aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
144 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
145 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
148 if (anEdge->isLine()) {
149 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
150 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
152 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
153 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
154 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
155 return; // line is semi-orthogonal to the sketch plane
158 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
160 // update attributes of projection
161 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162 aProjection->attribute(SketchPlugin_Line::START_ID()));
163 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164 aProjection->attribute(SketchPlugin_Line::END_ID()));
165 aStartPnt->setValue(aFirstInSketch);
166 aEndPnt->setValue(aLastInSketch);
168 else if (anEdge->isCircle()) {
169 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
170 double aRadius = aCircle->radius();
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_Circle::ID());
178 // update attributes of projection
179 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
180 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
182 aCenterPnt->setValue(aCenterInSketch);
183 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
185 else if (anEdge->isArc()) {
186 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
187 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
188 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
189 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
191 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
192 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
193 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
196 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
198 // update attributes of projection
199 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
200 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
201 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
202 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
203 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204 aProjection->attribute(SketchPlugin_Arc::START_ID()));
205 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
206 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
207 aProjection->attribute(SketchPlugin_Arc::END_ID()));
208 aStartPnt->setValue(aFirstInSketch);
209 aEndPnt->setValue(aLastInSketch);
210 aCenterPnt->setValue(aCenterInSketch);
213 aProjection->boolean(COPY_ID())->setValue(true);
214 aProjection->execute();
215 aRefAttr->setObject(aProjection);
217 if (theID == EXTERNAL_FEATURE_ID()) {
218 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
221 aResult->setShape(aProjection->lastResult()->shape());
223 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());