<p>I'm designing a few mechanisms with OpenSCAD, and one of the parameters that need adjustment between printers/filaments is allowances between pieces that need to be assembled together (for example, if I want a "5&nbsp;mm diameter pin" to fasten two pieces together, how much larger than 5&nbsp;mm will the holes / how much smaller than 5&nbsp;mm will the pin need to be).</p>

<p>Currently I approach the problem by setting a global variable <code>allowance</code> and manually using it in the code, something like:</p>

<pre><code>module pin(radius) {
    cylinder(r = radius + allowance)
}

module hole(radius) {
    cylinder(r = radius - allowance)
}
</code></pre>

<p>I have no real world experience with design though, so I wonder if there are common or <strong>coding</strong> best practices to account for allowances when designing parts like for example:</p>

<ul>
<li>specific modules to be used,</li>
<li>conventional names for variables</li>
<li>specific techniques to be used (scaling? vectors?)</li>
<li>conventions (like only use tolerances on the fastener, not the fastened object)</li>
<li>...?</li>
</ul>

<p>To clarify: I'm not looking on advice on how to plan the dimensions of my designs.  Rather, I am looking for advice on how to organise the OpenSCAD code generating them.</p>

[{'answer_id': 5178, 'author': 'Fernando Baltazar', 'author_id': 4454, 'author_profile': 'https://3dprinting.stackexchange.com/users/4454', 'pm_score': -1, 'selected': False, 'text': '<p>Well, the tolerances will depend on material to be used for fabrication of the required part and also where the part will go and fit. Remember the all parts need some clearance to fit properly.</p>\n\n<p>Few years ago (10 years) I was working as Quality Engineer and some Design Engineers were complaining about a Dupont pin was not fitting on the PCB so they told me that I need to force the PCB manufacturing to increase the holes to the higher tolerance. Which I had to ask him firstly the pin size and told me 0.70 mm and hole size 0.80 and maximum 0.90 \n - hmmm and maximum size of the pin? I asked, and they told me proudly 0.78mm so the part will fit perfectly.\n - Oh, so one square pin of 0.78mm will fit on one hole of 0.9mm, but what about the diagonal dimension? if the pin on the higher dimension is close to 1.2mm.</p>\n\n<p>Imagine what happened later, engineering changes and modifying other designs due improper tolerances. pro-engineer software was unable to calculate what the designers needed.</p>\n\n<p>Material has shrinkage, warpage, and other conditions that is needed to know like malleability and hardness and some of this data is on the specification material or the part specification.</p>\n'}, {'answer_id': 5187, 'author': 'John Deters', 'author_id': 7197, 'author_profile': 'https://3dprinting.stackexchange.com/users/7197', 'pm_score': 3, 'selected': False, 'text': "<p>It helps to understand the different aspects of dimensions, so you can use the terminology correctly.  This will help you define your variables in OpenSCAD with correct names.  (Tolerance is the wrong term to use.)  And once you have correct names, you'll understand how to specify the dimensions in OpenSCAD.</p>\n\n<ul>\n<li><em>Tolerance</em> is the amount of random deviation or variation permitted for a given dimension.</li>\n<li><em>Allowance</em> is a planned difference between a nominal or reference value and an exact value.  </li>\n<li><em>Clearance</em> is the intentional space between two parts.</li>\n<li><em>Interference</em> is the intentional overlap between two parts. </li>\n</ul>\n\n<p>Two other terms </p>\n\n<ul>\n<li><em>Accuracy</em> is the maximum dimensional variation between parts.  A machine cannot produce parts with a tighter tolerance than its accuracy.</li>\n<li><em>Precision</em> is the size of the steps your machine is capable of. It is often confused with accuracy.</li>\n</ul>\n\n<p>In your case you need to define the <em>allowance</em> in order to create the <em>clearance</em> you desire.</p>\n\n<p>To design your 5&nbsp;mm pin and 5&nbsp;mm hole, you need to understand your machine's accuracy.  The printer could print the pin larger than 5&nbsp;mm or smaller than 5&nbsp;mm.  Or it could print the hole larger than 5&nbsp;mm or smaller than 5&nbsp;mm.  You'll need to print some pins and holes and measure the differences between what you defined and what you printed.  The difference between the largest and smallest measurements you take is your machine's accuracy.  And be sure to check the accuracy in your X, Y, and Z dimensions; your printer might have a difference between them that would impact the roundness of the parts.  </p>\n\n<p>Let's say that your printer's measured accuracy is &plusmn;&nbsp;0.2&nbsp;mm.</p>\n\n<p>Then, we move to clearance.  What is the minimum gap between parts you are looking for, and what is the maximum you can accept?</p>\n\n<p>Let's say you want a clearance of at least 0.2&nbsp;mm between the pin and hole, but no more than 1.0&nbsp;mm.  Since your accuracy is &plusmn; 0.2&nbsp;mm, your pin will be 5.0&nbsp;&plusmn;&nbsp;0.2&nbsp;mm, so the hole must therefore be 5.6&nbsp;mm&nbsp;&plusmn;&nbsp;0.2&nbsp;mm.  The minimum tolerance condition would be an minimum sized hole (5.4&nbsp;mm) and a maximum sized pin (5.2&nbsp;mm); the maximum tolerance would be a maximum sized hole (5.8&nbsp;mm) and a minimum sized pin (4.8&nbsp;mm).</p>\n\n<p>Note that a clearance of 1.0&nbsp;mm might be too sloppy for your application.  You might think to tighten the tolerances to 0.05&nbsp;mm in order to reduce the clearance.  But if your printer can't produce a part that meets your specified tolerances, you would need to find a different way to manufacture or finish the parts.</p>\n"}, {'answer_id': 5398, 'author': 'Nick Dancer', 'author_id': 9835, 'author_profile': 'https://3dprinting.stackexchange.com/users/9835', 'pm_score': 2, 'selected': False, 'text': '<p>I’m not aware of a standard in Openscad but I can share what I have done in the past.</p>\n\n<pre><code>cutoutActualDiameter = 10;\ncutoutDiameterClearance = 0.1;\ncutoutDiameter = cutoutActualDiameter + cutoutDiameterClearance;\n</code></pre>\n\n<p>I know this is verbose but unfortunately in my experience that is a necessity if you want your code to be maintainable in Openscad. The benefit of this is that up until you need any clearance added you can just use the variable <code>cutoutDiameter</code>and then if and when you do need clearance added you can rename that variable and you don’t have to change any of the code where the variable is consumed.</p>\n\n<p>Would love to hear how others manage this though.</p>\n'}]
['https://3dprinting.stackexchange.com/questions/5151', 'https://3dprinting.stackexchange.com', 'https://3dprinting.stackexchange.com/users/9134/']