- [font="]'----------------------------------------------------------------------[/font]
- [font="]Sub Main()[/font]
- [font="] Begin Dialog TUBEMITERDIALOG 50,47, 160, 96, "Tube Miter"[/font]
- [font="] Text 4 ,12,120,12, "Diameter of intersecting tube (mm)"[/font]
- [font="] TextBox 120,12,25 ,12, .IDD_D1[/font]
- [font="] Text 4 ,24,120,12, "Diameter of mitered tube (mm)"[/font]
- [font="] TextBox 120,24,25 ,12, .IDD_D2[/font]
- [font="] Text 4 ,36,120,12, "included angle"[/font]
- [font="] TextBox 120,36,25 ,12, .IDD_PHI[/font]
- [font="] Text 4 ,48,120,12, "Offset (mm)"[/font]
- [font="] TextBox 120,48,25 ,12, .IDD_OFFSET[/font]
- [font="] OKButton 4,80,37,12[/font]
- [font="] CancelButton 45,80,37,12[/font]
- [font="] End Dialog[/font]
- [font="] Dim dlg As TUBEMITERDIALOG [/font]
- [font="] Dim ot As Long[/font]
-
- [font="] If(dcSelectAll) Then dcEraseSelObjs[/font]
-
- [font="] dcSetDrawingScale 25.4[/font]
- [font="] dcSetLineParms dcBLACK, dcSOLID, dcTHIN[/font]
- [font="] dcSetSplineParms dcBLACK, dcSOLID, dcTHIN[/font]
-
-
- [font="] dlg.IDD_D1 = "25.4"[/font]
- [font="] dlg.IDD_D2 = "25.4"[/font]
- [font="] dlg.IDD_PHI = "90"[/font]
- [font="] dlg.IDD_OFFSET= "0.0"[/font]
-
- [font="] Button = Dialog(dlg)[/font]
-
- [font="] If Button = -1 Then[/font]
- [font="] Make_TubeMiter dlg.IDD_d1/2,dlg.IDD_D2/2,rad(90 - dlg.IDD_PHI),dlg.IDD_OFFSET/2[/font]
- [font="] dcViewAll[/font]
- [font="] End If[/font]
- [font="]End Sub[/font]
-
- [font="]'----------------------------------------------------------------------[/font]
- [font="]' Square a value[/font]
- [font="]'----------------------------------------------------------------------[/font]
- [font="]Function square(ByVal x As Double)[/font]
- [font="] square = x * x [/font]
- [font="]End Function[/font]
-
- [font="]'----------------------------------------------------------------------[/font]
- [font="]' Convert from degrees into radians[/font]
- [font="]'----------------------------------------------------------------------[/font]
- [font="]Function rad(ByVal deg As Double)[/font]
- [font="] rad = (2*3.1415926535/360)*deg[/font]
- [font="]End Function[/font]
-
- [font="]'----------------------------------------------------------------------[/font]
- [font="]' Plot the tube miter[/font]
- [font="]' R1 = radius of intersecting tube[/font]
- [font="]' R2 = radius of intersected (cut) tube[/font]
- [font="]' Phi = included angle between tubes (in radians)[/font]
- [font="]' Offset = offset along the z axis between the tubes[/font]
- [font="]'[/font]
- [font="]'[/font]
- [font="]' The generalized equation for a cylinder of radius r about the x axis is 1 = (y/r)^2 + (z/r)^2[/font]
- [font="]' apply the transformation y = y'cos(phi) + x'sin(phi) to rotate the cylinder about the z axis by angle phi[/font]
- [font="]' this gives the equation 1 = ((y cos(phi) + x sin(phi))/r)^2 + (z/r)^2[/font]
- [font="]' now solve for x to get: x = (+- r*sqrt(1-(z/r)^2) - y cos(phi))/sin(phi)[/font]
-
- [font="]' we can now iterate over values of y and z to find the discreet x points that make up our curve and fit them with [/font]
- [font="]' a spline[/font]
-
- [font="]' so we'll find our y and z values by rotating around the mitered cylinder which is along the x axis. So we get:[/font]
- [font="]' y = R sin(alpha)[/font]
- [font="]' z = R cos(alpha) for alpha = 0 to 360 [/font]
- [font="]' actually let's not forget the offset, which applies only to the Z axis, so z = R cos(alpha) + Offset[/font]
- [font="]' we can then substitute these back into the above equation for x.[/font]
-
- [font="]' This X dimension is exactly what we want to plot, but the y parmeter for plotting needs to be the circumfrence [/font]
- [font="]' of the mitered cylinder. so Yplot= R * alpha (if alpha is in radians) [/font]
-
- [font="]' we also need to account for the fact that we support having a mitered cylinder larger than the intersecting cylinder[/font]
- [font="]' so there is some logic there to figure out when a value is valid and not. [/font]
-
- [font="]'---------------------------------------------------------------------- [/font]
- [font="]Sub Make_TubeMiter(ByVal R1 As Double,ByVal R2 As Double,ByVal Phi As Double,ByVal Offset As Double)[/font]
- [font="] Dim X As Double ' X(a)[/font]
- [font="] Dim s(361) As Double 'Array for the spline[/font]
- [font="] Dim i As Double 'i for for loop[/font]
- [font="] Dim start As Double 'start of a spline[/font]
- [font="] Dim sign As Double 'either 1 or -1 to determine the sign in the equation below[/font]
- [font="] Dim loopCount As Integer 'loop once if the intersecting tube is larger, twice if smaller[/font]
- [font="] Dim j As Integer[/font]
- [font="] Dim ya As Double[/font]
- [font="] Dim za As Double [/font]
- [font="] Dim alpha As Double[/font]
- [font="] Dim max As Double[/font]
-
- [font="] sign = -1[/font]
- [font="] loopCount = 1[/font]
- [font="] max = 0[/font]
-
- [font="] ' If the intersecting tube is smaller, we need to draw both sides[/font]
- [font="] ' of the intersection since it is making a hole though the tube, Thus we want to run through[/font]
- [font="] ' the loop twice. [/font]
-
- [font="] If (R2 + Offset > R1) Then [/font]
- [font="] loopCount = 2[/font]
- [font="] End If[/font]
-
- [font="] For j = 1 to loopCount [/font]
- [font="] start = -1[/font]
- [font="] For i=0 To 360 Step 2 'iterate 0 - 360 by 2 degree increments[/font]
- [font="] alpha = rad(i) 'we need everything in radians[/font]
- [font="] ya = R2*Sin(alpha)[/font]
- [font="] za = R2*Cos(alpha)+Offset[/font]
-
- [font="] 'if there is something to cut here[/font]
- [font="] If square(za/R1) <= 1 Then[/font]
- [font="] X= (R1 * sign * Sqr(1 - square(za/R1)) - ya * Sin(Phi))/Cos(Phi) ' from the equation derived int he comments above[/font]