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>
12 #include <ModelAPI_ResultConstruction.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <GeomAPI_Pln.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_Edge.h>
22 #include <GeomAPI_Dir.h>
23 #include <GeomAPI_XYZ.h>
25 #include <SketchPlugin_Feature.h>
26 #include <SketchPlugin_Sketch.h>
27 #include <SketchPlugin_ConstraintCoincidence.h>
28 #include <SketchPlugin_ConstraintDistance.h>
29 #include <SketchPlugin_ConstraintLength.h>
30 #include <SketchPlugin_ConstraintRadius.h>
31 #include <SketchPlugin_ConstraintRigid.h>
32 #include <SketchPlugin_Constraint.h>
33 #include <SketchPlugin_Circle.h>
34 #include <SketchPlugin_Arc.h>
35 #include <SketchPlugin_Line.h>
37 #include <ModuleBase_ViewerPrs.h>
39 #include <V3d_View.hxx>
41 #include <gp_Circ.hxx>
42 #include <ProjLib.hxx>
44 #include <Geom_Line.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <BRep_Tool.hxx>
48 #include <TopoDS_Edge.hxx>
54 const double PRECISION_TOLERANCE = 0.000001;
56 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
61 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
62 theView->Eye(XEye, YEye, ZEye);
64 theView->At(XAt, YAt, ZAt);
65 gp_Pnt EyePoint(XEye, YEye, ZEye);
66 gp_Pnt AtPoint(XAt, YAt, ZAt);
68 gp_Vec EyeVector(EyePoint, AtPoint);
69 gp_Dir EyeDir(EyeVector);
71 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
72 Standard_Real X, Y, Z;
73 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
74 gp_Pnt ConvertedPoint(X, Y, Z);
76 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
77 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
82 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
83 Handle(V3d_View) theView,
84 double& theX, double& theY)
89 AttributeDoublePtr anAttr;
90 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
92 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
93 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
95 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
96 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
97 boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
98 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
100 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
101 gp_Vec aVec(anOriginPnt, thePoint);
103 if (!theView.IsNull()) {
104 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
105 theView->Eye(XEye, YEye, ZEye);
107 theView->At(XAt, YAt, ZAt);
108 gp_Pnt EyePoint(XEye, YEye, ZEye);
109 gp_Pnt AtPoint(XAt, YAt, ZAt);
111 gp_Vec anEyeVec(EyePoint, AtPoint);
112 anEyeVec.Normalize();
114 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
115 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
116 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
118 double aDen = anEyeVec * aNormalVec;
119 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
121 gp_Vec aDeltaVec = anEyeVec * aLVec;
122 aVec = aVec - aDeltaVec;
124 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
125 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
128 void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
134 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
136 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
137 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
138 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
139 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
140 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
141 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
143 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
144 ->added(aY->dir()->xyz()->multiplied(theY));
146 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
147 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
150 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
151 FeaturePtr theSketch,
152 const QList<ModuleBase_ViewerPrs>& theSelected,
153 const QList<ModuleBase_ViewerPrs>& theHighlighted)
155 ObjectPtr aDeltaObject;
156 // 1. find the object in the highlighted list
157 if (theHighlighted.size() > 0) {
158 aDeltaObject = theHighlighted.first().object();
160 // 2. find it in the selected list by the selected point
163 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
164 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
167 double aMinDelta = -1;
168 ModuleBase_ViewerPrs aPrs;
169 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
172 FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
173 boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
174 SketchPlugin_Feature>(aFeature);
178 double aDelta = aSketchFeature->distanceToPoint(
179 boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
180 if (aMinDelta < 0 || aMinDelta > aDelta) {
182 // TODO aDeltaObject = aPrs.result();
186 // 3. if the object is not found, returns the first selected one
187 if (!aDeltaObject && theSelected.size() > 0)
188 aDeltaObject = theSelected.first().object();
193 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
195 return ModelAPI_Session::get()->moduleDocument();
198 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
199 double theX, double theY)
201 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
202 new GeomAPI_Pnt2d(theX, theY));
203 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
204 theFeature->data()->attributes(GeomDataAPI_Point2D::type());
205 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
206 aLast = anAttiributes.end();
207 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
208 for (; anIt != aLast && !aFPoint; anIt++) {
209 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
210 GeomDataAPI_Point2D>(*anIt);
211 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
218 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
219 const std::string& theAttribute)
223 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
224 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
225 aData->attribute(theAttribute));
227 anAttribute->setValue(theValue);
230 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
236 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
237 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
238 aData->attribute(theAttribute));
240 aValue = anAttribute->value();
247 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
248 const std::string& theKind)
254 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
255 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
256 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
258 aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
259 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
260 aFeature = FeaturePtr();
266 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
267 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
268 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
272 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
274 boost::shared_ptr<ModelAPI_Document> aDoc = document();
275 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
278 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
280 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
281 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
282 aRef1->setAttr(thePoint1);
284 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
285 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
286 aRef2->setAttr(thePoint2);
288 if (aFeature) // TODO: generate an error if feature was not created
292 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
293 const std::string& theAttribute, double theClickedX,
296 // find a feature point by the selection mode
297 //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
298 boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
299 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
303 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
304 // the constraint is created between the feature point and the found sketch point
305 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
306 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
307 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
309 std::list<ObjectPtr> aFeatures = aRefList->list();
310 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
311 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
312 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
313 new GeomAPI_Pnt2d(theClickedX, theClickedY));
314 for (; anIt != aLast; anIt++) {
315 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
316 // find the given point in the feature attributes
317 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
318 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
319 aLast = anAttiributes.end();
320 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
321 for (; anIt != aLast && !aFPoint; anIt++) {
322 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
323 GeomDataAPI_Point2D>(*anIt);
324 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
328 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
332 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
334 boost::shared_ptr<GeomAPI_Pln> aPlane;
335 double aA, aB, aC, aD;
337 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
338 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
339 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
340 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
341 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
347 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
351 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
352 CompositeFeaturePtr theSketch)
354 boost::shared_ptr<GeomAPI_Pnt> aPoint;
355 if (!theSketch || !thePoint2D)
358 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
359 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
360 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
361 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
362 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
363 theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
365 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
368 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
370 return theKind == SketchPlugin_ConstraintDistance::ID()
371 || theKind == SketchPlugin_ConstraintLength::ID()
372 || theKind == SketchPlugin_ConstraintRadius::ID()
373 || theKind == SketchPlugin_ConstraintRigid::ID();
376 ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
378 TopoDS_Shape aShape = thePrs.shape();
379 if (aShape.ShapeType() != TopAbs_EDGE)
382 // Check that we already have such external edge
383 boost::shared_ptr<GeomAPI_Edge> aInEdge = boost::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
384 aInEdge->setImpl(new TopoDS_Shape(aShape));
385 ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
389 // If not found then we have to create new
390 Standard_Real aStart, aEnd;
391 Handle(V3d_View) aNullView;
392 FeaturePtr aMyFeature;
394 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
395 GeomAdaptor_Curve aAdaptor(aCurve);
396 if (aAdaptor.GetType() == GeomAbs_Line) {
398 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
400 //DataPtr aData = myFeature->data();
401 //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
402 // boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
405 //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
406 //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
407 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
409 //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
410 //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
411 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
412 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
413 if (aAdaptor.IsClosed()) {
415 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
416 //gp_Circ aCirc = aAdaptor.Circle();
417 //gp_Pnt aCenter = aCirc.Location();
420 //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
421 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
422 //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
425 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
429 DataPtr aData = aMyFeature->data();
430 AttributeSelectionPtr anAttr =
431 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
432 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
434 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
435 if (anAttr && aRes) {
436 boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
437 anEdge->setImpl(new TopoDS_Shape(aShape));
439 anAttr->setValue(aRes, anEdge);
441 aMyFeature->execute();
444 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
445 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
446 setObject(aMyFeature->lastResult());
448 return aMyFeature->lastResult();
454 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
455 const ModuleBase_ViewerPrs& thePrs)
457 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
458 if (aPrs.object() == thePrs.object())
464 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, boost::shared_ptr<GeomAPI_Edge> theEdge)
466 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
467 FeaturePtr aFeature = theSketch->subFeature(i);
468 boost::shared_ptr<SketchPlugin_Feature> aSketchFea =
469 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
471 if (aSketchFea->isExternal()) {
472 std::list<ResultPtr> aResults = aSketchFea->results();
473 std::list<ResultPtr>::const_iterator aIt;
474 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
475 ResultConstructionPtr aRes =
476 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
478 boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
480 if (theEdge->isEqual(aShape))