LinetypeDialog with Linetypes from db.ReadDwgFile?

LinetypeDialog with Linetypes from db.ReadDwgFile?

doleischLG4UV
Enthusiast Enthusiast
440 Views
4 Replies
Message 1 of 5

LinetypeDialog with Linetypes from db.ReadDwgFile?

doleischLG4UV
Enthusiast
Enthusiast

Hello, good afternoon everyone,

 

is it possible to use the LinetypeDialog, e.g.

 

LinetypeDialog dlgLinetype = new LinetypeDialog();
dlgLinetype.ShowDialog();
ObjectId idLinetype = dlgLinetype.Linetype;

 

not in the context of the currently open dwg, but in relation to a dwg whose database is open in the background as follows?

 

db.ReadDwgFile(strFileName, FileOpenMode.OpenForReadAndAllShare, false, null)

 

What I'm wanting to get at is that in the LinetypeDialog the linetypes of the dwg are read, which is opened via db.ReadDwgFile.

 

Many thanks for an answer.

 

 

0 Likes
Accepted solutions (2)
441 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

To be able to show the LinetypeDialog from a side database, you have to this side database as working database (do not forget to restore the previous working database).

var previousDatabase = HostApplicationServices.WorkingDatabase;
try
{
    using (var sideDatabase = new Database(false, true))
    {
        sideDatabase.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
        HostApplicationServices.WorkingDatabase = sideDatabase;
        var dialog = new LinetypeDialog();
        dialog.ShowDialog();
        // ...
    }
}
finally
{
    HostApplicationServices.WorkingDatabase = previousDatabase;
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

doleischLG4UV
Enthusiast
Enthusiast

 

Hi Gilles,

 

Thank you very much for your quick and helpful reply and the example shown. 👌

 

It worked for me exactly as you listed.

 

Have a nice evening and a nice weekend! 😊

0 Likes
Message 4 of 5

ActivistInvestor
Mentor
Mentor
Accepted solution

Instead of having to remember to restore the previous working database, and because having to temporarily change the working database is fairly-common, you can use the simple helper class and extension method below to simplify your application code and reduce the chances of bugs creeping in. The helper classes exploit the Disposable pattern to avoid the need to have to remember to undo something that your code does when its finished.

 

Example code like that posted by @_gile is good for helping someone understand the basic API usage, but it would be a mistake to misinterpret example code snippets as being  good examples of how you should design and write real code, because example code snippets are usually very poor examples of that.

 

So, taking a more-holistic approach using the helper classes below, your code would become:

 

using(Database db = new Database(false, true))
{
   db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
   using(db.AsWorkingDatabase())
   {
      var dialog = new LinetypeDialog();
      dialog.ShowDialog();
      /// ....
   }          // the previous working database is restored here
}

 

The reusable helper classes:

 

public class WorkingDatabase : IDisposable
{
   Database previous;

   public WorkingDatabase(Database newWorkingDb)
   {
      if(newWorkingDb == null)
         throw new ArgumentNullException(nameof(newWorkingDb));
      Database current = HostApplicationServices.WorkingDatabase;
      if(newWorkingDb != current)
      {
         previous = current;
         HostApplicationServices.WorkingDatabase = newWorkingDb;
      }
   }

   public void Dispose()
   {
      if(previous != null)
      {
         HostApplicationServices.WorkingDatabase = previous;
         previous = null;
      }
   }
}

public static partial class DatabaseExtensions
{
   public static IDisposable AsWorkingDatabase(this Database db)
   {
      return new WorkingDatabase(db);
   }
}

 

0 Likes
Message 5 of 5

doleischLG4UV
Enthusiast
Enthusiast

 

Thank you very much for your optimized solution to use general helper classes in such a case and to improve the code design. This is very helpful for me. 

 

Best regards  😊

 

0 Likes