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 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
151 FeaturePtr theSketch,
152 const QList<ModuleBase_ViewerPrs>& theFeatures)
155 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
156 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
159 FeaturePtr aDeltaFeature;
160 double aMinDelta = -1;
161 ModuleBase_ViewerPrs aPrs;
162 foreach (ModuleBase_ViewerPrs aPrs, theFeatures) {
165 boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
166 SketchPlugin_Feature>(aPrs.object());
169 double aDelta = aSketchFeature->distanceToPoint(
170 boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
171 if (aMinDelta < 0 || aMinDelta > aDelta) {
173 // TODO aDeltaFeature = aPrs.result();
176 return aDeltaFeature;
179 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
181 return ModelAPI_Session::get()->moduleDocument();
184 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
185 double theX, double theY)
187 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
188 new GeomAPI_Pnt2d(theX, theY));
189 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
190 theFeature->data()->attributes(GeomDataAPI_Point2D::type());
191 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
192 aLast = anAttiributes.end();
193 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
194 for (; anIt != aLast && !aFPoint; anIt++) {
195 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
196 GeomDataAPI_Point2D>(*anIt);
197 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
204 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
205 const std::string& theAttribute)
209 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
210 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
211 aData->attribute(theAttribute));
213 anAttribute->setValue(theValue);
216 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
222 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
223 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
224 aData->attribute(theAttribute));
226 aValue = anAttribute->value();
233 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
234 const std::string& theKind)
240 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
241 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
242 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
244 aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
245 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
246 aFeature = FeaturePtr();
252 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
253 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
254 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
258 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
260 boost::shared_ptr<ModelAPI_Document> aDoc = document();
261 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
264 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
266 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
267 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
268 aRef1->setAttr(thePoint1);
270 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
271 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
272 aRef2->setAttr(thePoint2);
274 if (aFeature) // TODO: generate an error if feature was not created
278 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
279 const std::string& theAttribute, double theClickedX,
282 // find a feature point by the selection mode
283 //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
284 boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
285 GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
289 // get all sketch features. If the point with the given coordinates belong to any sketch feature,
290 // the constraint is created between the feature point and the found sketch point
291 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
292 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
293 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
295 std::list<ObjectPtr> aFeatures = aRefList->list();
296 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
297 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
298 boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
299 new GeomAPI_Pnt2d(theClickedX, theClickedY));
300 for (; anIt != aLast; anIt++) {
301 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
302 // find the given point in the feature attributes
303 anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
304 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
305 aLast = anAttiributes.end();
306 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
307 for (; anIt != aLast && !aFPoint; anIt++) {
308 boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
309 GeomDataAPI_Point2D>(*anIt);
310 if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
314 PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
318 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
320 boost::shared_ptr<GeomAPI_Pln> aPlane;
321 double aA, aB, aC, aD;
323 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
324 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
325 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
326 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
327 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
333 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
337 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
338 CompositeFeaturePtr theSketch)
340 boost::shared_ptr<GeomAPI_Pnt> aPoint;
341 if (!theSketch || !thePoint2D)
344 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
345 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
346 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
347 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
348 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
349 theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
351 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
354 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
356 return theKind == SketchPlugin_ConstraintDistance::ID()
357 || theKind == SketchPlugin_ConstraintLength::ID()
358 || theKind == SketchPlugin_ConstraintRadius::ID()
359 || theKind == SketchPlugin_ConstraintRigid::ID();
362 ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
364 TopoDS_Shape aShape = thePrs.shape();
365 if (aShape.ShapeType() != TopAbs_EDGE)
368 // Check that we already have such external edge
369 boost::shared_ptr<GeomAPI_Edge> aInEdge = boost::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
370 aInEdge->setImpl(new TopoDS_Shape(aShape));
371 ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
375 // If not found then we have to create new
376 Standard_Real aStart, aEnd;
377 Handle(V3d_View) aNullView;
378 FeaturePtr aMyFeature;
380 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
381 GeomAdaptor_Curve aAdaptor(aCurve);
382 if (aAdaptor.GetType() == GeomAbs_Line) {
384 aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
386 //DataPtr aData = myFeature->data();
387 //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
388 // boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
391 //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
392 //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
393 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
395 //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
396 //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
397 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
398 } else if (aAdaptor.GetType() == GeomAbs_Circle) {
399 if (aAdaptor.IsClosed()) {
401 aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
402 //gp_Circ aCirc = aAdaptor.Circle();
403 //gp_Pnt aCenter = aCirc.Location();
406 //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
407 //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
408 //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
411 aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
415 DataPtr aData = aMyFeature->data();
416 AttributeSelectionPtr anAttr =
417 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
418 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
420 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
421 if (anAttr && aRes) {
422 boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
423 anEdge->setImpl(new TopoDS_Shape(aShape));
425 anAttr->setValue(aRes, anEdge);
427 aMyFeature->execute();
430 FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
431 aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
432 setObject(aMyFeature->lastResult());
434 return aMyFeature->lastResult();
440 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
441 const ModuleBase_ViewerPrs& thePrs)
443 foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
444 if (aPrs.object() == thePrs.object())
450 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, boost::shared_ptr<GeomAPI_Edge> theEdge)
452 for (int i = 0; i < theSketch->numberOfSubs(); i++) {
453 FeaturePtr aFeature = theSketch->subFeature(i);
454 boost::shared_ptr<SketchPlugin_Feature> aSketchFea =
455 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
457 if (aSketchFea->isExternal()) {
458 std::list<ResultPtr> aResults = aSketchFea->results();
459 std::list<ResultPtr>::const_iterator aIt;
460 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
461 ResultConstructionPtr aRes =
462 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
464 boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
466 if (theEdge->isEqual(aShape))