1 // File: PartSet_Tools.h
2 // Created: 28 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_Tools.h>
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeRefList.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Session.h>
13 #include <GeomDataAPI_Point.h>
14 #include <GeomDataAPI_Dir.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pln.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_Dir.h>
21 #include <GeomAPI_XYZ.h>
23 #include <SketchPlugin_Feature.h>
24 #include <SketchPlugin_Sketch.h>
25 #include <SketchPlugin_ConstraintCoincidence.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintLength.h>
28 #include <SketchPlugin_ConstraintRadius.h>
29 #include <SketchPlugin_ConstraintRigid.h>
30 #include <SketchPlugin_Constraint.h>
31 #include <SketchPlugin_Circle.h>
32 #include <SketchPlugin_Arc.h>
33 #include <SketchPlugin_Line.h>
35 #include <ModuleBase_ViewerPrs.h>
37 #include <V3d_View.hxx>
39 #include <gp_Circ.hxx>
40 #include <ProjLib.hxx>
42 #include <Geom_Line.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <BRep_Tool.hxx>
51 const double PRECISION_TOLERANCE = 0.000001;
53 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
58 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
59 theView->Eye(XEye, YEye, ZEye);
61 theView->At(XAt, YAt, ZAt);
62 gp_Pnt EyePoint(XEye, YEye, ZEye);
63 gp_Pnt AtPoint(XAt, YAt, ZAt);
65 gp_Vec EyeVector(EyePoint, AtPoint);
66 gp_Dir EyeDir(EyeVector);
68 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
69 Standard_Real X, Y, Z;
70 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
71 gp_Pnt ConvertedPoint(X, Y, Z);
73 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
74 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
79 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
80 Handle(V3d_View) theView,
81 double& theX, double& theY)
86 AttributeDoublePtr anAttr;
87 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
89 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
90 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
92 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
93 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
94 boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
95 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
97 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
98 gp_Vec aVec(anOriginPnt, thePoint);
100 if (!theView.IsNull()) {
101 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
102 theView->Eye(XEye, YEye, ZEye);
104 theView->At(XAt, YAt, ZAt);
105 gp_Pnt EyePoint(XEye, YEye, ZEye);
106 gp_Pnt AtPoint(XAt, YAt, ZAt);
108 gp_Vec anEyeVec(EyePoint, AtPoint);
109 anEyeVec.Normalize();
111 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
112 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
113 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
115 double aDen = anEyeVec * aNormalVec;
116 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
118 gp_Vec aDeltaVec = anEyeVec * aLVec;
119 aVec = aVec - aDeltaVec;
121 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
122 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
125 void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
131 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
133 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
134 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
135 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
136 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
137 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
138 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
140 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
141 ->added(aY->dir()->xyz()->multiplied(theY));
143 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
144 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
147 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
148 FeaturePtr theSketch,
149 const QList<ModuleBase_ViewerPrs>& theFeatures)
152 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
153 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
156 FeaturePtr aDeltaFeature;
157 double aMinDelta = -1;
158 ModuleBase_ViewerPrs aPrs;
159 foreach (ModuleBase_ViewerPrs aPrs, theFeatures) {
162 boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
163 SketchPlugin_Feature>(aPrs.object());
166 double aDelta = aSketchFeature->distanceToPoint(
167 boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
168 if (aMinDelta < 0 || aMinDelta > aDelta) {
170 // TODO aDeltaFeature = aPrs.result();
173 return aDeltaFeature;
176 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
178 return ModelAPI_Session::get()->moduleDocument();
181 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
182 const std::string& theAttribute)
186 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
187 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
188 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
190 aPoint->setValue(theX, theY);
193 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
194 const std::string& theAttribute)
198 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
199 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
200 aData->attribute(theAttribute));
202 anAttribute->setValue(theValue);
205 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
211 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
212 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
213 aData->attribute(theAttribute));
215 aValue = anAttribute->value();
222 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
223 const std::string& theKind)
229 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
230 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
231 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
233 aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
234 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
235 aFeature = FeaturePtr();
241 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
242 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
243 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
247 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
249 boost::shared_ptr<ModelAPI_Document> aDoc = document();
250 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
253 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
255 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
256 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
257 aRef1->setAttr(thePoint1);
259 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
260 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
261 aRef2->setAttr(thePoint2);
263 if (aFeature) // TODO: generate an error if feature was not created
267 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
268 const std::string& theAttribute, double theClickedX,
271 // find a feature point by the selection mode
272 //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
273 boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
274 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
278 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
279 // the constraint is created between the feature point and the found sketch point
280 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
281 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
282 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
284 std::list<ObjectPtr> aFeatures = aRefList->list();
285 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
286 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
287 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
288 new GeomAPI_Pnt2d(theClickedX, theClickedY));
289 for (; anIt != aLast; anIt++) {
290 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
291 // find the given point in the feature attributes
292 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
293 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
294 aLast = anAttiributes.end();
295 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
296 for (; anIt != aLast && !aFPoint; anIt++) {
297 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
298 GeomDataAPI_Point2D>(*anIt);
299 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
303 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
307 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
309 boost::shared_ptr<GeomAPI_Pln> aPlane;
310 double aA, aB, aC, aD;
312 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
313 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
314 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
315 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
316 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
322 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
326 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
327 CompositeFeaturePtr theSketch)
329 boost::shared_ptr<GeomAPI_Pnt> aPoint;
330 if (!theSketch || !thePoint2D)
333 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
334 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
335 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
336 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
337 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
338 theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
340 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
343 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
345 return theKind == SketchPlugin_ConstraintDistance::ID()
346 || theKind == SketchPlugin_ConstraintLength::ID()
347 || theKind == SketchPlugin_ConstraintRadius::ID()
348 || theKind == SketchPlugin_ConstraintRigid::ID();
351 ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
353 TopoDS_Shape aShape = thePrs.shape();
354 if (aShape.ShapeType() != TopAbs_EDGE)
357 Standard_Real aStart, aEnd;
358 Handle(V3d_View) aNullView;
359 FeaturePtr myFeature;
361 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
362 GeomAdaptor_Curve aAdaptor(aCurve);
363 if (aAdaptor.GetType() == GeomAbs_Line) {
365 myFeature = theSketch->addFeature(SketchPlugin_Line::ID());
367 //DataPtr aData = myFeature->data();
368 //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
369 // boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
372 //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
373 //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
374 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
376 //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
377 //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
378 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
379 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
380 if (aAdaptor.IsClosed()) {
382 myFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
383 //gp_Circ aCirc = aAdaptor.Circle();
384 //gp_Pnt aCenter = aCirc.Location();
387 //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
388 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
389 //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
392 myFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
396 DataPtr aData = myFeature->data();
397 AttributeSelectionPtr anAttr =
398 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
399 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
401 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
402 if (anAttr && aRes) {
403 boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
404 anEdge->setImpl(new TopoDS_Shape(aShape));
406 anAttr->setValue(aRes, anEdge);
407 myFeature->execute();
408 return myFeature->firstResult();