openjscad-aurora-webapp
69 строк · 2.0 Кб
1// title: Jar
2// author: Eduard Bespalov
3// license: MIT License
4// description: testing solidFromSlices()
5
6// Here we define the user editable parameters:
7function getParameterDefinitions() {8return [9{ name: 'diameter', caption: 'Jar diameter:', type: 'float', default: 35 },10{ name: 'height', caption: 'Jar height:', type: 'float', default: 35 },11{ name: 'wallThick', caption: 'Jar wall thick:', type: 'float', default: 1 }12];13}
14
15function main(params)16{
17var radius = params.diameter / 2;18var thick = params.wallThick;19var height = params.height;20var bottomThick = 2 * thick;21
22var jar = hexTwisted(radius, height, 60).subtract(23hexTwisted( radius - thick, height - bottomThick, 60)24//rotate because jar is already rotated on that level25//otherwise will get holes26.rotateZ(60 * (bottomThick / height))27.translate([0,0,bottomThick]) //make jar bottom28);29return jar;30}
31
32function hexTwisted(radius, height, twistangle) {33twistangle = twistangle || 0;34var sqrt3 = Math.sqrt(3) / 2;35var cag = CAG.fromPoints([36[radius, 0, 0],37[radius / 2, radius * sqrt3, 0],38[-radius / 2, radius * sqrt3, 0],39[-radius, 0, 0],40[-radius / 2, -radius * sqrt3, 0],41[radius / 2, -radius * sqrt3, 0]42]).expand(4, CSG.defaultResolution2D);43
44var flatBottom = CSG.Polygon.createFromPoints(45cag.getOutlinePaths()[0].points46);47
48var bounds = flatBottom.boundingBox();49//save bounds to remove barrel effect created by rotation50var bottomBounds = {51x: Math.abs(bounds[1].x - bounds[0].x),52y: Math.abs(bounds[1].y - bounds[0].y)53};54
55
56var hex = flatBottom.solidFromSlices({57numslices: height58,callback: function(t) {59var polygon = this.rotateZ(t * twistangle).translate([0, 0, height * t]);60var bounds = polygon.boundingBox();61
62var xScale = Math.max(1, Math.abs(bounds[1].x - bounds[0].x)/bottomBounds.x);63var yScale = Math.max(1, Math.abs(bounds[1].y - bounds[0].y)/bottomBounds.y);64
65return polygon.scale(1/ Math.max(xScale, yScale));66}67});68return hex;69}
70