File size: 16,326 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | /***************************************************************************
* Copyright (c) 2011 Juergen Riegel <FreeCAD@juergen-riegel.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <QAction>
#include <QFontMetrics>
#include <QListWidget>
#include <QMessageBox>
#include <Base/Interpreter.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <Gui/Selection/Selection.h>
#include <Gui/Tools.h>
#include <Gui/ViewProvider.h>
#include <Gui/Inventor/Draggers/SoLinearDragger.h>
#include <Gui/Inventor/Draggers/SoRotationDragger.h>
#include <Gui/Utilities.h>
#include <Mod/PartDesign/App/FeatureChamfer.h>
#include <Mod/Part/App/Geometry.h>
#include <Mod/Part/App/GizmoHelper.h>
#include <TopoDS.hxx>
#include <BRep_Tool.hxx>
#include <BRepGProp.hxx>
#include <GProp_GProps.hxx>
#include <GeomAPI_ProjectPointOnSurf.hxx>
#include <Base/Converter.h>
#include "ui_TaskChamferParameters.h"
#include "TaskChamferParameters.h"
using namespace PartDesignGui;
using namespace Gui;
/* TRANSLATOR PartDesignGui::TaskChamferParameters */
TaskChamferParameters::TaskChamferParameters(ViewProviderDressUp* DressUpView, QWidget* parent)
: TaskDressUpParameters(DressUpView, true, true, parent)
, ui(new Ui_TaskChamferParameters)
{
// we need a separate container widget to add all controls to
proxy = new QWidget(this);
ui->setupUi(proxy);
this->groupLayout()->addWidget(proxy);
PartDesign::Chamfer* pcChamfer = DressUpView->getObject<PartDesign::Chamfer>();
setUpUI(pcChamfer);
bool useAllEdges = pcChamfer->UseAllEdges.getValue();
ui->checkBoxUseAllEdges->setChecked(useAllEdges);
ui->buttonRefSel->setEnabled(!useAllEdges);
ui->listWidgetReferences->setEnabled(!useAllEdges);
QMetaObject::invokeMethod(ui->chamferSize, "setFocus", Qt::QueuedConnection);
std::vector<std::string> strings = pcChamfer->Base.getSubValues();
for (const auto& string : strings) {
ui->listWidgetReferences->addItem(QString::fromStdString(string));
}
QMetaObject::connectSlotsByName(this);
// clang-format off
connect(ui->chamferType, qOverload<int>(&QComboBox::currentIndexChanged),
this, &TaskChamferParameters::onTypeChanged);
connect(ui->chamferSize, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskChamferParameters::onSizeChanged);
connect(ui->chamferSize2, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskChamferParameters::onSize2Changed);
connect(ui->chamferAngle, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskChamferParameters::onAngleChanged);
connect(ui->flipDirection, &QCheckBox::toggled,
this, &TaskChamferParameters::onFlipDirection);
connect(ui->buttonRefSel, &QToolButton::toggled,
this, &TaskChamferParameters::onButtonRefSel);
connect(ui->checkBoxUseAllEdges, &QCheckBox::toggled,
this, &TaskChamferParameters::onCheckBoxUseAllEdgesToggled);
// Create context menu
createDeleteAction(ui->listWidgetReferences);
connect(deleteAction, &QAction::triggered,
this, &TaskChamferParameters::onRefDeleted);
createAddAllEdgesAction(ui->listWidgetReferences);
connect(addAllEdgesAction, &QAction::triggered,
this, &TaskChamferParameters::onAddAllEdges);
connect(ui->listWidgetReferences, &QListWidget::currentItemChanged,
this, &TaskChamferParameters::setSelection);
connect(ui->listWidgetReferences, &QListWidget::itemClicked,
this, &TaskChamferParameters::setSelection);
connect(ui->listWidgetReferences, &QListWidget::itemDoubleClicked,
this, &TaskChamferParameters::doubleClicked);
// clang-format on
if (strings.size() == 0) {
setSelectionMode(refSel);
}
else {
hideOnError();
}
setupGizmos(DressUpView);
}
void TaskChamferParameters::setUpUI(PartDesign::Chamfer* pcChamfer)
{
const int index = pcChamfer->ChamferType.getValue();
ui->chamferType->setCurrentIndex(index);
ui->flipDirection->setEnabled(index != 0); // Enable if type is not "Equal distance"
ui->flipDirection->setChecked(pcChamfer->FlipDirection.getValue());
ui->chamferSize->setUnit(Base::Unit::Length);
ui->chamferSize->setMinimum(0);
ui->chamferSize->setValue(pcChamfer->Size.getValue());
ui->chamferSize->bind(pcChamfer->Size);
ui->chamferSize->selectNumber();
ui->chamferSize2->setUnit(Base::Unit::Length);
ui->chamferSize2->setMinimum(0);
ui->chamferSize2->setValue(pcChamfer->Size2.getValue());
ui->chamferSize2->bind(pcChamfer->Size2);
ui->chamferAngle->setUnit(Base::Unit::Angle);
ui->chamferAngle->setMinimum(pcChamfer->Angle.getMinimum());
ui->chamferAngle->setMaximum(pcChamfer->Angle.getMaximum());
ui->chamferAngle->setValue(pcChamfer->Angle.getValue());
ui->chamferAngle->bind(pcChamfer->Angle);
ui->stackedWidget->setFixedHeight(ui->chamferSize2->sizeHint().height());
QFontMetrics fm(ui->typeLabel->font());
int minWidth = Gui::QtTools::horizontalAdvance(fm, ui->typeLabel->text());
minWidth = std::max<int>(minWidth, Gui::QtTools::horizontalAdvance(fm, ui->sizeLabel->text()));
minWidth = std::max<int>(minWidth, Gui::QtTools::horizontalAdvance(fm, ui->size2Label->text()));
minWidth = std::max<int>(minWidth, Gui::QtTools::horizontalAdvance(fm, ui->angleLabel->text()));
minWidth = minWidth + 5; // spacing
ui->typeLabel->setMinimumWidth(minWidth);
ui->sizeLabel->setMinimumWidth(minWidth);
ui->size2Label->setMinimumWidth(minWidth);
ui->angleLabel->setMinimumWidth(minWidth);
}
void TaskChamferParameters::onSelectionChanged(const Gui::SelectionChanges& msg)
{
// executed when the user selected something in the CAD object
// adds/deletes the selection accordingly
if (msg.Type == Gui::SelectionChanges::AddSelection) {
if (selectionMode == refSel) {
referenceSelected(msg, ui->listWidgetReferences);
}
}
else if (msg.Type == Gui::SelectionChanges::ClrSelection) {
// TODO: the gizmo position should be only recalculated when the feature associated
// with the gizmo is removed from the list
setGizmoPositions();
}
}
void TaskChamferParameters::onCheckBoxUseAllEdgesToggled(bool checked)
{
if (auto chamfer = getObject<PartDesign::Chamfer>()) {
if (checked) {
setSelectionMode(none);
}
ui->buttonRefSel->setEnabled(!checked);
ui->listWidgetReferences->setEnabled(!checked);
chamfer->UseAllEdges.setValue(checked);
chamfer->recomputeFeature();
}
}
void TaskChamferParameters::setButtons(const selectionModes mode)
{
ui->buttonRefSel->setChecked(mode == refSel);
ui->buttonRefSel->setText(mode == refSel ? stopSelectionLabel() : startSelectionLabel());
}
void TaskChamferParameters::onRefDeleted()
{
TaskDressUpParameters::deleteRef(ui->listWidgetReferences);
setGizmoPositions();
}
void TaskChamferParameters::onAddAllEdges()
{
TaskDressUpParameters::addAllEdges(ui->listWidgetReferences);
}
void TaskChamferParameters::onTypeChanged(int index)
{
if (auto chamfer = getObject<PartDesign::Chamfer>()) {
setSelectionMode(none);
chamfer->ChamferType.setValue(index);
ui->stackedWidget->setCurrentIndex(index);
ui->flipDirection->setEnabled(index != 0); // Enable if type is not "Equal distance"
chamfer->recomputeFeature();
// hide the chamfer if there was a computation error
hideOnError();
}
}
void TaskChamferParameters::onSizeChanged(double len)
{
if (auto chamfer = getObject<PartDesign::Chamfer>()) {
setSelectionMode(none);
setupTransaction();
chamfer->Size.setValue(len);
chamfer->recomputeFeature();
// hide the chamfer if there was a computation error
hideOnError();
}
}
void TaskChamferParameters::onSize2Changed(double len)
{
if (auto chamfer = getObject<PartDesign::Chamfer>()) {
setSelectionMode(none);
setupTransaction();
chamfer->Size2.setValue(len);
chamfer->recomputeFeature();
// hide the chamfer if there was a computation error
hideOnError();
}
}
void TaskChamferParameters::onAngleChanged(double angle)
{
if (auto chamfer = getObject<PartDesign::Chamfer>()) {
setSelectionMode(none);
setupTransaction();
chamfer->Angle.setValue(angle);
chamfer->recomputeFeature();
// hide the chamfer if there was a computation error
hideOnError();
}
}
void TaskChamferParameters::onFlipDirection(bool flip)
{
if (auto chamfer = getObject<PartDesign::Chamfer>()) {
setSelectionMode(none);
setupTransaction();
chamfer->FlipDirection.setValue(flip);
chamfer->recomputeFeature();
// hide the chamfer if there was a computation error
hideOnError();
setGizmoPositions();
}
}
int TaskChamferParameters::getType() const
{
return ui->chamferType->currentIndex();
}
double TaskChamferParameters::getSize() const
{
return ui->chamferSize->value().getValue();
}
double TaskChamferParameters::getSize2() const
{
return ui->chamferSize2->value().getValue();
}
double TaskChamferParameters::getAngle() const
{
return ui->chamferAngle->value().getValue();
}
bool TaskChamferParameters::getFlipDirection() const
{
return ui->flipDirection->isChecked();
}
TaskChamferParameters::~TaskChamferParameters()
{
try {
Gui::Selection().clearSelection();
Gui::Selection().rmvSelectionGate();
}
catch (const Py::Exception&) {
Base::PyException e; // extract the Python error text
e.reportException();
}
}
void TaskChamferParameters::changeEvent(QEvent* e)
{
TaskBox::changeEvent(e);
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(proxy);
}
}
void TaskChamferParameters::apply()
{
auto chamfer = getObject<PartDesign::Chamfer>();
const int chamfertype = chamfer->ChamferType.getValue();
switch (chamfertype) {
case 0: // "Equal distance"
ui->chamferSize->apply();
break;
case 1: // "Two distances"
ui->chamferSize->apply();
ui->chamferSize2->apply();
break;
case 2: // "Distance and Angle"
ui->chamferSize->apply();
ui->chamferAngle->apply();
break;
}
// Alert user if he created an empty feature
if (ui->listWidgetReferences->count() == 0) {
Base::Console().warning(tr("Empty chamfer created!\n").toStdString().c_str());
}
}
void TaskChamferParameters::setupGizmos(ViewProviderDressUp* vp)
{
if (!GizmoContainer::isEnabled()) {
return;
}
distanceGizmo = new Gui::LinearGizmo(ui->chamferSize);
secondDistanceGizmo = new Gui::LinearGizmo(ui->chamferSize);
angleGizmo = new Gui::RotationGizmo(ui->chamferAngle);
connect(ui->chamferType, qOverload<int>(&QComboBox::currentIndexChanged), [this](int index) {
auto type = static_cast<Part::ChamferType>(index);
switch (type) {
case Part::ChamferType::equalDistance:
secondDistanceGizmo->setVisibility(true);
angleGizmo->setVisibility(false);
secondDistanceGizmo->setProperty(ui->chamferSize);
break;
case Part::ChamferType::twoDistances:
secondDistanceGizmo->setVisibility(true);
angleGizmo->setVisibility(false);
secondDistanceGizmo->setProperty(ui->chamferSize2);
break;
case Part::ChamferType::distanceAngle:
secondDistanceGizmo->setVisibility(false);
angleGizmo->setVisibility(true);
}
});
gizmoContainer = GizmoContainer::create({distanceGizmo, secondDistanceGizmo, angleGizmo}, vp);
setGizmoPositions();
ui->chamferType->currentIndexChanged(ui->chamferType->currentIndex());
}
void TaskChamferParameters::setGizmoPositions()
{
if (!gizmoContainer) {
return;
}
auto chamfer = getObject<PartDesign::Chamfer>();
if (!chamfer || chamfer->isError()) {
gizmoContainer->visible = false;
return;
}
PartDesign::TopoShape baseShape = chamfer->getBaseTopoShape(true);
auto shapes = chamfer->getContinuousEdges(baseShape);
if (shapes.size() == 0) {
gizmoContainer->visible = false;
return;
}
gizmoContainer->visible = true;
Part::TopoShape edge = shapes[0];
auto [face1, face2] = getAdjacentFacesFromEdge(edge, baseShape);
DraggerPlacementProps props = getDraggerPlacementFromEdgeAndFace(edge, face1);
DraggerPlacementProps props2 = getDraggerPlacementFromEdgeAndFace(edge, face2);
if (ui->flipDirection->isChecked()) {
std::swap(props, props2);
}
distanceGizmo->Gizmo::setDraggerPlacement(props.position, props.dir);
secondDistanceGizmo->Gizmo::setDraggerPlacement(props2.position, props2.dir);
angleGizmo->placeBelowLinearGizmo(distanceGizmo);
angleGizmo->getDraggerContainer()->setArcNormalDirection(
Base::convertTo<SbVec3f>(-props.dir.Cross(props2.dir))
);
// Only show the gizmo if the chamfer type is set to distance and angle
angleGizmo->setVisibility(getType() == 2);
}
//**************************************************************************
//**************************************************************************
// TaskDialog
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TaskDlgChamferParameters::TaskDlgChamferParameters(ViewProviderChamfer* DressUpView)
: TaskDlgDressUpParameters(DressUpView)
{
parameter = new TaskChamferParameters(DressUpView);
Content.push_back(parameter);
Content.push_back(preview);
}
TaskDlgChamferParameters::~TaskDlgChamferParameters() = default;
//==== calls from the TaskView ===============================================================
bool TaskDlgChamferParameters::accept()
{
auto obj = getObject();
if (!obj->isError()) {
getViewObject()->showPreviousFeature(false);
}
parameter->apply();
return TaskDlgDressUpParameters::accept();
}
#include "moc_TaskChamferParameters.cpp"
|