ExLineDove.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Copyright (c) 2013 by Matthias Melcher. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD 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 General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/EAction.js");
  20. /**
  21. * \class ExLineDove
  22. * \brief Dovetail joint line between two points.
  23. * \ingroup ecma_misc_draw
  24. */
  25. function ExLineDove(guiAction) {
  26. EAction.call(this, guiAction);
  27. this.corner1 = undefined;
  28. this.corner2 = undefined;
  29. }
  30. ExLineDove.prototype = new EAction();
  31. ExLineDove.State = {
  32. SettingCorner1 : 0,
  33. SettingCorner2 : 1
  34. };
  35. ExLineDove.prototype.beginEvent = function() {
  36. EAction.prototype.beginEvent.call(this);
  37. this.setState(ExLineDove.State.SettingCorner1);
  38. };
  39. ExLineDove.prototype.setState = function(state) {
  40. EAction.prototype.setState.call(this, state);
  41. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  42. this.setCrosshairCursor();
  43. var appWin = RMainWindowQt.getMainWindow();
  44. switch (this.state) {
  45. case ExLineDove.State.SettingCorner1:
  46. var trFirstCorner = qsTr("First corner");
  47. this.setCommandPrompt(trFirstCorner);
  48. this.setLeftMouseTip(trFirstCorner);
  49. this.setRightMouseTip(EAction.trCancel);
  50. break;
  51. case ExLineDove.State.SettingCorner2:
  52. var trSecondCorner = qsTr("Second corner");
  53. this.setCommandPrompt(trSecondCorner);
  54. this.setLeftMouseTip(trSecondCorner);
  55. this.setRightMouseTip(EAction.trCancel);
  56. break;
  57. }
  58. EAction.showSnapTools();
  59. };
  60. ExLineDove.prototype.escapeEvent = function() {
  61. switch (this.state) {
  62. case ExLineDove.State.SettingCorner1:
  63. EAction.prototype.escapeEvent.call(this);
  64. break;
  65. case ExLineDove.State.SettingCorner2:
  66. this.setState(ExLineDove.State.SettingCorner1);
  67. break;
  68. }
  69. };
  70. ExLineDove.prototype.coordinateEvent = function(event) {
  71. var di = this.getDocumentInterface();
  72. switch (this.state) {
  73. case ExLineDove.State.SettingCorner1:
  74. this.corner1 = event.getModelPosition();
  75. di.setRelativeZero(this.corner1);
  76. this.setState(ExLineDove.State.SettingCorner2);
  77. break;
  78. case ExLineDove.State.SettingCorner2:
  79. this.corner2 = event.getModelPosition();
  80. di.setRelativeZero(this.corner2);
  81. var op = this.getOperation(false);
  82. if (!isNull(op)) {
  83. di.applyOperation(op);
  84. this.setState(ExLineDove.State.SettingCorner1);
  85. }
  86. break;
  87. }
  88. };
  89. ExLineDove.prototype.coordinateEventPreview = function(event) {
  90. switch (this.state) {
  91. case ExLineDove.State.SettingCorner2:
  92. this.corner2 = event.getModelPosition();
  93. this.updatePreview();
  94. break;
  95. default:
  96. break;
  97. }
  98. };
  99. ExLineDove.prototype.getOperation = function(preview) {
  100. var len = this.corner1.getDistanceTo(this.corner2);
  101. var ang = this.corner1.getAngleTo(this.corner2);
  102. var corners = new Array(
  103. new RVector( 0, 0),
  104. new RVector( 6, 0),
  105. new RVector( 4, 8),
  106. new RVector(16, 8),
  107. new RVector(14, 0),
  108. new RVector(20, 0)
  109. );
  110. var op = new RAddObjectsOperation();
  111. for (var i=0; i<5; ++i) {
  112. var lineData = new RLineData(corners[i],corners[i+1]);
  113. var line = new RLineEntity(this.getDocument(), lineData);
  114. line.scale(len/20);
  115. line.move(this.corner1);
  116. line.rotate(ang, this.corner1);
  117. op.addObject(line);
  118. }
  119. return op;
  120. };