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>
23 #include <ModuleBase_LabelValue.h>
25 #include <Config_Keywords.h>
26 #include <Config_WidgetAPI.h>
28 #include <Events_Loop.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_AttributeBoolean.h>
31 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_Feature.h>
34 #include <ModelAPI_Data.h>
35 #include <ModelAPI_Object.h>
36 #include <GeomDataAPI_Point2D.h>
37 #include <GeomAPI_Pnt2d.h>
39 #include <GeomAPI_ShapeExplorer.h>
40 #include <GeomAPI_Vertex.h>
42 #include <SketchPlugin_Feature.h>
43 #include <SketchPlugin_ConstraintCoincidence.h>
44 #include <SketchPlugin_Line.h>
45 #include <SketchPlugin_Arc.h>
46 #include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_Point.h>
50 #include <QGridLayout>
53 #include <QMouseEvent>
54 #include <QApplication>
57 #include <TopoDS_Vertex.hxx>
58 #include <BRep_Tool.hxx>
63 const double MaxCoordinate = 1e12;
65 static QStringList MyFeaturesForCoincedence;
67 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
68 ModuleBase_IWorkshop* theWorkshop,
69 const Config_WidgetAPI* theData)
70 : ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop),
71 myValueIsCashed(false), myIsFeatureVisibleInCash(true),
72 myXValueInCash(0), myYValueInCash(0)
74 myRefAttribute = theData->getProperty("reference_attribute");
75 if (MyFeaturesForCoincedence.isEmpty()) {
76 MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
77 << SketchPlugin_Arc::ID().c_str()
78 << SketchPlugin_Point::ID().c_str()
79 << SketchPlugin_Circle::ID().c_str();
82 // the control should accept the focus, so the boolean flag is corrected to be true
83 myIsObligatory = true;
84 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
85 myGroupBox = new QGroupBox(aPageName, theParent);
86 myGroupBox->setFlat(false);
88 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
90 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
91 ModuleBase_Tools::adjustMargins(aGroupLay);
92 aGroupLay->setSpacing(2);
93 aGroupLay->setColumnStretch(1, 1);
95 QLabel* aLabel = new QLabel(myGroupBox);
97 myXSpin = new ModuleBase_LabelValue(myGroupBox, tr("X"));
98 //ModuleBase_ParamSpinBox(myGroupBox);
99 //myXSpin->setAcceptVariables(aAcceptVariables);
100 //myXSpin->setMinimum(-DBL_MAX);
101 //myXSpin->setMaximum(DBL_MAX);
102 //myXSpin->setToolTip(tr("X"));
103 aGroupLay->addWidget(myXSpin, 0, 1);
105 //connect(myXSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
106 //myXSpin->setValueEnabled(isValueEnabled());
109 //QLabel* aLabel = new QLabel(myGroupBox);
110 //aLabel->setText(tr("Y "));
111 //aGroupLay->addWidget(aLabel, 1, 0);
113 myYSpin = new ModuleBase_LabelValue(myGroupBox, tr("Y"));
114 //ModuleBase_ParamSpinBox(myGroupBox);
115 //myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
116 //myYSpin->setAcceptVariables(aAcceptVariables);
117 //myYSpin->setMinimum(-DBL_MAX);
118 //myYSpin->setMaximum(DBL_MAX);
119 //myYSpin->setToolTip(tr("Y"));
120 aGroupLay->addWidget(myYSpin, 1, 1);
122 //connect(myYSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
123 //myYSpin->setValueEnabled(isValueEnabled());
125 QVBoxLayout* aLayout = new QVBoxLayout(this);
126 ModuleBase_Tools::zeroMargins(aLayout);
127 aLayout->addWidget(myGroupBox);
130 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
133 bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
137 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
138 if (aModule->sketchReentranceMgr()->isInternalEditActive())
139 return true; /// when internal edit is started a new feature is created. I has not results, AIS
141 /// the selection is not possible if the current feature has no presentation for the current
142 /// attribute not in AIS not in results. If so, no object in current feature where make
143 /// coincidence, so selection is not necessary
144 bool aFoundPoint = false;
145 GeomShapePtr anAISShape;
146 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(myFeature);
149 anAIS = aPrs->getAISObject(anAIS);
151 anAISShape = anAIS->getShape();
154 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = myFeature->results();
155 if (!anAISShape.get() && aResults.empty())
159 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
160 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161 aData->attribute(attributeID()));
162 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
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()
182 /*|| myXSpin->hasVariable() || myYSpin->hasVariable()*/) {
186 if (myValueIsCashed) {
187 // if the restored value should be hidden, aDone = true to set
188 // reset state for the widget in the parent
189 aDone = restoreCurentValue();
190 emit objectUpdated();
194 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
195 // it is important to block the spin box control in order to do not through out the
196 // locking of the validating state.
197 myXSpin->setValue(isOk ? aDefValue : 0.0);
198 myYSpin->setValue(isOk ? aDefValue : 0.0);
200 //ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
201 //ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
209 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
213 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
214 const bool theToValidate)
217 if (theValues.empty())
220 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
221 GeomShapePtr aShape = aValue->shape();
222 if (aShape.get() && !aShape->isNull()) {
223 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
225 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
226 if (getPoint2d(aView, aTDShape, aX, aY)) {
227 isDone = setPoint(aX, aY);
228 setConstraintTo(aX, aY);
229 //PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
235 void PartSet_WidgetPoint2D::selectContent()
237 // myXSpin->selectAll();
240 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
242 if (fabs(theX) >= MaxCoordinate)
244 if (fabs(theY) >= MaxCoordinate)
247 myXSpin->setValue(theX);
248 myYSpin->setValue(theY);
250 //ModuleBase_Tools::setSpinValue(myXSpin, theX);
251 //ModuleBase_Tools::setSpinValue(myYSpin, theY);
257 bool PartSet_WidgetPoint2D::storeValueCustom()
259 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
260 if (!aData) // can be on abort of sketcher element
262 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
263 aData->attribute(attributeID()));
265 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
266 bool isBlocked = that->blockSignals(true);
267 bool isImmutable = aPoint->setImmutable(true);
269 // if text is not empty then setValue will be ignored
270 // so we should set the text at first
271 //aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
272 // myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
273 //aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
274 // !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
275 aPoint->setValue(myXSpin->value(), myYSpin->value());
277 // after movement the solver will call the update event: optimization
278 moveObject(myFeature);
279 aPoint->setImmutable(isImmutable);
280 that->blockSignals(isBlocked);
285 bool PartSet_WidgetPoint2D::restoreValueCustom()
287 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
288 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
289 aData->attribute(attributeID()));
290 double aValueX = aPoint->isInitialized() ? aPoint->x() : 0.;
291 double aValueY = aPoint->isInitialized() ? aPoint->y() : 0.;
292 myXSpin->setValue(aValueX);
293 myYSpin->setValue(aValueY);
298 void PartSet_WidgetPoint2D::storeCurentValue()
300 // do not use cash if a variable is used
301 //if (myXSpin->hasVariable() || myYSpin->hasVariable())
304 myValueIsCashed = true;
305 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
306 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
307 myXValueInCash = myXSpin->value();
308 myYValueInCash = myYSpin->value();
311 bool PartSet_WidgetPoint2D::restoreCurentValue()
313 bool aRestoredAndHidden = true;
315 bool isVisible = myIsFeatureVisibleInCash;
316 // fill the control widgets by the cashed value
318 myValueIsCashed = false;
319 myIsFeatureVisibleInCash = true;
320 myXSpin->setValue(myXValueInCash);
321 myYSpin->setValue(myYValueInCash);
322 //ModuleBase_Tools::setSpinValue(myXSpin, myXValueInCash);
323 //ModuleBase_Tools::setSpinValue(myYSpin, myYValueInCash);
325 // store value to the model
328 setValueState(Stored);
329 aRestoredAndHidden = false;
332 aRestoredAndHidden = true;
334 return aRestoredAndHidden;
337 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
339 QList<QWidget*> aControls;
340 aControls.append(myXSpin);
341 aControls.append(myYSpin);
346 void PartSet_WidgetPoint2D::activateCustom()
349 aModes << TopAbs_VERTEX;
350 aModes << TopAbs_EDGE;
351 myWorkshop->activateSubShapesSelection(aModes);
353 if (!isEditingMode()) {
354 FeaturePtr aFeature = feature();
355 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID())
360 void PartSet_WidgetPoint2D::setHighlighted(bool isHighlighted)
364 void PartSet_WidgetPoint2D::deactivate()
366 // the value of the control should be stored to model if it was not
367 // initialized yet. It is important when we leave this control by Tab key.
368 // It should not be performed by the widget activation as the preview
369 // is visualized with default value. Line point is moved to origin.
370 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
371 if (anAttribute && !anAttribute->isInitialized())
374 ModuleBase_ModelWidget::deactivate();
375 myWorkshop->deactivateSubShapesSelection();
378 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
379 const TopoDS_Shape& theShape,
380 double& theX, double& theY) const
382 if (!theShape.IsNull()) {
383 if (theShape.ShapeType() == TopAbs_VERTEX) {
384 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
385 if (!aVertex.IsNull()) {
386 // A case when point is taken from existing vertex
387 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
388 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
396 bool PartSet_WidgetPoint2D::setConstraintTo(double theClickedX, double theClickedY)
398 FeaturePtr aFeature = feature();
399 std::string anAttribute = attributeID();
404 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
405 new GeomAPI_Pnt2d(theClickedX, theClickedY));
407 // find a feature point by the selection mode
408 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
409 if (aFeature->isMacro()) {
410 // the macro feature will be removed after the operation is stopped, so we need to build
411 // coicidence to possible sub-features
412 aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(aFeature, aClickedPoint);
415 aFeaturePoint = std::dynamic_pointer_cast<
416 GeomDataAPI_Point2D>(aFeature->data()->attribute(anAttribute));
418 if (!aFeaturePoint.get())
421 std::shared_ptr<GeomDataAPI_Point2D> aFPoint = PartSet_Tools::findFirstEqualPointInSketch(
422 mySketch, aFeaturePoint, aClickedPoint);
426 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
428 aRefAttr->setAttr(aFPoint);
430 PartSet_Tools::createConstraint(mySketch, aFPoint, aFeaturePoint);
435 bool PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
437 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
439 if (feature()->isMacro()) {
440 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
441 std::shared_ptr<GeomDataAPI_Point2D> anAttrPoint =
442 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
443 if (anAttrPoint.get()) {
444 // the macro feature will be removed after the operation is stopped, so we need to build
445 // coicidence to possible sub-features
446 aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(feature(),
451 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
452 aFeaturePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
454 if (!aFeaturePoint.get())
457 AttributeRefAttrPtr aRefAttr = attributeRefAttr();
459 aRefAttr->setObject(theObject);
461 // Create point-edge coincedence
462 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
463 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
465 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
466 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
468 aRef1->setAttr(aFeaturePoint);
470 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
471 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
472 aRef2->setObject(theObject);
474 // we need to flush created signal in order to coincidence is processed by solver
475 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
480 void PartSet_WidgetPoint2D::mouseReleased(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
482 // the contex menu release by the right button should not be processed by this widget
483 if (theEvent->button() != Qt::LeftButton)
486 ModuleBase_ISelection* aSelection = myWorkshop->selection();
487 Handle(V3d_View) aView = theWindow->v3dView();
489 QList<ModuleBase_ViewerPrsPtr> aList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
490 ModuleBase_ViewerPrsPtr aFirstValue =
491 aList.size() > 0 ? aList.first() : ModuleBase_ViewerPrsPtr();
492 if (!aFirstValue.get() && myPreSelected.get()) {
493 aFirstValue = myPreSelected;
495 // if we have selection and use it
496 if (aFirstValue.get() && isValidSelectionCustom(aFirstValue) &&
497 aFirstValue->shape().get()) { /// Trihedron Axis may be selected, but shape is empty
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);
508 ResultPtr aFixedObject;
509 bool aSketchExternalFeature = aSPFeature.get() && aSPFeature->isExternal();
510 if ((!aSPFeature && !aShape.IsNull()) || aSketchExternalFeature) {
511 aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
512 if (aSketchExternalFeature && !aFixedObject.get()) {/// local selection on external feature
517 if (!aFixedObject.get())
518 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
523 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
524 // do not create a constraint to the point, which already used by the feature
525 // if the feature contains the point, focus is not switched
529 if (getPoint2d(aView, aShape, aX, aY))
532 setValueState(Stored); // in case of edge selection, Apply state should also be updated
533 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
534 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
536 // we should not stop reentrant operation on external objects because
537 anOrphanPoint = true;
538 // they are not participate in the contour creation excepting external vertices
539 if (aShape.ShapeType() == TopAbs_VERTEX) {
540 FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
541 if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
542 anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
546 if (aFixedObject.get())
547 setConstraintWith(aFixedObject);
548 // fignal updated should be flushed in order to visualize possible created
549 // external objects e.g. selection of trihedron axis when input end arc point
550 updateObject(feature());
553 emit vertexSelected(); // it stops the reentrant operation
555 emit focusOutWidget(this);
560 bool isProcessed = false;
561 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
562 // when the point is selected, the coordinates of the point should be set into the attribute
563 // if the feature contains the point, focus is not switched
567 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
568 // do not set a coincidence constraint in the attribute if the feature contains a point
569 // with the same coordinates. It is important for line creation in order to do not set
570 // the same constraints for the same points, oterwise the result line has zero length.
571 bool isAuxiliaryFeature = false;
572 if (getPoint2d(aView, aShape, aX, aY)) {
574 feature()->execute();
576 setConstraintTo(aX, aY);
577 //PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
579 else if (aShape.ShapeType() == TopAbs_EDGE) {
580 // point is taken from mouse event and set in attribute. It should be done before setting
581 // coinident constraint to the external line. If a point is created, it should be in
582 // the mouse clicked point
583 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
584 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
586 setConstraintWith(aObject);
587 setValueState(Stored); // in case of edge selection, Apply state should also be updated
588 isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
590 // it is important to perform updateObject() in order to the current value is
591 // processed by Sketch Solver. Test case: line is created from a previous point
592 // to some distance, but in the area of the highlighting of the point. Constraint
593 // coincidence is created, after the solver is performed, the distance between the
594 // points of the line becomes less than the tolerance. Validator of the line returns
595 // false, the line will be aborted, but sketch stays valid.
596 updateObject(feature());
597 if (!anOrphanPoint && !anExternal && !isAuxiliaryFeature)
598 emit vertexSelected();
599 emit focusOutWidget(this);
603 // End of Bug dependent fragment
605 // A case when point is taken from mouse event
606 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
608 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
610 // if the feature contains the point, focus is not switched
611 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
614 /// Start alternative code
615 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
616 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
617 //QList<FeaturePtr> aIgnore;
618 //aIgnore.append(feature());
620 //double aTolerance = aView->Convert(7);
621 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
622 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
623 //if (aAttrPnt.get() != NULL) {
624 // aFeaturePoint->setValue(aAttrPnt->pnt());
625 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
626 // emit vertexSelected();
628 /// End alternative code
629 emit focusOutWidget(this);
633 void PartSet_WidgetPoint2D::setPreSelection(
634 const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
635 ModuleBase_IViewWindow* theWnd,
636 QMouseEvent* theEvent)
638 myPreSelected = thePreSelected;
639 mouseReleased(theWnd, theEvent);
640 myPreSelected = ModuleBase_ViewerPrsPtr();
643 void PartSet_WidgetPoint2D::mouseMoved(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent)
645 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
647 if (isEditingMode() || aModule->sketchReentranceMgr()->isInternalEditStarted())
650 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
653 PartSet_Tools::convertTo2D(aPoint, mySketch, theWindow->v3dView(), aX, anY);
654 if (myState != ModifiedInViewer)
656 // we need to block the value state change
657 bool isBlocked = blockValueState(true);
659 blockValueState(isBlocked);
660 setValueState(ModifiedInViewer);
663 double PartSet_WidgetPoint2D::x() const
665 return myXSpin->value();
668 double PartSet_WidgetPoint2D::y() const
670 return myYSpin->value();
674 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
675 double theX, double theY)
677 bool aPointIsFound = false;
679 if (feature()->getKind() != SketchPlugin_Line::ID())
680 return aPointIsFound;
682 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
684 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
685 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
686 std::list<AttributePtr> anAttributes =
687 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
688 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
689 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
690 AttributePoint2DPtr aPoint2DAttribute =
691 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
692 if (aPoint2DAttribute == aWidgetAttribute)
694 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
695 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
698 return aPointIsFound;
701 void PartSet_WidgetPoint2D::initializeValueByActivate()
705 /*void PartSet_WidgetPoint2D::onValuesChanged()
707 emit valuesChanged();
710 bool PartSet_WidgetPoint2D::processEnter()
713 /*bool isModified = getValueState() == ModifiedInPP;
715 bool isXModified = myXSpin->hasFocus();
716 emit valuesChanged();
718 myXSpin->selectAll();
720 myYSpin->selectAll();
725 bool PartSet_WidgetPoint2D::useSelectedShapes() const
730 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
731 const CompositeFeaturePtr& theSketch,
732 double theX, double theY)
734 bool anOrphanPoint = false;
735 if (theFeature.get()) {
736 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
737 std::string aFeatureKind = theFeature->getKind();
738 if (aFeatureKind == SketchPlugin_Point::ID())
739 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
740 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
741 else if (aFeatureKind == SketchPlugin_Circle::ID())
742 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
743 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
745 else if (aFeatureKind == SketchPlugin_Arc::ID())
746 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
747 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
749 /// check that the geometry point with the given coordinates is the checked point
750 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
751 /// means that this point is not an orphant one.
752 if (aPointAttr.get()) {
753 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
754 new GeomAPI_Pnt2d(theX, theY));
755 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
756 return anOrphanPoint;
759 if (aPointAttr.get()) {
760 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
761 // we need to find coincidence features in results also, because external object(point)
762 // uses refs to me in another feature.
763 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
764 anOrphanPoint = true;
765 // if there is at least one concident line to the point, the point is not an orphant
766 if (aCoincidence.get()) {
767 QList<FeaturePtr> aCoinsideLines;
768 QList<FeaturePtr> aCoins;
769 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
770 SketchPlugin_ConstraintCoincidence::ENTITY_A());
771 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
772 SketchPlugin_ConstraintCoincidence::ENTITY_B());
773 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
774 aLast = aCoinsideLines.end();
775 for (; anIt != aLast && anOrphanPoint; anIt++) {
776 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
781 return anOrphanPoint;
784 bool PartSet_WidgetPoint2D::shapeContainsPoint(const GeomShapePtr& theShape,
785 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
786 const CompositeFeaturePtr& theSketch)
788 std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::point3D(thePoint, theSketch);
790 bool aContainPoint = false;
791 GeomAPI_ShapeExplorer anExp(theShape, GeomAPI_Shape::VERTEX);
792 for(; anExp.more() && !aContainPoint; anExp.next()) {
793 std::shared_ptr<GeomAPI_Shape> aVertexInCompSolid = anExp.current();
794 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aVertexInCompSolid));
796 aContainPoint = aPoint->isEqual(aVertex->point());
798 return aContainPoint;
801 AttributeRefAttrPtr PartSet_WidgetPoint2D::attributeRefAttr() const
803 AttributeRefAttrPtr anAttribute;
804 if (myRefAttribute.empty())
807 AttributePtr anAttributeRef = feature()->attribute(myRefAttribute);
808 if (!anAttributeRef.get())
811 return std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttributeRef);