File size: 7,478 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
/***************************************************************************

 *   Copyright (c) 2013 Jan Rheinländer                                    *

 *                                   <jrheinlaender@users.sourceforge.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 <QMessageBox>
#include <Standard_Failure.hxx>


#include <App/DocumentObject.h>
#include <App/Origin.h>
#include <App/Part.h>
#include <Gui/MainWindow.h>
#include <Gui/ViewProvider.h>
#include <Gui/Selection/Selection.h>
#include <Mod/Part/App/DatumFeature.h>
#include <Mod/PartDesign/App/Body.h>

#include <ui_DlgReference.h>

#include "TaskDatumParameters.h"
#include "ReferenceSelection.h"
#include "TaskFeaturePick.h"
#include "Utils.h"

using namespace PartDesignGui;
using namespace Gui;
using namespace Attacher;

/* TRANSLATOR PartDesignGui::TaskDatumParameters */

TaskDatumParameters::TaskDatumParameters(ViewProviderDatum* ViewProvider, QWidget* parent)
    : PartGui::TaskAttacher(
          ViewProvider,
          parent,
          QStringLiteral("PartDesign_") + ViewProvider->datumType,
          ViewProvider->datumMenuText
      )
{
    Gui::Selection().addSelectionGate(new NoDependentsSelection(ViewProvider->getObject()));
    ViewProvider->setPickable(false);
}

TaskDatumParameters::~TaskDatumParameters()
{
    if (this->ViewProvider && this->ViewProvider->isDerivedFrom<ViewProviderDatum>()) {
        static_cast<ViewProviderDatum*>(this->ViewProvider)->setPickable(true);
    }
    Gui::Selection().rmvSelectionGate();
}


//**************************************************************************
//**************************************************************************
// TaskDialog
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

TaskDlgDatumParameters::TaskDlgDatumParameters(ViewProviderDatum* ViewProvider)
    : TaskDlgAttacher(ViewProvider, false)
{
    assert(ViewProvider);
    parameter = new TaskDatumParameters(ViewProvider);
    Content.push_back(parameter);
}

TaskDlgDatumParameters::~TaskDlgDatumParameters() = default;

bool TaskDlgDatumParameters::reject()

{

    return PartGui::TaskDlgAttacher::reject();
}


bool TaskDlgDatumParameters::accept()

{

    Part::Datum* pcDatum = ViewProvider->getObject<Part::Datum>();
    auto pcActiveBody = PartDesignGui::getBodyFor(pcDatum, false);
    auto pcActivePart = PartDesignGui::getPartFor(pcActiveBody, false);
    std::vector<App::DocumentObject*> copies;

    // see if we are able to assign a mode
    if (parameter->getActiveMapMode() == mmDeactivated) {
        QMessageBox msg(Gui::getMainWindow());
        msg.setWindowTitle(tr("Incompatible Reference Set"));
        msg.setText(
            tr("There is no attachment mode that fits the current set"
               " of references. If you choose to continue, the feature will remain where"
               " it is now, and will not be moved as the references change."
               " Continue?")
        );
        msg.addButton(QMessageBox::Yes);
        auto btNo = msg.addButton(QMessageBox::No);
        msg.setDefaultButton(btNo);
        msg.setIcon(QMessageBox::Warning);
        msg.exec();
        if (msg.buttonRole(msg.clickedButton()) == QMessageBox::NoRole) {
            return false;
        }
    }

    // see what to do with external references
    // check the prerequisites for the selected objects
    // the user has to decide which option we should take if external references are used
    bool extReference = false;
    for (App::DocumentObject* obj : pcDatum->AttachmentSupport.getValues()) {
        if (pcActiveBody && !pcActiveBody->hasObject(obj)
            && !pcActiveBody->getOrigin()->hasObject(obj)) {
            extReference = true;
        }
    }

    if (extReference) {
        // TODO: rewrite this to be shared with CmdPartDesignNewSketch::activated() (2015-10-20, Fat-Zer)
        QDialog dia(Gui::getMainWindow());
        PartDesignGui::Ui_DlgReference dlg;
        dlg.setupUi(&dia);
        dia.setModal(true);
        int result = dia.exec();
        if (result == QDialog::DialogCode::Rejected) {
            return false;
        }
        else if (!dlg.radioXRef->isChecked()) {
            std::vector<App::DocumentObject*> copyObjects;
            std::vector<std::string> copySubValues;
            std::vector<std::string> subs = pcDatum->AttachmentSupport.getSubValues();
            int index = 0;
            for (App::DocumentObject* obj : pcDatum->AttachmentSupport.getValues()) {
                if (pcActiveBody && !pcActiveBody->hasObject(obj)
                    && !pcActiveBody->getOrigin()->hasObject(obj)) {
                    auto* copy = PartDesignGui::TaskFeaturePick::makeCopy(
                        obj,
                        subs[index],
                        dlg.radioIndependent->isChecked()
                    );
                    if (copy) {
                        copyObjects.push_back(copy);
                        copies.push_back(copyObjects.back());
                        copySubValues.emplace_back();
                    }
                }
                else {
                    copyObjects.push_back(obj);
                    copySubValues.push_back(subs[index]);
                }

                index++;
            }

            pcDatum->AttachmentSupport.setValues(copyObjects, copySubValues);
        }
    }

    if (!PartGui::TaskDlgAttacher::accept()) {
        return false;
    }

    // we need to add the copied features to the body after the command action, as otherwise FreeCAD
    // crashes unexplainably
    for (auto obj : copies) {
        if (pcActiveBody) {
            pcActiveBody->addObject(obj);
        }
        else if (pcActivePart) {
            pcActivePart->addObject(obj);
        }
    }

    return true;
}

#include "moc_TaskDatumParameters.cpp"