TurboCAD Forums

The Ultimate Resource for TurboCAD Knowledge

Register
 
The purpose of these forums is to discuss TurboCAD.  Please keep the discussion of other products to compatibility issues or how the other software’s can be used with TurboCAD.


VBA - Draw a Square Spline
Read 4319 times
* September 23, 2009, 07:40:10 AM
Check out this routine to draw a square spline.  You need textbox fields for # Teeth, OD, ID, and face angle (0 = square).  Now, anyone know how to make 'Join Polyline' work from VBA so I can join the segments automatically?  Right now I join them manually then extrude.


    Private Sub MakeGearBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeGearBtn.Click
        Dim TCApp As IMSIGX.Application
        Dim TCDraw As IMSIGX.Drawing
        Dim TCGraphics As IMSIGX.Graphics
        Dim OutputLayer As IMSIGX.Layer

        Dim iStatus As Integer, i As Integer
        Dim OD As Double, ID As Double, PA As Double, NumTeeth As Double
        Dim l12 As Double
        Dim phi1 As Double, phi2 As Double, dTheta As Double, theta As Double, tmp As Double
        Dim x0 As Double, y0 As Double, x1 As Double, y1 As Double
        Dim x2 As Double, y2 As Double, x3 As Double, y3 As Double


        StatusLbl.Text = "Status:  Drawing Spline"
        On Error Resume Next
        TCApp = GetObject(, "TurboCAD.Application")
        If TCApp Is Nothing Then
            TCApp = CreateObject("TurboCAD.Application")
            If TCApp Is Nothing Then
                StatusLbl.Text = "Status: Cannot Acces TurboCAD"
                Exit Sub
            End If
        End If
        TCApp.Visible = True
        If TCApp.Drawings.Count < 1 Then
            TCDraw = TCApp.Drawings.Add("Drawing1")
            If TCDraw Is Nothing Then
                StatusLbl.Text = "Status: Cannot Create a Drawing"
                Exit Sub
            End If
        Else
            TCDraw = TCApp.ActiveDrawing
        End If
        TCGraphics = TCDraw.Graphics
        OutputLayer = TCDraw.Layers("VBAOut")
        If OutputLayer Is Nothing Then
            OutputLayer = TCDraw.Layers.Add("VBAOut")
        End If
        OutputLayer.Clear()

        OD = ODTbx.Text
        ID = IDTbx.Text
        PA = (180 - PATbx.Text) * Math.PI / 180
        NumTeeth = NumTeethTbx.Text
        dTheta = 2 * Math.PI / NumTeeth

        ' Distance along face
        l12 = ID * Math.Cos(PA) + Math.Sqrt(OD ^ 2 - (ID * Math.Sin(PA)) ^ 2)
        phi1 = Math.Acos((OD ^ 2 + ID ^ 2 - l12 ^ 2) / (2 * OD * ID))
        phi2 = (dTheta - 2 * phi1) / 2

        x0 = 0
        y0 = ID
        theta = 0
        For i = 0 To (NumTeeth - 1)
            x1 = OD * Math.Cos(Math.PI / 2 - (theta + phi1))
            y1 = OD * Math.Sin(Math.PI / 2 - (theta + phi1))
            OutputLayer.AddLineSingle(x0, y0, 0, x1, y1, 0)
            x2 = OD * Math.Cos(Math.PI / 2 - (theta + phi1 + phi2))
            y2 = OD * Math.Sin(Math.PI / 2 - (theta + phi1 + phi2))
            OutputLayer.AddLineSingle(x1, y1, 0, x2, y2, 0)
            x3 = ID * Math.Cos(Math.PI / 2 - (theta + 2 * phi1 + phi2))
            y3 = ID * Math.Sin(Math.PI / 2 - (theta + 2 * phi1 + phi2))
            OutputLayer.AddLineSingle(x2, y2, 0, x3, y3, 0)
            theta = theta + dTheta
            x0 = ID * Math.Cos(Math.PI / 2 - theta)
            y0 = ID * Math.Sin(Math.PI / 2 - theta)
            OutputLayer.AddLineSingle(x3, y3, 0, x0, y0, 0)
        Next

        StatusLbl.Text = "Status: Ready"
    End Sub

Logged


* September 23, 2009, 11:51:30 AM
#1
re: ...anyone know how to make 'Join Polyline' work from VBA so I can join the segments automatically?  Right now I join them manually then extrude.

I know nothing about the SDK, but would it not be better to use the "Use Compound Profile" feature in the Simple Extrude tool, rather than the Join Polyline tool? Is "Use Compound Profile" even available, since it's turned Off in certain editions? 


Logged
John R.

V17—V21, 2015, 2016, 2017
Designer, Deluxe, Basic, Platinum
RedSDK enabled
Windows 10, 64-bit