Author Topic: Import Layerstate - Exception eUndefinedLineType  (Read 2273 times)

0 Members and 1 Guest are viewing this topic.

Hackysack

  • Guest
Import Layerstate - Exception eUndefinedLineType
« on: October 23, 2014, 08:07:35 AM »
Hi!

I'm trying to import a layerstate with .Net - fortunately, the AutoCAD help file provides a helpful example ('Export and Import Saved Layer States (.NET)') But I always get an eUndefinedLineType exception.

Here is my subfunction:
Code - C#: [Select]
  1.         public static void ImportLayerState(Database db, String strPathToLayerState)
  2.         {
  3.             LayerStateManager lyrStMan = db.LayerStateManager;
  4.  
  5.             if (System.IO.File.Exists(strPathToLayerState))
  6.             {
  7.                 try
  8.                 {
  9.                     lyrStMan.ImportLayerState(strPathToLayerState);
  10.                 }
  11.                 catch (Autodesk.AutoCAD.Runtime.Exception ex)
  12.                 {
  13.                     Application.ShowAlertDialog(ex.Message);
  14.                 }
  15.             }
  16.         }

Has anybody an idea what should change to avoid the exception? My workaround in the moment is to use an empty catch {} Block, which just looks nasty to me :D

Thanks in advance!

EDIT: I'm using Visual Studio Express 2013, Autcad 2013 on Win7 x64

n.yuan

  • Bull Frog
  • Posts: 348
Re: Import Layerstate - Exception eUndefinedLineType
« Reply #1 on: October 23, 2014, 10:05:08 AM »
Rather than trying to find code trick to avoid the exception, you'd better try to find out why it happens. The exception type is not that clueless: undefined line type.

Is the the layer state file (*.las) exported from the same drawing file you are now trying to import into? I'd guess it is not. What happened is that the layer state file was exported from a drawing which has some line types loaded, but one or more of those line types are not loaded into the drawing the layer state file is to be imported into. That is, prior to import the *.las file, all the line types referred in the *.las file must have been loaded into the drawing.

If you do a manual import, AutoCAD would prompts you the same line type issue. However, AutoCAD's build-in import command allows you to continue in spite of the line type issue, and use "continuous" line type of replace all the missing line types.

Obviously, try..catch.. will not solve your problem: it only stops the importing when first layer state with missing line type is reached. That means any layer state after that is not imported.

Should the .NET API's LayerStateManager.ImportLayerState() provide a chance to handle missing line type when each layer's state is read in (for example, by supplying an argument of alternate line type name in the method), then we would easily achieve what AutoCAD's built-in layer state import command does.

One possible solution I can see is to read through the *.las file before importing (it is just a plain text file) to find line types in the *.las file and compare them to the line types loaded into the drawing. Once you find the difference, you can either load all the missing line types into the drawing before importing *.las file, or substitute the all the missing line types in the *.las file with one of the line types existing in the drawing (meaning you need to update the *.las file before importing it).
« Last Edit: October 23, 2014, 10:08:36 AM by n.yuan »

Hackysack

  • Guest
Re: Import Layerstate - Exception eUndefinedLineType
« Reply #2 on: October 23, 2014, 11:48:28 AM »
Wow, thanks for your fast and detailed reply! I will experiment with your suggestions and report back soon! :)