More F# Adventures: You win some; you lose some.

I just about have the second part of the manipulating polylines series complete.  Unfortunately I had to attend to some less than blog worthy aspects of the job the first part of the week and I haven’t quite got it finalized.  I did have one interesting item pop up.

A user came to me with an ACA project where the fields in the titleblocks would not update.  I checked a few things and discovered that the FIELDEVAL system variable was set to only update fields in the event of a category 5 tornado colliding with a 100 foot tidal wave on Friday the 13th.  The project had 60 some odd sheets that needed updating and was due at the end of the day, so I said I’d take care of it.  My first thought was to run a simple LISP routine to batch process the sheets.  But I had a little time and thought I’d try to write a quick F# command to do it.

It didn’t take long to write and it processed all the files in a matter of minutes.  I learned how to combine .NET Enums and I dipped into the Array module for the first time.  There was only one tiny problem.  The function didn’t update the FIELDEVAL variable.  It opened them all and saved them too.  It just didn’t make the change…

I’ll investigate why after I finish the polyline series, or when I start loosing sleep thinking about it.

One word of caution, there is no error checking in this.  It’s blindly opening and saving files and I don’t recommend this unless you have a completely controlled environment, and even then, user beware.

   1: #light
   2:  
   3: module BobbyCJones.BatchFieldUpdate
   4:  
   5: open Autodesk.AutoCAD.ApplicationServices
   6: open Autodesk.AutoCAD.Runtime
   7: open Autodesk.AutoCAD.Windows
   8: open System.Windows.Forms
   9:  
  10: [<CommandMethod("UpdateFields", CommandFlags.Modal ||| CommandFlags.Session)>]
  11: let UpdateFields () =
  12:   let fileDialog = new Autodesk.AutoCAD.Windows.OpenFileDialog("Files to update", 
  13:                                                                 "", 
  14:                                                                 "dwg", 
  15:                                                                 "fields", 
  16:                                                                 OpenFileDialog.OpenFileDialogFlags.AllowMultiple)
  17:   
  18:   match fileDialog.ShowDialog() with
  19:   | DialogResult.OK ->
  20:     let UpdateFile fileName =
  21:         let doc = Application.DocumentManager.Open(fileName, false)
  22:                         
  23:         Application.SetSystemVariable("FIELDEVAL", 31)
  24:                 
  25:         fileName |> doc.CloseAndSave
  26:         
  27:     Array.iter UpdateFile (fileDialog.GetFilenames())
  28:  
  29:   | _ -> ()
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment