FreeCAD

Форк
0
/
TaskSectionView.cpp 
789 строк · 27.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 WandererFan <wandererfan@gmail.com>                *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <QMessageBox>
26
#include <cmath>
27
#endif// #ifndef _PreComp_
28

29
#include "Widgets/CompassWidget.h"
30
#include "Widgets/VectorEditWidget.h"
31
#include <App/Document.h>
32
#include <App/DocumentObject.h>
33
#include <Base/Console.h>
34
#include <Base/Tools.h>
35
#include <Gui/BitmapFactory.h>
36
#include <Gui/Command.h>
37
#include <Gui/Control.h>
38
#include <Gui/MainWindow.h>
39
#include <Gui/Selection.h>
40
#include <Gui/ViewProvider.h>
41
#include <Gui/WaitCursor.h>
42
#include <Mod/TechDraw/App/DrawPage.h>
43
#include <Mod/TechDraw/App/DrawViewPart.h>
44
#include <Mod/TechDraw/App/DrawViewSection.h>
45

46
// clang-format off
47
#include "ui_TaskSectionView.h"
48
#include "TaskSectionView.h"
49
// clang-format on
50

51

52
using namespace Gui;
53
using namespace TechDraw;
54
using namespace TechDrawGui;
55

56
//ctor for create
57
TaskSectionView::TaskSectionView(TechDraw::DrawViewPart* base) :
58
    ui(new Ui_TaskSectionView),
59
    m_base(base),
60
    m_section(nullptr),
61
    m_saveScale(1.0),
62
    m_dirName(""),
63
    m_doc(nullptr),
64
    m_createMode(true),
65
    m_saved(false),
66
    m_applyDeferred(0),
67
    m_directionIsSet(false),
68
    m_modelIsDirty(false),
69
    m_scaleEdited(false)
70
{
71
    //existence of base is guaranteed by CmdTechDrawSectionView (Command.cpp)
72

73
    m_sectionName = std::string();
74
    m_doc = m_base->getDocument();
75

76
    m_saveBaseName = m_base->getNameInDocument();
77
    m_savePageName = m_base->findParentPage()->getNameInDocument();
78

79
    ui->setupUi(this);
80
    setUiPrimary();
81

82
    m_applyDeferred = 0;//setting the direction widgets causes an increment of the deferred count,
83
                        //so we reset the counter and the message.
84
}
85

86
//ctor for edit
87
TaskSectionView::TaskSectionView(TechDraw::DrawViewSection* section) :
88
    ui(new Ui_TaskSectionView),
89
    m_base(nullptr),
90
    m_section(section),
91
    m_saveScale(1.0),
92
    m_doc(nullptr),
93
    m_createMode(false),
94
    m_saved(false),
95
    m_applyDeferred(0),
96
    m_directionIsSet(true),
97
    m_modelIsDirty(false),
98
    m_scaleEdited(false)
99
{
100
    //existence of section is guaranteed by ViewProviderViewSection.setEdit
101

102
    m_doc = m_section->getDocument();
103
    m_sectionName = m_section->getNameInDocument();
104
    App::DocumentObject* newObj = m_section->BaseView.getValue();
105
    m_base = dynamic_cast<TechDraw::DrawViewPart*>(newObj);
106
    if (!newObj || !m_base) {
107
        throw Base::RuntimeError("TaskSectionView - BaseView not found");
108
    }
109

110
    m_saveBaseName = m_base->getNameInDocument();
111
    m_savePageName = m_base->findParentPage()->getNameInDocument();
112

113
    ui->setupUi(this);
114

115
    m_dirName = m_section->SectionDirection.getValueAsString();
116
    saveSectionState();
117
    setUiEdit();
118

119
    m_applyDeferred = 0;//setting the direction widgets causes an increment of the deferred count,
120
                        //so we reset the counter and the message.
121
    ui->lPendingUpdates->setText(QString());
122
}
123

124
void TaskSectionView::setUiPrimary()
125
{
126
    //    Base::Console().Message("TSV::setUiPrimary()\n");
127
    setWindowTitle(QObject::tr("Create Section View"));
128

129
    // note DPGI will have a custom scale type and scale = 1.0.  In this case,
130
    // we need the values from the parent DPG!
131
    ui->sbScale->setValue(m_base->getScale());
132

133
    ui->cmbScaleType->setCurrentIndex(m_base->getScaleType());
134

135
    //Allow or prevent scale changing initially
136
    if (m_base->getScaleType() == 2) {
137
        // custom scale type
138
        ui->sbScale->setEnabled(true);
139
    }
140
    else {
141
        ui->sbScale->setEnabled(false);
142
    }
143

144
    Base::Vector3d origin = m_base->getOriginalCentroid();
145
    setUiCommon(origin);
146

147
    m_compass->setDialAngle(0.0);
148
    m_viewDirectionWidget->setValueNoNotify(Base::Vector3d(1.0, 0.0, 0.0));
149

150
    //don't allow updates until a direction is picked
151
    ui->pbUpdateNow->setEnabled(false);
152
    ui->cbLiveUpdate->setEnabled(false);
153
    QString msgLiteral =
154
        QString::fromUtf8(QT_TRANSLATE_NOOP("TaskSectionView", "No direction set"));
155
    ui->lPendingUpdates->setText(msgLiteral);
156
}
157

158
void TaskSectionView::setUiEdit()
159
{
160
    //    Base::Console().Message("TSV::setUiEdit()\n");
161
    setWindowTitle(QObject::tr("Edit Section View"));
162
    std::string temp = m_section->SectionSymbol.getValue();
163
    QString qTemp = Base::Tools::fromStdString(temp);
164
    ui->leSymbol->setText(qTemp);
165

166
    ui->sbScale->setValue(m_section->getScale());
167
    ui->cmbScaleType->setCurrentIndex(m_section->getScaleType());
168
    //Allow or prevent scale changing initially
169
    if (m_section->ScaleType.isValue("Custom")) {
170
        ui->sbScale->setEnabled(true);
171
    }
172
    else {
173
        ui->sbScale->setEnabled(false);
174
    }
175

176
    Base::Vector3d origin = m_section->SectionOrigin.getValue();
177
    setUiCommon(origin);
178

179
    // convert section normal to view angle
180
    Base::Vector3d sectionNormalVec = m_section->SectionNormal.getValue();
181
    sectionNormalVec.Normalize();
182
    Base::Vector3d projectedViewDirection = m_base->projectPoint(sectionNormalVec, false);
183
    projectedViewDirection.Normalize();
184
    double viewAngle = atan2(-projectedViewDirection.y, -projectedViewDirection.x);
185
    m_compass->setDialAngle(viewAngle * 180.0 / M_PI);
186
    m_viewDirectionWidget->setValueNoNotify(sectionNormalVec * -1.0);
187
}
188

189
void TaskSectionView::setUiCommon(Base::Vector3d origin)
190
{
191
    std::string temp = m_base->getNameInDocument();
192
    QString qTemp = Base::Tools::fromStdString(temp);
193
    ui->leBaseView->setText(qTemp);
194

195
    ui->sbOrgX->setUnit(Base::Unit::Length);
196
    ui->sbOrgX->setValue(origin.x);
197
    ui->sbOrgY->setUnit(Base::Unit::Length);
198
    ui->sbOrgY->setValue(origin.y);
199
    ui->sbOrgZ->setUnit(Base::Unit::Length);
200
    ui->sbOrgZ->setValue(origin.z);
201

202
    enableAll(false);
203

204
    connect(ui->leSymbol, &QLineEdit::editingFinished, this, &TaskSectionView::onIdentifierChanged);
205

206
    //TODO: use event filter instead of keyboard tracking to capture enter/return keys
207
    // the UI file uses keyboardTracking = false so that a recomputation
208
    // will only be triggered when the arrow keys of the spinboxes are used
209
    //if this is not done, recomputes are triggered on each key press giving
210
    //unaccceptable UX
211
    connect(ui->sbScale, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &TaskSectionView::onScaleChanged);
212
    connect(ui->sbOrgX, qOverload<double>(&QuantitySpinBox::valueChanged), this, &TaskSectionView::onXChanged);
213
    connect(ui->sbOrgY, qOverload<double>(&QuantitySpinBox::valueChanged), this, &TaskSectionView::onYChanged);
214
    connect(ui->sbOrgZ, qOverload<double>(&QuantitySpinBox::valueChanged), this, &TaskSectionView::onZChanged);
215

216
    connect(ui->cmbScaleType, qOverload<int>(&QComboBox::currentIndexChanged), this, &TaskSectionView::scaleTypeChanged);
217

218
    connect(ui->pbUp, &QToolButton::clicked, this, &TaskSectionView::onUpClicked);
219
    connect(ui->pbDown, &QToolButton::clicked, this, &TaskSectionView::onDownClicked);
220
    connect(ui->pbRight, &QToolButton::clicked, this, &TaskSectionView::onRightClicked);
221
    connect(ui->pbLeft, &QToolButton::clicked, this, &TaskSectionView::onLeftClicked);
222

223
    connect(ui->pbUpdateNow, &QToolButton::clicked, this, &TaskSectionView::updateNowClicked);
224
    connect(ui->cbLiveUpdate, &QToolButton::clicked, this, &TaskSectionView::liveUpdateClicked);
225

226
    m_compass = new CompassWidget(this);
227
    auto layout = ui->compassLayout;
228
    layout->addWidget(m_compass);
229
    connect(m_compass, &CompassWidget::angleChanged, this, &TaskSectionView::slotChangeAngle);
230

231
    m_viewDirectionWidget = new VectorEditWidget(this);
232
    m_viewDirectionWidget->setLabel(QObject::tr("Current View Direction"));
233
    m_viewDirectionWidget->setToolTip(QObject::tr("The view direction in BaseView coordinates"));
234
    auto editLayout = ui->viewDirectionLayout;
235
    editLayout->addWidget(m_viewDirectionWidget);
236
    connect(m_viewDirectionWidget, &VectorEditWidget::valueChanged, this,
237
            &TaskSectionView::slotViewDirectionChanged);
238
}
239

240
//save the start conditions
241
void TaskSectionView::saveSectionState()
242
{
243
    //    Base::Console().Message("TSV::saveSectionState()\n");
244
    if (m_section) {
245
        m_saveSymbol = m_section->SectionSymbol.getValue();
246
        m_saveScale = m_section->getScale();
247
        m_saveScaleType = m_section->getScaleType();
248
        m_saveNormal = m_section->SectionNormal.getValue();
249
        m_normal = m_saveNormal;
250
        m_saveDirection = m_section->Direction.getValue();
251
        m_saveOrigin = m_section->SectionOrigin.getValue();
252
        m_saveDirName = m_section->SectionDirection.getValueAsString();
253
        m_saved = true;
254
    }
255
}
256

257
//restore the start conditions
258
void TaskSectionView::restoreSectionState()
259
{
260
    //    Base::Console().Message("TSV::restoreSectionState()\n");
261
    if (!m_section)
262
        return;
263

264
    m_section->SectionSymbol.setValue(m_saveSymbol);
265
    m_section->Scale.setValue(m_saveScale);
266
    m_section->ScaleType.setValue(m_saveScaleType);
267
    m_section->SectionNormal.setValue(m_saveNormal);
268
    m_section->Direction.setValue(m_saveDirection);
269
    m_section->SectionOrigin.setValue(m_saveOrigin);
270
    m_section->SectionDirection.setValue(m_saveDirName.c_str());
271
}
272

273
//the VectorEditWidget reports a change in direction
274
void TaskSectionView::slotViewDirectionChanged(Base::Vector3d newDirection)
275
{
276
    //    Base::Console().Message("TSV::slotViewDirectionChanged(%s)\n",
277
    //                            DrawUtil::formatVector(newDirection).c_str());
278
    Base::Vector3d projectedViewDirection = m_base->projectPoint(newDirection, false);
279
    projectedViewDirection.Normalize();
280
    double viewAngle = atan2(projectedViewDirection.y, projectedViewDirection.x);
281
    m_compass->setDialAngle(viewAngle * 180.0 / M_PI);
282
    checkAll(false);
283
    applyAligned();
284
}
285

286
//the CompassWidget reports that the view direction angle has changed
287
void TaskSectionView::slotChangeAngle(double newAngle)
288
{
289
    //    Base::Console().Message("TSV::slotChangeAngle(%.3f)\n", newAngle);
290
    double angleRadians = newAngle * M_PI / 180.0;
291
    double unitX = cos(angleRadians);
292
    double unitY = sin(angleRadians);
293
    Base::Vector3d localUnit(unitX, unitY, 0.0);
294
    m_viewDirectionWidget->setValueNoNotify(localUnit);
295
    checkAll(false);
296
    applyAligned();
297
}
298

299
//preset view directions
300
void TaskSectionView::onUpClicked()
301
{
302
    //    Base::Console().Message("TSV::onUpClicked()\n");
303
    checkAll(false);
304
    m_compass->setToNorth();
305
    m_viewDirectionWidget->setValueNoNotify(Base::Vector3d(0.0, 1.0, 0.0));
306
    applyAligned();
307
}
308

309
void TaskSectionView::onDownClicked()
310
{
311
    //    Base::Console().Message("TSV::onDownClicked()\n");
312
    checkAll(false);
313
    m_compass->setToSouth();
314
    m_viewDirectionWidget->setValueNoNotify(Base::Vector3d(0.0, -1.0, 0.0));
315
    applyAligned();
316
}
317

318
void TaskSectionView::onLeftClicked()
319
{
320
    //    Base::Console().Message("TSV::onLeftClicked()\n");
321
    checkAll(false);
322
    m_compass->setToWest();
323
    m_viewDirectionWidget->setValueNoNotify(Base::Vector3d(-1.0, 0.0, 0.0));
324
    applyAligned();
325
}
326

327
void TaskSectionView::onRightClicked()
328
{
329
    //    Base::Console().Message("TSV::onRightClicked()\n");
330
    checkAll(false);
331
    m_compass->setToEast();
332
    m_viewDirectionWidget->setValueNoNotify(Base::Vector3d(1.0, 0.0, 0.0));
333
    applyAligned();
334
}
335

336
void TaskSectionView::onIdentifierChanged()
337
{
338
    checkAll(false);
339
    apply();
340
}
341

342
void TaskSectionView::onScaleChanged()
343
{
344
    m_scaleEdited = true;
345
    checkAll(false);
346
    apply();
347

348
}
349

350
//SectionOrigin changed
351
void TaskSectionView::onXChanged()
352
{
353
    checkAll(false);
354
    apply();
355
}
356
void TaskSectionView::onYChanged()
357
{
358
    checkAll(false);
359
    apply();
360
}
361
void TaskSectionView::onZChanged()
362
{
363
    checkAll(false);
364
    apply();
365
}
366

367
void TaskSectionView::scaleTypeChanged(int index)
368
{
369
    if (index == 0) {
370
        // Page Scale Type
371
        ui->sbScale->setEnabled(false);
372
        if (m_base->findParentPage()) {
373
            ui->sbScale->setValue(m_base->findParentPage()->Scale.getValue());
374
            ui->sbScale->setEnabled(false);
375
        }
376
    }
377
    else if (index == 1) {
378
        // Automatic Scale Type
379
        ui->sbScale->setEnabled(false);
380
        if (m_section) {
381
            ui->sbScale->setValue(m_section->autoScale());
382
        }
383
    }
384
    else if (index == 2) {
385
        // Custom Scale Type
386
        ui->sbScale->setEnabled(true);
387
        if (m_section) {
388
            ui->sbScale->setValue(m_section->Scale.getValue());
389
            ui->sbScale->setEnabled(true);
390
        }
391
    }
392
    else {
393
        return;
394
    }
395
}
396

397
void TaskSectionView::checkAll(bool check)
398
{
399
    ui->pbUp->setChecked(check);
400
    ui->pbDown->setChecked(check);
401
    ui->pbRight->setChecked(check);
402
    ui->pbLeft->setChecked(check);
403
}
404

405
void TaskSectionView::enableAll(bool enable)
406
{
407
    ui->leSymbol->setEnabled(enable);
408
    ui->sbScale->setEnabled(enable);
409
    ui->sbOrgX->setEnabled(enable);
410
    ui->sbOrgY->setEnabled(enable);
411
    ui->sbOrgZ->setEnabled(enable);
412
    ui->cmbScaleType->setEnabled(enable);
413
    QString qScaleType = ui->cmbScaleType->currentText();
414
    //Allow or prevent scale changing initially
415
    if (qScaleType == QString::fromUtf8("Custom")) {
416
        ui->sbScale->setEnabled(true);
417
    }
418
    else {
419
        ui->sbScale->setEnabled(false);
420
    }
421
}
422

423
void TaskSectionView::liveUpdateClicked() { apply(true); }
424

425
void TaskSectionView::updateNowClicked() { apply(true); }
426

427
//******************************************************************************
428
bool TaskSectionView::apply(bool forceUpdate)
429
{
430
//    Base::Console().Message("TSV::apply() - liveUpdate: %d force: %d deferred: %d\n",
431
//                            ui->cbLiveUpdate->isChecked(), forceUpdate, m_applyDeferred);
432
    if (!ui->cbLiveUpdate->isChecked() && !forceUpdate) {
433
        //nothing to do
434
        m_applyDeferred++;
435
        ui->lPendingUpdates->setText(tr("%n update(s) pending", "", m_applyDeferred));
436
        return false;
437
    }
438

439
    Gui::WaitCursor wc;
440
    m_modelIsDirty = true;
441

442
    if (m_dirName.empty()) {
443
        //this should never happen
444
        std::string msg =
445
            Base::Tools::toStdString(tr("Nothing to apply. No section direction picked yet"));
446
        Base::Console().Error((msg + "\n").c_str());
447
        return false;
448
    }
449
    if (!m_section) {
450
        m_section = createSectionView();
451
    }
452

453
    if (isSectionValid()) {
454
        updateSectionView();
455
    }
456
    else {
457
        failNoObject();
458
    }
459

460
    m_section->recomputeFeature();
461
    if (isBaseValid()) {
462
        m_base->requestPaint();
463
    }
464

465
    enableAll(true);
466
    checkAll(false);
467

468
    wc.restoreCursor();
469
    m_applyDeferred = 0;
470
    ui->lPendingUpdates->setText(QString());
471
    return true;
472
}
473

474
void TaskSectionView::applyQuick(std::string dir)
475
{
476
    //    Base::Console().Message("TSV::applyQuick(%s)\n", dir.c_str());
477
    m_dirName = dir;
478
    enableAll(true);
479
    apply();
480
}
481

482
void TaskSectionView::applyAligned()
483
{
484
    //    Base::Console().Message("TSV::applyAligned()\n");
485
    m_dirName = "Aligned";
486
    enableAll(true);
487
    m_directionIsSet = true;
488
    ui->pbUpdateNow->setEnabled(true);
489
    ui->cbLiveUpdate->setEnabled(true);
490
    apply();
491
}
492

493
//*********************************************************************
494

495
TechDraw::DrawViewSection* TaskSectionView::createSectionView(void)
496
{
497
    // Base::Console().Message("TSV::createSectionView()\n");
498
    if (!isBaseValid()) {
499
        failNoObject();
500
        return nullptr;
501
    }
502

503
    std::string baseName = m_base->getNameInDocument();
504

505
    Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Create SectionView"));
506
    if (!m_section) {
507
        const std::string objectName("SectionView");
508
        m_sectionName = m_base->getDocument()->getUniqueObjectName(objectName.c_str());
509
        Command::doCommand(Command::Doc, "App.ActiveDocument.addObject('TechDraw::DrawViewSection', '%s')",
510
                           m_sectionName.c_str());
511

512
        // section labels (Section A-A) are not unique, and are not the same as the object name (SectionView)
513
        // we pluck the generated suffix from the object name and append it to "Section" to generate
514
        // unique Labels
515
        QString qTemp = ui->leSymbol->text();
516
        std::string temp = Base::Tools::toStdString(qTemp);
517
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.SectionSymbol = '%s'",
518
                           m_sectionName.c_str(), temp.c_str());
519

520
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.Label = '%s'",
521
                           m_sectionName.c_str(),
522
                           makeSectionLabel(qTemp).c_str());
523
        Command::doCommand(Command::Doc, "App.activeDocument().%s.translateLabel('DrawViewSection', 'Section', '%s')",
524
              m_sectionName.c_str(), makeSectionLabel(qTemp).c_str());
525

526

527
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.addView(App.ActiveDocument.%s)",
528
                           m_savePageName.c_str(), m_sectionName.c_str());
529
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.BaseView = App.ActiveDocument.%s",
530
                           m_sectionName.c_str(), baseName.c_str());
531
        Command::doCommand(Command::Doc,
532
                           "App.ActiveDocument.%s.Source = App.ActiveDocument.%s.Source",
533
                           m_sectionName.c_str(), baseName.c_str());
534
        Command::doCommand(Command::Doc,
535
                           "App.ActiveDocument.%s.SectionOrigin = FreeCAD.Vector(%.6f, %.6f, %.6f)",
536
                           m_sectionName.c_str(), ui->sbOrgX->value().getValue(),
537
                           ui->sbOrgY->value().getValue(), ui->sbOrgZ->value().getValue());
538

539
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.Scale = %0.7f",
540
                           m_sectionName.c_str(), ui->sbScale->value());
541

542
        int scaleType = ui->cmbScaleType->currentIndex();
543
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.ScaleType = %d",
544
                           m_sectionName.c_str(), scaleType);
545
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.SectionDirection = '%s'",
546
                           m_sectionName.c_str(), m_dirName.c_str());
547

548
        App::DocumentObject* newObj = m_base->getDocument()->getObject(m_sectionName.c_str());
549
        m_section = dynamic_cast<TechDraw::DrawViewSection*>(newObj);
550
        if (!newObj || !m_section) {
551
            throw Base::RuntimeError("TaskSectionView - new section object not found");
552
        }
553
        Base::Vector3d localUnit = m_viewDirectionWidget->value();
554
        localUnit.Normalize();
555
        if (m_dirName == "Aligned") {
556
            //localUnit is a view direction so we need to reverse it to make a
557
            //section normal
558
            m_section->setCSFromBase(localUnit * -1.0);
559
        }
560
        else {
561
            //Note: DirectionName is to be deprecated in the future
562
            m_section->setCSFromBase(m_dirName.c_str());
563
        }
564
        //auto orientation of view relative to base view
565
        double viewDirectionAngle = m_compass->positiveValue();
566
        double rotation = requiredRotation(viewDirectionAngle);
567
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.Rotation = %.6f",
568
                           m_sectionName.c_str(), rotation);
569

570
    }
571
    Gui::Command::commitCommand();
572
    return m_section;
573
}
574

575
void TaskSectionView::updateSectionView()
576
{
577
    // Base::Console().Message("TSV::updateSectionView() - m_sectionName: %s\n", m_sectionName.c_str());
578
    if (!isSectionValid()) {
579
        failNoObject();
580
        return;
581
    }
582

583
    const std::string objectName("SectionView");
584
    std::string baseName = m_base->getNameInDocument();
585

586
    Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Edit SectionView"));
587
    if (m_section) {
588
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.SectionDirection = '%s'",
589
                           m_sectionName.c_str(), m_dirName.c_str());
590
        Command::doCommand(Command::Doc,
591
                           "App.ActiveDocument.%s.SectionOrigin = FreeCAD.Vector(%.3f, %.3f, %.3f)",
592
                           m_sectionName.c_str(), ui->sbOrgX->value().getValue(),
593
                           ui->sbOrgY->value().getValue(), ui->sbOrgZ->value().getValue());
594

595
        QString qTemp = ui->leSymbol->text();
596
        std::string temp = Base::Tools::toStdString(qTemp);
597
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.SectionSymbol = '%s'",
598
                           m_sectionName.c_str(), temp.c_str());
599

600
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.Label = '%s'",
601
                           m_sectionName.c_str(),
602
                           makeSectionLabel(qTemp).c_str());
603
        Command::doCommand(Command::Doc, "App.activeDocument().%s.translateLabel('DrawViewSection', 'Section', '%s')",
604
              m_sectionName.c_str(), makeSectionLabel(qTemp).c_str());
605

606
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.Scale = %0.7f",
607
                           m_sectionName.c_str(), ui->sbScale->value());
608

609
        int scaleType = ui->cmbScaleType->currentIndex();
610
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.ScaleType = %d",
611
                           m_sectionName.c_str(), scaleType);
612
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.SectionDirection = '%s'",
613
                           m_sectionName.c_str(), m_dirName.c_str());
614
        Base::Vector3d localUnit = m_viewDirectionWidget->value();
615
        localUnit.Normalize();
616
        if (m_dirName == "Aligned") {
617
            //localUnit is a view direction so we need to reverse it to make a
618
            //section normal
619
            m_section->setCSFromBase(localUnit * -1.0);
620
        }
621
        else {
622
            //Note: DirectionName is to be deprecated in the future
623
            m_section->setCSFromBase(m_dirName.c_str());
624
        }
625
        //auto orientation of view relative to base view
626
        double viewDirectionAngle = m_compass->positiveValue();
627
        double rotation = requiredRotation(viewDirectionAngle);
628
        Command::doCommand(Command::Doc, "App.ActiveDocument.%s.Rotation = %.6f",
629
                           m_sectionName.c_str(), rotation);
630
    }
631
    Gui::Command::commitCommand();
632
}
633

634
std::string TaskSectionView::makeSectionLabel(QString symbol)
635
{
636
    const std::string objectName("SectionView");
637
    std::string uniqueSuffix{m_sectionName.substr(objectName.length(), std::string::npos)};
638
    std::string uniqueLabel = "Section" + uniqueSuffix;
639
    std::string temp = Base::Tools::toStdString(symbol);
640
    return ( uniqueLabel + " " + temp + " - " + temp );
641
}
642

643
void TaskSectionView::failNoObject(void)
644
{
645
    QString qsectionName = Base::Tools::fromStdString(m_sectionName);
646
    QString qbaseName = Base::Tools::fromStdString(m_baseName);
647
    QString msg = tr("Can not continue. Object * %1 or %2 not found.").arg(qsectionName, qbaseName);
648
    QMessageBox::critical(Gui::getMainWindow(), QObject::tr("Operation Failed"), msg);
649
    Gui::Control().closeDialog();
650
}
651

652
bool TaskSectionView::isBaseValid()
653
{
654
    if (!m_base)
655
        return false;
656

657
    App::DocumentObject* baseObj = m_doc->getObject(m_saveBaseName.c_str());
658
    if (!baseObj)
659
        return false;
660

661
    return true;
662
}
663

664
bool TaskSectionView::isSectionValid()
665
{
666
    if (!m_section)
667
        return false;
668

669
    App::DocumentObject* sectionObj = m_doc->getObject(m_sectionName.c_str());
670
    if (!sectionObj)
671
        return false;
672

673
    return true;
674
}
675

676
//get required rotation from input angle in [0, 360]
677
double TaskSectionView::requiredRotation(double inputAngle)
678
{
679
    double rotation = inputAngle - 90.0;
680
    if (rotation == 180.0) {
681
        //if the view direction is 90/270, then the section is drawn properly and no
682
        //rotation is needed.  90.0 becomes 0.0, but 270.0 needs special handling.
683
        rotation = 0.0;
684
    }
685
    return rotation;
686
}
687

688
//******************************************************************************
689

690
bool TaskSectionView::accept()
691
{
692
    //    Base::Console().Message("TSV::accept()\n");
693
    apply(true);
694
    Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
695
    return true;
696
}
697

698
bool TaskSectionView::reject()
699
{
700
    //    Base::Console().Message("TSV::reject()\n");
701
    if (!m_section) {//no section created, nothing to undo
702
        Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
703
        return false;
704
    }
705

706
    if (!isSectionValid()) {//section !exist. nothing to undo
707
        if (isBaseValid()) {
708
            m_base->requestPaint();
709
        }
710
        Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
711
        return false;
712
    }
713

714
    if (m_createMode) {
715
        std::string SectionName = m_section->getNameInDocument();
716
        Gui::Command::doCommand(Gui::Command::Gui,
717
                                "App.ActiveDocument.%s.removeView(App.ActiveDocument.%s)",
718
                                m_savePageName.c_str(), SectionName.c_str());
719
        Gui::Command::doCommand(Gui::Command::Gui, "App.ActiveDocument.removeObject('%s')",
720
                                SectionName.c_str());
721

722
    } else {
723
        if (m_modelIsDirty) {
724
            restoreSectionState();
725
            m_section->recomputeFeature();
726
            m_section->requestPaint();
727
        }
728
    }
729

730
    if (isBaseValid()) {
731
        m_base->requestPaint();
732
    }
733
    Gui::Command::updateActive();
734
    Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
735

736
    return false;
737
}
738

739
void TaskSectionView::changeEvent(QEvent* event)
740
{
741
    if (event->type() == QEvent::LanguageChange) {
742
        ui->retranslateUi(this);
743
    }
744
}
745

746
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
747
TaskDlgSectionView::TaskDlgSectionView(TechDraw::DrawViewPart* base) : TaskDialog()
748
{
749
    widget = new TaskSectionView(base);
750
    taskbox =
751
        new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("actions/TechDraw_SectionView"),
752
                                   widget->windowTitle(), true, nullptr);
753
    taskbox->groupLayout()->addWidget(widget);
754
    Content.push_back(taskbox);
755
}
756

757
TaskDlgSectionView::TaskDlgSectionView(TechDraw::DrawViewSection* section) : TaskDialog()
758
{
759
    widget = new TaskSectionView(section);
760
    taskbox =
761
        new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("actions/TechDraw_SectionView"),
762
                                   widget->windowTitle(), true, nullptr);
763

764
    taskbox->groupLayout()->addWidget(widget);
765
    Content.push_back(taskbox);
766
}
767
TaskDlgSectionView::~TaskDlgSectionView() {}
768

769
void TaskDlgSectionView::update()
770
{
771
    //widget->updateTask();
772
}
773

774
//==== calls from the TaskView ===============================================================
775
void TaskDlgSectionView::open() {}
776

777
bool TaskDlgSectionView::accept()
778
{
779
    widget->accept();
780
    return true;
781
}
782

783
bool TaskDlgSectionView::reject()
784
{
785
    widget->reject();
786
    return true;
787
}
788

789
#include <Mod/TechDraw/Gui/moc_TaskSectionView.cpp>
790

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.