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(

            "\nC
annot 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.

4 responses to “Loading multiple linetypes into AutoCAD using .NET”

  1. Michael Csikos Avatar

    Hi Kean

    (I have just submitted an ADN request for this, but if you know the answer off the top of your head, I'd appreciate it, as it will save me some time.)

    For a global configuration option in our software, the user needs to be able to select a linetype for use in other new drawings. The linetype could have been loaded from a file. Is there a way to know what file the selected linetype has been loaded from? Is the linetype's name and filename sufficient information to programmatically load the selected linetype into a new drawing?

    Kind regards
    Michael

  2. Hi Michael,

    Nothing off the top of my head (nor with a little research), I'm afraid.

    I'm sure someone on my team will have a better answer for you, soon.

    Best regards,

    Kean

  3. hey these are prdefined....by software.thereb is no any file it just load the files

  4. You do not have to use HostApplicationServices.Current.FindFile. From the doc:

    > If filename is not a URL and does not contain a path, then the AutoCAD library search path is searched for the file.

    So you can simply do:

    db.LoadLineTypeFile("*", "acad.lin");

Leave a Reply to Kean Walmsley Cancel reply

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