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 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
82 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
83 myGroupBox = new QGroupBox(aPageName, theParent);
84 myGroupBox->setFlat(false);
86 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
88 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
89 ModuleBase_Tools::adjustMargins(aGroupLay);
90 aGroupLay->setSpacing(2);
91 aGroupLay->setColumnStretch(1, 1);
93 QLabel* aLabel = new QLabel(myGroupBox);
94 aLabel->setText(tr("X "));
95 aGroupLay->addWidget(aLabel, 0, 0);
97 myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
98 myXSpin->setAcceptVariables(aAcceptVariables);
99 myXSpin->setMinimum(-DBL_MAX);
100 myXSpin->setMaximum(DBL_MAX);
101 myXSpin->setToolTip(tr("X"));
102 aGroupLay->addWidget(myXSpin, 0, 1);
104 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
107 QLabel* aLabel = new QLabel(myGroupBox);
108 aLabel->setText(tr("Y "));
109 aGroupLay->addWidget(aLabel, 1, 0);
111 myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
112 myYSpin->setAcceptVariables(aAcceptVariables);
113 myYSpin->setMinimum(-DBL_MAX);
114 myYSpin->setMaximum(DBL_MAX);
115 myYSpin->setToolTip(tr("Y"));
116 aGroupLay->addWidget(myYSpin, 1, 1);
118 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
120 QVBoxLayout* aLayout = new QVBoxLayout(this);
121 ModuleBase_Tools::zeroMargins(aLayout);
122 aLayout->addWidget(myGroupBox);
125 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
128 bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
132 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
133 if (aModule->sketchReentranceMgr()->isInternalEditActive())
134 return true; /// when internal edit is started a new feature is created. I has not results, AIS
136 // workaround for feature, where there is no results
137 //if (myFeature->getKind() == "SketchRectangle")
140 /// the selection is not possible if the current feature has no presentation for the current
141 /// attribute not in AIS not in results. If so, no object in current feature where make
142 /// coincidence, so selection is not necessary
143 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
144 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145 aData->attribute(attributeID()));
146 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
148 bool aFoundPoint = false;
149 GeomShapePtr anAISShape;
150 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(myFeature);
153 anAIS = aPrs->getAISObject(anAIS);
155 anAISShape = anAIS->getShape();
158 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = myFeature->results();
159 if (!anAISShape.get() && aResults.empty())
163 if (anAISShape.get())
164 aFoundPoint = shapeContainsPoint(anAISShape, aPoint, mySketch);
166 /// analysis of results
167 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
168 for (; aRIter != aResults.cend() && !aFoundPoint; aRIter++) {
169 ResultPtr aResult = *aRIter;
170 if (aResult.get() && aResult->shape().get()) {
171 GeomShapePtr aShape = aResult->shape();
172 aFoundPoint = shapeContainsPoint(aShape, aPoint, mySketch);
178 bool PartSet_WidgetPoint2D::resetCustom()
181 if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
185 if (myValueIsCashed) {
186 // if the restored value should be hidden, aDone = true to set
187 // reset state for the widget in the parent
188 aDone = restoreCurentValue();
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() const
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 //NCollection_List<TopoDS_Shape> aShapes;
497 //std::list<ObjectPtr> aObjects;
498 //aSelection->selectedShapes(aShapes, aObjects);
499 // if we have selection and use it
500 //if (/*aShapes.Extent() > 0 && useSelectedShapes() &&*/ isValidSelectionCustom() {
501 if (aFirstValue.get() && isValidSelectionCustom(aFirstValue)) {
502 //TopoDS_Shape aShape = aShapes.First();
503 //ObjectPtr aObject = aObjects.front();
505 GeomShapePtr aGeomShape = aFirstValue->shape();
506 TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>(); /// to find axis shape
507 ObjectPtr aObject = aFirstValue->object(); /// to find owner
509 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
510 bool anExternal = false;
511 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
512 if (aSelectedFeature.get() != NULL)
513 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
514 if ((!aSPFeature && !aShape.IsNull()) ||
515 (aSPFeature.get() && aSPFeature->isExternal())) {
516 ResultPtr aFixedObject;
518 aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
519 if (!aFixedObject.get())
520 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
522 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
523 // do not create a constraint to the point, which already used by the feature
524 // if the feature contains the point, focus is not switched
528 if (getPoint2d(aView, aShape, aX, aY))
531 setValueState(Stored); // in case of edge selection, Apply state should also be updated
532 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
533 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
535 anOrphanPoint = true; // we should not stop reentrant operation on external objects because
536 // they are not participate in the contour creation excepting external vertices
537 if (aShape.ShapeType() == TopAbs_VERTEX) {
538 FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
539 if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
540 anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
544 if (aFixedObject.get())
545 setConstraintWith(aFixedObject);
546 // fignal updated should be flushed in order to visualize possible created external objects
547 // e.g. selection of trihedron axis when input end arc point
548 updateObject(feature());
551 emit vertexSelected(); // it stops the reentrant operation
553 emit focusOutWidget(this);
558 bool isProcessed = false;
559 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
560 // when the point is selected, the coordinates of the point should be set into the attribute
561 // if the feature contains the point, focus is not switched
565 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
566 // do not set a coincidence constraint in the attribute if the feature contains a point
567 // with the same coordinates. It is important for line creation in order to do not set
568 // the same constraints for the same points, oterwise the result line has zero length.
569 bool isAuxiliaryFeature = false;
570 if (getPoint2d(aView, aShape, aX, aY)) {
572 feature()->execute();
573 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
575 else if (aShape.ShapeType() == TopAbs_EDGE) {
576 if (!setConstraintWith(aObject)) {
577 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
578 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
581 setValueState(Stored); // in case of edge selection, Apply state should also be updated
582 isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
584 // it is important to perform updateObject() in order to the current value is
585 // processed by Sketch Solver. Test case: line is created from a previous point
586 // to some distance, but in the area of the highlighting of the point. Constraint
587 // coincidence is created, after the solver is performed, the distance between the
588 // points of the line becomes less than the tolerance. Validator of the line returns
589 // false, the line will be aborted, but sketch stays valid.
590 updateObject(feature());
591 if (!anOrphanPoint && !anExternal && !isAuxiliaryFeature)
592 emit vertexSelected();
593 emit focusOutWidget(this);
597 // End of Bug dependent fragment
599 // A case when point is taken from mouse event
600 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
602 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
604 // if the feature contains the point, focus is not switched
605 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
608 /// Start alternative code
609 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
610 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
611 //QList<FeaturePtr> aIgnore;
612 //aIgnore.append(feature());
614 //double aTolerance = aView->Convert(7);
615 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
616 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
617 //if (aAttrPnt.get() != NULL) {
618 // aFeaturePoint->setValue(aAttrPnt->pnt());
619 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
620 // emit vertexSelected();
622 /// End alternative code
623 emit focusOutWidget(this);
628 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
633 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
636 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
637 if (myState != ModifiedInViewer)
639 // we need to block the value state change
640 bool isBlocked = blockValueState(true);
642 blockValueState(isBlocked);
643 setValueState(ModifiedInViewer);
646 double PartSet_WidgetPoint2D::x() const
648 return myXSpin->value();
651 double PartSet_WidgetPoint2D::y() const
653 return myYSpin->value();
657 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
658 double theX, double theY)
660 bool aPointIsFound = false;
662 if (feature()->getKind() != SketchPlugin_Line::ID())
663 return aPointIsFound;
665 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
667 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
668 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
669 std::list<AttributePtr> anAttributes =
670 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
671 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
672 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
673 AttributePoint2DPtr aPoint2DAttribute =
674 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
675 if (aPoint2DAttribute == aWidgetAttribute)
677 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
678 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
681 return aPointIsFound;
684 void PartSet_WidgetPoint2D::initializeValueByActivate()
688 /*void PartSet_WidgetPoint2D::onValuesChanged()
690 emit valuesChanged();
693 bool PartSet_WidgetPoint2D::processEnter()
695 bool isModified = getValueState() == ModifiedInPP;
697 bool isXModified = myXSpin->hasFocus();
698 emit valuesChanged();
700 myXSpin->selectAll();
702 myYSpin->selectAll();
707 bool PartSet_WidgetPoint2D::useSelectedShapes() const
712 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
713 const CompositeFeaturePtr& theSketch,
714 double theX, double theY)
716 bool anOrphanPoint = false;
717 if (theFeature.get()) {
718 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
719 std::string aFeatureKind = theFeature->getKind();
720 if (aFeatureKind == SketchPlugin_Point::ID())
721 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
722 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
723 else if (aFeatureKind == SketchPlugin_Circle::ID())
724 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
725 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
727 else if (aFeatureKind == SketchPlugin_Arc::ID())
728 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
729 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
731 /// check that the geometry point with the given coordinates is the checked point
732 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
733 /// means that this point is not an orphant one.
734 if (aPointAttr.get()) {
735 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
736 new GeomAPI_Pnt2d(theX, theY));
737 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
738 return anOrphanPoint;
741 if (aPointAttr.get()) {
742 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
743 // we need to find coincidence features in results also, because external object(point)
744 // uses refs to me in another feature.
745 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
746 anOrphanPoint = true;
747 // if there is at least one concident line to the point, the point is not an orphant
748 if (aCoincidence.get()) {
749 QList<FeaturePtr> aCoinsideLines;
750 QList<FeaturePtr> aCoins;
751 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
752 SketchPlugin_ConstraintCoincidence::ENTITY_A());
753 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
754 SketchPlugin_ConstraintCoincidence::ENTITY_B());
755 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
756 aLast = aCoinsideLines.end();
757 for (; anIt != aLast && anOrphanPoint; anIt++) {
758 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
763 return anOrphanPoint;
766 bool PartSet_WidgetPoint2D::shapeContainsPoint(const GeomShapePtr& theShape,
767 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
768 const CompositeFeaturePtr& theSketch)
770 std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::point3D(thePoint, theSketch);
772 bool aContainPoint = false;
773 GeomAPI_ShapeExplorer anExp(theShape, GeomAPI_Shape::VERTEX);
774 for(; anExp.more() && !aContainPoint; anExp.next()) {
775 std::shared_ptr<GeomAPI_Shape> aVertexInCompSolid = anExp.current();
776 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aVertexInCompSolid));
778 aContainPoint = aPoint->isEqual(aVertex->point());
780 return aContainPoint;