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>
12 #include <GeomDataAPI_Point.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Pln.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Pnt.h>
19 #include <GeomAPI_Dir.h>
20 #include <GeomAPI_XYZ.h>
22 #include <SketchPlugin_Feature.h>
23 #include <SketchPlugin_Sketch.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintDistance.h>
26 #include <SketchPlugin_ConstraintLength.h>
27 #include <SketchPlugin_ConstraintRadius.h>
28 #include <SketchPlugin_ConstraintRigid.h>
29 #include <SketchPlugin_Constraint.h>
31 #include <ModuleBase_ViewerPrs.h>
33 #include <V3d_View.hxx>
35 #include <ProjLib.hxx>
37 #include <Geom_Line.hxx>
38 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 const double PRECISION_TOLERANCE = 0.000001;
46 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
51 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
52 theView->Eye(XEye, YEye, ZEye);
54 theView->At(XAt, YAt, ZAt);
55 gp_Pnt EyePoint(XEye, YEye, ZEye);
56 gp_Pnt AtPoint(XAt, YAt, ZAt);
58 gp_Vec EyeVector(EyePoint, AtPoint);
59 gp_Dir EyeDir(EyeVector);
61 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
62 Standard_Real X, Y, Z;
63 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
64 gp_Pnt ConvertedPoint(X, Y, Z);
66 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
67 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
72 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
73 Handle(V3d_View) theView,
74 double& theX, double& theY)
79 AttributeDoublePtr anAttr;
80 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
82 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
83 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
85 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
86 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
87 boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
88 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
90 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
91 gp_Vec aVec(anOriginPnt, thePoint);
93 if (!theView.IsNull()) {
94 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
95 theView->Eye(XEye, YEye, ZEye);
97 theView->At(XAt, YAt, ZAt);
98 gp_Pnt EyePoint(XEye, YEye, ZEye);
99 gp_Pnt AtPoint(XAt, YAt, ZAt);
101 gp_Vec anEyeVec(EyePoint, AtPoint);
102 anEyeVec.Normalize();
104 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
105 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
106 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
108 double aDen = anEyeVec * aNormalVec;
109 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
111 gp_Vec aDeltaVec = anEyeVec * aLVec;
112 aVec = aVec - aDeltaVec;
114 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
115 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
118 void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
124 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
126 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
127 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
128 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
129 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
130 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
131 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
133 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
134 ->added(aY->dir()->xyz()->multiplied(theY));
136 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
137 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
140 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
141 FeaturePtr theSketch,
142 const QList<ModuleBase_ViewerPrs>& theFeatures)
145 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
146 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
149 FeaturePtr aDeltaFeature;
150 double aMinDelta = -1;
151 ModuleBase_ViewerPrs aPrs;
152 foreach (ModuleBase_ViewerPrs aPrs, theFeatures) {
155 boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
156 SketchPlugin_Feature>(aPrs.object());
159 double aDelta = aSketchFeature->distanceToPoint(
160 boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
161 if (aMinDelta < 0 || aMinDelta > aDelta) {
163 // TODO aDeltaFeature = aPrs.result();
166 return aDeltaFeature;
169 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
171 return ModelAPI_Session::get()->moduleDocument();
174 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
175 const std::string& theAttribute)
179 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
180 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181 aData->attribute(theAttribute));
183 aPoint->setValue(theX, theY);
186 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
187 const std::string& theAttribute)
191 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
192 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
193 aData->attribute(theAttribute));
195 anAttribute->setValue(theValue);
198 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
204 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
205 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
206 aData->attribute(theAttribute));
208 aValue = anAttribute->value();
215 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
216 const std::string& theKind)
222 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
223 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
224 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
226 aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
227 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
228 aFeature = FeaturePtr();
234 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
235 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
236 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
240 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
242 boost::shared_ptr<ModelAPI_Document> aDoc = document();
243 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
246 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
248 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
249 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
250 aRef1->setAttr(thePoint1);
252 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
253 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
254 aRef2->setAttr(thePoint2);
256 if (aFeature) // TODO: generate an error if feature was not created
260 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
261 const std::string& theAttribute, double theClickedX,
264 // find a feature point by the selection mode
265 //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
266 boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
267 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
271 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
272 // the constraint is created between the feature point and the found sketch point
273 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
274 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
275 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
277 std::list<ObjectPtr> aFeatures = aRefList->list();
278 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
279 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
280 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
281 new GeomAPI_Pnt2d(theClickedX, theClickedY));
282 for (; anIt != aLast; anIt++) {
283 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
284 // find the given point in the feature attributes
285 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
286 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
287 aLast = anAttiributes.end();
288 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
289 for (; anIt != aLast && !aFPoint; anIt++) {
290 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
291 GeomDataAPI_Point2D>(*anIt);
292 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
296 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
300 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
302 boost::shared_ptr<GeomAPI_Pln> aPlane;
303 double aA, aB, aC, aD;
305 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
306 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
307 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
308 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
309 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
315 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
319 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
320 CompositeFeaturePtr theSketch)
322 boost::shared_ptr<GeomAPI_Pnt> aPoint;
323 if (!theSketch || !thePoint2D)
326 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
327 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
328 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
329 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
330 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
331 theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
333 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
336 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
338 return theKind == SketchPlugin_ConstraintDistance::ID()
339 || theKind == SketchPlugin_ConstraintLength::ID()
340 || theKind == SketchPlugin_ConstraintRadius::ID()
341 || theKind == SketchPlugin_ConstraintRigid::ID();