openjscad-aurora-webapp
214 строк · 8.2 Кб
1// title: iPhone 4 Dock
2// author: Joost Nieuwenhuijse
3
4/*
5
6OpenJsCad script for iPhone 4 dock
7Note: not tried printing yet, not sure if it really wil fit.
8
9To create the STL file, launch Google Chrome and go to:
10http://joostn.github.com/OpenJsCad/processfile.html or http://openjscad.org
11
12Then drag&drop this file into the page
13
14OpenJsCad is an open source 3d solid modeling tool using JavaScript.
15For more information see http://joostn.github.com/OpenJsCad
16
17*/
18
19function getParameterDefinitions() {20return [21{22name: 'quality',23type: 'choice',24caption: 'Quality:',25values: ["draft", "smooth", "supersmooth"],26captions: ["Draft (no rounded corners)", "Smooth (rounded corners)", "Super smooth (rounded corners)"],27initial: "draft"28},29{30name: 'iphonemargin',31type: 'float',32initial: 0.5,33caption: "Margin around iphone (in mm):"34},35{36name: 'plugmargin',37type: 'float',38initial: 0.25,39caption: "Margin around dock connector (in mm):"40},41{ name: 'mouseear', caption: 'Add mouse ear:', type: 'choice', values: [0, 1], initial: 1, captions: ["No", "Yes"]}42];43}
44
45function main(params)46{47var resolution = 16; // resolution for all the curved surfaces48var smoothing = (params.quality != "draft"); // set to false during development, for fast rendering49var draft = (params.quality != "supersmooth"); // set to false for high resolution smoothing, rendering will take several minutes!50
51var x1=40, x2=25, x3=8;52var y1=35, y2=y1+8, y3=y2+25;53var frontheight = 30;54
55// Build the base from two 2D polygons:56var base1=new CSG.Polygon2D([[0,0],[x1,0],[x1,y1],[x2,y2],[0,y2]]);57var base2=new CSG.Polygon2D([[0,y2],[x2,y2],[x3,y3],[0,y3]]);58var extruded1=base1.extrude({offset: [0, 0,frontheight]});59var extruded2=base2.extrude({offset: [0, 0,frontheight]});60// We now have the right half; mirror to create the left half:61extruded1 = extruded1.union(extruded1.mirroredX());62extruded2 = extruded2.union(extruded2.mirroredX());63var base = extruded1.union(extruded2);64
65// Make the top back surface slightly slanted:66var backtopplane = CSG.Plane.fromNormalAndPoint([0, 8, 10], [0, y2, frontheight]);67base = base.cutByPlane(backtopplane);68
69// Make the front surface slightly slanted:70var frontplane = CSG.Plane.fromNormalAndPoint([0, -10, 2], [0, 0, 0]);71base = base.cutByPlane(frontplane);72
73// Add a CSG.Connector to the base, at the point where the iphone should74// rest in the base. The connector's axis points upwards and its normal75// points towards the front:76var recessionDepth = 10;77var distanceFromFront = 20;78var angle = 15;79base.properties.iphoneConnector = new CSG.Connector(80[0, distanceFromFront, frontheight - recessionDepth], // the point81[0, 0, 1], // axis vector82[1, 0, 0] // normal vector83);84// rotate the base.properties.iphoneConnector, so that the iphone will be tilted:85var rotmaxtrix = CSG.Matrix4x4.rotation(base.properties.iphoneConnector.point, [1, 0, 0], angle);86base.properties.iphoneConnector = base.properties.iphoneConnector.transform(rotmaxtrix);87
88// Create iphone placeholder (it's just a cube with the iphone's dimensions):89var iphoneradius = new CSG.Vector3D(58.6/2 + params.iphonemargin, 9.4/2 + params.iphonemargin, 115/2);90var iphone = CSG.cube({radius: iphoneradius});91
92// Create the little tab behind the iphone:93var tabradius = new CSG.Vector3D(15, 5, 12);94var tab = CSG.cube({radius: tabradius});95tab = tab.translate([0, (iphoneradius.y+tabradius.y), (-iphoneradius.z+tabradius.z)]);96
97// Add a CSG.Connector to the iphone's properties. This is the place where the98// 30 pin dock plug will snap in. In this case it is the center of the bottom99// Z plane of the cube. Since the cube already has 6 predefined connectors100// at the center of each face, we can just use one of those instead of creating101// a new CSG.Connector:102iphone.properties.dockConnector = iphone.properties.cube.facecenters[5];103
104// transform the iphone so it sits in the base:105var iphoneTransformation = iphone.properties.dockConnector.getTransformationTo(base.properties.iphoneConnector, true, 0);106iphone = iphone.transform(iphoneTransformation);107
108// transform the tab so it stays behind the iphone:109tab = tab.transform(iphoneTransformation);110
111// and add the tab to the base:112base = base.union(tab);113
114// build the iphone plug:115var plug = getIphoneDockConnector(params.plugmargin, resolution);116
117// The plug has a predefined Connector plug.properties.iphoneConnector118// To attach the plug to the iphone we connect plug.properties.iphoneConnector119// to iphone.properties.dockConnector120plug = plug.connectTo(plug.properties.iphoneConnector,121iphone.properties.dockConnector, true, 0);122
123// We must make sure we can pull the USB cable through the dock. So there needs124// to be space for the plug. We just create a tall cube with the size of the USB125// plug (8 by 40 mm), which will be subtracted from the dock's shape:126var gapForUsbPlug = CSG.cube({radius: [8, 4, 40]});127
128// Align it to the plug. Again, since gapForUsbPlug is a CSG.cube it already129// has predefined CSG.Connectors at every face. Connect the Z face to the iphone:130gapForUsbPlug = gapForUsbPlug.connectTo(gapForUsbPlug.properties.cube.facecenters[5],131iphone.properties.dockConnector, true, 0);132
133// return plug.union(iphone).union(gapForUsbPlug);
134
135//var cutout = plug.union(iphone).union(gapForUsbPlug);136//return cutout;137
138base = base.subtract(iphone);139
140// make the cutout for the cable:141var cablewidth = 3;142var cableheight = 4;143var bottomplane = CSG.Plane.fromNormalAndPoint([0, 0, -1], [0, 0, 0]);144var cableline = plug.properties.cableConnector.axisLine();145var cableexitpoint = bottomplane.intersectWithLine(cableline);146var cableentrypoint = new CSG.Vector3D(0, y3, 0);147
148var cablepath = new CSG.Path2D([[cableentrypoint.x, cableentrypoint.y], [cableexitpoint.x, cableexitpoint.y]], false);149var cablecutout = cablepath.rectangularExtrude(cablewidth, cableheight, resolution, false);150
151// Smooth the base:152if(smoothing)153{154base = base.contract(4, draft? 4:16);155base = base.expand(4, draft? 4:16);156}157
158// Subtract the connector and cable:159base = base.subtract(iphone).subtract(plug).subtract(cablecutout).subtract(gapForUsbPlug);160
161// add mouse ear:162if(params.mouseear == 1)163{164var mouseearpoint = new CSG.Vector3D(0, y3, 0);165var mouseearthickness = 0.5;166var mouseearradius = 15;167var mouseear = CSG.cylinder({168start: mouseearpoint,169end: mouseearpoint.plus(new CSG.Vector3D(0, 0, mouseearthickness)),170radius: mouseearradius,171resolution: 16172});173base = base.union(mouseear);174}175
176// center:177var basecenter = base.getBounds()[0].plus(base.getBounds()[1]).times(0.5);178base = base.translate(basecenter.negated());179
180return base;181}
182
183/////////////////////
184function getIphoneDockConnector(margin, resolution)185{
186margin = new CSG.Vector3D(margin);187var dockConnectorSize = new CSG.Vector3D([26.2, 5.7, 9.5]).plus(margin.times(2));188var dockConnector = CSG.cube({radius: [(dockConnectorSize.x-dockConnectorSize.y)/2, dockConnectorSize.y/2, dockConnectorSize.z/2]});189dockConnector = dockConnector.union(CSG.cylinder({190start: [(dockConnectorSize.x-dockConnectorSize.y)/2, 0, -dockConnectorSize.z/2],191end: [(dockConnectorSize.x-dockConnectorSize.y)/2, 0, dockConnectorSize.z/2],192radius: dockConnectorSize.y/2,193resolution: resolution194}));195dockConnector = dockConnector.union(CSG.cylinder({196start: [-(dockConnectorSize.x-dockConnectorSize.y)/2, 0, -dockConnectorSize.z/2],197end: [-(dockConnectorSize.x-dockConnectorSize.y)/2, 0, dockConnectorSize.z/2],198radius: dockConnectorSize.y/2,199resolution: resolution200}));201var cableTube = CSG.cylinder({202start: [0, 0, -dockConnectorSize.z/2-5.70-margin.z],203end: [0, 0, -dockConnectorSize.z/2],204radius: 2.25+margin.y,205resolution: resolution206});207dockConnector = dockConnector.union(cableTube);208
209// Add CSG.Connector properties:210dockConnector.properties.iphoneConnector = dockConnector.properties.cube.facecenters[4];211dockConnector.properties.cableConnector = cableTube.properties.cylinder.start;212
213return dockConnector;214}
215
216
217