1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <PartSet_Tools.h>
22 #include <PartSet_Module.h>
23 #include <PartSet_SketcherMgr.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_Tools.h>
35 #include <ModuleBase_IViewWindow.h>
37 #include <ModelGeomAlgo_Point2D.h>
39 #include <Events_Loop.h>
41 #include <SketcherPrs_Tools.h>
43 #include <XGUI_ModuleConnector.h>
44 #include <XGUI_Displayer.h>
45 #include <XGUI_Workshop.h>
46 #include <XGUI_SelectionMgr.h>
47 #include <XGUI_Selection.h>
49 #include <GeomDataAPI_Point.h>
50 #include <GeomDataAPI_Dir.h>
51 #include <GeomDataAPI_Point2D.h>
52 #include <GeomAPI_Pln.h>
53 #include <GeomAPI_Pnt2d.h>
54 #include <GeomAPI_Pnt.h>
55 #include <GeomAPI_Edge.h>
56 #include <GeomAPI_Vertex.h>
58 #include <GeomAPI_Dir.h>
59 #include <GeomAPI_XYZ.h>
61 #include <SketchPlugin_Feature.h>
62 #include <SketchPlugin_Sketch.h>
63 #include <SketchPlugin_ConstraintCoincidence.h>
64 #include <SketchPlugin_ConstraintDistance.h>
65 #include <SketchPlugin_ConstraintLength.h>
66 #include <SketchPlugin_ConstraintRadius.h>
67 #include <SketchPlugin_ConstraintRigid.h>
68 #include <SketchPlugin_Constraint.h>
69 #include <SketchPlugin_Circle.h>
70 #include <SketchPlugin_Arc.h>
71 #include <SketchPlugin_Line.h>
72 #include <SketchPlugin_Point.h>
73 #include <SketchPlugin_Projection.h>
75 #include <ModuleBase_IWorkshop.h>
76 #include <ModuleBase_ViewerPrs.h>
77 #include <ModuleBase_Tools.h>
79 #include <V3d_View.hxx>
81 #include <gp_Circ.hxx>
82 #include <ProjLib.hxx>
84 #include <Geom_Line.hxx>
85 #include <GeomAPI_ProjectPointOnCurve.hxx>
86 #include <BRep_Tool.hxx>
88 #include <TopoDS_Edge.hxx>
89 #include <TopoDS_Vertex.hxx>
90 #include <AIS_InteractiveObject.hxx>
91 #include <StdSelect_BRepOwner.hxx>
92 #include <SelectMgr_IndexedMapOfOwner.hxx>
93 #include <V3d_Coordinate.hxx>
95 #include <QMouseEvent>
101 const double PRECISION_TOLERANCE = 0.000001;
102 const int AIS_DEFAULT_WIDTH = 2;
103 const bool SKETCH_PROJECTION_INCLUDE_INTO_RESULT = false; // by default, it is not presented
105 int PartSet_Tools::getAISDefaultWidth()
107 return AIS_DEFAULT_WIDTH;
110 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
112 if (theView.IsNull())
115 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
116 theView->Eye(XEye, YEye, ZEye);
118 theView->At(XAt, YAt, ZAt);
119 gp_Pnt EyePoint(XEye, YEye, ZEye);
120 gp_Pnt AtPoint(XAt, YAt, ZAt);
122 gp_Vec EyeVector(EyePoint, AtPoint);
123 gp_Dir EyeDir(EyeVector);
125 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
126 Standard_Real X, Y, Z;
127 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
128 gp_Pnt ConvertedPoint(X, Y, Z);
130 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
131 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
136 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
137 Handle(V3d_View) theView,
138 double& theX, double& theY)
143 AttributeDoublePtr anAttr;
144 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
146 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
147 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
149 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
150 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
151 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
152 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
153 std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
155 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
156 gp_Vec aVec(anOriginPnt, thePoint);
158 if (!theView.IsNull()) {
159 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
160 theView->Eye(XEye, YEye, ZEye);
162 theView->At(XAt, YAt, ZAt);
163 gp_Pnt EyePoint(XEye, YEye, ZEye);
164 gp_Pnt AtPoint(XAt, YAt, ZAt);
166 gp_Vec anEyeVec(EyePoint, AtPoint);
167 anEyeVec.Normalize();
169 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
170 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
171 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
173 double aDen = anEyeVec * aNormalVec;
174 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
176 gp_Vec aDeltaVec = anEyeVec * aLVec;
177 aVec = aVec - aDeltaVec;
179 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
180 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
183 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
184 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
186 std::shared_ptr<GeomAPI_Pnt2d> aRes;
187 if (theSketch->getKind() != SketchPlugin_Sketch::ID())
189 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
190 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
191 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
192 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
193 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
194 theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
195 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
196 return thePnt->to2D(aC->pnt(), aX->dir(), aY);
200 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY,
201 FeaturePtr theSketch)
203 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
205 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
206 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
207 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
208 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
209 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
210 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
211 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
213 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
214 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
216 return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
219 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
221 return ModelAPI_Session::get()->moduleDocument();
224 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
225 const std::string& theAttribute)
229 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
230 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
231 aData->attribute(theAttribute));
233 anAttribute->setValue(theValue);
236 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
242 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
243 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
244 aData->attribute(theAttribute));
246 aValue = anAttribute->value();
253 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
254 const std::string& theKind)
260 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
261 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
262 ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
264 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
265 if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
266 aFeature = FeaturePtr();
272 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
273 std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
274 std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
278 aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
280 std::shared_ptr<ModelAPI_Document> aDoc = document();
281 aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
284 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
286 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
287 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
288 aRef1->setAttr(thePoint1);
290 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
291 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
292 aRef2->setAttr(thePoint2);
294 // we need to flush created signal in order to coincidence is processed by solver
295 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
298 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
300 std::shared_ptr<GeomAPI_Pln> aPlane;
302 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
303 theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
304 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
305 theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
307 if (aNormal.get() && aNormal->isInitialized() &&
308 anOrigin.get() && anOrigin->isInitialized())
309 aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNormal->dir()));
314 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
315 CompositeFeaturePtr theSketch)
317 std::shared_ptr<GeomAPI_Pnt> aPoint;
318 if (!theSketch || !thePoint2D)
321 DataPtr aData = theSketch->data();
322 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
323 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
324 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
325 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
326 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
327 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
328 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
330 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
333 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
334 const ObjectPtr& theObject,
335 CompositeFeaturePtr theSketch)
337 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
341 for (int i = 0, aNbSubs = theSketch->numberOfSubs(); i < aNbSubs; i++) {
342 FeaturePtr aFeature = theSketch->subFeature(i);
343 if (aFeature->getKind() != SketchPlugin_Projection::PROJECTED_FEATURE_ID())
345 if (aFeature->lastResult() == aResult)
351 ResultPtr PartSet_Tools::createFixedObjectByExternal(
352 const std::shared_ptr<GeomAPI_Shape>& theShape,
353 const ObjectPtr& theObject,
354 CompositeFeaturePtr theSketch,
355 const bool theTemporary,
356 FeaturePtr& theCreatedFeature)
358 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
362 FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
363 theCreatedFeature = aProjectionFeature;
364 AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
365 aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
366 anExternalAttr->setValue(aResult, theShape);
368 AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
369 (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
370 anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
371 aProjectionFeature->execute();
373 // if projection feature has not been created, exit
374 AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
375 SketchPlugin_Projection::PROJECTED_FEATURE_ID());
376 if (!aRefAttr || !aRefAttr->isInitialized())
379 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
380 if (aProjection.get() && aProjection->lastResult().get())
381 return aProjection->lastResult();
386 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
387 const ModuleBase_ViewerPrsPtr& thePrs)
389 foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
390 if (aPrs->object() == thePrs->object())
396 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
397 ModuleBase_IWorkshop* theWorkshop)
400 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
401 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
403 // 2. find visualized vertices of the attribute and if the attribute of the vertex is
404 // the same, return it
405 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
406 // 2.1 get visualized results of the feature
407 const std::list<ResultPtr>& aResList = anAttributeFeature->results();
408 std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
409 for (; anIt != aLast; anIt++) {
410 AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
411 if (aAISObj.get() != NULL) {
412 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
413 // 2.2 find selected owners of a visualizedd object
414 SelectMgr_IndexedMapOfOwner aSelectedOwners;
415 aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
416 for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
417 Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
418 if (!anOwner.IsNull()) {
419 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
420 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
421 const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
422 if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
423 // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
424 // attribute, returns the shape
425 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
426 PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
427 AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
428 aBRepShape, aSketchMgr->activeSketch());
429 if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
430 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
431 aShape->setImpl(new TopoDS_Shape(aBRepShape));
443 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(
444 std::shared_ptr<ModelAPI_Feature>& theFeature,
445 const std::string& theAttribute)
447 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
448 theFeature.get(), theAttribute, SketchPlugin_Point::ID(),
449 SketchPlugin_Point::COORD_ID());
450 if (aPointAttr.get() != NULL)
451 return aPointAttr->pnt();
452 return std::shared_ptr<GeomAPI_Pnt2d>();
455 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPnt2d(QMouseEvent* theEvent,
456 ModuleBase_IViewWindow* theWindow,
457 const FeaturePtr& theSketch)
459 gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
461 Handle(V3d_View) aView = theWindow->v3dView();
462 PartSet_Tools::convertTo2D(aPnt, theSketch, aView, aX, anY);
464 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY));
467 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData,
468 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
470 FeaturePtr aCoincident;
472 const std::set<AttributePtr>& aRefsList = theData->refsToMe();
473 std::set<AttributePtr>::const_iterator aIt;
474 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
475 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
476 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
477 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
478 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
479 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
480 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
481 aCoincident = aConstrFeature;
484 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
485 SketchPlugin_ConstraintCoincidence::ENTITY_B());
486 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
487 aCoincident = aConstrFeature;
496 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
497 std::shared_ptr<GeomAPI_Pnt2d> thePoint)
499 FeaturePtr aCoincident;
500 if (theFeature.get() == NULL)
503 const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
504 std::set<AttributePtr>::const_iterator aIt;
505 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
506 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
507 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
508 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
509 std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
510 PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
511 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
512 aCoincident = aConstrFeature;
515 a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
516 SketchPlugin_ConstraintCoincidence::ENTITY_B());
517 if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
518 aCoincident = aConstrFeature;
525 if (!aCoincident.get()) {
526 std::list<ResultPtr> aResults = theFeature->results();
527 std::list<ResultPtr>::const_iterator aIt;
528 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
529 ResultPtr aResult = *aIt;
530 aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
531 if (aCoincident.get())
538 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
539 QList<FeaturePtr>& theCoincidencies,
540 std::string theAttr, QList<bool>& theIsAttributes)
542 std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
543 if (aOrig.get() == NULL)
546 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
549 ObjectPtr aObj = aPnt->object();
550 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
551 if (aFeature.get()) {
552 if (!theList.contains(aFeature)) {
553 theList.append(aFeature);
554 theCoincidencies.append(theStartCoin);
555 theIsAttributes.append(true); // point attribute on a feature
556 const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
557 std::set<AttributePtr>::const_iterator aIt;
558 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
559 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
560 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
561 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
562 if (!theCoincidencies.contains(aConstrFeature)) {
563 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
564 if (aPnt.get() && aOrig->isEqual(aPnt)) {
565 findCoincidences(aConstrFeature, theList, theCoincidencies,
566 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
567 findCoincidences(aConstrFeature, theList, theCoincidencies,
568 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
576 ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
578 FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
579 if (!theList.contains(aFeature))
580 theList.append(aFeature);
581 theCoincidencies.append(theStartCoin);
582 theIsAttributes.append(false); // point attribute on a feature
584 const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
585 std::set<AttributePtr>::const_iterator aIt;
586 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
587 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
588 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
589 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
590 if (!theCoincidencies.contains(aConstrFeature)) {
591 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
592 if (aPnt.get() && aOrig->isEqual(aPnt)) {
593 findCoincidences(aConstrFeature, theList, theCoincidencies,
594 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
595 findCoincidences(aConstrFeature, theList, theCoincidencies,
596 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
605 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
607 std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
608 SketchPlugin_Constraint::ENTITY_A());
609 if (aPnt.get() == NULL)
610 aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
614 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
615 const TopoDS_Shape theShape,
616 FeaturePtr theSketch)
619 AttributePtr anAttribute;
620 FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
622 if (theShape.ShapeType() == TopAbs_VERTEX) {
623 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
624 if (!aVertex.IsNull()) {
625 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
626 std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
627 new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
629 // find the given point in the feature attributes
630 std::list<AttributePtr> anAttiributes =
631 aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
632 std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
633 aLast = anAttiributes.end();
634 for (; anIt != aLast && !anAttribute; anIt++) {
635 std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
636 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
637 if (!aCurPoint->isInitialized())
640 std::shared_ptr<GeomAPI_Pnt> aPnt =
641 convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
642 if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
643 anAttribute = aCurPoint;
653 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
654 const Events_ID theEventId)
656 if (!theComposite.get())
659 static Events_Loop* aLoop = Events_Loop::loop();
660 for (int i = 0; i < theComposite->numberOfSubs(); i++) {
661 FeaturePtr aSubFeature = theComposite->subFeature(i);
662 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
663 aECreator->sendUpdated(aSubFeature, theEventId);
665 Events_Loop::loop()->flush(theEventId);
668 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
670 bool isAuxiliaryFeature = false;
672 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
673 std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
674 AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
675 anObjectFeature->data()->attribute(anAuxiliaryAttribute));
676 if (anAuxiliaryAttr.get())
677 isAuxiliaryFeature = anAuxiliaryAttr->value();
680 return isAuxiliaryFeature;
684 ResultPtr PartSet_Tools::createFixedByExternalCenter(
685 const ObjectPtr& theObject,
686 const std::shared_ptr<GeomAPI_Edge>& theEdge,
687 ModelAPI_AttributeSelection::CenterType theType,
688 const CompositeFeaturePtr& theSketch,
691 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
695 FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
696 AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
697 aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
698 anExternalAttr->setValueCenter(aResult, theEdge, theType, theTemporary);
700 AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
701 (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
702 anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
703 aProjectionFeature->execute();
705 // if projection feature has not been created, exit
706 AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
707 SketchPlugin_Projection::PROJECTED_FEATURE_ID());
708 if (!aRefAttr || !aRefAttr->isInitialized())
711 FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
712 if (aProjection.get() && aProjection->lastResult().get())
713 return aProjection->lastResult();