AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Slotted Holes.

Slot Diagram

This application will draw slotted holes as per the above diagram.
It begins by asking the user for an insertion point, the slot length, and then the slot diameter using the VBA Get functions.
It then uses the PolarPoint function to calculate the other points required to draw the slot. Then, using the AddLine and AddArc command, it draws the
slotted hole.

Note how we declare 'pi' as a Constant and construct a function to convert Degrees to Radians. 


Open a new module and add this coding to it : 
Const pi = 3.14159
'create Pi as a constant
'This function converts Degrees to Radians Function dtr(a As Double) As Double dtr = (a / 180) * pi End Function

Sub Slot() 'define the function Dim InsertPoint As Variant Dim SlotLength As Double Dim SlotDia As Double Dim Prompt1 As String Dim Prompt2 As String Dim Prompt3 As String Dim pt1 As Variant Dim pt2 As Variant Dim pt3 As Variant Dim pt4 As Variant Dim pt5 As Variant Dim pt6 As Variant Dim pt7 As Variant Dim LineObj As AcadLine Dim ArcObj As AcadArc 'declare all variables Prompt1 = vbCrLf & "Insertion Point : " 'store the prompt InsertPoint = ThisDrawing.Utility.GetPoint(, Prompt1) 'get the insertion point Prompt2 = vbCrLf & "Slot Length : " 'store the prompt SlotLength = ThisDrawing.Utility.GetReal(Prompt2) 'get the slot length Prompt3 = vbCrLf & "Slot Diameter : " 'store the prompt SlotDia = ThisDrawing.Utility.GetReal(Prompt3) 'get the slot diameter pt1 = ThisDrawing.Utility. _ PolarPoint(InsertPoint, dtr(270#), SlotDia / 2) pt2 = ThisDrawing.Utility. _ PolarPoint(pt1, dtr(180#), SlotLength / 2) pt3 = ThisDrawing.Utility. _ PolarPoint(pt2, dtr(90#), SlotDia) pt4 = ThisDrawing.Utility. _ PolarPoint(pt3, dtr(0#), SlotLength) pt5 = ThisDrawing.Utility. _ PolarPoint(pt4, dtr(270#), SlotDia) pt6 = ThisDrawing.Utility. _ PolarPoint(InsertPoint, dtr(180#), SlotLength / 2) pt7 = ThisDrawing.Utility. _ PolarPoint(InsertPoint, dtr(0#), SlotLength / 2) 'calculate all the points using the PolarPoint Function Set LineObj = ThisDrawing.ModelSpace. _ AddLine(pt1, pt2) Set LineObj = ThisDrawing.ModelSpace. _ AddLine(pt3, pt4) Set LineObj = ThisDrawing.ModelSpace. _ AddLine(pt5, pt1) Set ArcObj = ThisDrawing.ModelSpace. _ AddArc(pt6, SlotDia / 2, dtr(90), dtr(270)) Set ArcObj = ThisDrawing.ModelSpace. _ AddArc(pt7, SlotDia / 2, dtr(270), dtr(90)) 'Draw the Slotted Hole End Sub

 


Now, let's add a dialogue box to our application to streamline the user input :

Slot Dialogue Box

ScrollBar Properties :

 

  • Max = 100
  • Min = 1
  • Value = 50
  • SmallChange = 1
  • LargeChange = 5

TextBox1 :

 

  • Value = 50

TextBox1 :

 

  • Value = 20

 


Open a new module and add this coding to it : 
Const pi = 3.14159
'create Pi as a constant

'This function converts Degrees to Radians Function dtr(a As Double) As Double dtr = (a / 180) * pi End Function
Sub Slot1() 'define the function UserForm1.Show 'display the dialogue box End Sub

 


Now, under the Click Event for CommandButton1, add this coding : 
Private Sub CommandButton1_Click()
    Dim InsertPoint As Variant
    Dim SlotLength As Double
    Dim SlotDia As Double
    Dim Prompt1 As String
    Dim pt1 As Variant
    Dim pt2 As Variant
    Dim pt3 As Variant
    Dim pt4 As Variant
    Dim pt5 As Variant
    Dim pt6 As Variant
    Dim pt7 As Variant
    Dim LineObj As AcadLine
    Dim ArcObj As AcadArc
    'declare all variables
    
    SlotLength = TextBox1.Value
    'retrieve the Slot Length
    
    SlotDia = TextBox2.Value
    'retrieve the Slot Diameter
    
    UserForm1.Hide
    'hide the dialogue box
    
    Prompt1 = vbCrLf & "Insertion Point : "
    'store the prompt
    
    InsertPoint = ThisDrawing.Utility.GetPoint(, Prompt1)
    'get the insertion point
          
    pt1 = ThisDrawing.Utility. _
    PolarPoint(InsertPoint, dtr(270#), SlotDia / 2)
    pt2 = ThisDrawing.Utility. _
    PolarPoint(pt1, dtr(180#), SlotLength / 2)
    pt3 = ThisDrawing.Utility. _
    PolarPoint(pt2, dtr(90#), SlotDia)
    pt4 = ThisDrawing.Utility. _
    PolarPoint(pt3, dtr(0#), SlotLength)
    pt5 = ThisDrawing.Utility. _
    PolarPoint(pt4, dtr(270#), SlotDia)
    pt6 = ThisDrawing.Utility. _
    PolarPoint(InsertPoint, dtr(180#), SlotLength / 2)
    pt7 = ThisDrawing.Utility. _
    PolarPoint(InsertPoint, dtr(0#), SlotLength / 2)
    'calculate all the points using the PolarPoint Function
    
    Set LineObj = ThisDrawing.ModelSpace. _
    AddLine(pt1, pt2)
    Set LineObj = ThisDrawing.ModelSpace. _
    AddLine(pt3, pt4)
    Set LineObj = ThisDrawing.ModelSpace. _
    AddLine(pt5, pt1)
    Set ArcObj = ThisDrawing.ModelSpace. _
    AddArc(pt6, SlotDia / 2, dtr(90), dtr(270))
    Set ArcObj = ThisDrawing.ModelSpace. _
    AddArc(pt7, SlotDia / 2, dtr(270), dtr(90))
    'Draw the Slotted Hole
End Sub

Private Sub CommandButton2_Click()
    End
End Sub

Private Sub ScrollBar1_Change()
    TextBox1.Value = ScrollBar1.Value
    'set the value of the text box
    'to the value of the scrollbar
    
End Sub


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ScrollBar1.Value = TextBox1.Value
    'set the value of the scrollbar
    'to the value of the text box
    
    End Sub 

If you would like to download the source code for this Application/s, Then click Here
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website