TurboCAD VBA Crash Course in 60 Seconds first created 21/09/05 - last modified 22/09/05 Page Author: Ty Harness
All the Pro versions above 6.5 of TurboCAD can use Visual Basic for Applications. If you've had any experience with VB then you'll find it very easy to learn. VBA allows you to access objects created by the application i.e. TurboCAD. For example you can programmatically draw a line by passing the start point and end point into the addsingleline() function. I think V9 introduced a VBA macro recorder of key strokes - personally I don't like it, but it's OK to string a few button presses together.


If you do not have any experience with VB or any other high level language then take a look at the 'VB in 60 seconds' course
Unfortunately by default the typical setup of TC does not install the VBA SDK. To find out if you have VBA installed:
  • Run Turbocad and choose a new drawing
  • Tools - VBA Macros
  • and you should see the options VBA Editor and Macros.

    text here
    Figure 1


    If you don't see the above you'll need to run your TC setup again and modify the setup by checking the VBA SDK - it's worth installing the examples as well because you'll learn a lot from them.

    text here
    Figure 2


    How to run an example macro:

  • Tools
  • VBA Macros
  • Macros
  • Project1 should be highlighted. Now change the project name to anything (I know it doesn't make any sense)
  • Add - the default TC macros folder should open
  • Choose (say) gear.tcm
  • Close the macros dialog


  • You should then see a new tabbed dialog appear in the editor called VBA Macros. You can then see any public subroutine names that will run the macro. For the gear macro it's called CreateGear. A macro may have 1 or more subroutines to call, which in turn may call a userform (a modal dialog) or even a dll function.

    text here
    Figure 3


    Repeat the procedure if you need 2 or more macros in the same drawing. When you reopen your TC drawing the macros will be loaded automatically - if you pass your drawing to a 3rd party and they don't have macro installed TC won't fall over but if the macro is an essential you'll have to send the tcm and explain how to copy it into the macros path or they just load it with the above procedure. The TCM files are compiled binary files and cannot be read in a text editor - we've all seen and had an MS Word virus - do you trust the sender. I like to supply the source code, but that means you need to know how to create a VBA macro. Tip: Save you're drawing as a template if you need to load a macro with every new drawing.

    text here
    Figure 4


    Let's create a new VBA macro - start a new drawing.
  • Tools
  • VBA Macros
  • Macros
  • Over type the project name with (say) helloworld
  • Press New this time
  • Press Editor
  • You should see the helloworld project in the Project Pane
  • Insert Module
  • From the properties pane rename the Module to (say) hellomodule


  • text here
    Figure 5


    text here
    Figure 6/i>


    now copy and paste the functions below into the VBA editor


    Sub helloworld() MsgBox "Hello World" Application.ActiveDrawing.Graphics.AddText "Hello World", 0, 0, 0, 10 ActiveDrawing.ActiveView.ZoomToExtents End Sub


    text here
    Figure 7


  • File - Save helloworld
  • Minimize the VBA editor

  • You should now see the tabbed dialog appear at the forefront of TC and press helloworld.

    text here
    Figure 8


    Note it is possible to run a subroutine from the VBA editor, but it will execute the code and return to the editor so either file and close to TC or minimize the editor and run you macro from the tabbed dialog. If you close your tabbed dialog then you can still see the macros from the Tools - macros menu. The tabbed dialog will return when you start a new drawing.

    If your program errors then the VBA editor will highlight the point where program was halted - you'll need press Reset (from the Run menu) and then correct the error before attempting to execute the macro again

    Let's briefly look at the code and rewrite it a little. The below example contains comments which should make the code easier to follow and maintain (I'm normally lazy when it comes to comments and documentation. I hope you do as I say and not as I do).

    Sub helloworldagain() 'remarks/comments are made after the apostrophe Dim App As Application 'TurboCAD Top level object Dim drg As Drawing 'Where all the business will be done Dim t As Graphic 'We'll use this to store some text Dim l As New Graphic 'We'll use this to store a line (New is optional) 'too many ways of doing the same thing in VB Dim BB As BoundingBox 'Special helper objects Dim s As String 'Conventional VB string 256 chars s = InputBox("Hello", "My first app", "type your name here") 'Conventional VB Set App = IMSIGX.Application 'setting a reference (not a new object) Set drg = App.ActiveDrawing 'just saves us from typing it out all the time Set t = drg.Graphics.AddText(s, 0, 10, 0, 10) 'this will add a new graphic to the drawing t.Properties("PenColor").Value = RGB(255, 0, 0) 'now we set the properties - layer,color etc Set BB = t.CalcBoundingBox 'Get TC to calculate the bounding box 'Because I used new in the Dim statement I don't need to use Set l = drg.Graphics.AddLineSingle(BB.Min.X, BB.Min.Y, 0, BB.Max.X, BB.Min.Y, 0) ' adding a line graphic l.Properties("Penstyle").Value = "DASHED" 'the properties are well documented l.Properties("Pencolor").Value = RGB(0, 0, 255) 'or you could probe them with code drg.ActiveView.ZoomToExtents 'the drawing also has properties 'if you've created and object or reference to an object then you get rid of it Set l = Nothing Set BB = Nothing Set t = Nothing Set drg = Nothing Set App = Nothing End Sub



    text here
    Figure 9


    I guess if you're new to VBA the above example looks a little complex. But that's it in a nutshell - you can connect to TC and use its objects. Learn a few more TC objects and bit more conventional VB like for next,loop until, arrays etc then add a dose of mathematics and hopefully you can automate many tedious drawing tasks.

    I'm working on the Advanced TC VBA in 60 Seconds web page at the moment. How to use and probe more complex objects for undocumented methods and properties plus working in a 3D world.

    TC bundles a score of good VBA examples and there are several good sources on the web. 99/100 you'll just need to tweak those macros to suit your problem.

    You've seen those books out-there Teach yourself D++# in 1 hour which I always believed should be against the trades descriptions act. When I say crash VBA in 60 seconds I mean it is possible to crash TC out. I've never caused TC any harm yet, but remember to save your drawing before trying anything too wild.

    Ty Harness

    [Home] [Mechanics] [Welding] [Software] [CAD] [Math]

    Tested in Firefox 1.06: FireFox homepage