Initialization code in your F# AutoCAD application

Back from a nice long weekend, although I spent most of it sick with a cold. I find this increasingly the way with me: I fend off illness for months at a time (probably through stress, truth be told) but then I get a few days off and wham. A shame, as we had a huge dump of snow over the weekend... we get white Christmases here every five years or so, but it's really uncommon to get a white Easter.

I had a very interesting question come in by email from 冷血儿, who wanted to get the technique shown in this post working in his F# application.

Here's the F# code I managed to put together after consulting hubFS, in particular:

#light

namespace MyNamespace

open Autodesk.AutoCAD.Runtime

open Autodesk.AutoCAD.ApplicationServices

type InitTest() =

  class

    let ed =

      Application.DocumentManager.MdiActiveDocument.Editor

    interface IExtensionApplication with

      member x.Initialize() =

        ed.WriteMessage

          ("\nInitializing - do something useful.")

      member x.Terminate() =

        printfn "\nCleaning up..."

  end

end

module MyApplication =

  let ed =

    Application.DocumentManager.MdiActiveDocument.Editor

  [<CommandMethod("TST")>]

  let f () =

    ed.WriteMessage("\nThis is the TST command.")

  [<assembly: ExtensionApplication(type InitTest)>]

  do

    ed.WriteMessage("\nModule do")

Here's what happens when we load our module and run the TST command:

Command: NETLOAD

Module do

Initializing - do something useful.

Command: TST

This is the TST command.

Leave a Reply

Your email address will not be published. Required fields are marked *