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 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
182 double theX, double theY)
184 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
185 new GeomAPI_Pnt2d(theX, theY));
186 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
187 theFeature->data()->attributes(GeomDataAPI_Point2D::type());
188 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
189 aLast = anAttiributes.end();
190 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
191 for (; anIt != aLast && !aFPoint; anIt++) {
192 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
193 GeomDataAPI_Point2D>(*anIt);
194 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
201 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
202 const std::string& theAttribute)
206 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
207 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
208 aData->attribute(theAttribute));
210 anAttribute->setValue(theValue);
213 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
219 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
220 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
221 aData->attribute(theAttribute));
223 aValue = anAttribute->value();
230 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
231 const std::string& theKind)
237 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
238 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
239 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
241 aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
242 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
243 aFeature = FeaturePtr();
249 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
250 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
251 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
255 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
257 boost::shared_ptr<ModelAPI_Document> aDoc = document();
258 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
261 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
263 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
264 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
265 aRef1->setAttr(thePoint1);
267 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
268 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
269 aRef2->setAttr(thePoint2);
271 if (aFeature) // TODO: generate an error if feature was not created
275 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
276 const std::string& theAttribute, double theClickedX,
279 // find a feature point by the selection mode
280 //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
281 boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
282 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
286 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
287 // the constraint is created between the feature point and the found sketch point
288 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
289 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
290 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
292 std::list<ObjectPtr> aFeatures = aRefList->list();
293 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
294 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
295 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
296 new GeomAPI_Pnt2d(theClickedX, theClickedY));
297 for (; anIt != aLast; anIt++) {
298 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
299 // find the given point in the feature attributes
300 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
301 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
302 aLast = anAttiributes.end();
303 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
304 for (; anIt != aLast && !aFPoint; anIt++) {
305 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
306 GeomDataAPI_Point2D>(*anIt);
307 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
311 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
315 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
317 boost::shared_ptr<GeomAPI_Pln> aPlane;
318 double aA, aB, aC, aD;
320 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
321 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
322 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
323 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
324 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
330 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
334 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
335 CompositeFeaturePtr theSketch)
337 boost::shared_ptr<GeomAPI_Pnt> aPoint;
338 if (!theSketch || !thePoint2D)
341 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
342 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
343 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
344 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
345 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
346 theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
348 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
351 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
353 return theKind == SketchPlugin_ConstraintDistance::ID()
354 || theKind == SketchPlugin_ConstraintLength::ID()
355 || theKind == SketchPlugin_ConstraintRadius::ID()
356 || theKind == SketchPlugin_ConstraintRigid::ID();
359 ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
361 TopoDS_Shape aShape = thePrs.shape();
362 if (aShape.ShapeType() != TopAbs_EDGE)
365 Standard_Real aStart, aEnd;
366 Handle(V3d_View) aNullView;
367 FeaturePtr aMyFeature;
369 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
370 GeomAdaptor_Curve aAdaptor(aCurve);
371 if (aAdaptor.GetType() == GeomAbs_Line) {
373 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
375 //DataPtr aData = myFeature->data();
376 //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
377 // boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
380 //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
381 //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
382 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
384 //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
385 //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
386 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
387 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
388 if (aAdaptor.IsClosed()) {
390 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
391 //gp_Circ aCirc = aAdaptor.Circle();
392 //gp_Pnt aCenter = aCirc.Location();
395 //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
396 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
397 //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
400 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
404 DataPtr aData = aMyFeature->data();
405 AttributeSelectionPtr anAttr =
406 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
407 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
409 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
410 if (anAttr && aRes) {
411 boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
412 anEdge->setImpl(new TopoDS_Shape(aShape));
414 anAttr->setValue(aRes, anEdge);
415 aMyFeature->execute();
416 return aMyFeature->lastResult();
422 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
423 const ModuleBase_ViewerPrs& thePrs)
425 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
426 if (aPrs.object() == thePrs.object())