SnapIntersectionManual.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. 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/Snap/Snap.js");
  20. /**
  21. * \class SnapIntersectionManual
  22. * \ingroup ecma_snap
  23. * \brief Snap to intersection points by selecting two entities.
  24. */
  25. function SnapIntersectionManual(guiAction) {
  26. Snap.call(this, guiAction);
  27. this.autoTerminate = RSettings.getBoolValue("SnapIntersectionManual/AutoTerminate", true);
  28. this.entity1 = undefined;
  29. this.shape1 = undefined;
  30. this.entity2 = undefined;
  31. this.shape2 = undefined;
  32. this.entityId1 = -1;
  33. }
  34. SnapIntersectionManual.prototype = new Snap();
  35. SnapIntersectionManual.State = {
  36. PickingEntity1 : 0,
  37. PickingEntity2 : 1
  38. };
  39. SnapIntersectionManual.getPreferencesCategory = function() {
  40. return [qsTr("Snap"), qsTr("Intersection Manual")];
  41. };
  42. SnapIntersectionManual.prototype.setState = function(state) {
  43. Snap.prototype.setState.call(this, state);
  44. switch (this.state) {
  45. case SnapIntersectionManual.State.PickingEntity1:
  46. this.entity1 = undefined;
  47. this.shape1 = undefined;
  48. this.entity2 = undefined;
  49. this.shape2 = undefined;
  50. this.entityId1 = -1;
  51. var trFirstEntity = qsTr("First entity");
  52. this.setCommandPrompt(trFirstEntity);
  53. this.setLeftMouseTip(trFirstEntity);
  54. this.setRightMouseTip(EAction.trCancel);
  55. this.di = this.getDocumentInterface();
  56. this.di.setSnap(null);
  57. this.di.setClickMode(RAction.PickEntity);
  58. this.di.setCursorOverride(true);
  59. break;
  60. case SnapIntersectionManual.State.PickingEntity2:
  61. var trSecondEntity = qsTr("Second entity");
  62. this.setCommandPrompt(trSecondEntity);
  63. this.setLeftMouseTip(trSecondEntity);
  64. this.setRightMouseTip(EAction.trCancel);
  65. break;
  66. }
  67. };
  68. SnapIntersectionManual.prototype.beginEvent = function() {
  69. Snap.prototype.beginEvent.call(this);
  70. this.base = this.getOverrideBase();
  71. if (isNull(this.base)) {
  72. qWarning("No base action to override");
  73. this.terminate();
  74. return;
  75. }
  76. this.setState(SnapIntersectionManual.State.PickingEntity1);
  77. // if (this.base.getClickMode()!==RAction.PickCoordinate) {
  78. // qWarning("No coordinate requested at this point");
  79. // this.terminate();
  80. // }
  81. };
  82. SnapIntersectionManual.prototype.finishEvent = function() {
  83. this.di.setCursorOverride(false);
  84. Snap.prototype.finishEvent.call(this);
  85. };
  86. SnapIntersectionManual.prototype.entityPickEvent = function(event) {
  87. var doc = this.getDocument();
  88. if (isNull(doc)) {
  89. return;
  90. }
  91. var pos = event.getModelPosition();
  92. if (this.state===SnapIntersectionManual.State.PickingEntity1) {
  93. var id = EAction.getEntityId(this.di, this, event, true);
  94. this.entity1 = doc.queryEntity(id);
  95. if (!isNull(this.entity1)) {
  96. this.shape1 = this.entity1.getClosestSimpleShape(pos);
  97. this.entityId1 = EAction.prototype.getEntityId.call(this, event, false);
  98. this.setState(SnapIntersectionManual.State.PickingEntity2);
  99. }
  100. }
  101. else {
  102. var ip = this.getIntersection(event, false);
  103. if (!isNull(this.base) && ip.isValid()) {
  104. ip = this.restrict(ip);
  105. this.base.coordinateEvent(
  106. new RCoordinateEvent(ip,
  107. event.getGraphicsScene(),
  108. event.getGraphicsView()
  109. )
  110. );
  111. }
  112. if (this.autoTerminate) {
  113. this.terminate();
  114. }
  115. else {
  116. this.setState(SnapIntersectionManual.State.PickingEntity1);
  117. }
  118. }
  119. };
  120. SnapIntersectionManual.prototype.entityPickEventPreview = function(event) {
  121. if (this.state===SnapIntersectionManual.State.PickingEntity1) {
  122. if (event.getEntityId()!==-1) {
  123. this.di.highlightEntity(event.getEntityId());
  124. }
  125. this.di.setCursorPosition(RVector.invalid);
  126. }
  127. else if (this.state===SnapIntersectionManual.State.PickingEntity2) {
  128. if (this.entityId1!==-1) {
  129. this.di.highlightEntity(this.entityId1);
  130. }
  131. if (event.getEntityId()!==-1) {
  132. this.di.highlightEntity(event.getEntityId());
  133. }
  134. var ip = this.getIntersection(event, true);
  135. if (!isNull(this.base) && ip.isValid()) {
  136. ip = this.restrict(ip);
  137. this.base.coordinateEventPreview(
  138. new RCoordinateEvent(ip,
  139. event.getGraphicsScene(),
  140. event.getGraphicsView()
  141. )
  142. );
  143. this.di.setCursorPosition(ip);
  144. }
  145. else {
  146. this.di.setCursorPosition(RVector.invalid);
  147. }
  148. }
  149. };
  150. SnapIntersectionManual.prototype.getIntersection = function(event, preview) {
  151. var position = event.getModelPosition();
  152. var id = EAction.getEntityId(this.getDocumentInterface(), this, event, preview);
  153. var entity2 = this.getDocument().queryEntity(id);
  154. if (isNull(entity2)) {
  155. return RVector.invalid;
  156. }
  157. this.entity2 = entity2;
  158. this.shape2 = entity2.getClosestSimpleShape(position);
  159. if (isNull(this.shape2)) {
  160. return RVector.invalid;
  161. }
  162. var ips = this.shape1.getIntersectionPoints(getPtr(this.shape2), false, false, true);
  163. return position.getClosest(ips);
  164. };