openjscad-aurora-webapp

Форк
0
141 строка · 5.0 Кб
1
// title: S Hook
2
// author: Joost Nieuwenhuijse
3
// license: MIT License
4

5
// Here we define the user editable parameters: 
6
function getParameterDefinitions() {
7
  return [
8
    { name: 'topdiameter', caption: 'Inner diameter of top hook:', type: 'float', initial: 16.7 },
9
    { name: 'clampfactor', caption: 'Snugness of top hook (0 - 100):', type: 'float', initial: 25 },
10
    { name: 'cliplength', caption: 'Top hook clip length:', type: 'float', initial: 5 },
11
    { name: 'bottomdiameter', caption: 'Inner diameter of bottom hook:', type: 'float', initial: 20 },
12
    { name: 'height', caption: 'Outer height of the hook:', type: 'float', initial: 60 },
13
    { name: 'thickness', caption: 'Thickness:', type: 'float', initial: 5 },
14
    { name: 'width', caption: 'Width:', type: 'float', initial: 7 },
15
    {
16
      name: 'rounded', 
17
      type: 'choice',
18
      caption: 'Rounded edges',
19
      values: [0, 1],
20
      captions: ["No", "Yes (rendering will take a long time!)"], 
21
      initial: 0
22
    },    
23
    { name: 'roundness', caption: 'Diameter of rounded edges (if enabled):', type: 'float', initial: 1.5 },
24
    { name: 'buildwidth', caption: 'Width (x) of build area (to print multiple copies):', type: 'float', initial: 90 },
25
    { name: 'builddepth', caption: 'Depth (y) of build area (to print multiple copies):', type: 'float', initial: 90 }    
26
  ];
27
}
28

29
function main(params) {
30
  if(OpenJsCad.log) OpenJsCad.log("start");
31
  
32
  var pathresolution = 16;
33
  var expandresolution = 6;
34
  
35
  // construct the 2D path:
36
  var topradius = params.topdiameter/2;
37
  var bottomradius = params.bottomdiameter/2;
38
  var halfthickness = params.thickness/2;
39
  topradius += halfthickness;
40
  bottomradius += halfthickness;
41
  
42
  var roundness = params.roundness;
43
  if(params.rounded != 1)
44
  {
45
    roundness = 0;
46
  }
47
  roundness = Math.min(roundness, halfthickness-0.1, params.width/2-0.1);
48
  if(roundness < 0) roundness = 0;
49
  
50
  var clampfactor = params.clampfactor / 100;
51
  if(clampfactor < 0) clampfactor = 0;
52
  if(clampfactor >= 1) clampfactor = 1;
53
  clampfactor *= (topradius-halfthickness)/topradius;  
54
  
55
  var topstartangle = - 180 * Math.acos(1 - 2*clampfactor) / Math.PI;
56
  var tophookcenter = new CSG.Vector2D(topradius, 0);
57
  var tophookstart = tophookcenter.plus(CSG.Vector2D.fromAngleDegrees(topstartangle).times(topradius));
58
  var circledistance = params.height - topradius - bottomradius - 2 * params.thickness;
59
  if(circledistance < 0) circledistance = 0;
60
  var bottomhookcenter = new CSG.Vector2D(-bottomradius, -circledistance);
61
  var gravityangle = 90 - tophookcenter.minus(bottomhookcenter).angleDegrees();
62
  
63
  var path = new CSG.Path2D();
64

65
  // top hook curve:
66
  if(params.cliplength > 0)
67
  {
68
    var clipstart = new CSG.Vector2D([0, -1]).times(params.cliplength).plus(tophookstart);
69
    path = path.appendPoint(clipstart);
70
  }
71
  var topcurvepath = CSG.Path2D.arc({
72
    center: tophookcenter,
73
    radius: topradius,
74
    startangle: topstartangle,
75
    endangle: 180,
76
    resolution: pathresolution,
77
    maketangent: true
78
  });
79
  path = path.concat(topcurvepath);
80

81
  // straight middle part:
82
  if(circledistance > 0)
83
  {
84
    path = path.appendPoint([0, -circledistance]);
85
  }
86
  
87
  // bottom hook curve:
88
  var bottomcurvepath = CSG.Path2D.arc({
89
    center: bottomhookcenter,
90
    radius: bottomradius,
91
    startangle: 0,
92
    endangle: -180-gravityangle,
93
    resolution: pathresolution,
94
    maketangent: true
95
  });
96
  path = path.concat(bottomcurvepath);
97
  
98
  // center around origin, and rotate as it would hang under gravity:
99
  var centerpoint = tophookcenter.plus(bottomhookcenter).times(0.5);  
100
  var matrix = CSG.Matrix4x4.translation(centerpoint.negated().toVector3D(0));
101
  matrix = matrix.multiply(CSG.Matrix4x4.rotationZ(gravityangle));
102
  path = path.transform(matrix);
103

104
  // extrude the path to create a 3D solid
105
  var hook = path.rectangularExtrude(2*(halfthickness-roundness), params.width-2*roundness, pathresolution, true);
106
  hook = hook.translate([0, 0, -params.width/2+roundness]);
107
  
108
  // expand to create rounded corners:
109
  if(roundness > 0)
110
  {
111
    hook = hook.expand(roundness, expandresolution);
112
  }
113
  // hook = hook.toPointCloud(0.1);
114
  
115
  // determine how many objects will fit in the build area:
116
  var bounds = hook.getBounds();
117
  var objsize = bounds[1].minus(bounds[0]);
118
  var margin = 5; // distance between the copies
119
  var numx = Math.floor((params.buildwidth + margin) / (objsize.x + margin));  
120
  var numy = Math.floor((params.builddepth + margin) / (objsize.y + margin));
121
  if(numx < 1) numx = 1;  
122
  if(numy < 1) numy = 1;
123

124
  // and make the copies:
125
  var result = new CSG();
126
  for(var x = 0; x < numx; x++)
127
  {
128
    var deltax = ((1-numx)/2+x) * (objsize.x + margin); 
129
    var colresult = new CSG();
130
    for(var y = 0; y < numy; y++)
131
    {
132
      var deltay = ((1-numy)/2+y) * (objsize.y + margin);
133
      var translated = hook.translate(new CSG.Vector3D(deltax, deltay, 0));
134
      colresult = colresult.union(translated); 
135
    }
136
    result = result.union(colresult);
137
  }    
138
   
139
  if(OpenJsCad.log) OpenJsCad.log("finish");
140
  return result;
141
}
142

143

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.