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_Point.h>
27 #include <SketchPlugin_Sketch.h>
28 #include <SketchPlugin_ConstraintRigid.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Tools.h>
38 #include <GeomAPI_Circ.h>
39 #include <GeomAPI_Edge.h>
40 #include <GeomAPI_Lin.h>
41 #include <GeomAPI_Pnt.h>
42 #include <GeomAPI_Pnt2d.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomAlgoAPI_EdgeBuilder.h>
45 #include <GeomDataAPI_Point2D.h>
49 static const double tolerance = 1.e-7;
51 SketchPlugin_Projection::SketchPlugin_Projection()
52 : SketchPlugin_SketchEntity(),
57 void SketchPlugin_Projection::initDerivedClassAttributes()
59 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
61 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
63 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
66 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
68 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
71 void SketchPlugin_Projection::execute()
73 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
74 if (!aRefAttr || !aRefAttr->isInitialized())
76 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
78 if (!lastResult().get() && aProjection->lastResult().get()) {
79 ResultConstructionPtr aConstr = document()->createConstruction(data());
80 aConstr->setShape(aProjection->lastResult()->shape());
81 aConstr->setIsInHistory(false);
82 aConstr->setDisplayed(false);
85 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
88 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
89 // projection must be fully recomputed
90 computeProjection(EXTERNAL_FEATURE_ID());
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 static bool isValidProjectionType(FeaturePtr theProjection,
104 GeomVertexPtr theVertex)
106 if (theVertex && theProjection->getKind() == SketchPlugin_Point::ID())
109 if (theEdge->isLine() && theProjection->getKind() == SketchPlugin_Line::ID())
111 else if (theEdge->isCircle() && theProjection->getKind() == SketchPlugin_Circle::ID())
113 else if (theEdge->isArc() && theProjection->getKind() == SketchPlugin_Arc::ID())
119 void SketchPlugin_Projection::computeProjection(const std::string& theID)
121 AttributeSelectionPtr aExtFeature =
122 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
126 GeomVertexPtr aVertex;
128 aShape = aExtFeature->value();
129 if (!aShape && aExtFeature->context())
130 aShape = aExtFeature->context()->shape();
132 if (aShape->isEdge())
133 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
134 else if (aShape->isVertex())
135 aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
137 if (!anEdge && !aVertex)
140 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
141 FeaturePtr aProjection;
142 if (aRefAttr && aRefAttr->isInitialized())
143 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
145 // if the type of feature differs with already selected, remove it and create once again
146 bool hasPrevProj = aProjection.get() != 0;
147 if (hasPrevProj && !isValidProjectionType(aProjection, anEdge, aVertex)) {
148 DocumentPtr aDoc = sketch()->document();
150 std::set<FeaturePtr> aFeaturesToBeRemoved;
151 aFeaturesToBeRemoved.insert(aProjection);
152 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
153 aProjection = FeaturePtr();
154 aRefAttr->setObject(aProjection);
158 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
160 ResultConstructionPtr aResult =
161 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
162 if (aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
163 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
164 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
168 std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
169 std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
172 aProjection = sketch()->addFeature(SketchPlugin_Point::ID());
174 // update coordinates of projection
175 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
178 else if (anEdge->isLine()) {
179 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
180 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
182 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
183 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
184 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
185 return; // line is semi-orthogonal to the sketch plane
188 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
190 // update attributes of projection
191 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aProjection->attribute(SketchPlugin_Line::START_ID()));
193 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194 aProjection->attribute(SketchPlugin_Line::END_ID()));
195 aStartPnt->setValue(aFirstInSketch);
196 aEndPnt->setValue(aLastInSketch);
198 else if (anEdge->isCircle()) {
199 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
200 double aRadius = aCircle->radius();
202 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
203 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
206 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
208 // update attributes of projection
209 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
210 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
211 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
212 aCenterPnt->setValue(aCenterInSketch);
213 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
215 else if (anEdge->isArc()) {
216 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
217 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
218 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
219 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
221 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
222 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
223 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
225 bool isInversed = aCircle->normal()->dot(aSketchPlane->direction()) < 0.;
228 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
230 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
232 // update attributes of projection
233 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
234 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
235 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
236 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
237 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
238 aProjection->attribute(SketchPlugin_Arc::START_ID()));
239 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
240 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
241 aProjection->attribute(SketchPlugin_Arc::END_ID()));
242 aStartPnt->setValue(aFirstInSketch);
243 aEndPnt->setValue(aLastInSketch);
244 aCenterPnt->setValue(aCenterInSketch);
245 aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
247 aProjection->data()->blockSendAttributeUpdated(aWasBlocked, false);
250 aProjection->boolean(COPY_ID())->setValue(true);
251 aProjection->execute();
252 aRefAttr->setObject(aProjection);
254 if (theID == EXTERNAL_FEATURE_ID()) {
255 selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
258 aResult->setShape(aProjection->lastResult()->shape());
260 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());