Advertisement
lilo_booter

Parametric Arduino Box Builder

Feb 25th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Parametric Arduino Box Builder
  2.  
  3. function getParameterDefinitions( )
  4. {
  5.   return [
  6.     { name: 'type', type: 'choice', initial: 0, values: [ 0, 1, 2 ], captions: [ 'Box', 'Lid', 'Base' ], caption: "Part to generate:" },
  7.     { name: 's_length', type: 'float', initial: 79.0, caption: "Shield Length:" },
  8.     { name: 'w_thickness', type: 'float', initial: 2.0, caption: "Wall Thickness:" },
  9.     { name: 'w_height', type: 'float', initial: 21.0, caption: "Wall Height:" },
  10.     { name: 'b_thickness', type: 'float', initial: 3, caption: "Bottom Thickness:" },
  11.     { name: 'b_offset', type: 'float', initial: 1, caption: "Bottom Offset:" },
  12.     { name: 'x_gap', type: 'float', initial: 0.6, caption: "Tolerance for lid and base" },
  13.     { name: 'a_width', type: 'float', initial: 54, caption: "Arduino Width:" },
  14.     { name: 'a_length', type: 'float', initial: 69, caption: "Arduino Length:" },
  15.     { name: 'u_width', type: 'float', initial: 13, caption: "USB Width:" },
  16.     { name: 'u_height', type: 'float', initial: 15, caption: "USB Height:" },
  17.     { name: 'u_offset', type: 'float', initial: 9.5, caption: "USB Offset:" },
  18.     { name: 'p_width', type: 'float', initial: 9.5, caption: "Power Width:" },
  19.     { name: 'p_height', type: 'float', initial: 15, caption: "Power Height:" },
  20.     { name: 'p_offset', type: 'float', initial: 3.5, caption: "Power Offset:" },
  21.   ];
  22. }
  23.  
  24. function main( p )
  25. {
  26.     // Will hold the result of the function
  27.     var result;
  28.  
  29.     // Dimensions of the box (should be migrated to functions)
  30.     var bw = p.a_width + p.w_thickness * 2;
  31.     var bl = ( p.s_length <= p.a_length ? p.a_length : p.s_length ) + p.w_thickness * 2;
  32.     var bh = p.w_height + p.b_thickness;
  33.  
  34.     if ( p.type == 0 )
  35.     {
  36.         result = difference(
  37.             // Main box
  38.             cube( { size: [ bw, bl, bh ] } ),
  39.  
  40.             // Arduino
  41.             cube( { size: [ p.a_width, p.a_length, p.w_height ] } ).translate( [ p.w_thickness, p.w_thickness, p.b_thickness ] ),
  42.  
  43.             // Power
  44.             cube( { size: [ p.p_width, p.w_thickness, p.p_height ] } ).translate( [ bw - p.w_thickness - p.p_offset - p.p_width, 0, p.b_thickness ] ),
  45.  
  46.             // USB
  47.             cube( { size: [ p.u_width, p.w_thickness, p.u_height ] } ).translate( [ p.w_thickness + p.u_offset, 0, p.b_thickness ] ),
  48.  
  49.             // Base
  50.             cube( { size: [ p.a_width - p.b_offset * 2, p.a_length - p.b_offset * 2, p.b_thickness ] } ).translate( [ p.b_offset + p.w_thickness, p.b_offset + p.w_thickness, 0 ] )
  51.         );
  52.  
  53.         if ( p.s_length > p.a_length )
  54.         {
  55.             result = difference(
  56.                 // Derived result which we'll modify here
  57.                 result,
  58.  
  59.                 // Clear the space for the shield
  60.                 cube( { size: [ p.a_width, p.s_length - p.a_length, p.w_height ] } ).translate( [ p.w_thickness, p.w_thickness +  p.a_length, p.b_thickness ] )
  61.             );
  62.         }
  63.     }
  64.     else if ( p.type == 1 )
  65.     {
  66.         result = difference(
  67.             // Slightly larger than the main box
  68.             cube( { size: [ bw + p.w_thickness * 2 + p.x_gap * 2, bl + p.w_thickness * 2 + p.x_gap * 2, p.b_thickness + p.w_thickness ] } ),
  69.  
  70.             cube( { size: [ bw + p.x_gap, bl + p.x_gap, p.b_thickness ] } ).translate( [ p.w_thickness + p.x_gap / 2, p.w_thickness + p.x_gap / 2, p.w_thickness ] ),
  71.  
  72.             cube( { size: [ p.a_width - p.b_offset * 2, p.a_length - p.b_offset * 2, p.b_thickness ] } ).translate( [ p.b_offset + p.w_thickness * 2 + p.x_gap, p.b_offset + p.w_thickness * 2 + p.x_gap, 0 ] )
  73.         );
  74.     }
  75.     else if ( p.type == 2 )
  76.     {
  77.         result = difference(
  78.             // Slightly larger than the main box
  79.             cube( { size: [ bw + p.w_thickness * 2 + p.x_gap * 2, bl + p.w_thickness * 2 + p.x_gap * 2, p.b_thickness + p.w_thickness ] } ),
  80.  
  81.             cube( { size: [ bw + p.x_gap, bl + p.x_gap, p.b_thickness ] } ).translate( [ p.w_thickness + p.x_gap / 2, p.w_thickness + p.x_gap / 2, p.w_thickness ] )
  82.         );
  83.     }
  84.  
  85.     return result;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement