FreeCAD

Форк
0
/
Command.cpp 
876 строк · 31.8 Кб
1
/***************************************************************************
2
 *                                                                         *
3
 *   This program is free software; you can redistribute it and/or modify  *
4
 *   it under the terms of the GNU Library General Public License as       *
5
 *   published by the Free Software Foundation; either version 2 of the    *
6
 *   License, or (at your option) any later version.                       *
7
 *   for detail see the LICENCE text file.                                 *
8
 *   Jürgen Riegel 2002                                                    *
9
 *                                                                         *
10
 ***************************************************************************/
11

12
#include "PreCompiled.h"
13
#ifndef _PreComp_
14
#include <sstream>
15
#include <vector>
16

17
#include <QCoreApplication>
18
#include <QDir>
19
#include <QFile>
20
#include <QFileInfo>
21
#include <QMessageBox>
22
#include <QRegularExpression>
23
#endif
24

25
#include <App/Document.h>
26
#include <App/PropertyGeo.h>
27
#include <Base/Tools.h>
28
#include <Gui/Action.h>
29
#include <Gui/Application.h>
30
#include <Gui/BitmapFactory.h>
31
#include <Gui/Command.h>
32
#include <Gui/Control.h>
33
#include <Gui/FileDialog.h>
34
#include <Gui/MainWindow.h>
35
#include <Gui/Selection.h>
36
#include <Mod/Drawing/App/FeaturePage.h>
37
#include <Mod/Part/App/PartFeature.h>
38
#include <Mod/Spreadsheet/App/Sheet.h>
39

40
#include "TaskDialog.h"
41
#include "TaskOrthoViews.h"
42

43

44
using namespace DrawingGui;
45
using namespace std;
46

47

48
//===========================================================================
49
// CmdDrawingOpen
50
//===========================================================================
51

52
DEF_STD_CMD(CmdDrawingOpen)
53

54
CmdDrawingOpen::CmdDrawingOpen()
55
    : Command("Drawing_Open")
56
{
57
    sAppModule = "Drawing";
58
    sGroup = QT_TR_NOOP("Drawing");
59
    sMenuText = QT_TR_NOOP("Open SVG...");
60
    sToolTipText = QT_TR_NOOP("Open a scalable vector graphic");
61
    sWhatsThis = "Drawing_Open";
62
    sStatusTip = sToolTipText;
63
    sPixmap = "actions/document-new";
64
}
65

66
void CmdDrawingOpen::activated(int iMsg)
67
{
68
    Q_UNUSED(iMsg);
69
    // Reading an image
70
    QString filename = Gui::FileDialog::getOpenFileName(
71
        Gui::getMainWindow(),
72
        QObject::tr("Choose an SVG file to open"),
73
        QString(),
74
        QString::fromLatin1("%1 (*.svg *.svgz)").arg(QObject::tr("Scalable Vector Graphic")));
75
    if (!filename.isEmpty()) {
76
        filename = Base::Tools::escapeEncodeFilename(filename);
77
        // load the file with the module
78
        Command::doCommand(Command::Gui, "import Drawing, DrawingGui");
79
        Command::doCommand(Command::Gui, "DrawingGui.open(\"%s\")", (const char*)filename.toUtf8());
80
    }
81
}
82

83
//===========================================================================
84
// Drawing_NewPage
85
//===========================================================================
86

87
DEF_STD_CMD_ACL(CmdDrawingNewPage)
88

89
CmdDrawingNewPage::CmdDrawingNewPage()
90
    : Command("Drawing_NewPage")
91
{
92
    sAppModule = "Drawing";
93
    sGroup = QT_TR_NOOP("Drawing");
94
    sMenuText = QT_TR_NOOP("Insert new drawing");
95
    sToolTipText = QT_TR_NOOP("Insert new drawing");
96
    sWhatsThis = "Drawing_NewPage";
97
    sStatusTip = sToolTipText;
98
}
99

100
void CmdDrawingNewPage::activated(int iMsg)
101
{
102
    Gui::ActionGroup* pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction);
103
    QAction* a = qAsConst(pcAction)->actions()[iMsg];
104

105
    std::string FeatName = getUniqueObjectName(
106
        QCoreApplication::translate("Drawing_NewPage", "Page").toStdString().c_str());
107

108
    QFileInfo tfi(a->property("Template").toString());
109
    if (tfi.isReadable()) {
110
        QString filename = Base::Tools::escapeEncodeFilename(tfi.filePath());
111
        openCommand("Create page");
112
        doCommand(Doc,
113
                  "App.activeDocument().addObject('Drawing::FeaturePage','%s')",
114
                  FeatName.c_str());
115
        doCommand(Doc,
116
                  "App.activeDocument().%s.Template = '%s'",
117
                  FeatName.c_str(),
118
                  (const char*)filename.toUtf8());
119
        doCommand(Doc, "App.activeDocument().recompute()");
120
        doCommand(Doc, "Gui.activeDocument().getObject('%s').show()", FeatName.c_str());
121
        commitCommand();
122
    }
123
    else {
124
        QMessageBox::critical(Gui::getMainWindow(),
125
                              QLatin1String("No template"),
126
                              QLatin1String("No template available for this page size"));
127
    }
128
}
129

130
Gui::Action* CmdDrawingNewPage::createAction(void)
131
{
132
    Gui::ActionGroup* pcAction = new Gui::ActionGroup(this, Gui::getMainWindow());
133
    pcAction->setDropDownMenu(true);
134
    applyCommandData(this->className(), pcAction);
135

136
    QAction* defaultAction = nullptr;
137
    int defaultId = 0;
138

139
    QString lastPaper;
140
    int lastId = -1;
141

142
    std::string path = App::Application::getResourceDir();
143
    path += "Mod/Drawing/Templates/";
144
    QDir dir(QString::fromUtf8(path.c_str()), QString::fromLatin1("*.svg"));
145
    for (unsigned int i = 0; i < dir.count(); i++) {
146
        QRegularExpression rx(
147
            QString::fromLatin1("(A|B|C|D|E)(\\d)_(Landscape|Portrait)(_.*\\.|\\.)svg$"));
148
        auto match = rx.match(dir[i]);
149
        if (match.hasMatch()) {
150
            QString paper = match.captured(1);
151
            int id = match.captured(2).toInt();
152
            QString orientation = match.captured(3);
153
            QString info = match.captured(4).mid(1);
154
            info.chop(1);
155
            if (!info.isEmpty()) {
156
                info[0] = info[0].toUpper();
157
            }
158

159
            // group by paper size
160
            if (!lastPaper.isEmpty()) {
161
                if (lastPaper != paper) {
162
                    QAction* sep = pcAction->addAction(QString());
163
                    sep->setSeparator(true);
164
                }
165
                else if (lastId != id) {
166
                    QAction* sep = pcAction->addAction(QString());
167
                    sep->setSeparator(true);
168
                }
169
            }
170

171
            lastPaper = paper;
172
            lastId = id;
173

174
            QFile file(QString::fromLatin1(":/icons/actions/drawing-%1-%2%3.svg")
175
                           .arg(orientation.toLower(), paper)
176
                           .arg(id));
177
            QAction* a = pcAction->addAction(QString());
178
            if (file.open(QFile::ReadOnly)) {
179
                QByteArray data = file.readAll();
180
                a->setIcon(Gui::BitmapFactory().pixmapFromSvg(data, QSize(64, 64)));
181
            }
182

183
            a->setProperty("TemplatePaper", paper);
184
            a->setProperty("TemplateOrientation", orientation);
185
            a->setProperty("TemplateId", id);
186
            a->setProperty("TemplateInfo", info);
187
            a->setProperty("Template", dir.absoluteFilePath(dir[i]));
188

189
            if (id == 3) {
190
                if (!defaultAction) {
191
                    // set the first found A3 (A3_Landscape) as default
192
                    defaultAction = a;
193
                    defaultId = pcAction->actions().size() - 1;
194
                }
195
            }
196
        }
197
    }
198

199
    _pcAction = pcAction;
200

201
    languageChange();
202
    if (defaultAction) {
203
        pcAction->setIcon(defaultAction->icon());
204
        pcAction->setProperty("defaultAction", QVariant(defaultId));
205
    }
206
    else if (!pcAction->actions().isEmpty()) {
207
        pcAction->setIcon(qAsConst(pcAction)->actions()[0]->icon());
208
        pcAction->setProperty("defaultAction", QVariant(0));
209
    }
210

211
    return pcAction;
212
}
213

214
void CmdDrawingNewPage::languageChange()
215
{
216
    Command::languageChange();
217

218
    if (!_pcAction) {
219
        return;
220
    }
221
    Gui::ActionGroup* pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction);
222
    QList<QAction*> a = pcAction->actions();
223
    for (QList<QAction*>::iterator it = a.begin(); it != a.end(); ++it) {
224
        if ((*it)->isSeparator()) {
225
            continue;
226
        }
227
        QString paper = (*it)->property("TemplatePaper").toString();
228
        int id = (*it)->property("TemplateId").toInt();
229
        QString orientation = (*it)->property("TemplateOrientation").toString();
230
        if (orientation.compare(QLatin1String("landscape"), Qt::CaseInsensitive) == 0) {
231
            orientation = QCoreApplication::translate("Drawing_NewPage", "Landscape");
232
        }
233
        else if (orientation.compare(QLatin1String("portrait"), Qt::CaseInsensitive) == 0) {
234
            orientation = QCoreApplication::translate("Drawing_NewPage", "Portrait");
235
        }
236
        QString info = (*it)->property("TemplateInfo").toString();
237

238
        if (info.isEmpty()) {
239
            (*it)->setText(QCoreApplication::translate("Drawing_NewPage", "%1%2 %3")
240
                               .arg(paper, QString::number(id), orientation));
241
            (*it)->setToolTip(
242
                QCoreApplication::translate("Drawing_NewPage", "Insert new %1%2 %3 drawing")
243
                    .arg(paper, QString::number(id), orientation));
244
        }
245
        else {
246
            (*it)->setText(QCoreApplication::translate("Drawing_NewPage", "%1%2 %3 (%4)")
247
                               .arg(paper, QString::number(id), orientation, info));
248
            (*it)->setToolTip(
249
                QCoreApplication::translate("Drawing_NewPage", "Insert new %1%2 %3 (%4) drawing")
250
                    .arg(paper, QString::number(id), orientation, info));
251
        }
252
    }
253
}
254

255
bool CmdDrawingNewPage::isActive(void)
256
{
257
    if (getActiveGuiDocument()) {
258
        return true;
259
    }
260
    else {
261
        return false;
262
    }
263
}
264

265
//===========================================================================
266
// Drawing_NewA3Landscape
267
//===========================================================================
268

269
DEF_STD_CMD_A(CmdDrawingNewA3Landscape)
270

271
CmdDrawingNewA3Landscape::CmdDrawingNewA3Landscape()
272
    : Command("Drawing_NewA3Landscape")
273
{
274
    sAppModule = "Drawing";
275
    sGroup = QT_TR_NOOP("Drawing");
276
    sMenuText = QT_TR_NOOP("Insert new A3 landscape drawing");
277
    sToolTipText = QT_TR_NOOP("Insert new A3 landscape drawing");
278
    sWhatsThis = "Drawing_NewA3Landscape";
279
    sStatusTip = sToolTipText;
280
    sPixmap = "actions/drawing-landscape-A3";
281
}
282

283
void CmdDrawingNewA3Landscape::activated(int iMsg)
284
{
285
    Q_UNUSED(iMsg);
286
    std::string FeatName = getUniqueObjectName("Page");
287

288
    openCommand("Create page");
289
    doCommand(Doc, "App.activeDocument().addObject('Drawing::FeaturePage','%s')", FeatName.c_str());
290
    doCommand(Doc, "App.activeDocument().%s.Template = 'A3_Landscape.svg'", FeatName.c_str());
291
    doCommand(Doc, "App.activeDocument().recompute()");
292
    commitCommand();
293
}
294

295
bool CmdDrawingNewA3Landscape::isActive(void)
296
{
297
    if (getActiveGuiDocument()) {
298
        return true;
299
    }
300
    else {
301
        return false;
302
    }
303
}
304

305

306
//===========================================================================
307
// Drawing_NewView
308
//===========================================================================
309

310
DEF_STD_CMD(CmdDrawingNewView)
311

312
CmdDrawingNewView::CmdDrawingNewView()
313
    : Command("Drawing_NewView")
314
{
315
    sAppModule = "Drawing";
316
    sGroup = QT_TR_NOOP("Drawing");
317
    sMenuText = QT_TR_NOOP("Insert view in drawing");
318
    sToolTipText = QT_TR_NOOP("Insert a new View of a Part in the active drawing");
319
    sWhatsThis = "Drawing_NewView";
320
    sStatusTip = sToolTipText;
321
    sPixmap = "actions/drawing-view";
322
}
323

324
void CmdDrawingNewView::activated(int iMsg)
325
{
326
    Q_UNUSED(iMsg);
327
    std::vector<App::DocumentObject*> shapes =
328
        getSelection().getObjectsOfType(Part::Feature::getClassTypeId());
329
    if (shapes.empty()) {
330
        QMessageBox::warning(Gui::getMainWindow(),
331
                             QObject::tr("Wrong selection"),
332
                             QObject::tr("Select a Part object."));
333
        return;
334
    }
335

336
    std::vector<App::DocumentObject*> pages =
337
        getSelection().getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
338
    if (pages.empty()) {
339
        pages = this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
340
        if (pages.empty()) {
341
            QMessageBox::warning(Gui::getMainWindow(),
342
                                 QObject::tr("No page found"),
343
                                 QObject::tr("Create a page first."));
344
            return;
345
        }
346
    }
347

348
    const std::vector<App::DocumentObject*> selectedProjections =
349
        getSelection().getObjectsOfType(Drawing::FeatureView::getClassTypeId());
350
    float newX = 10.0;
351
    float newY = 10.0;
352
    float newScale = 1.0;
353
    float newRotation = 0.0;
354
    Base::Vector3d newDirection(0.0, 0.0, 1.0);
355
    if (!selectedProjections.empty()) {
356
        const Drawing::FeatureView* const myView =
357
            static_cast<Drawing::FeatureView*>(selectedProjections.front());
358

359
        newX = myView->X.getValue();
360
        newY = myView->Y.getValue();
361
        newScale = myView->Scale.getValue();
362
        newRotation = myView->Rotation.getValue();
363

364
        // The "Direction" property does not belong to Drawing::FeatureView, but to one of the
365
        // many child classes that are projecting objects into the drawing. Therefore, we get the
366
        // property by name.
367
        const App::PropertyVector* const propDirection =
368
            dynamic_cast<App::PropertyVector*>(myView->getPropertyByName("Direction"));
369
        if (propDirection) {
370
            newDirection = propDirection->getValue();
371
        }
372
    }
373

374
    std::string PageName = pages.front()->getNameInDocument();
375

376
    openCommand("Create view");
377
    for (std::vector<App::DocumentObject*>::iterator it = shapes.begin(); it != shapes.end();
378
         ++it) {
379
        std::string FeatName = getUniqueObjectName("View");
380
        doCommand(Doc,
381
                  "App.activeDocument().addObject('Drawing::FeatureViewPart','%s')",
382
                  FeatName.c_str());
383
        doCommand(Doc,
384
                  "App.activeDocument().%s.Source = App.activeDocument().%s",
385
                  FeatName.c_str(),
386
                  (*it)->getNameInDocument());
387
        doCommand(Doc,
388
                  "App.activeDocument().%s.Direction = (%e,%e,%e)",
389
                  FeatName.c_str(),
390
                  newDirection.x,
391
                  newDirection.y,
392
                  newDirection.z);
393
        doCommand(Doc, "App.activeDocument().%s.X = %e", FeatName.c_str(), newX);
394
        doCommand(Doc, "App.activeDocument().%s.Y = %e", FeatName.c_str(), newY);
395
        doCommand(Doc, "App.activeDocument().%s.Scale = %e", FeatName.c_str(), newScale);
396
        doCommand(Doc, "App.activeDocument().%s.Rotation = %e", FeatName.c_str(), newRotation);
397
        doCommand(Doc,
398
                  "App.activeDocument().%s.addObject(App.activeDocument().%s)",
399
                  PageName.c_str(),
400
                  FeatName.c_str());
401
    }
402
    updateActive();
403
    commitCommand();
404
}
405

406
//===========================================================================
407
// Drawing_OrthoView
408
//===========================================================================
409

410
DEF_STD_CMD_A(CmdDrawingOrthoViews)
411

412
CmdDrawingOrthoViews::CmdDrawingOrthoViews()
413
    : Command("Drawing_OrthoViews")
414
{
415
    sAppModule = "Drawing";
416
    sGroup = QT_TR_NOOP("Drawing");
417
    sMenuText = QT_TR_NOOP("Insert orthographic views");
418
    sToolTipText = QT_TR_NOOP("Insert an orthographic projection of a part in the active drawing");
419
    sWhatsThis = "Drawing_OrthoView";
420
    sStatusTip = sToolTipText;
421
    sPixmap = "actions/drawing-orthoviews";
422
}
423

424
void CmdDrawingOrthoViews::activated(int iMsg)
425
{
426
    Q_UNUSED(iMsg);
427
    const std::vector<App::DocumentObject*> shapes =
428
        getSelection().getObjectsOfType(Part::Feature::getClassTypeId());
429
    if (shapes.size() != 1) {
430
        QMessageBox::warning(Gui::getMainWindow(),
431
                             QObject::tr("Wrong selection"),
432
                             QObject::tr("Select exactly one Part object."));
433
        return;
434
    }
435

436
    // Check that a page object exists. TaskDlgOrthoViews will then check for a selected page object
437
    // and use that, otherwise it will use the first page in the document.
438
    const std::vector<App::DocumentObject*> pages =
439
        this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
440
    if (pages.empty()) {
441
        QMessageBox::warning(Gui::getMainWindow(),
442
                             QObject::tr("No page found"),
443
                             QObject::tr("Create a page first."));
444
        return;
445
    }
446

447
    TaskDlgOrthoViews* dlg = new TaskDlgOrthoViews();
448
    dlg->setDocumentName(this->getDocument()->getName());
449
    Gui::Control().showDialog(dlg);
450
}
451

452
bool CmdDrawingOrthoViews::isActive(void)
453
{
454
    if (Gui::Control().activeDialog()) {
455
        return false;
456
    }
457
    return true;
458
}
459

460

461
//===========================================================================
462
// Drawing_OpenBrowserView
463
//===========================================================================
464

465
DEF_STD_CMD_A(CmdDrawingOpenBrowserView)
466

467
CmdDrawingOpenBrowserView::CmdDrawingOpenBrowserView()
468
    : Command("Drawing_OpenBrowserView")
469
{
470
    // setting the
471
    sGroup = QT_TR_NOOP("Drawing");
472
    sMenuText = QT_TR_NOOP("Open &browser view");
473
    sToolTipText = QT_TR_NOOP("Opens the selected page in a browser view");
474
    sWhatsThis = "Drawing_OpenBrowserView";
475
    sStatusTip = QT_TR_NOOP("Opens the selected page in a browser view");
476
    sPixmap = "actions/drawing-openbrowser";
477
}
478

479
void CmdDrawingOpenBrowserView::activated(int iMsg)
480
{
481
    Q_UNUSED(iMsg);
482
    unsigned int n = getSelection().countObjectsOfType(Drawing::FeaturePage::getClassTypeId());
483
    if (n != 1) {
484
        QMessageBox::warning(Gui::getMainWindow(),
485
                             QObject::tr("Wrong selection"),
486
                             QObject::tr("Select one Page object."));
487
        return;
488
    }
489
    std::vector<Gui::SelectionSingleton::SelObj> Sel = getSelection().getSelection();
490
    doCommand(Doc, "PageName = App.activeDocument().%s.PageResult", Sel[0].FeatName);
491
    doCommand(Doc, "import WebGui");
492
    doCommand(Doc, "WebGui.openBrowser(PageName)");
493
}
494

495
bool CmdDrawingOpenBrowserView::isActive(void)
496
{
497
    return (getActiveGuiDocument() ? true : false);
498
}
499

500
//===========================================================================
501
// Drawing_Annotation
502
//===========================================================================
503

504
DEF_STD_CMD_A(CmdDrawingAnnotation)
505

506
CmdDrawingAnnotation::CmdDrawingAnnotation()
507
    : Command("Drawing_Annotation")
508
{
509
    // setting the
510
    sGroup = QT_TR_NOOP("Drawing");
511
    sMenuText = QT_TR_NOOP("&Annotation");
512
    sToolTipText = QT_TR_NOOP("Inserts an Annotation view in the active drawing");
513
    sWhatsThis = "Drawing_Annotation";
514
    sStatusTip = QT_TR_NOOP("Inserts an Annotation view in the active drawing");
515
    sPixmap = "actions/drawing-annotation";
516
}
517

518
void CmdDrawingAnnotation::activated(int iMsg)
519
{
520
    Q_UNUSED(iMsg);
521
    std::vector<App::DocumentObject*> pages =
522
        getSelection().getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
523
    if (pages.empty()) {
524
        pages = this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
525
        if (pages.empty()) {
526
            QMessageBox::warning(Gui::getMainWindow(),
527
                                 QObject::tr("No page found"),
528
                                 QObject::tr("Create a page first."));
529
            return;
530
        }
531
    }
532
    std::string PageName = pages.front()->getNameInDocument();
533
    std::string FeatName = getUniqueObjectName("Annotation");
534
    openCommand("Create Annotation");
535
    doCommand(Doc,
536
              "App.activeDocument().addObject('Drawing::FeatureViewAnnotation','%s')",
537
              FeatName.c_str());
538
    doCommand(Doc, "App.activeDocument().%s.X = 10.0", FeatName.c_str());
539
    doCommand(Doc, "App.activeDocument().%s.Y = 10.0", FeatName.c_str());
540
    doCommand(Doc, "App.activeDocument().%s.Scale = 7.0", FeatName.c_str());
541
    doCommand(Doc,
542
              "App.activeDocument().%s.addObject(App.activeDocument().%s)",
543
              PageName.c_str(),
544
              FeatName.c_str());
545
    updateActive();
546
    commitCommand();
547
}
548

549
bool CmdDrawingAnnotation::isActive(void)
550
{
551
    return (getActiveGuiDocument() ? true : false);
552
}
553

554

555
//===========================================================================
556
// Drawing_Clip
557
//===========================================================================
558

559
DEF_STD_CMD_A(CmdDrawingClip)
560

561
CmdDrawingClip::CmdDrawingClip()
562
    : Command("Drawing_Clip")
563
{
564
    // setting the
565
    sGroup = QT_TR_NOOP("Drawing");
566
    sMenuText = QT_TR_NOOP("&Clip");
567
    sToolTipText = QT_TR_NOOP("Inserts a clip group in the active drawing");
568
    sWhatsThis = "Drawing_Annotation";
569
    sStatusTip = QT_TR_NOOP("Inserts a clip group in the active drawing");
570
    sPixmap = "actions/drawing-clip";
571
}
572

573
void CmdDrawingClip::activated(int iMsg)
574
{
575
    Q_UNUSED(iMsg);
576
    std::vector<App::DocumentObject*> pages =
577
        getSelection().getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
578
    if (pages.empty()) {
579
        pages = this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
580
        if (pages.empty()) {
581
            QMessageBox::warning(Gui::getMainWindow(),
582
                                 QObject::tr("No page found"),
583
                                 QObject::tr("Create a page first."));
584
            return;
585
        }
586
    }
587
    std::string PageName = pages.front()->getNameInDocument();
588
    std::string FeatName = getUniqueObjectName("Clip");
589
    openCommand("Create Clip");
590
    doCommand(Doc, "App.activeDocument().addObject('Drawing::FeatureClip','%s')", FeatName.c_str());
591
    doCommand(Doc,
592
              "App.activeDocument().%s.addObject(App.activeDocument().%s)",
593
              PageName.c_str(),
594
              FeatName.c_str());
595
    updateActive();
596
    commitCommand();
597
}
598

599
bool CmdDrawingClip::isActive(void)
600
{
601
    return (getActiveGuiDocument() ? true : false);
602
}
603

604

605
//===========================================================================
606
// Drawing_Symbol
607
//===========================================================================
608

609
DEF_STD_CMD_A(CmdDrawingSymbol)
610

611
CmdDrawingSymbol::CmdDrawingSymbol()
612
    : Command("Drawing_Symbol")
613
{
614
    // setting the
615
    sGroup = QT_TR_NOOP("Drawing");
616
    sMenuText = QT_TR_NOOP("&Symbol");
617
    sToolTipText = QT_TR_NOOP("Inserts a symbol from a svg file in the active drawing");
618
    sWhatsThis = "Drawing_Symbol";
619
    sStatusTip = QT_TR_NOOP("Inserts a symbol from a svg file in the active drawing");
620
    sPixmap = "actions/drawing-symbol";
621
}
622

623
void CmdDrawingSymbol::activated(int iMsg)
624
{
625
    Q_UNUSED(iMsg);
626
    std::vector<App::DocumentObject*> pages =
627
        getSelection().getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
628
    if (pages.empty()) {
629
        pages = this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
630
        if (pages.empty()) {
631
            QMessageBox::warning(Gui::getMainWindow(),
632
                                 QObject::tr("No page found"),
633
                                 QObject::tr("Create a page first."));
634
            return;
635
        }
636
    }
637
    // Reading an image
638
    QString filename = Gui::FileDialog::getOpenFileName(
639
        Gui::getMainWindow(),
640
        QObject::tr("Choose an SVG file to open"),
641
        QString(),
642
        QString::fromLatin1("%1 (*.svg *.svgz)").arg(QObject::tr("Scalable Vector Graphic")));
643
    if (!filename.isEmpty()) {
644
        std::string PageName = pages.front()->getNameInDocument();
645
        std::string FeatName = getUniqueObjectName("Symbol");
646
        filename = Base::Tools::escapeEncodeFilename(filename);
647
        openCommand("Create Symbol");
648
        doCommand(Doc, "import Drawing");
649
        doCommand(Doc, "f = open(\"%s\",'r')", (const char*)filename.toUtf8());
650
        doCommand(Doc, "svg = f.read()");
651
        doCommand(Doc, "f.close()");
652
        doCommand(Doc,
653
                  "App.activeDocument().addObject('Drawing::FeatureViewSymbol','%s')",
654
                  FeatName.c_str());
655
        doCommand(Doc,
656
                  "App.activeDocument().%s.Symbol = Drawing.removeSvgTags(svg)",
657
                  FeatName.c_str());
658
        doCommand(Doc,
659
                  "App.activeDocument().%s.addObject(App.activeDocument().%s)",
660
                  PageName.c_str(),
661
                  FeatName.c_str());
662
        updateActive();
663
        commitCommand();
664
    }
665
}
666

667
bool CmdDrawingSymbol::isActive(void)
668
{
669
    return (getActiveGuiDocument() ? true : false);
670
}
671

672

673
//===========================================================================
674
// Drawing_ExportPage
675
//===========================================================================
676

677
DEF_STD_CMD_A(CmdDrawingExportPage)
678

679
CmdDrawingExportPage::CmdDrawingExportPage()
680
    : Command("Drawing_ExportPage")
681
{
682
    // setting the
683
    sGroup = QT_TR_NOOP("File");
684
    sMenuText = QT_TR_NOOP("&Export page...");
685
    sToolTipText = QT_TR_NOOP("Export a page to an SVG file");
686
    sWhatsThis = "Drawing_ExportPage";
687
    sStatusTip = QT_TR_NOOP("Export a page to an SVG file");
688
    sPixmap = "document-save";
689
}
690

691
void CmdDrawingExportPage::activated(int iMsg)
692
{
693
    Q_UNUSED(iMsg);
694
    unsigned int n = getSelection().countObjectsOfType(Drawing::FeaturePage::getClassTypeId());
695
    if (n != 1) {
696
        QMessageBox::warning(Gui::getMainWindow(),
697
                             QObject::tr("Wrong selection"),
698
                             QObject::tr("Select one Page object."));
699
        return;
700
    }
701

702
    QStringList filter;
703
    filter << QString::fromLatin1("%1 (*.svg)").arg(QObject::tr("Scalable Vector Graphic"));
704
    filter << QString::fromLatin1("%1 (*.*)").arg(QObject::tr("All Files"));
705

706
    QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(),
707
                                                  QObject::tr("Export page"),
708
                                                  QString(),
709
                                                  filter.join(QLatin1String(";;")));
710
    if (!fn.isEmpty()) {
711
        std::vector<Gui::SelectionSingleton::SelObj> Sel = getSelection().getSelection();
712
        openCommand("Drawing export page");
713

714
        doCommand(Doc, "PageFile = open(App.activeDocument().%s.PageResult,'r')", Sel[0].FeatName);
715
        std::string fname = (const char*)fn.toUtf8();
716
        fname = Base::Tools::escapeEncodeFilename(fname);
717
        doCommand(Doc, "OutFile = open(\"%s\",'w')", fname.c_str());
718
        doCommand(Doc, "OutFile.write(PageFile.read())");
719
        doCommand(Doc, "del OutFile,PageFile");
720

721
        commitCommand();
722
    }
723
}
724

725
bool CmdDrawingExportPage::isActive(void)
726
{
727
    return (getActiveGuiDocument() ? true : false);
728
}
729

730
//===========================================================================
731
// Drawing_ProjectShape
732
//===========================================================================
733

734
DEF_STD_CMD_A(CmdDrawingProjectShape)
735

736
CmdDrawingProjectShape::CmdDrawingProjectShape()
737
    : Command("Drawing_ProjectShape")
738
{
739
    // setting the
740
    sGroup = QT_TR_NOOP("Drawing");
741
    sMenuText = QT_TR_NOOP("Project shape...");
742
    sToolTipText = QT_TR_NOOP("Project shape onto a user-defined plane");
743
    sStatusTip = QT_TR_NOOP("Project shape onto a user-defined plane");
744
    sWhatsThis = "Drawing_ProjectShape";
745
}
746

747
void CmdDrawingProjectShape::activated(int iMsg)
748
{
749
    Q_UNUSED(iMsg);
750
    Gui::TaskView::TaskDialog* dlg = Gui::Control().activeDialog();
751
    if (!dlg) {
752
        dlg = new DrawingGui::TaskProjection();
753
        dlg->setButtonPosition(Gui::TaskView::TaskDialog::South);
754
    }
755
    Gui::Control().showDialog(dlg);
756
}
757

758
bool CmdDrawingProjectShape::isActive(void)
759
{
760
    int ct = Gui::Selection().countObjectsOfType(Part::Feature::getClassTypeId());
761
    return (ct > 0 && !Gui::Control().activeDialog());
762
}
763

764

765
//===========================================================================
766
// Drawing_Draft_View
767
//===========================================================================
768

769
DEF_STD_CMD_A(CmdDrawingDraftView)
770

771
CmdDrawingDraftView::CmdDrawingDraftView()
772
    : Command("Drawing_DraftView")
773
{
774
    // setting the
775
    sGroup = QT_TR_NOOP("Drawing");
776
    sMenuText = QT_TR_NOOP("&Draft View");
777
    sToolTipText =
778
        QT_TR_NOOP("Inserts a Draft view of the selected object(s) in the active drawing");
779
    sWhatsThis = "Drawing_DraftView";
780
    sStatusTip = QT_TR_NOOP("Inserts a Draft view of the selected object(s) in the active drawing");
781
    sPixmap = "actions/drawing-draft-view";
782
}
783

784
void CmdDrawingDraftView::activated(int iMsg)
785
{
786
    Q_UNUSED(iMsg);
787
    addModule(Gui, "Draft");
788
    doCommand(Gui, "Gui.runCommand(\"Draft_Drawing\")");
789
}
790

791
bool CmdDrawingDraftView::isActive(void)
792
{
793
    return (getActiveGuiDocument() ? true : false);
794
}
795

796

797
//===========================================================================
798
// Drawing_Spreadheet_View
799
//===========================================================================
800

801
DEF_STD_CMD_A(CmdDrawingSpreadsheetView)
802

803
CmdDrawingSpreadsheetView::CmdDrawingSpreadsheetView()
804
    : Command("Drawing_SpreadsheetView")
805
{
806
    // setting the
807
    sGroup = QT_TR_NOOP("Drawing");
808
    sMenuText = QT_TR_NOOP("&Spreadsheet View");
809
    sToolTipText = QT_TR_NOOP("Inserts a view of a selected spreadsheet in the active drawing");
810
    sWhatsThis = "Drawing_SpreadsheetView";
811
    sStatusTip = QT_TR_NOOP("Inserts a view of a selected spreadsheet in the active drawing");
812
    sPixmap = "actions/drawing-spreadsheet";
813
}
814

815
void CmdDrawingSpreadsheetView::activated(int iMsg)
816
{
817
    Q_UNUSED(iMsg);
818
    const std::vector<App::DocumentObject*> spreads =
819
        getSelection().getObjectsOfType(Spreadsheet::Sheet::getClassTypeId());
820
    if (spreads.size() != 1) {
821
        QMessageBox::warning(Gui::getMainWindow(),
822
                             QObject::tr("Wrong selection"),
823
                             QObject::tr("Select exactly one Spreadsheet object."));
824
        return;
825
    }
826
    const std::vector<App::DocumentObject*> pages =
827
        this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
828
    if (pages.empty()) {
829
        QMessageBox::warning(Gui::getMainWindow(),
830
                             QObject::tr("No page found"),
831
                             QObject::tr("Create a page first."));
832
        return;
833
    }
834
    std::string SpreadName = spreads.front()->getNameInDocument();
835
    std::string PageName = pages.front()->getNameInDocument();
836
    openCommand("Create spreadsheet view");
837
    std::string FeatName = getUniqueObjectName("View");
838
    doCommand(Doc,
839
              "App.activeDocument().addObject('Drawing::FeatureViewSpreadsheet','%s')",
840
              FeatName.c_str());
841
    doCommand(Doc,
842
              "App.activeDocument().%s.Source = App.activeDocument().%s",
843
              FeatName.c_str(),
844
              SpreadName.c_str());
845
    doCommand(Doc,
846
              "App.activeDocument().%s.addObject(App.activeDocument().%s)",
847
              PageName.c_str(),
848
              FeatName.c_str());
849
    updateActive();
850
    commitCommand();
851
}
852

853
bool CmdDrawingSpreadsheetView::isActive(void)
854
{
855
    return (getActiveGuiDocument() ? true : false);
856
}
857

858

859
void CreateDrawingCommands(void)
860
{
861
    Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
862

863
    rcCmdMgr.addCommand(new CmdDrawingOpen());
864
    rcCmdMgr.addCommand(new CmdDrawingNewPage());
865
    rcCmdMgr.addCommand(new CmdDrawingNewA3Landscape());
866
    rcCmdMgr.addCommand(new CmdDrawingNewView());
867
    rcCmdMgr.addCommand(new CmdDrawingOrthoViews());
868
    rcCmdMgr.addCommand(new CmdDrawingOpenBrowserView());
869
    rcCmdMgr.addCommand(new CmdDrawingAnnotation());
870
    rcCmdMgr.addCommand(new CmdDrawingClip());
871
    rcCmdMgr.addCommand(new CmdDrawingSymbol());
872
    rcCmdMgr.addCommand(new CmdDrawingExportPage());
873
    rcCmdMgr.addCommand(new CmdDrawingProjectShape());
874
    rcCmdMgr.addCommand(new CmdDrawingDraftView());
875
    rcCmdMgr.addCommand(new CmdDrawingSpreadsheetView());
876
}
877

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

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

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

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