1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetPoint2D.cpp
4 // Created: 25 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "PartSet_WidgetPoint2d.h"
8 #include <PartSet_Tools.h>
9 #include <PartSet_Module.h>
10 #include <PartSet_SketcherReetntrantMgr.h>
12 #include <XGUI_Tools.h>
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
16 #include <ModuleBase_ParamSpinBox.h>
17 #include <ModuleBase_Tools.h>
18 #include <ModuleBase_IViewer.h>
19 #include <ModuleBase_IViewWindow.h>
20 #include <ModuleBase_ISelection.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_WidgetValidator.h>
24 #include <Config_Keywords.h>
25 #include <Config_WidgetAPI.h>
27 #include <Events_Loop.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_AttributeBoolean.h>
31 #include <ModelAPI_Feature.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Object.h>
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Pnt2d.h>
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAPI_Vertex.h>
40 #include <SketchPlugin_Feature.h>
41 #include <SketchPlugin_ConstraintCoincidence.h>
42 #include <SketchPlugin_Line.h>
43 #include <SketchPlugin_Arc.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_Point.h>
48 #include <QGridLayout>
51 #include <QMouseEvent>
52 #include <QApplication>
55 #include <TopoDS_Vertex.hxx>
56 #include <BRep_Tool.hxx>
61 const double MaxCoordinate = 1e12;
63 static QStringList MyFeaturesForCoincedence;
65 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
66 ModuleBase_IWorkshop* theWorkshop,
67 const Config_WidgetAPI* theData)
68 : ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop),
69 myValueIsCashed(false), myIsFeatureVisibleInCash(true),
70 myXValueInCash(0), myYValueInCash(0)
72 if (MyFeaturesForCoincedence.isEmpty()) {
73 MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
74 << SketchPlugin_Arc::ID().c_str()
75 << SketchPlugin_Point::ID().c_str()
76 << SketchPlugin_Circle::ID().c_str();
79 // the control should accept the focus, so the boolean flag is corrected to be true
80 myIsObligatory = true;
81 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
82 myGroupBox = new QGroupBox(aPageName, theParent);
83 myGroupBox->setFlat(false);
85 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
87 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
88 ModuleBase_Tools::adjustMargins(aGroupLay);
89 aGroupLay->setSpacing(2);
90 aGroupLay->setColumnStretch(1, 1);
92 QLabel* aLabel = new QLabel(myGroupBox);
93 aLabel->setText(tr("X "));
94 aGroupLay->addWidget(aLabel, 0, 0);
96 myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
97 myXSpin->setAcceptVariables(aAcceptVariables);
98 myXSpin->setMinimum(-DBL_MAX);
99 myXSpin->setMaximum(DBL_MAX);
100 myXSpin->setToolTip(tr("X"));
101 aGroupLay->addWidget(myXSpin, 0, 1);
103 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
106 QLabel* aLabel = new QLabel(myGroupBox);
107 aLabel->setText(tr("Y "));
108 aGroupLay->addWidget(aLabel, 1, 0);
110 myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
111 myYSpin->setAcceptVariables(aAcceptVariables);
112 myYSpin->setMinimum(-DBL_MAX);
113 myYSpin->setMaximum(DBL_MAX);
114 myYSpin->setToolTip(tr("Y"));
115 aGroupLay->addWidget(myYSpin, 1, 1);
117 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
119 QVBoxLayout* aLayout = new QVBoxLayout(this);
120 ModuleBase_Tools::zeroMargins(aLayout);
121 aLayout->addWidget(myGroupBox);
124 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
127 bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
131 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
132 if (aModule->sketchReentranceMgr()->isInternalEditActive())
133 return true; /// when internal edit is started a new feature is created. I has not results, AIS
135 // workaround for feature, where there is no results
136 //if (myFeature->getKind() == "SketchRectangle")
139 /// the selection is not possible if the current feature has no presentation for the current
140 /// attribute not in AIS not in results. If so, no object in current feature where make
141 /// coincidence, so selection is not necessary
142 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
143 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
144 aData->attribute(attributeID()));
145 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
147 bool aFoundPoint = false;
148 GeomShapePtr anAISShape;
149 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(myFeature);
152 anAIS = aPrs->getAISObject(anAIS);
154 anAISShape = anAIS->getShape();
157 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = myFeature->results();
158 if (!anAISShape.get() && aResults.empty())
162 if (anAISShape.get())
163 aFoundPoint = shapeContainsPoint(anAISShape, aPoint, mySketch);
165 /// analysis of results
166 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
167 for (; aRIter != aResults.cend() && !aFoundPoint; aRIter++) {
168 ResultPtr aResult = *aRIter;
169 if (aResult.get() && aResult->shape().get()) {
170 GeomShapePtr aShape = aResult->shape();
171 aFoundPoint = shapeContainsPoint(aShape, aPoint, mySketch);
177 bool PartSet_WidgetPoint2D::resetCustom()
180 if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
184 if (myValueIsCashed) {
185 // if the restored value should be hidden, aDone = true to set
186 // reset state for the widget in the parent
187 aDone = restoreCurentValue();
188 emit objectUpdated();
192 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
193 // it is important to block the spin box control in order to do not through out the
194 // locking of the validating state.
195 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
196 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
204 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
208 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
209 const bool theToValidate)
212 if (theValues.empty())
215 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
216 GeomShapePtr aShape = aValue->shape();
217 if (aShape.get() && !aShape->isNull()) {
218 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
220 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
221 if (getPoint2d(aView, aTDShape, aX, aY)) {
222 isDone = setPoint(aX, aY);
223 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
226 else if (canBeActivatedByMove()) {
227 if (feature()->getKind() == SketchPlugin_Line::ID()) {
228 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue->object());
229 // Initialize new line with first point equal to end of previous
230 if (aFeature.get()) {
231 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
232 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
233 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
234 aData->attribute(SketchPlugin_Line::END_ID()));
236 setPoint(aPoint->x(), aPoint->y());
237 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aPoint->x(),
247 void PartSet_WidgetPoint2D::selectContent()
249 myXSpin->selectAll();
252 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
254 if (fabs(theX) >= MaxCoordinate)
256 if (fabs(theY) >= MaxCoordinate)
259 ModuleBase_Tools::setSpinValue(myXSpin, theX);
260 ModuleBase_Tools::setSpinValue(myYSpin, theY);
266 bool PartSet_WidgetPoint2D::storeValueCustom()
268 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
269 if (!aData) // can be on abort of sketcher element
271 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
272 aData->attribute(attributeID()));
274 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
275 bool isBlocked = that->blockSignals(true);
276 bool isImmutable = aPoint->setImmutable(true);
278 // if text is not empty then setValue will be ignored
279 // so we should set the text at first
280 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
281 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
282 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
283 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
285 // after movement the solver will call the update event: optimization
286 moveObject(myFeature);
287 aPoint->setImmutable(isImmutable);
288 that->blockSignals(isBlocked);
293 bool PartSet_WidgetPoint2D::restoreValueCustom()
295 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
296 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
297 aData->attribute(attributeID()));
298 QString aTextX = QString::fromStdString(aPoint->textX());
299 QString aTextY = QString::fromStdString(aPoint->textY());
301 bool isDouble = false;
303 if (aTextX.isEmpty()) {
304 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
306 aVal = aTextX.toDouble(&isDouble);
308 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
310 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
312 if (aTextY.isEmpty()) {
313 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
315 aVal = aTextY.toDouble(&isDouble);
317 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
319 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
321 //if (aTextX.empty() || aTextY.empty()) {
322 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
323 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
325 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
326 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
331 void PartSet_WidgetPoint2D::storeCurentValue()
333 // do not use cash if a variable is used
334 if (myXSpin->hasVariable() || myYSpin->hasVariable())
337 myValueIsCashed = true;
338 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
339 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
340 myXValueInCash = myXSpin->value();
341 myYValueInCash = myYSpin->value();
344 bool PartSet_WidgetPoint2D::restoreCurentValue()
346 bool aRestoredAndHidden = true;
348 bool isVisible = myIsFeatureVisibleInCash;
349 // fill the control widgets by the cashed value
351 myValueIsCashed = false;
352 myIsFeatureVisibleInCash = true;
353 ModuleBase_Tools::setSpinValue(myXSpin, myXValueInCash);
354 ModuleBase_Tools::setSpinValue(myYSpin, myYValueInCash);
356 // store value to the model
359 setValueState(Stored);
360 aRestoredAndHidden = false;
363 aRestoredAndHidden = true;
365 return aRestoredAndHidden;
368 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
370 QList<QWidget*> aControls;
371 aControls.append(myXSpin);
372 aControls.append(myYSpin);
377 void PartSet_WidgetPoint2D::activateCustom()
379 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
380 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
381 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
382 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
383 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
386 aModes << TopAbs_VERTEX;
387 aModes << TopAbs_EDGE;
388 myWorkshop->activateSubShapesSelection(aModes);
390 if (!isEditingMode()) {
391 FeaturePtr aFeature = feature();
392 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID())
397 bool PartSet_WidgetPoint2D::canBeActivatedByMove()
399 bool aCanBeActivated = false;
400 if (feature()->getKind() == SketchPlugin_Line::ID() &&
401 attributeID() == SketchPlugin_Line::START_ID())
402 aCanBeActivated = true;
404 return aCanBeActivated;
407 void PartSet_WidgetPoint2D::deactivate()
409 // the value of the control should be stored to model if it was not
410 // initialized yet. It is important when we leave this control by Tab key.
411 // It should not be performed by the widget activation as the preview
412 // is visualized with default value. Line point is moved to origin.
413 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
414 if (anAttribute && !anAttribute->isInitialized())
417 ModuleBase_ModelWidget::deactivate();
418 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
419 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
420 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
421 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
422 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
424 myWorkshop->deactivateSubShapesSelection();
427 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
428 const TopoDS_Shape& theShape,
429 double& theX, double& theY) const
431 if (!theShape.IsNull()) {
432 if (theShape.ShapeType() == TopAbs_VERTEX) {
433 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
434 if (!aVertex.IsNull()) {
435 // A case when point is taken from existing vertex
436 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
437 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
445 bool PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
447 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
448 if (feature()->isMacro()) {
449 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
450 std::shared_ptr<GeomDataAPI_Point2D> anAttrPoint =
451 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
452 if (anAttrPoint.get()) {
453 // the macro feature will be removed after the operation is stopped, so we need to build
454 // coicidence to possible sub-features
455 aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(feature(),
460 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
461 aFeaturePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
463 if (!aFeaturePoint.get())
466 // Create point-edge coincedence
467 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
468 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
470 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
471 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
473 aRef1->setAttr(aFeaturePoint);
475 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
476 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
477 aRef2->setObject(theObject);
479 // we need to flush created signal in order to coincidence is processed by solver
480 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
485 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
487 // the contex menu release by the right button should not be processed by this widget
488 if (theEvent->button() != Qt::LeftButton)
491 ModuleBase_ISelection* aSelection = myWorkshop->selection();
492 Handle(V3d_View) aView = theWnd->v3dView();
494 QList<ModuleBase_ViewerPrsPtr> aList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
495 ModuleBase_ViewerPrsPtr aFirstValue = aList.size() > 0 ? aList.first() : ModuleBase_ViewerPrsPtr();
496 // if we have selection and use it
497 if (aFirstValue.get() && isValidSelectionCustom(aFirstValue)) {
498 GeomShapePtr aGeomShape = aFirstValue->shape();
499 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
500 ObjectPtr aObject = aFirstValue->object();
502 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
503 bool anExternal = false;
504 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
505 if (aSelectedFeature.get() != NULL)
506 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
507 if ((!aSPFeature && !aShape.IsNull()) ||
508 (aSPFeature.get() && aSPFeature->isExternal())) {
509 ResultPtr aFixedObject;
511 aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
512 if (!aFixedObject.get())
513 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
515 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
516 // do not create a constraint to the point, which already used by the feature
517 // if the feature contains the point, focus is not switched
521 if (getPoint2d(aView, aShape, aX, aY))
524 setValueState(Stored); // in case of edge selection, Apply state should also be updated
525 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
526 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
528 anOrphanPoint = true; // we should not stop reentrant operation on external objects because
529 // they are not participate in the contour creation excepting external vertices
530 if (aShape.ShapeType() == TopAbs_VERTEX) {
531 FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
532 if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
533 anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
537 if (aFixedObject.get())
538 setConstraintWith(aFixedObject);
539 // fignal updated should be flushed in order to visualize possible created external objects
540 // e.g. selection of trihedron axis when input end arc point
541 updateObject(feature());
544 emit vertexSelected(); // it stops the reentrant operation
546 emit focusOutWidget(this);
551 bool isProcessed = false;
552 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
553 // when the point is selected, the coordinates of the point should be set into the attribute
554 // if the feature contains the point, focus is not switched
558 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
559 // do not set a coincidence constraint in the attribute if the feature contains a point
560 // with the same coordinates. It is important for line creation in order to do not set
561 // the same constraints for the same points, oterwise the result line has zero length.
562 bool isAuxiliaryFeature = false;
563 if (getPoint2d(aView, aShape, aX, aY)) {
565 feature()->execute();
566 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
568 else if (aShape.ShapeType() == TopAbs_EDGE) {
569 if (!setConstraintWith(aObject)) {
570 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
571 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
574 setValueState(Stored); // in case of edge selection, Apply state should also be updated
575 isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
577 // it is important to perform updateObject() in order to the current value is
578 // processed by Sketch Solver. Test case: line is created from a previous point
579 // to some distance, but in the area of the highlighting of the point. Constraint
580 // coincidence is created, after the solver is performed, the distance between the
581 // points of the line becomes less than the tolerance. Validator of the line returns
582 // false, the line will be aborted, but sketch stays valid.
583 updateObject(feature());
584 if (!anOrphanPoint && !anExternal && !isAuxiliaryFeature)
585 emit vertexSelected();
586 emit focusOutWidget(this);
590 // End of Bug dependent fragment
592 // A case when point is taken from mouse event
593 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
595 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
597 // if the feature contains the point, focus is not switched
598 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
601 /// Start alternative code
602 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
603 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
604 //QList<FeaturePtr> aIgnore;
605 //aIgnore.append(feature());
607 //double aTolerance = aView->Convert(7);
608 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
609 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
610 //if (aAttrPnt.get() != NULL) {
611 // aFeaturePoint->setValue(aAttrPnt->pnt());
612 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
613 // emit vertexSelected();
615 /// End alternative code
616 emit focusOutWidget(this);
621 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
626 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
629 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
630 if (myState != ModifiedInViewer)
632 // we need to block the value state change
633 bool isBlocked = blockValueState(true);
635 blockValueState(isBlocked);
636 setValueState(ModifiedInViewer);
639 double PartSet_WidgetPoint2D::x() const
641 return myXSpin->value();
644 double PartSet_WidgetPoint2D::y() const
646 return myYSpin->value();
650 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
651 double theX, double theY)
653 bool aPointIsFound = false;
655 if (feature()->getKind() != SketchPlugin_Line::ID())
656 return aPointIsFound;
658 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
660 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
661 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
662 std::list<AttributePtr> anAttributes =
663 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
664 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
665 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
666 AttributePoint2DPtr aPoint2DAttribute =
667 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
668 if (aPoint2DAttribute == aWidgetAttribute)
670 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
671 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
674 return aPointIsFound;
677 void PartSet_WidgetPoint2D::initializeValueByActivate()
681 /*void PartSet_WidgetPoint2D::onValuesChanged()
683 emit valuesChanged();
686 bool PartSet_WidgetPoint2D::processEnter()
688 bool isModified = getValueState() == ModifiedInPP;
690 bool isXModified = myXSpin->hasFocus();
691 emit valuesChanged();
693 myXSpin->selectAll();
695 myYSpin->selectAll();
700 bool PartSet_WidgetPoint2D::useSelectedShapes() const
705 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
706 const CompositeFeaturePtr& theSketch,
707 double theX, double theY)
709 bool anOrphanPoint = false;
710 if (theFeature.get()) {
711 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
712 std::string aFeatureKind = theFeature->getKind();
713 if (aFeatureKind == SketchPlugin_Point::ID())
714 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
715 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
716 else if (aFeatureKind == SketchPlugin_Circle::ID())
717 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
718 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
720 else if (aFeatureKind == SketchPlugin_Arc::ID())
721 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
722 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
724 /// check that the geometry point with the given coordinates is the checked point
725 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
726 /// means that this point is not an orphant one.
727 if (aPointAttr.get()) {
728 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
729 new GeomAPI_Pnt2d(theX, theY));
730 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
731 return anOrphanPoint;
734 if (aPointAttr.get()) {
735 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
736 // we need to find coincidence features in results also, because external object(point)
737 // uses refs to me in another feature.
738 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
739 anOrphanPoint = true;
740 // if there is at least one concident line to the point, the point is not an orphant
741 if (aCoincidence.get()) {
742 QList<FeaturePtr> aCoinsideLines;
743 QList<FeaturePtr> aCoins;
744 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
745 SketchPlugin_ConstraintCoincidence::ENTITY_A());
746 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
747 SketchPlugin_ConstraintCoincidence::ENTITY_B());
748 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
749 aLast = aCoinsideLines.end();
750 for (; anIt != aLast && anOrphanPoint; anIt++) {
751 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
756 return anOrphanPoint;
759 bool PartSet_WidgetPoint2D::shapeContainsPoint(const GeomShapePtr& theShape,
760 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
761 const CompositeFeaturePtr& theSketch)
763 std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::point3D(thePoint, theSketch);
765 bool aContainPoint = false;
766 GeomAPI_ShapeExplorer anExp(theShape, GeomAPI_Shape::VERTEX);
767 for(; anExp.more() && !aContainPoint; anExp.next()) {
768 std::shared_ptr<GeomAPI_Shape> aVertexInCompSolid = anExp.current();
769 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aVertexInCompSolid));
771 aContainPoint = aPoint->isEqual(aVertex->point());
773 return aContainPoint;