diff --git a/web/app/sketcher/canvas.js b/web/app/sketcher/canvas.js index 08edf12c..077c8521 100644 --- a/web/app/sketcher/canvas.js +++ b/web/app/sketcher/canvas.js @@ -258,14 +258,16 @@ TCAD.TWO.Viewer.prototype.cleanSnap = function() { } }; -TCAD.TWO.Viewer.prototype.showBounds = function(x1, y1, x2, y2) { - this.translate.x = -x1; - this.translate.y = -y1; +TCAD.TWO.Viewer.prototype.showBounds = function(x1, y1, x2, y2, offset) { var dx = x2 - x1; var dy = y2 - y1; - console.log(this.scale); - this.scale = this.canvas.width / dx; - this.scale *= 0.7; + if (this.canvas.width > this.canvas.height) { + this.scale = this.canvas.height / dy; + } else { + this.scale = this.canvas.width / dx; + } + this.translate.x = -x1 * this.scale; + this.translate.y = -y1 * this.scale; }; TCAD.TWO.Viewer.prototype.screenToModel2 = function(x, y, out) { diff --git a/web/app/sketcher/io.js b/web/app/sketcher/io.js index 665aa4ee..09305da3 100644 --- a/web/app/sketcher/io.js +++ b/web/app/sketcher/io.js @@ -372,6 +372,15 @@ TCAD.io.BBox = function() { bbox[2] += by; bbox[3] += by; }; + + this.width = function() { + return bbox[2] - bbox[0]; + }; + + this.height = function() { + return bbox[3] - bbox[1]; + }; + this.bbox = bbox; }; diff --git a/web/app/sketcher/main2d.js b/web/app/sketcher/main2d.js index d54a7690..3efe3830 100644 --- a/web/app/sketcher/main2d.js +++ b/web/app/sketcher/main2d.js @@ -166,6 +166,33 @@ TCAD.App2D = function() { app.cleanUpData(); app.viewer.refresh(); }); + + this.registerAction('fit', "Fit Sketch On Screen", function () { + app.fit(); + app.viewer.refresh(); + }); +}; + +TCAD.App2D.prototype.fit = function() { + + var bbox = new TCAD.io.BBox(); + + for (var l = 0; l < this.viewer.layers.length; ++l) { + var layer = this.viewer.layers[l]; + for (var i = 0; i < layer.objects.length; ++i) { + var obj = layer.objects[i]; + obj.accept(function(obj) { + if (obj._class === 'TCAD.TWO.EndPoint') { + bbox.checkBounds(obj.x, obj.y); + } + return true; + }); + } + } + var bounds = bbox.bbox; + this.viewer.showBounds(bounds[0], bounds[1], bounds[2], bounds[3]); + bbox.inc(20 / this.viewer.scale); + this.viewer.showBounds(bounds[0], bounds[1], bounds[2], bounds[3]); }; TCAD.App2D.prototype.cloneSketch = function() {