File size: 10,053 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
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de>              *

 *                                                                         *

 *   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 <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/Control.h>
#include <Gui/Document.h>
#include <Gui/MainWindow.h>
#include <Gui/Selection/Selection.h>
#include <Gui/Selection/SelectionFilter.h>
#include <Mod/Robot/App/RobotObject.h>
#include <Mod/Robot/App/TrajectoryObject.h>

#include "TaskDlgSimulate.h"
#include "TrajectorySimulate.h"


using namespace std;
using namespace RobotGui;

#include <QFileDialog>

namespace
{

std::string getWrl(const QString& hint_directory)

{
    QString fileName = QFileDialog::getOpenFileName(
        Gui::getMainWindow(),
        QObject::tr("Select VRML file for Robot"),
        hint_directory,
        QObject::tr("VRML Files (*.wrl *.vrml)")
    );

    return fileName.toStdString();
}

std::string getCsv(const std::string& wrl_path)

{
    QFileInfo wrlInfo(QString::fromStdString(wrl_path));
    QString hintDir = wrlInfo.absolutePath();
    QString fileName = QFileDialog::getOpenFileName(
        Gui::getMainWindow(),
        QObject::tr("Select Kinematic CSV file for Robot"),
        hintDir,
        QObject::tr("CSV Files (*.csv)")
    );
    return fileName.toStdString();
}

}  // namespace

DEF_STD_CMD_A(CmdRobotSetHomePos)

CmdRobotSetHomePos::CmdRobotSetHomePos()
    : Command("Robot_SetHomePos")
{
    sAppModule = "Robot";
    sGroup = QT_TR_NOOP("Robot");
    sMenuText = QT_TR_NOOP("Set Home Position");
    sToolTipText = QT_TR_NOOP("Sets the home position");
    sWhatsThis = "Robot_SetHomePos";
    sStatusTip = sToolTipText;
    sPixmap = "Robot_SetHomePos";
}


void CmdRobotSetHomePos::activated(int)

{
    const char* SelFilter = "SELECT Robot::RobotObject COUNT 1 ";

    Gui::SelectionFilter filter(SelFilter);
    Robot::RobotObject* pcRobotObject;
    if (filter.match()) {
        pcRobotObject = static_cast<Robot::RobotObject*>(filter.Result[0][0].getObject());
    }
    else {
        QMessageBox::warning(
            Gui::getMainWindow(),
            QObject::tr("Wrong selection"),
            QObject::tr("Select one Robot to set home position")
        );
        return;
    }


    std::string FeatName = pcRobotObject->getNameInDocument();

    const char* n = FeatName.c_str();
    openCommand("Set home");
    doCommand(
        Doc,
        "App.activeDocument().%s.Home = "
        "[App.activeDocument().%s.Axis1,App.activeDocument().%s.Axis2,App.activeDocument().%"
        "s.Axis3,App.activeDocument().%s.Axis4,App.activeDocument().%s.Axis5,App."
        "activeDocument().%s.Axis6]",
        n,
        n,
        n,
        n,
        n,
        n,
        n
    );
    updateActive();
    commitCommand();
}

bool CmdRobotSetHomePos::isActive()

{
    return hasActiveDocument();
}


// #####################################################################################################
DEF_STD_CMD_A(CmdRobotRestoreHomePos)

CmdRobotRestoreHomePos::CmdRobotRestoreHomePos()
    : Command("Robot_RestoreHomePos")
{
    sAppModule = "Robot";
    sGroup = QT_TR_NOOP("Robot");
    sMenuText = QT_TR_NOOP("Move to Home");
    sToolTipText = QT_TR_NOOP("Moves to the home position");
    sWhatsThis = "Robot_RestoreHomePos";
    sStatusTip = sToolTipText;
    sPixmap = "Robot_RestoreHomePos";
}


void CmdRobotRestoreHomePos::activated(int)

{
    const char* SelFilter = "SELECT Robot::RobotObject COUNT 1 ";

    Gui::SelectionFilter filter(SelFilter);
    Robot::RobotObject* pcRobotObject;
    if (filter.match()) {
        pcRobotObject = static_cast<Robot::RobotObject*>(filter.Result[0][0].getObject());
    }
    else {
        QMessageBox::warning(
            Gui::getMainWindow(),
            QObject::tr("Wrong selection"),
            QObject::tr("Select one Robot")
        );
        return;
    }


    std::string FeatName = pcRobotObject->getNameInDocument();

    const char* n = FeatName.c_str();
    openCommand("Move to home");
    doCommand(Doc, "App.activeDocument().%s.Axis1 = App.activeDocument().%s.Home[0]", n, n);
    doCommand(Doc, "App.activeDocument().%s.Axis2 = App.activeDocument().%s.Home[1]", n, n);
    doCommand(Doc, "App.activeDocument().%s.Axis3 = App.activeDocument().%s.Home[2]", n, n);
    doCommand(Doc, "App.activeDocument().%s.Axis4 = App.activeDocument().%s.Home[3]", n, n);
    doCommand(Doc, "App.activeDocument().%s.Axis5 = App.activeDocument().%s.Home[4]", n, n);
    doCommand(Doc, "App.activeDocument().%s.Axis6 = App.activeDocument().%s.Home[5]", n, n);
    updateActive();
    commitCommand();
}

bool CmdRobotRestoreHomePos::isActive()

{
    return hasActiveDocument();
}


// #####################################################################################################
DEF_STD_CMD_A(CmdRobotConstraintAxle)

CmdRobotConstraintAxle::CmdRobotConstraintAxle()
    : Command("Robot_Create")
{
    sAppModule = "Robot";
    sGroup = QT_TR_NOOP("Robot");
    sMenuText = QT_TR_NOOP("Place Robot");
    sToolTipText = QT_TR_NOOP("Places a robot in the scene");

    sWhatsThis = "Robot_Create";
    sStatusTip = sToolTipText;
    sPixmap = "Robot_CreateRobot";
}


void CmdRobotConstraintAxle::activated([[maybe_unused]] int msg)

{
    const std::string FeatName = getUniqueObjectName("Robot");
    const std::string WrlPath = getWrl(QString());
    const std::string KinematicPath = getCsv(WrlPath);

    openCommand("Place robot");
    doCommand(Doc, "App.activeDocument().addObject(\"Robot::RobotObject\",\"%s\")", FeatName.c_str());
    doCommand(Doc, "App.activeDocument().%s.RobotVrmlFile = \"%s\"", FeatName.c_str(), WrlPath.c_str());
    doCommand(
        Doc,
        "App.activeDocument().%s.RobotKinematicFile = \"%s\"",
        FeatName.c_str(),
        KinematicPath.c_str()
    );
    updateActive();
    commitCommand();
}

bool CmdRobotConstraintAxle::isActive()

{
    return hasActiveDocument();
}


// #####################################################################################################

DEF_STD_CMD_A(CmdRobotSimulate)

CmdRobotSimulate::CmdRobotSimulate()
    : Command("Robot_Simulate")
{
    sAppModule = "Robot";
    sGroup = QT_TR_NOOP("Robot");
    sMenuText = QT_TR_NOOP("Simulate Trajectory");
    sToolTipText = QT_TR_NOOP("Simulates robot movement along a selected trajectory");
    sWhatsThis = "Robot_Simulate";
    sStatusTip = sToolTipText;
    sPixmap = "Robot_Simulate";
}


void CmdRobotSimulate::activated(int)

{
    const char* SelFilter = "SELECT Robot::RobotObject  \n"
                            "SELECT Robot::TrajectoryObject  ";

    Gui::SelectionFilter filter(SelFilter);
    Robot::RobotObject* pcRobotObject;
    Robot::TrajectoryObject* pcTrajectoryObject;

    if (filter.match()) {
        pcRobotObject = static_cast<Robot::RobotObject*>(filter.Result[0][0].getObject());
        pcTrajectoryObject = static_cast<Robot::TrajectoryObject*>(filter.Result[1][0].getObject());
    }
    else {
        QMessageBox::warning(
            Gui::getMainWindow(),
            QObject::tr("Wrong selection"),
            QObject::tr("Select one Robot and one Trajectory object.")
        );
        return;
    }

    if (pcTrajectoryObject->Trajectory.getValue().getSize() < 2) {
        QMessageBox::warning(
            Gui::getMainWindow(),
            QObject::tr("Trajectory not valid"),
            QObject::tr("You need at least two waypoints in a trajectory to simulate.")
        );
        return;
    }

    Gui::TaskView::TaskDialog* dlg = new TaskDlgSimulate(pcRobotObject, pcTrajectoryObject);
    Gui::Control().showDialog(dlg);
}

bool CmdRobotSimulate::isActive()

{
    return (hasActiveDocument() && !Gui::Control().activeDialog());
}


// #####################################################################################################


void CreateRobotCommands()

{
    Gui::CommandManager& command_manager = Gui::Application::Instance->commandManager();

    command_manager.addCommand(new CmdRobotRestoreHomePos());
    command_manager.addCommand(new CmdRobotSetHomePos());
    command_manager.addCommand(new CmdRobotConstraintAxle());
    command_manager.addCommand(new CmdRobotSimulate());
}