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());
171 keepCurrentFeature();
174 std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
175 std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
178 aProjection = sketch()->addFeature(SketchPlugin_Point::ID());
180 // update coordinates of projection
181 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
184 else if (anEdge->isLine()) {
185 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
186 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);
190 if (aFirstInSketch->distance(aLastInSketch) < tolerance)
191 return; // line is semi-orthogonal to the sketch plane
194 aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
196 // update attributes of projection
197 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198 aProjection->attribute(SketchPlugin_Line::START_ID()));
199 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200 aProjection->attribute(SketchPlugin_Line::END_ID()));
201 aStartPnt->setValue(aFirstInSketch);
202 aEndPnt->setValue(aLastInSketch);
204 else if (anEdge->isCircle()) {
205 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
206 double aRadius = aCircle->radius();
208 double aNormalsDot = aCircle->normal()->dot(aSketchPlane->direction());
209 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
210 return; // circle is not in the plane, parallel to the sketch plane
212 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
213 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
216 aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
218 // update attributes of projection
219 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
220 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
221 aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
222 aCenterPnt->setValue(aCenterInSketch);
223 aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
225 else if (anEdge->isArc()) {
226 std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
227 std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
228 std::shared_ptr<GeomAPI_Pnt2d> aFirstInSketch = sketch()->to2D(aFirst);
229 std::shared_ptr<GeomAPI_Pnt2d> aLastInSketch = sketch()->to2D(aLast);
231 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
232 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
233 std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
235 double aNormalsDot = aCircle->normal()->dot(aSketchPlane->direction());
236 if (fabs(fabs(aNormalsDot) - 1.0) > tolerance)
237 return; // arc is not in the plane, parallel to the sketch plane
239 bool isInversed = aNormalsDot < 0.;
242 aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
244 bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
246 // update attributes of projection
247 std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
248 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
249 aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
250 std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
251 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
252 aProjection->attribute(SketchPlugin_Arc::START_ID()));
253 std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
254 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
255 aProjection->attribute(SketchPlugin_Arc::END_ID()));
256 aStartPnt->setValue(aFirstInSketch);
257 aEndPnt->setValue(aLastInSketch);
258 aCenterPnt->setValue(aCenterInSketch);
259 aProjection->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
261 aProjection->data()->blockSendAttributeUpdated(aWasBlocked);
265 aProjection->boolean(COPY_ID())->setValue(true);
266 aProjection->execute();
267 aRefAttr->setObject(aProjection);
269 restoreCurrentFeature();
271 if (theID == EXTERNAL_FEATURE_ID()) {
272 selection(EXTERNAL_ID())->selectValue(aExtFeature);
275 aResult->setShape(aProjection->lastResult()->shape());
277 GeomShapePtr anEmptyVal;
278 aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);