October 2014

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  










« Using a DrawJig from F# to create Spirograph patterns in AutoCAD | Main | Call for proposals now open for Autodesk University 2010 »

March 16, 2010

Loading multiple linetypes into AutoCAD using .NET

I received this question by email from Chris Witt:

I know you can load specific linetypes with vb.net, but is there a way to load *all* line types from a specified lin file with vb.net without having to name each one?

This is an easy one, as the Database.LoadLineTypeFile() method supports wildcard characters in the linetype name.

Here’s some C# code that does this:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

 

namespace LoadLinetypes

{

  public class Commands

  {

    [CommandMethod("LL")]

    public void LoadLinetypes()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;

 

      const string filename = "acad.lin";

      try

      {

        string path =

          HostApplicationServices.Current.FindFile(

            filename, db, FindFileHint.Default

          );

        db.LoadLineTypeFile("*", path);

      }

      catch (Autodesk.AutoCAD.Runtime.Exception ex)

      {

        if (ex.ErrorStatus == ErrorStatus.FilerError)

          ed.WriteMessage(

            "\nCould not find file \"{0}\".",

            filename

          );

        else if (ex.ErrorStatus == ErrorStatus.DuplicateRecordName)

          ed.WriteMessage(

            "\nCannot load already defined linetypes."

          );

        else

          ed.WriteMessage(

            "\nException: {0}", ex.Message

          );

      }

    }

  }

}

For a VB.NET version (as that was specified in the question), please use one of the conversion tools in this previous post.

Now to see the LL command in action… Let’s start by calling the LINETYPE command, to check the initial state of a blank drawing:

No linetypes loaded (nothing up my sleeves)

If we re-check the loaded linetypes once we’ve run the LL command we see a much longer list:

Et voila - beaucoup de linetypes!

The code has been structured to catch exceptions, two of which might get hit quite easily: if the file has not been found (and we use HostApplicationServices.FindFile() to help get a full path to our file), then we’ll get an “eFilerError”. And if we run the LL command a second time we’ll get an “eDuplicateRecordName” exception.

blog comments powered by Disqus

Feed/Share

10 Random Posts