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>
32 #include <ModuleBase_ViewerPrs.h>
34 #include <V3d_View.hxx>
36 #include <ProjLib.hxx>
38 #include <Geom_Line.hxx>
39 #include <GeomAPI_ProjectPointOnCurve.hxx>
45 const double PRECISION_TOLERANCE = 0.000001;
47 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
52 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
53 theView->Eye(XEye, YEye, ZEye);
55 theView->At(XAt, YAt, ZAt);
56 gp_Pnt EyePoint(XEye, YEye, ZEye);
57 gp_Pnt AtPoint(XAt, YAt, ZAt);
59 gp_Vec EyeVector(EyePoint, AtPoint);
60 gp_Dir EyeDir(EyeVector);
62 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
63 Standard_Real X, Y, Z;
64 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
65 gp_Pnt ConvertedPoint(X, Y, Z);
67 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
68 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
73 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
74 Handle(V3d_View) theView,
75 double& theX, double& theY)
80 AttributeDoublePtr anAttr;
81 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
83 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
84 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
86 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
87 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
88 boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
89 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
91 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
92 gp_Vec aVec(anOriginPnt, thePoint);
94 if (!theView.IsNull()) {
95 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
96 theView->Eye(XEye, YEye, ZEye);
98 theView->At(XAt, YAt, ZAt);
99 gp_Pnt EyePoint(XEye, YEye, ZEye);
100 gp_Pnt AtPoint(XAt, YAt, ZAt);
102 gp_Vec anEyeVec(EyePoint, AtPoint);
103 anEyeVec.Normalize();
105 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
106 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
107 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
109 double aDen = anEyeVec * aNormalVec;
110 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
112 gp_Vec aDeltaVec = anEyeVec * aLVec;
113 aVec = aVec - aDeltaVec;
115 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
116 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
119 void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
125 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
127 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
128 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
129 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
130 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
131 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
132 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
134 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
135 ->added(aY->dir()->xyz()->multiplied(theY));
137 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
138 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
141 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
142 FeaturePtr theSketch,
143 const QList<ModuleBase_ViewerPrs>& theFeatures)
146 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
147 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
150 FeaturePtr aDeltaFeature;
151 double aMinDelta = -1;
152 ModuleBase_ViewerPrs aPrs;
153 foreach (ModuleBase_ViewerPrs aPrs, theFeatures) {
156 boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
157 SketchPlugin_Feature>(aPrs.object());
160 double aDelta = aSketchFeature->distanceToPoint(
161 boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
162 if (aMinDelta < 0 || aMinDelta > aDelta) {
164 // TODO aDeltaFeature = aPrs.result();
167 return aDeltaFeature;
170 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
172 return ModelAPI_Session::get()->moduleDocument();
175 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
176 const std::string& theAttribute)
180 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
181 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 aData->attribute(theAttribute));
184 aPoint->setValue(theX, theY);
187 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
188 const std::string& theAttribute)
192 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
193 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
194 aData->attribute(theAttribute));
196 anAttribute->setValue(theValue);
199 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
205 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
206 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
207 aData->attribute(theAttribute));
209 aValue = anAttribute->value();
216 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
217 const std::string& theKind)
223 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
224 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
225 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
227 aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
228 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
229 aFeature = FeaturePtr();
235 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
236 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
237 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
241 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
243 boost::shared_ptr<ModelAPI_Document> aDoc = document();
244 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
247 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
249 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
250 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
251 aRef1->setAttr(thePoint1);
253 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
254 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
255 aRef2->setAttr(thePoint2);
257 if (aFeature) // TODO: generate an error if feature was not created
261 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
262 const std::string& theAttribute, double theClickedX,
265 // find a feature point by the selection mode
266 //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
267 boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
268 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
272 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
273 // the constraint is created between the feature point and the found sketch point
274 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
275 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
276 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
278 std::list<ObjectPtr> aFeatures = aRefList->list();
279 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
280 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
281 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
282 new GeomAPI_Pnt2d(theClickedX, theClickedY));
283 for (; anIt != aLast; anIt++) {
284 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
285 // find the given point in the feature attributes
286 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
287 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
288 aLast = anAttiributes.end();
289 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
290 for (; anIt != aLast && !aFPoint; anIt++) {
291 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
292 GeomDataAPI_Point2D>(*anIt);
293 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
297 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
301 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
303 boost::shared_ptr<GeomAPI_Pln> aPlane;
304 double aA, aB, aC, aD;
306 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
307 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
308 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
309 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
310 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
316 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
320 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
321 CompositeFeaturePtr theSketch)
323 boost::shared_ptr<GeomAPI_Pnt> aPoint;
324 if (!theSketch || !thePoint2D)
327 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
328 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
329 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
330 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
331 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
332 theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
334 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
337 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
339 return theKind == SketchPlugin_ConstraintDistance::ID()
340 || theKind == SketchPlugin_ConstraintLength::ID()
341 || theKind == SketchPlugin_ConstraintRadius::ID()
342 || theKind == SketchPlugin_ConstraintRigid::ID();