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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include <SketchPlugin_Projection.h>
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Line.h>
25 #include <SketchPlugin_Sketch.h>
26 #include <SketchPlugin_ConstraintRigid.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29 #include <ModelAPI_AttributeSelection.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_Tools.h>
36 #include <GeomAPI_Circ.h>
37 #include <GeomAPI_Edge.h>
38 #include <GeomAPI_Lin.h>
39 #include <GeomAPI_Pnt.h>
40 #include <GeomAPI_Pnt2d.h>
41 #include <GeomAlgoAPI_EdgeBuilder.h>
42 #include <GeomDataAPI_Point2D.h>
46 static const double tolerance = 1.e-7;
48 SketchPlugin_Projection::SketchPlugin_Projection()
49 : SketchPlugin_SketchEntity(),
54 void SketchPlugin_Projection::initDerivedClassAttributes()
56 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
58 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
60 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
66 void SketchPlugin_Projection::execute()
68 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
69 if (!aRefAttr || !aRefAttr->isInitialized())
71 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
73 if (!lastResult().get() && aProjection->lastResult().get()) {
74 ResultConstructionPtr aConstr = document()->createConstruction(data());
75 aConstr->setShape(aProjection->lastResult()->shape());
76 aConstr->setIsInHistory(false);
77 aConstr->setDisplayed(false);
80 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
83 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
84 // projection must be fully recomputed
85 computeProjection(EXTERNAL_FEATURE_ID());
88 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
90 // feature cannot be moved
93 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
95 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
97 computeProjection(theID);
98 myIsComputing = false;
102 void SketchPlugin_Projection::computeProjection(const std::string& theID)
104 AttributeSelectionPtr aExtFeature =
105 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
107 std::shared_ptr<GeomAPI_Edge> anEdge;
108 if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
109 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
110 } else if (aExtFeature->context() && aExtFeature->context()->shape() &&
111 aExtFeature->context()->shape()->isEdge()) {
112 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
117 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
118 FeaturePtr aProjection;
119 if (aRefAttr && aRefAttr->isInitialized())
120 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
122 // if the type of feature differs with already selected, remove it and create once again
123 bool hasPrevProj = aProjection.get() != 0;
125 if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
126 (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
127 (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
128 DocumentPtr aDoc = sketch()->document();
130 std::set<FeaturePtr> aFeaturesToBeRemoved;
131 aFeaturesToBeRemoved.insert(aProjection);
132 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
133 aProjection = FeaturePtr();
134 aRefAttr->setObject(aProjection);
138 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
140 ResultConstructionPtr aResult =
141 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
142 if (aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
143 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
144 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
147 if (anEdge->isLine()) {
148 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
149 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
151 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
152 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
153 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
154 return; // line is semi-orthogonal to the sketch plane
157 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
159 // update attributes of projection
160 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161 aProjection->attribute(SketchPlugin_Line::START_ID()));
162 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
163 aProjection->attribute(SketchPlugin_Line::END_ID()));
164 aStartPnt->setValue(aFirstInSketch);
165 aEndPnt->setValue(aLastInSketch);
167 else if (anEdge->isCircle()) {
168 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
169 double aRadius = aCircle->radius();
171 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
172 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
175 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
177 // update attributes of projection
178 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
179 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
181 aCenterPnt->setValue(aCenterInSketch);
182 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
184 else if (anEdge->isArc()) {
185 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
186 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
187 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
188 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
190 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
191 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
192 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
195 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
197 // update attributes of projection
198 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
199 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
201 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
202 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203 aProjection->attribute(SketchPlugin_Arc::START_ID()));
204 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
205 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206 aProjection->attribute(SketchPlugin_Arc::END_ID()));
207 aStartPnt->setValue(aFirstInSketch);
208 aEndPnt->setValue(aLastInSketch);
209 aCenterPnt->setValue(aCenterInSketch);
212 aProjection->boolean(COPY_ID())->setValue(true);
213 aProjection->execute();
214 aRefAttr->setObject(aProjection);
216 if (theID == EXTERNAL_FEATURE_ID()) {
217 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
220 aResult->setShape(aProjection->lastResult()->shape());
222 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());