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_Document.h>
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_Pnt2d.h>
16 #include <GeomAPI_Pnt.h>
18 #include <GeomAPI_Dir.h>
19 #include <GeomAPI_XYZ.h>
21 #include <SketchPlugin_Feature.h>
22 #include <SketchPlugin_Sketch.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_Constraint.h>
26 #include <PartSet_FeatureLinePrs.h>
27 #include <PartSet_FeaturePointPrs.h>
28 #include <PartSet_FeatureCirclePrs.h>
29 #include <PartSet_FeatureArcPrs.h>
31 #include <PartSet_ConstraintLengthPrs.h>
32 #include <PartSet_ConstraintRadiusPrs.h>
33 #include <PartSet_ConstraintDistancePrs.h>
35 #include <XGUI_ViewerPrs.h>
37 #include <V3d_View.hxx>
39 #include <ProjLib.hxx>
41 #include <Geom_Line.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
48 const double PRECISION_TOLERANCE = 0.000001;
50 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
55 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
56 theView->Eye(XEye, YEye, ZEye);
58 theView->At(XAt, YAt, ZAt);
59 gp_Pnt EyePoint(XEye, YEye, ZEye);
60 gp_Pnt AtPoint(XAt, YAt, ZAt);
62 gp_Vec EyeVector(EyePoint, AtPoint);
63 gp_Dir EyeDir(EyeVector);
65 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
66 Standard_Real X, Y, Z;
67 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
68 gp_Pnt ConvertedPoint(X, Y, Z);
70 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
71 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
75 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
76 Handle(V3d_View) theView, double& theX, double& theY)
81 AttributeDoublePtr anAttr;
82 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
84 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
85 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
87 boost::shared_ptr<GeomDataAPI_Dir> aX =
88 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
89 boost::shared_ptr<GeomDataAPI_Dir> anY =
90 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
92 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
93 gp_Vec aVec(anOriginPnt, thePoint);
95 if (!theView.IsNull())
97 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
98 theView->Eye(XEye, YEye, ZEye);
100 theView->At(XAt, YAt, ZAt);
101 gp_Pnt EyePoint(XEye, YEye, ZEye);
102 gp_Pnt AtPoint(XAt, YAt, ZAt);
104 gp_Vec anEyeVec(EyePoint, AtPoint);
105 anEyeVec.Normalize();
107 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
108 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
109 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
111 double aDen = anEyeVec * aNormalVec;
112 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
114 gp_Vec aDeltaVec = anEyeVec*aLVec;
115 aVec = aVec - aDeltaVec;
117 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
118 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
121 void PartSet_Tools::convertTo3D(const double theX, const double theY,
122 FeaturePtr theSketch,
128 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
130 boost::shared_ptr<GeomDataAPI_Point> aC =
131 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
132 boost::shared_ptr<GeomDataAPI_Dir> aX =
133 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
134 boost::shared_ptr<GeomDataAPI_Dir> aY =
135 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
137 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
138 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
140 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
141 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
144 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
145 FeaturePtr theSketch,
146 FeaturePtr theFeature)
148 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
150 if (theKind == PartSet_FeaturePointPrs::getKind()) {
151 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
153 else if (theKind == PartSet_FeatureLinePrs::getKind()) {
154 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
156 else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
157 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
159 else if (theKind == PartSet_FeatureArcPrs::getKind()) {
160 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
162 else if (theKind == PartSet_ConstraintLengthPrs::getKind()) {
163 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_ConstraintLengthPrs(theSketch));
165 else if (theKind == PartSet_ConstraintRadiusPrs::getKind()) {
166 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintRadiusPrs>(new PartSet_ConstraintRadiusPrs(theSketch));
168 else if (theKind == PartSet_ConstraintDistancePrs::getKind()) {
169 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintDistancePrs>(new PartSet_ConstraintDistancePrs(theSketch));
173 if (theFeature && aFeaturePrs)
174 aFeaturePrs->init(theFeature);
179 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
180 FeaturePtr theSketch,
181 const std::list<XGUI_ViewerPrs>& theFeatures)
184 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
185 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
188 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
190 FeaturePtr aDeltaFeature;
191 double aMinDelta = -1;
193 for (; anIt != aLast; anIt++) {
197 double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
198 if (aMinDelta < 0 || aMinDelta > aDelta) {
200 aDeltaFeature = aPrs.feature();
203 return aDeltaFeature;
206 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
207 double theX, double theY)
209 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
210 theFeature->getKind(), FeaturePtr(), theFeature);
213 aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
218 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
220 return ModelAPI_PluginManager::get()->rootDocument();
223 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
224 const std::string& theAttribute)
228 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
229 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
230 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
232 aPoint->setValue(theX, theY);
235 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
236 const std::string& theAttribute)
240 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
241 AttributeDoublePtr anAttribute =
242 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
244 anAttribute->setValue(theValue);
247 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
253 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
254 AttributeDoublePtr anAttribute =
255 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
257 aValue = anAttribute->value();
264 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
265 const std::string& theKind)
271 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
272 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
273 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
275 aFeature = anAttr->feature();
276 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
277 aFeature = FeaturePtr();
283 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
284 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
285 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
287 boost::shared_ptr<ModelAPI_Document> aDoc = document();
288 FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
291 boost::shared_ptr<SketchPlugin_Feature> aSketch =
292 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
293 aSketch->addSub(aFeature);
296 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
298 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
299 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
300 aRef1->setAttr(thePoint1);
302 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
303 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
304 aRef2->setAttr(thePoint2);
306 if (aFeature) // TODO: generate an error if feature was not created
310 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
311 double theX, double theY)
313 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
314 theFeature->getKind(), FeaturePtr(), theFeature);
316 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
318 aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
323 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(FeaturePtr theSketch)
325 boost::shared_ptr<GeomAPI_Pln> aPlane;
326 double aA, aB, aC, aD;
328 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
329 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
330 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
331 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
332 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
338 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
342 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(
343 boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
344 FeaturePtr theSketch)
346 boost::shared_ptr<GeomAPI_Pnt> aPoint;
347 if (!theSketch || !thePoint2D)
350 boost::shared_ptr<GeomDataAPI_Point> aC =
351 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theSketch->data()->attribute(SKETCH_ATTR_ORIGIN));
352 boost::shared_ptr<GeomDataAPI_Dir> aX =
353 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRX));
354 boost::shared_ptr<GeomDataAPI_Dir> aY =
355 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRY));
357 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
360 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
362 return theKind == PartSet_ConstraintLengthPrs::getKind() ||
363 theKind == PartSet_ConstraintDistancePrs::getKind() ||
364 theKind == PartSet_ConstraintRadiusPrs::getKind();