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_Constraint.h>
27 #include <PartSet_FeatureLinePrs.h>
28 #include <PartSet_FeaturePointPrs.h>
29 #include <PartSet_FeatureCirclePrs.h>
30 #include <PartSet_FeatureArcPrs.h>
32 #include <PartSet_ConstraintLengthPrs.h>
33 #include <PartSet_ConstraintRadiusPrs.h>
34 #include <PartSet_ConstraintDistancePrs.h>
36 #include <XGUI_ViewerPrs.h>
38 #include <V3d_View.hxx>
40 #include <ProjLib.hxx>
42 #include <Geom_Line.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
49 const double PRECISION_TOLERANCE = 0.000001;
51 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
56 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
57 theView->Eye(XEye, YEye, ZEye);
59 theView->At(XAt, YAt, ZAt);
60 gp_Pnt EyePoint(XEye, YEye, ZEye);
61 gp_Pnt AtPoint(XAt, YAt, ZAt);
63 gp_Vec EyeVector(EyePoint, AtPoint);
64 gp_Dir EyeDir(EyeVector);
66 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
67 Standard_Real X, Y, Z;
68 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
69 gp_Pnt ConvertedPoint(X, Y, Z);
71 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
72 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
76 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
77 Handle(V3d_View) theView, double& theX, double& theY)
82 AttributeDoublePtr anAttr;
83 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
85 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
86 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
88 boost::shared_ptr<GeomDataAPI_Dir> aX =
89 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
90 boost::shared_ptr<GeomDataAPI_Dir> anY =
91 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
93 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
94 gp_Vec aVec(anOriginPnt, thePoint);
96 if (!theView.IsNull())
98 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
99 theView->Eye(XEye, YEye, ZEye);
101 theView->At(XAt, YAt, ZAt);
102 gp_Pnt EyePoint(XEye, YEye, ZEye);
103 gp_Pnt AtPoint(XAt, YAt, ZAt);
105 gp_Vec anEyeVec(EyePoint, AtPoint);
106 anEyeVec.Normalize();
108 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
109 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
110 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
112 double aDen = anEyeVec * aNormalVec;
113 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
115 gp_Vec aDeltaVec = anEyeVec*aLVec;
116 aVec = aVec - aDeltaVec;
118 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
119 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
122 void PartSet_Tools::convertTo3D(const double theX, const double theY,
123 FeaturePtr theSketch,
129 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
131 boost::shared_ptr<GeomDataAPI_Point> aC =
132 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
133 boost::shared_ptr<GeomDataAPI_Dir> aX =
134 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
135 boost::shared_ptr<GeomDataAPI_Dir> aY =
136 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
138 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
139 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
141 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
142 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
145 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
146 FeaturePtr theSketch,
147 FeaturePtr theFeature)
149 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
151 if (theKind == PartSet_FeaturePointPrs::getKind()) {
152 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
154 else if (theKind == PartSet_FeatureLinePrs::getKind()) {
155 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
157 else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
158 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
160 else if (theKind == PartSet_FeatureArcPrs::getKind()) {
161 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
163 else if (theKind == PartSet_ConstraintLengthPrs::getKind()) {
164 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_ConstraintLengthPrs(theSketch));
166 else if (theKind == PartSet_ConstraintRadiusPrs::getKind()) {
167 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintRadiusPrs>(new PartSet_ConstraintRadiusPrs(theSketch));
169 else if (theKind == PartSet_ConstraintDistancePrs::getKind()) {
170 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintDistancePrs>(new PartSet_ConstraintDistancePrs(theSketch));
174 if (theFeature && aFeaturePrs)
175 aFeaturePrs->init(theFeature);
180 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
181 FeaturePtr theSketch,
182 const std::list<XGUI_ViewerPrs>& theFeatures)
185 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
186 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
189 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
191 FeaturePtr aDeltaFeature;
192 double aMinDelta = -1;
194 for (; anIt != aLast; anIt++) {
198 boost::shared_ptr<SketchPlugin_Feature> aSketchFeature =
199 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aPrs.feature());
202 double aDelta = aSketchFeature->distanceToPoint(
203 boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
204 if (aMinDelta < 0 || aMinDelta > aDelta) {
206 aDeltaFeature = aPrs.feature();
209 return aDeltaFeature;
212 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
214 return ModelAPI_PluginManager::get()->rootDocument();
217 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
218 const std::string& theAttribute)
222 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
223 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
224 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
226 aPoint->setValue(theX, theY);
229 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
230 const std::string& theAttribute)
234 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
235 AttributeDoublePtr anAttribute =
236 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
238 anAttribute->setValue(theValue);
241 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
247 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
248 AttributeDoublePtr anAttribute =
249 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
251 aValue = anAttribute->value();
258 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
259 const std::string& theKind)
265 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
266 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
267 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
269 aFeature = anAttr->feature();
270 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
271 aFeature = FeaturePtr();
277 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
278 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
279 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
281 boost::shared_ptr<ModelAPI_Document> aDoc = document();
282 FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
285 boost::shared_ptr<SketchPlugin_Feature> aSketch =
286 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
287 aSketch->addSub(aFeature);
290 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
292 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
293 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
294 aRef1->setAttr(thePoint1);
296 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
297 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
298 aRef2->setAttr(thePoint2);
300 if (aFeature) // TODO: generate an error if feature was not created
304 void PartSet_Tools::setConstraints(FeaturePtr theSketch, FeaturePtr theFeature,
305 const std::string& theAttribute,
306 double theClickedX, double theClickedY)
308 // find a feature point by the selection mode
309 //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
310 boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint =
311 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
315 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
316 // the constraint is created between the feature point and the found sketch point
317 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
318 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
319 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
321 std::list<FeaturePtr > aFeatures = aRefList->list();
322 std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
323 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
324 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>
325 (new GeomAPI_Pnt2d(theClickedX, theClickedY));
326 for (; anIt != aLast; anIt++)
328 FeaturePtr aFeature = *anIt;
329 // find the given point in the feature attributes
330 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
331 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
332 aLast = anAttiributes.end();
333 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
334 for (;anIt!=aLast && !aFPoint; anIt++) {
335 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
336 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
337 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
341 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
345 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(FeaturePtr theSketch)
347 boost::shared_ptr<GeomAPI_Pln> aPlane;
348 double aA, aB, aC, aD;
350 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
351 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
352 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
353 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
354 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
360 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
364 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(
365 boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
366 FeaturePtr theSketch)
368 boost::shared_ptr<GeomAPI_Pnt> aPoint;
369 if (!theSketch || !thePoint2D)
372 boost::shared_ptr<GeomDataAPI_Point> aC =
373 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theSketch->data()->attribute(SKETCH_ATTR_ORIGIN));
374 boost::shared_ptr<GeomDataAPI_Dir> aX =
375 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRX));
376 boost::shared_ptr<GeomDataAPI_Dir> aY =
377 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRY));
379 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
382 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
384 return theKind == PartSet_ConstraintLengthPrs::getKind() ||
385 theKind == PartSet_ConstraintDistancePrs::getKind() ||
386 theKind == PartSet_ConstraintRadiusPrs::getKind();