| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /**
- * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
- *
- * This file is part of the QCAD project.
- *
- * QCAD is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * QCAD 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with QCAD.
- */
- include("scripts/EAction.js");
- include("scripts/ShapeAlgorithms.js");
- include("scripts/Draw/Line/Line.js");
- /**
- * \class Offset
- * \brief Parallel lines, concentric arcs, circles, equidistant curve to ellipses.
- * \ingroup ecma_modify
- */
- function Offset(guiAction) {
- EAction.call(this, guiAction);
- this.segmentMode = true;
- this.distance = undefined;
- this.number = undefined;
- this.entity = undefined;
- this.entityId = undefined;
- this.shape = undefined;
- this.pos = undefined;
- this.lineType = undefined;
- if (!isNull(guiAction)) {
- this.setUiOptions(Offset.includeBasePath + "/Offset.ui");
- }
- }
- Offset.prototype = new EAction();
- Offset.includeBasePath = includeBasePath;
- Offset.State = {
- ChoosingEntity : 0
- };
- Offset.prototype.beginEvent = function() {
- EAction.prototype.beginEvent.call(this);
- this.setState(Offset.State.ChoosingEntity);
- };
- Offset.prototype.initState = function() {
- EAction.prototype.initState.call(this);
- this.getDocumentInterface().setClickMode(RAction.PickEntity);
- this.setCrosshairCursor();
- switch (this.state) {
- case Offset.State.ChoosingEntity:
- var tr = this.getLeftMouseTip();
- this.setLeftMouseTip(tr);
- this.setCommandPrompt(tr);
- break;
- }
- this.setRightMouseTip(EAction.trCancel);
- };
- Offset.prototype.getLeftMouseTip = function() {
- return qsTr("Choose line, arc, circle or ellipse");
- };
- Offset.prototype.isShapeSupported = function(shape) {
- return isLineBasedShape(shape) ||
- isArcShape(shape) ||
- isCircleShape(shape) ||
- isEllipseShape(shape);
- };
- Offset.prototype.warnUnsupportedShape = function() {
- EAction.warnNotLineArcCircleEllipse();
- };
- Offset.prototype.escapeEvent = function() {
- switch (this.state) {
- case Offset.State.ChoosingEntity:
- EAction.prototype.escapeEvent.call(this);
- break;
- }
- };
- Offset.prototype.pickEntity = function(event, preview) {
- var di = this.getDocumentInterface();
- var doc = this.getDocument();
- var entityId = this.getEntityId(event, preview);
- var entity = doc.queryEntity(entityId);
- var pos = event.getModelPosition();
- if (isNull(entity)) {
- this.entity = undefined;
- this.entityId = RObject.INVALID_ID;
- return;
- }
- if (!this.isEntitySnappable(entity)) {
- // entity not on a snappable layer:
- return;
- }
- switch (this.state) {
- case Offset.State.ChoosingEntity:
- var shape;
- if (this.segmentMode) {
- // parallel to polyline segment:
- shape = entity.getClosestSimpleShape(pos);
- }
- else {
- shape = entity.getClosestShape(pos);
- }
- if (this.isShapeSupported(shape)) {
- this.entity = entity;
- this.entityId = entityId;
- this.shape = shape;
- this.pos = pos;
- }
- else {
- if (!preview) {
- this.warnUnsupportedShape();
- break;
- }
- }
- if (preview) {
- this.updatePreview();
- }
- else {
- var op = this.getOperation(false);
- if (!isNull(op)) {
- di.applyOperation(op);
- if (!isNull(this.error)) {
- EAction.handleUserWarning(this.error);
- }
- }
- }
- break;
- }
- };
- Offset.prototype.getOperation = function(preview) {
- if (isNull(this.pos) || isNull(this.entity) ||
- !isNumber(this.distance) || !isNumber(this.number) ||
- !isShape(this.shape)) {
- return undefined;
- }
- var offsetShapes = this.getOffsetShapes(preview);
- if (!preview) {
- this.error = undefined;
- var c = RShape.getErrorCode();
- if (c!==0) {
- if (isCircleShape(this.shape)) {
- this.error = qsTr("Radius dropped below 0.0 after %n concentric circle(s).", "", c-1);
- }
- else if (isArcShape(this.shape)) {
- this.error = qsTr("Radius dropped below 0.0 after %n concentric arc(s).", "", c-1);
- }
- }
- }
- if (isNull(offsetShapes)) {
- return undefined;
- }
- var doc = this.getDocument();
- var e;
- var op = new RAddObjectsOperation();
- op.setText(this.getToolTitle());
- for (var i=0; i<offsetShapes.length; ++i) {
- var offsetShape = offsetShapes[i];
- if (isNull(offsetShape)) {
- continue;
- }
- if (isLineBasedShape(offsetShape) && !isNull(this.lineType)) {
- var lt = this.lineType;
- if (lt===Line.LineType.Auto) {
- lt = Line.getLineType(offsetShape);
- }
- e = Line.createLineEntity(
- doc,
- offsetShape.getStartPoint(),
- offsetShape.getEndPoint(),
- lt);
- }
- else {
- if (isFunction(offsetShape.data)) {
- e = shapeToEntity(doc, offsetShape.data());
- }
- else {
- e = shapeToEntity(doc, offsetShape);
- }
- }
- if (!isNull(e)) {
- this.addObjectToOperation(op, e);
- }
- }
- return op;
- };
- Offset.prototype.addObjectToOperation = function(operation, entity) {
- operation.addObject(entity);
- };
- Offset.prototype.getOffsetShapes = function(preview) {
- return ShapeAlgorithms.getOffsetShapes(this.shape, this.distance, this.number, this.pos);
- };
- Offset.prototype.getHighlightedEntities = function() {
- var ret = [];
- // don't highlight hatches:
- if (this.isShapeSupported(this.shape) && isEntity(this.entity) && !isHatchEntity(this.entity)) {
- ret.push(this.entity.getId());
- }
- return ret;
- };
- Offset.prototype.slotDistanceChanged = function(value) {
- this.distance = value;
- this.updatePreview(true);
- };
- Offset.prototype.slotNumberChanged = function(value) {
- this.number = value;
- this.updatePreview(true);
- };
|