1 // Copyright (C) 2014-2019 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
20 #include <SketchPlugin_Projection.h>
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Line.h>
25 #include <SketchPlugin_Point.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 <GeomAPI_Vertex.h>
43 #include <GeomAlgoAPI_EdgeBuilder.h>
44 #include <GeomDataAPI_Point2D.h>
48 static const double tolerance = 1.e-7;
50 SketchPlugin_Projection::SketchPlugin_Projection()
51 : SketchPlugin_SketchEntity(),
56 void SketchPlugin_Projection::initDerivedClassAttributes()
58 data()->addAttribute(EXTERNAL_FEATURE_ID(), ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(PROJECTED_FEATURE_ID(), ModelAPI_AttributeRefAttr::typeId());
60 data()->attribute(PROJECTED_FEATURE_ID())->setIsArgument(false);
62 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
65 data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
67 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
70 void SketchPlugin_Projection::execute()
72 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
73 if (!aRefAttr || !aRefAttr->isInitialized())
75 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
77 if (!lastResult().get()) {
78 bool hasProjResult = aProjection->lastResult().get() != NULL;
79 ResultConstructionPtr aConstr = document()->createConstruction(data());
81 aConstr->setShape(aProjection->lastResult()->shape());
82 aConstr->setIsInHistory(false);
83 aConstr->setDisplayed(false);
87 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
90 // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
91 // projection must be fully recomputed
92 computeProjection(EXTERNAL_FEATURE_ID());
95 void SketchPlugin_Projection::attributeChanged(const std::string& theID)
97 if ((theID == EXTERNAL_FEATURE_ID() || theID == EXTERNAL_ID()) && !myIsComputing) {
99 computeProjection(theID);
100 myIsComputing = false;
104 static bool isValidProjectionType(FeaturePtr theProjection,
106 GeomVertexPtr theVertex)
108 if (theVertex && theProjection->getKind() == SketchPlugin_Point::ID())
111 if (theEdge->isLine() && theProjection->getKind() == SketchPlugin_Line::ID())
113 else if (theEdge->isCircle() && theProjection->getKind() == SketchPlugin_Circle::ID())
115 else if (theEdge->isArc() && theProjection->getKind() == SketchPlugin_Arc::ID())
121 void SketchPlugin_Projection::computeProjection(const std::string& theID)
123 AttributeSelectionPtr aExtFeature =
124 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
128 GeomVertexPtr aVertex;
130 aShape = aExtFeature->value();
131 if (!aShape && aExtFeature->context())
132 aShape = aExtFeature->context()->shape();
134 if (aShape->isEdge())
135 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
136 else if (aShape->isVertex())
137 aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
139 if (!anEdge && !aVertex)
142 AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
143 FeaturePtr aProjection;
144 if (aRefAttr && aRefAttr->isInitialized())
145 aProjection = ModelAPI_Feature::feature(aRefAttr->object());
147 // if the type of feature differs with already selected, remove it and create once again
148 bool hasPrevProj = aProjection.get() != 0;
149 if (hasPrevProj && !isValidProjectionType(aProjection, anEdge, aVertex)) {
150 DocumentPtr aDoc = sketch()->document();
152 aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
153 std::set<FeaturePtr> aFeaturesToBeRemoved;
154 aFeaturesToBeRemoved.insert(aProjection);
155 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
156 aProjection = FeaturePtr();
157 aRefAttr->setObject(aProjection);
161 std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
163 ResultConstructionPtr aResult =
164 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
165 if (aResult && aResult->shape() && theID == EXTERNAL_FEATURE_ID()) {
166 aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
168 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
172 std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
173 std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
176 aProjection = sketch()->addFeature(SketchPlugin_Point::ID());
178 // update coordinates of projection
179 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180 aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
182 else if (anEdge->isLine()) {
183 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
184 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
186 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
187 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
188 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
189 return; // line is semi-orthogonal to the sketch plane
192 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
194 // update attributes of projection
195 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
196 aProjection->attribute(SketchPlugin_Line::START_ID()));
197 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198 aProjection->attribute(SketchPlugin_Line::END_ID()));
199 aStartPnt->setValue(aFirstInSketch);
200 aEndPnt->setValue(aLastInSketch);
202 else if (anEdge->isCircle()) {
203 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
204 double aRadius = aCircle->radius();
206 double aNormalsDot = aCircle->normal()->dot(aSketchPlane->direction());
207 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
208 return; // circle is not in the plane, parallel to the sketch plane
210 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
211 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
214 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
216 // update attributes of projection
217 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
218 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
220 aCenterPnt->setValue(aCenterInSketch);
221 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
223 else if (anEdge->isArc()) {
224 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
225 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
226 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
227 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
229 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
230 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
231 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
233 double aNormalsDot = aCircle->normal()->dot(aSketchPlane->direction());
234 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
235 return; // arc is not in the plane, parallel to the sketch plane
237 bool isInversed = aNormalsDot < 0.;
240 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
242 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
244 // update attributes of projection
245 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
246 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
247 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
248 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
249 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
250 aProjection->attribute(SketchPlugin_Arc::START_ID()));
251 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
252 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
253 aProjection->attribute(SketchPlugin_Arc::END_ID()));
254 aStartPnt->setValue(aFirstInSketch);
255 aEndPnt->setValue(aLastInSketch);
256 aCenterPnt->setValue(aCenterInSketch);
257 aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
259 aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
263 aProjection->boolean(COPY_ID())->setValue(true);
264 aProjection->execute();
265 aRefAttr->setObject(aProjection);
267 if (theID == EXTERNAL_FEATURE_ID()) {
268 selection(EXTERNAL_ID())->selectValue(aExtFeature);
271 aResult->setShape(aProjection->lastResult()->shape());
273 GeomShapePtr anEmptyVal;
274 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);