-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurdfgen_cpp.cpp
1187 lines (998 loc) · 35.8 KB
/
urdfgen_cpp.cpp
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>
#include "urdftree.h"
#include "ujl.h"
#include <filesystem>
#include <iostream>
#include <fstream>
#include "shared_funcs.h"
INITIALIZE_EASYLOGGINGPP;
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
namespace fs = std::filesystem;
Ptr<Application> app;
Ptr<UserInterface> ui;
Ptr<Design> design;
// this file has mostly gui things.
const bool runfrommenu = true; // this allowed to be run as script as well. TODO: KEEP?
class MotherShip
{
public:
int rowNumber = 0, elnum = 0, oldrow = -1, numlinks = -1, numjoints = -1,lastrow = 0;
std::string packagename = "mypackage";
fs::path thisscriptpath;
//missing! jtctrl lastjoint is maybe not a ujoint object?
UJoint lastjoint;
UrdfTree thistree;
MotherShip() {
rowNumber = 0, elnum = 0, oldrow = -1, numlinks = -1, numjoints = -1, lastrow = 0;
//setting thisscriptpath
fs::path appdatadir = "";
char* buf = nullptr;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, "APPDATA") == 0 && buf != nullptr)
{
appdatadir = buf;
free(buf);
}
thisscriptpath = appdatadir / "Autodesk" / "Autodesk Fusion 360" / "API" / "AddIns" / "urdfgen_cpp";
};
~MotherShip() {};
void addRowToTable(Ptr<TableCommandInput>, std::string);
} _ms;
class SixDegree : OrVec
{
/*
This is a 6 degree of freedom control. I've seen this in fusion and it looks nicer, but I am not sure it made it into the API.
TODO: use the real control from Fusion, if it exists.
*/
public:
std::string name;
void setxyzrpy()
{
};
void interact()
{
};
void setdist(std::string var)
{
Ptr<DistanceValueCommandInput> distanceValueInput;
};
void setangle(std::string var)
{
Ptr<AngleValueCommandInput> angleValueInput;
};
SixDegree(Ptr<CommandInputs> ownctrl_, std::string name_)
{
name = name_;
ownctrl = ownctrl_;
//maybe it has a name. not sure I might need to use this to create offsets for links later. If everything works correctly this won't be necessary though.
// creates distance controls for X, Y, Z offsets
addDistanceControl("X", 1, 0, 0);
addDistanceControl("Y", 0, 1, 0);
addDistanceControl("Z", 0, 0, 1);
//creates angle controls for Roll, Pitch and Yaw
addAngleControl("Roll" , 0, 1, 0, 0, 0, 1);
addAngleControl("Pitch" , 0, 0, 1, 1, 0, 0);
addAngleControl("Yaw" , 1, 0, 0, 0, 1, 0);
};
private:
bool allvisible = true;
bool allenabled = false;
Ptr<CommandInputs> ownctrl;
std::vector<Ptr<DistanceValueCommandInput>> distVect;
std::vector<Ptr<AngleValueCommandInput>> angVect;
void addDistanceControl(std::string var, double x, double y, double z)
{
Ptr<DistanceValueCommandInput> distanceValueInput = ownctrl->addDistanceValueCommandInput("distanceValue"+var, var, ValueInput::createByReal(0));
distanceValueInput->setManipulator(Point3D::create(0, 0, 0), Vector3D::create(x, y, z));
distanceValueInput->hasMinimumValue(false);
distanceValueInput->hasMaximumValue(false);
distanceValueInput->isVisible(allvisible);
distanceValueInput->isEnabled(allenabled);
distVect.push_back(distanceValueInput); //not sure if I will need this, but just in case
}
void addAngleControl(std::string name, double x1, double y1, double z1, double x2, double y2, double z2)
{
Ptr<AngleValueCommandInput> angleValueInput = ownctrl->addAngleValueCommandInput("angleValue" + name, name, ValueInput::createByReal(0));
angleValueInput->setManipulator(Point3D::create(0, 0, 0), Vector3D::create(x1, y1, z1), Vector3D::create(x2, y2, z2));
angleValueInput->hasMinimumValue(false);
angleValueInput->hasMaximumValue(false);
angleValueInput->isVisible(allvisible);
angleValueInput->isEnabled(allenabled);
angVect.push_back(angleValueInput); //not sure if I will need this, but just in case
}
};
void MotherShip::addRowToTable(Ptr<TableCommandInput> tableInput, std::string LinkOrJoint)
{
//Adds element to tree, i.e., row to table
bool islink;
std::string elname;
if (!tableInput)
return;
// Get the CommandInputs object associated with the parent command.
Ptr<CommandInputs> cmdInputs = tableInput->commandInputs();
// Setting up controls to be added for each row
Ptr<CommandInput> elnnumInput = cmdInputs->addStringValueInput("elnum" + std::to_string(elnum), "elnumTable" + std::to_string(elnum), std::to_string(elnum));
elnnumInput->isEnabled(false);
if (LinkOrJoint == "" || LinkOrJoint == "Link")
{
islink = true;
numlinks += 1;
if (elnum == 0 || thistree.allLinks().second.size() ==0 )
elname = "base"; //first link has to be named base
else
elname = "link" + std::to_string(numlinks);
}
else if (LinkOrJoint == "Joint")
{
islink = false;
numjoints += 1;
elname = "joint" + std::to_string(numjoints);
}
Ptr<DropDownCommandInput> JorLInput = cmdInputs->addDropDownCommandInput("TableInput_value" + std::to_string(elnum), "JorLTable" + std::to_string(elnum), DropDownStyles::TextListDropDownStyle);
Ptr<ListItems> dropdownItems = JorLInput->listItems();
if (!dropdownItems)
return;
dropdownItems->add("Link", islink, "");
dropdownItems->add("Joint", !islink, "");
JorLInput->isEnabled(false);
Ptr<CommandInput> stringInput = cmdInputs->addStringValueInput("TableInput_string" + std::to_string(elnum), "StringTable" + std::to_string(elnum), elname);
stringInput->isEnabled(false); //I'm disabling the ability to change element's name randomly...
Ptr<CommandInput> slbutInput = cmdInputs->addBoolValueInput("butselectClick" + std::to_string(elnum), "Select", false, "", true);
// Add the inputs to the table.
lastrow = tableInput->rowCount();
tableInput->addCommandInput(elnnumInput, lastrow, 0);
tableInput->addCommandInput(JorLInput, lastrow, 1);
tableInput->addCommandInput(stringInput, lastrow, 2);
tableInput->addCommandInput(slbutInput, lastrow, 3);
// Increment a counter used to make each row unique.
rowNumber = rowNumber + 1;
elnum += 1;
};
vector<fs::path> createpaths(string _ms_packagename, fs::path thisscriptpath)
{
vector<fs::path> returnvectstr;
try {
LOG(DEBUG) << "called createpaths";
fs::path userdir = "";
char* buf = nullptr;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, "USERPROFILE") == 0 && buf != nullptr)
{
userdir = buf;
free(buf);
}
auto folderDlg = ui->createFolderDialog();
folderDlg->title("Choose location to save your URDF new package");
folderDlg->initialDirectory(userdir.string());
DialogResults dlgResult = folderDlg->showDialog();
if (dlgResult == DialogError)
LOG(ERROR) << "failed to create dialog";
if (dlgResult != DialogOK)
{
ui->messageBox("you need to select a folder!");
throw std::runtime_error("Directory not selected. cannot continue.\nError code: DialogResults enum" +std::to_string(dlgResult));
}
fs::path outputdir = folderDlg->folder();
fs::path base_directory = outputdir / _ms_packagename;
LOG(INFO) << "This script's path: "+ (thisscriptpath.string());
LOG(INFO) <<"Base directory:"+base_directory.string();
if (!fs::exists(base_directory))
fs::create_directories(base_directory); //// will create whole tree if needed
fs::path meshes_directory = base_directory / "meshes";
fs::path components_directory = base_directory / "components";
if (!fs::exists(meshes_directory))
fs::create_directory(meshes_directory);
if (!fs::exists(components_directory))
fs::create_directory(components_directory);
vector<string> filestochange = { "display.launch", "urdf_.rviz", "package.xml", "CMakeLists.txt" };
for (auto myfilename : filestochange)
{
ifstream file_in;
// Read in the file
auto thisfilename = thisscriptpath / "resources" / myfilename;
LOG(INFO)<<"Opening file:"+(thisfilename.string());
file_in.open(thisfilename);
if (!file_in)
LOG(ERROR) << "failed to open input file:" + thisfilename.string();
auto thisoutfilename = base_directory / myfilename;
ofstream file_out(thisoutfilename.string(), std::ofstream::out);
string filedata;
while (std::getline(file_in,filedata))
{
// Replace the target string
replaceAll(filedata, "somepackage", _ms_packagename);
// Write the file out again
file_out << filedata << endl;
}
file_out.close();
}
returnvectstr = { base_directory, meshes_directory, components_directory };
}
catch (const char* msg) {
LOG(ERROR) << msg;
ui->messageBox(msg);
}
catch (const std::exception& e)
{
LOG(ERROR) << e.what();
std::string errormsg = "issues creating folders...\n";
LOG(ERROR) << errormsg;
ui->messageBox(errormsg);
}
catch (...)
{
string errormessage = "issues creating folders!";
LOG(ERROR) << errormessage;
ui->messageBox(errormessage);
}
return returnvectstr;
};
// InputChange event handler.
class UrdfGenOnInputChangedEventHander : public adsk::core::InputChangedEventHandler
{
public:
void notify(const Ptr<InputChangedEventArgs>& eventArgs) override
{
bool rows_differ = false;
//not efficient check, but
if (_ms.oldrow !=_ms.rowNumber)
rows_differ = true;
std::string jointname;
Ptr<CommandInputs> inputs = eventArgs->inputs();
if (!inputs)
return;
Ptr<CommandInput> cmdInput = eventArgs->input();
if (!cmdInput)
return;
Ptr<TableCommandInput> tableInput = inputs->itemById("table");
//if (!tableInput)
// return;
LOG_IF(!tableInput, DEBUG) << "no table input. inside a group, probably";
Ptr<TextBoxCommandInput> debugInput = inputs->itemById("debugbox");
//define the groups first:
Ptr<GroupCommandInput> linkgroupInput = inputs->itemById("linkgroup");
Ptr<GroupCommandInput> jointgroupInput = inputs->itemById("jointgroup");
Ptr<SelectionCommandInput> linkselInput;
Ptr<SelectionCommandInput> jointselInput;
Ptr<DropDownCommandInput> cln;
Ptr<DropDownCommandInput> pln;
//inside the group, there is no group!
if (!linkgroupInput)
{
LOG(DEBUG) << "probably inside linkgroup!";
linkselInput = inputs->itemById("linkselection");
}
else
linkselInput = linkgroupInput->children()->itemById("linkselection");
LOG_IF(!linkselInput, WARNING) << "linkselInput is a null pointer, either things are gonna fail, or I am inside jointgroup";
LOG(DEBUG) << "Middle part check.";
//inside the group, there is no group!
if (!jointgroupInput) // linkgroupInput is None:
{
if (!linkselInput)
{
LOG(DEBUG) << "probably inside jointgroup!";
jointselInput = inputs->itemById("jointselection");
cln = inputs->itemById("childlinkname");
if (!cln)
{
ui->messageBox("cln is a null pointer!");
LOG(WARNING) << "cln is a null pointer!";
}
pln = inputs->itemById("parentlinkname");
if (!pln)
{
ui->messageBox("pln is a null pointer!");
LOG(WARNING) << "pln is a null pointer!";
}
}
}
else
{
jointselInput = jointgroupInput->children()->itemById("jointselection");
cln = jointgroupInput->children()->itemById("childlinkname");
pln = jointgroupInput->children()->itemById("parentlinkname");
}
LOG_IF(!jointselInput, ERROR) << "jointselInput is a null pointer, references are gonna fail";
LOG(DEBUG) << "Currently executing command:" + cmdInput->id();
if (tableInput)
{
int currrow = tableInput->selectedRow();
Ptr<StringValueCommandInput> thisstringinput = tableInput->getInputAtPosition(currrow, 0);
if (thisstringinput)
int elementtobedefined = std::stoi(thisstringinput->value());
}
// Reactions
if (cmdInput->id() == "tableLinkAdd") {
try {
_ms.addRowToTable(tableInput, "Link");
{
//actually I changed addrow and removed the add element button, so I don't really need to do this.
Ptr<DropDownCommandInput> JorLInput = tableInput->getInputAtPosition(_ms.lastrow, 1);
if (!JorLInput) {
std::string thiserror = "error getting dropdown command box in position" + std::to_string(_ms.lastrow);
throw thiserror;
}
JorLInput->isEnabled(false);
}
Ptr<StringValueCommandInput> thisstringinput = tableInput->getInputAtPosition(_ms.lastrow, 2);
if (!thisstringinput)
throw "error getting row!";
jointname = thisstringinput->value();
_ms.thistree.addLink(jointname, _ms.elnum - 1);
}
catch (const char* msg) {
LOG(ERROR) << msg;
ui->messageBox(msg);
}
catch (...)
{
string errormessage = "issues adding link!";
LOG(ERROR) << errormessage;
ui->messageBox(errormessage);
}
}
else if (cmdInput->id() == "tableJointAdd") {
try {
_ms.addRowToTable(tableInput, "Joint");
tableInput->getInputAtPosition(_ms.lastrow, 1)->isEnabled(false);
Ptr<StringValueCommandInput> thisstringinput = tableInput->getInputAtPosition(_ms.lastrow, 2);
if (!thisstringinput)
throw "error getting row!";
jointname = thisstringinput->value();
LOG(DEBUG) << "added joint:" + jointname;
_ms.thistree.addJoint(jointname, _ms.elnum - 1);
}
catch (const char* msg) {
LOG(ERROR) << msg;
ui->messageBox(msg);
}
catch (...)
{
string errormessage = "issues adding joint!";
LOG(ERROR) << errormessage;
ui->messageBox(errormessage);
}
}
else if (cmdInput->id() == "tableDelete") {
if (tableInput->selectedRow() == -1) {
ui->messageBox("Select one row to delete.");
}
else {
int elementtobedeleted = tableInput->selectedRow();
//first I set the current link to the one above it
Ptr<StringValueCommandInput> thisstringinput;
if (tableInput->selectedRow() != 0)
thisstringinput = tableInput->getInputAtPosition(tableInput->selectedRow()-1, 0);
else
{
//if it is the first row, we can't get the one above it
thisstringinput = tableInput->getInputAtPosition( 1, 0);
}
if (thisstringinput)
{
std::string oneaboveorbelownumstr = thisstringinput->value();
_ms.thistree.setCurrentEl(std::stoi(oneaboveorbelownumstr));
}
else
{
//table is empty
_ms.thistree.setCurrentEl(-1);
}
//now I can delete the row
tableInput->deleteRow(elementtobedeleted);
//and delete the element
_ms.thistree.rmElement(elementtobedeleted);
_ms.rowNumber -= 1;
//I also want to update the debug message text!
debugInput->text(_ms.thistree.getdebugtext());
LOG(DEBUG) << "deleted element okay";
}
}
if (cmdInput->id() == "linkselection")
{
ULink* currLink = dynamic_cast<ULink*>(_ms.thistree.currentEl);
if (currLink)
{
currLink->group.clear();
for (int i = 0; i < linkselInput->selectionCount();i++)
{
Ptr<Occurrence> thisFusionOcc = linkselInput->selection(i)->entity();
LOG(DEBUG) << "adding link entity:" + thisFusionOcc->name();
currLink->group.push_back(linkselInput->selection(i)->entity());
}
}
LOG(DEBUG) << "linkselection: done ";
}
else if (cmdInput->id() == "jointselection" && jointselInput->selectionCount() == 1)
{
Ptr<Joint> thisFusionJoint = jointselInput->selection(0)->entity();
LOG(DEBUG) << "adding joint entity:"+ thisFusionJoint->name();
UJoint* thisjoint = dynamic_cast<UJoint*>(_ms.thistree.currentEl);
if (thisjoint)
{
thisjoint->setjoint(thisFusionJoint);
}
else
{
string errormsg = "the cast didn't work. it should have.";
LOG(ERROR) << errormsg;
ui->messageBox(errormsg);
}
LOG(DEBUG) << "jointselection: done ";
}
else if (cmdInput->id() == "parentlinkname")
{
LOG(DEBUG) << "i am aware i am a parentlinkname control";
//since I changed the visibility of controls I know current element is a joint, if this is accessible
UJoint* thisjoint = dynamic_cast<UJoint*>(_ms.thistree.currentEl);
if (thisjoint)
{
Ptr<ListItem> dropdown_parent1 = pln->selectedItem();
std::string myparentlinkname = dropdown_parent1->name();
thisjoint->parentlink = myparentlinkname;
_ms.thistree.currentEl = thisjoint;
}
else
{
string errormsg = "the cast didn't work";
LOG(ERROR) << errormsg;
ui->messageBox(errormsg);
}
LOG(DEBUG) << "parentlinkname: done ";
}
else if (cmdInput->id() == "childlinkname")
{
LOG(DEBUG) << "i am aware i am a childlinkname control";
//since I changed the visibility of controls I know current element is a joint, if this is accessible
UJoint* thisjoint = dynamic_cast<UJoint*>(_ms.thistree.currentEl);
if (thisjoint)
{
Ptr<ListItem> dropdown_child2 = cln->selectedItem();
std::string childlinkname = dropdown_child2->name();
thisjoint->childlink = childlinkname;
LOG(DEBUG) << "thisjointchildlink:"+ thisjoint->childlink +"\ndropdownthing:"+ dropdown_child2->name();
_ms.thistree.currentEl = thisjoint;
}
else
{
string errormsg = "the cast didn't work";
LOG(ERROR) << errormsg;
ui->messageBox(errormsg);
}
LOG(DEBUG) << "childlinkname: done ";
}
else if (cmdInput->id() == "createtree")
{
linkgroupInput->isVisible(false);
jointgroupInput->isVisible(false);
ui->messageBox(_ms.thistree.genTree());
}
else if (cmdInput->id().find("butselectClick") != std::string::npos) ///// TODO: add all other controls from table here, if we ever want to make them selectable/changeable then how it is, will break!
{
//first we set current element
if (tableInput)
{
std::string num_str = cmdInput->id().substr(14); // I used to get the value of the index from the control of the selected row. I guess I can double check
if (tableInput->selectedRow() != -1)
{
Ptr<StringValueCommandInput> thisstringinput = tableInput->getInputAtPosition(tableInput->selectedRow(), 0);
if (thisstringinput)
{
std::string num_str2 = thisstringinput->value();
if (num_str2 != num_str)
{
rows_differ = true;
//actually, getting the number from the controlname string is better, since the value of the selected row will only change after this input events are processed!
LOG(DEBUG) << "numstr:" + num_str + "\nnumstr2:" + num_str2; //this will only show if you have ui->messageBox's
_ms.oldrow = std::stoi(num_str);
_ms.rowNumber = std::stoi(num_str2);
}
}
}
_ms.thistree.setCurrentEl(std::stoi(num_str));
LOG(DEBUG) << "current selected element's name is:"+_ms.thistree.currentEl->name;
debugInput->text(_ms.thistree.getdebugtext());
}
}
else if (cmdInput->id() == "packagename")
{
Ptr<StringValueCommandInput> thisstringinput = inputs->itemById("packagename");
_ms.packagename = thisstringinput->value();
}
//else if (cmdInput->id() == "") {}
//checks current element and updates all things!
LOG(DEBUG) << "Entering the old function setcurrelement from Mothership";
bool runonce = true;
while (runonce) {
runonce = false;
try {
if (_ms.thistree.currentEl)
{
if (rows_differ)
{
if (!linkselInput || !jointselInput)
{
//no need to throw an error here, this happens in harmless situations. i'll just break out
break;
//throw "either linkselInput or jointselInput are null. this will fail, aborting";
}
if (!linkgroupInput || !jointgroupInput)
{
//no need to throw an error here, this happens in harmless situations. i'll just break out
break;
//throw "either linkgroupInput or jointgroupInput are null. this will fail, aborting";
}
linkselInput->clearSelection();
jointselInput->clearSelection();
//is it a link?
ULink* currLink = dynamic_cast<ULink*>(_ms.thistree.currentEl);
if (currLink)
{
std::vector<Ptr<Occurrence>> group = currLink->group;
LOG(DEBUG) << "repopulating link selection";
for (auto it = group.cbegin(); it != group.cend(); it++)
{
linkselInput->addSelection(*it);
}
//we hide the joint selection
linkgroupInput->isVisible(true);
jointgroupInput->isVisible(false);
}
//same for joints
UJoint* currJoint = dynamic_cast<UJoint*>(_ms.thistree.currentEl);
if (currJoint)
{
LOG(DEBUG) << "repopulating joint selection";
jointselInput->addSelection(currJoint->entity);
//we hide the link selection
linkgroupInput->isVisible(false);
jointgroupInput->isVisible(true);
//we set the controls for parent and child links
vector<string> alllinkgr = _ms.thistree.allLinksvec();
if (!cln || !pln)
throw "either cln or pln (or both!) don't exist. this will fail, aborting";
cln->listItems()->clear();
pln->listItems()->clear();
cln->listItems()->add(">>not set<<", true);
pln->listItems()->add(">>not set<<", true);
LOG(DEBUG) << "repopulating joint names";
for (auto linknamestr : alllinkgr)
{
bool isthischildselected = linknamestr == currJoint->childlink;
bool isthisparentselected = linknamestr == currJoint->parentlink;
cln->listItems()->add(linknamestr, isthischildselected);
pln->listItems()->add(linknamestr, isthisparentselected);
}
}
}
}
else
{
LOG(WARNING) << "could not resolve _ms.thistree.currentEl\nThis happens if there is no selected row and can usually be ignored.";
}
}
catch (const char* msg) {
LOG(ERROR) << msg;
ui->messageBox(msg);
}
catch (std::exception& e)
{
LOG(ERROR) << e.what();
std::string errormsg = "the update region bit failed...\n" ;
LOG(ERROR) << errormsg;
ui->messageBox(errormsg);
}
catch (...)
{
std::string errormsg = "Error: the update region bit failed hard!";
LOG(ERROR) << errormsg;
ui->messageBox(errormsg);
}
}
}
};
// CommandExecuted event handler.
class UrdfGenOnExecuteEventHander : public adsk::core::CommandEventHandler
{
public:
void notify(const Ptr<CommandEventArgs>& eventArgs) override
{
ui->messageBox("Executing! ");
LOG(INFO) << "Executing! ";
vector<fs::path> mypaths = createpaths(_ms.packagename, _ms.thisscriptpath);
// lets create a simple xml to make sure we understand tinyxml sintax
TiXmlDocument urdfdoc;
TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "", "");
urdfdoc.LinkEndChild(decl);
TiXmlElement * robot_root = new TiXmlElement("robot");
robot_root->SetAttribute("name", "gummi");
urdfdoc.LinkEndChild(robot_root);
ULink base_link;
base_link.name = "base_link";
base_link.makexml(robot_root, _ms.packagename);
#
UJoint setaxisjoint;
setaxisjoint.name = "set_worldaxis";
setaxisjoint.isset = true;
setaxisjoint.type = "fixed";
setaxisjoint.realorigin.rpy = std::to_string(PI / 2) + " 0 0";
setaxisjoint.parentlink = "base_link";
setaxisjoint.childlink = "base";
setaxisjoint.makexml(robot_root, _ms.packagename);
//now parse the urdftree
for (auto el:_ms.thistree.elementsDict)
{
ULink* currLink = dynamic_cast<ULink*>(el.second);
if (currLink)
{
LOG(INFO) << "calling genlink for link:" + currLink->name;
bool succeeded = currLink->genlink(mypaths[1], mypaths[2], design, app);
if (!succeeded)
{
string errormsg = "failed generating link" + currLink->name;
LOG(ERROR) << errormsg;
ui->messageBox(errormsg);
return;
}
}
el.second->makexml(robot_root, _ms.packagename);
};
string filenametosave = (mypaths[0] / "robot.urdf").string();
LOG(INFO) << "Saving file" + (filenametosave);
urdfdoc.SaveFile(filenametosave.c_str());
}
};
// CommandDestroyed event handler
class UrdfGenOnDestroyEventHandler : public adsk::core::CommandEventHandler
{
public:
void notify(const Ptr<CommandEventArgs>& eventArgs) override
{
// probably just need to reset _ms
MotherShip _ms;
(&_ms)->~MotherShip();
new (&_ms) MotherShip();
//adsk::terminate(); terminate will unload it. i want to keep unloading it to test it, but uncomment next line before commiting to master
if (!runfrommenu)
{
LOG(INFO) << "Bye bye!";
adsk::terminate();
}
}
};
// Event handler for my custom validateInputs event.
class UrdfGenValidateInputsEventHandler : public adsk::core::ValidateInputsEventHandler
{
public:
void notify(const Ptr<ValidateInputsEventArgs>& eventArgs) override
{
// Code to react to the event.
//this event is actually triggered all the time. not sure how to use it.
//ui->messageBox("In UrdfGenValidateInputsEventHandler event handler.");
// if the tree is empty, then just quit?
LOG(DEBUG) << "Validating inputs";
}
} _validateInputs;
// CommandCreated event handlers.
class UrdfGenCommandCreatedEventHandler : public adsk::core::CommandCreatedEventHandler
{
public:
void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override
{
if (eventArgs)
{
LOG(INFO) << "Hello from urdfgen. Creating GUI ";
try
{
//need to update default design!
Ptr<Product> product = app->activeProduct();
if (!product)
throw "error: can't get active product!";
design = product;
if (!design)
throw "can't get current design.";
//
// Creates our simple GUI
//
// Get the command that was created.
Ptr<Command> command = eventArgs->command();
if (command)
{
// Connect to the command destroyed event.
Ptr<CommandEvent> onDestroy = command->destroy();
if (!onDestroy)
return;
bool isOk = onDestroy->add(&onDestroyHandler);
if (!isOk)
return;
// Connect to the input changed event.
Ptr<InputChangedEvent> onInputChanged = command->inputChanged();
if (!onInputChanged)
return;
isOk = onInputChanged->add(&onInputChangedHandler);
if (!isOk)
return;
// Connect to the execute event.
Ptr<CommandEvent> onExecute = command->execute();//execute or doexecute?
if (!onInputChanged)
return;
isOk = onExecute->add(&onExecuteHandler);
if (!isOk)
return;
//added later. trying for custom validation.
// Connect the handler function to the event.
Ptr<ValidateInputsEvent> validateInputsEvent = command->validateInputs();
if (!validateInputsEvent)
return;
//I am not validating inputs right now; remove comments to use input validation callback.
//isOk = validateInputsEvent->add(&_validateInputs);
if (!isOk)
return;
// Get the CommandInputs collection associated with the command.
Ptr<CommandInputs> inputs = command->commandInputs();
if (!inputs)
return;
// Create a tab input.
Ptr<TabCommandInput> tabCmdInput3 = inputs->addTabCommandInput("tab_1", "URDFGEN");
if (!tabCmdInput3)
return;
Ptr<CommandInputs> tab3ChildInputs = tabCmdInput3->children();
if (!tab3ChildInputs)
return;
tab3ChildInputs->addStringValueInput("packagename", "Name of your URDF package", _ms.packagename);
// Create table input.
Ptr<TableCommandInput> tableInput = tab3ChildInputs->addTableCommandInput("table", "Table", 3, "1:2:3:1");
tableInput->maximumVisibleRows(20);
tableInput->minimumVisibleRows(10);
// Add inputs into the table.
Ptr<CommandInput> addLinkButtonInput = tab3ChildInputs->addBoolValueInput("tableLinkAdd", "Add Link", false, "", true);
tableInput->addToolbarCommandInput(addLinkButtonInput);
Ptr<CommandInput> addJointButtonInput = tab3ChildInputs->addBoolValueInput("tableJointAdd", "Add Joint", false, "", true);
tableInput->addToolbarCommandInput(addJointButtonInput);
Ptr<CommandInput> deleteButtonInput = tab3ChildInputs->addBoolValueInput("tableDelete", "Delete", false, "", true);
tableInput->addToolbarCommandInput(deleteButtonInput);
tab3ChildInputs->addBoolValueInput("createtree", "Create tree", false, "", true);
std::string messaged = "";
tab3ChildInputs->addTextBoxCommandInput("debugbox", "", messaged, 10, true);
// Adds a group for the link
Ptr<GroupCommandInput> linkGroupCmdInput = tab3ChildInputs->addGroupCommandInput("linkgroup", "Link Stuff");
if (!linkGroupCmdInput)
return;
linkGroupCmdInput->isVisible(false);
Ptr<CommandInputs> linkGroupChildInputs = linkGroupCmdInput->children();
if (!linkGroupChildInputs)
return;
// Create a selection input for the link.
Ptr<SelectionCommandInput> selectionInput1 = linkGroupChildInputs->addSelectionInput("linkselection", "Select Link Components", "Basic select command input");
selectionInput1->addSelectionFilter("Occurrences");
selectionInput1->setSelectionLimits(0);
// Adds a group for the joint
Ptr<GroupCommandInput> jointGroupCmdInput = tab3ChildInputs->addGroupCommandInput("jointgroup", "Joint Stuff");
if (!jointGroupCmdInput)
return;
jointGroupCmdInput->isVisible(false);
Ptr<CommandInputs> jointGroupChildInputs = jointGroupCmdInput->children();
if (!jointGroupChildInputs)
return;
// Create a selection input for the joint.
Ptr<SelectionCommandInput> selectionInput2 = jointGroupChildInputs->addSelectionInput("jointselection", "Select Joint", "Basic select command input");
selectionInput2->addSelectionFilter("Joints");
selectionInput2->setSelectionLimits(0,1);
selectionInput2->isVisible(true);
//variant. doesn not set a single control, rather multiple ones and changes visibility.
// Adds the joint offset control (maybe there is a built in version of this; the "move" command shows something like it)
//JointControl jtctrl(jointGroupChildInputs);
// Add parent and child controls for joint
Ptr<DropDownCommandInput> parentlinkin = jointGroupChildInputs->addDropDownCommandInput("parentlinkname", "Name of parent link", DropDownStyles::TextListDropDownStyle);
Ptr<DropDownCommandInput> childlinkin = jointGroupChildInputs->addDropDownCommandInput("childlinkname", "Name of child link", DropDownStyles::TextListDropDownStyle);
LOG(INFO) << "Created GUI alright. ";
}
}
catch (const char* msg) {
LOG(ERROR) << msg;
ui->messageBox(msg);
}
catch (...)
{
const char* msg = "unknown error!?!";
LOG(ERROR) << "unknown error!?!";
ui->messageBox(msg);
}
}
}
private:
UrdfGenOnExecuteEventHander onExecuteHandler;
UrdfGenOnDestroyEventHandler onDestroyHandler;
UrdfGenOnInputChangedEventHander onInputChangedHandler;
} _urdfGenCmdCreatedHandler;
class GenSTLCreatedEventHandler : public adsk::core::CommandCreatedEventHandler
{
public:
void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override
{
if (eventArgs)
{
LOG(INFO) << "Hello from genSTL ";
try
{
//need to update default design!
Ptr<Product> product = app->activeProduct();
if (!product)
throw "error: can't get active product!";
//this feels silly but they are different objects. Cpp is very clear...
design = product;
if (!design)
throw "can't get current design.";
Ptr<Component> rootComp = design->rootComponent();
if (!rootComp)
throw "error: can't find root component";
//Ptr<OccurrenceList> allOccs = rootComp->allOccurrences();
Ptr<ExportManager> exportMgr = design->exportManager();
if (!exportMgr)
throw "error: can't find export manager";
Ptr<FileDialog> fileDlg = ui->createFileDialog();
fileDlg->isMultiSelectEnabled(false);
fileDlg->title("Choose location to save your STL ");
fileDlg->filter("*.stl");
DialogResults dlgResult = fileDlg->showSave();
if (dlgResult != DialogResults::DialogOK)
throw "You need to select a folder";