using Autodesk.AutoCAD.DatabaseServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
namespace TemplateLayoutListSample
{
public partial class Dialog : Window, INotifyPropertyChanged
{
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChanged
?.Invoke(this,
new PropertyChangedEventArgs
(propertyName
)); }
#endregion
private IEnumerable<string> layouts;
private string fileName, layout;
public string FileName
{
get { return fileName; }
private set
{
fileName = value;
RaisePropertyChanged("FileName");
Layouts = GetLayouts();
Layout = "Model";
}
}
public IEnumerable<string> Layouts
{
get { return layouts; }
private set { layouts = value; RaisePropertyChanged("Layouts"); }
}
public string Layout
{
get { return layout; }
set { layout = value; RaisePropertyChanged("Layout"); }
}
public Dialog()
{
InitializeComponent();
DataContext = this;
FileName = HostApplicationServices.WorkingDatabase.Filename;
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
var openFileDialog
= new Microsoft.Win32.OpenFileDialog(); openFileDialog.Filter = "AutoCAD files (*.dwg)|*.dwg|Drawing Template (*.dwt)|*.dwt";
var result = openFileDialog.ShowDialog();
if (result.HasValue && result.Value)
{
FileName = openFileDialog.FileName;
}
}
private void OK_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private IEnumerable<string> GetLayouts()
{
using (var db
= new Database
(false,
true)) {
db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
using (var tr = db.TransactionManager.StartOpenCloseTransaction())
{
var layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
foreach (var entry in layoutDict)
{
yield return entry.Key;
}
}
}
}
}
}