Code:
#region Using directivesusing System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.DatabaseServices;
#endregion
namespace RSNNApplications.Utils.BlockList
{
partial class BlockList : Form
{
ArrayList BlockNameAndID = new ArrayList();
public BlockList()
{
InitializeComponent();
SetBlockNames();
}
public string BlockName
{
set
{
cmbBlockName.Text = value;
}
get
{
return (string)cmbBlockName.Text;
}
}
public ObjectId BlockID
{
get
{
System.Collections.IEnumerator myEnumerator = BlockNameAndID.GetEnumerator();
while (myEnumerator.MoveNext())
{
BlockNameID tmpBlockNameID = myEnumerator.Current as BlockNameID;
if ( tmpBlockNameID != null )
{
if ( tmpBlockNameID.BlockName == (string)cmbBlockName.Text )
{
return (ObjectId)tmpBlockNameID.BlockID;
}
}
}
return (ObjectId)cmbBlockName.SelectedValue;
}
}
private void SetBlockNames()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
using (BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false))
{
foreach (ObjectId blockId in bt)
{
using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead, false))
{
if (btr.IsLayout == false && btr.IsAnonymous == false)
{
cmbBlockName.Items.Add(btr.Name);
//Autodesk.AutoCAD.ApplicationServices.CommandLinePrompts.Message(String.Format("\n{0} - {1}", btr.Name, blockId.ToString()));
BlockNameAndID.Add(new BlockNameID(btr.Name, blockId));
}
btr.Close();
}
}
bt.Close();
tr.Commit();
}
}
}
public class BlockNameID
{
private string myBlockName;
private ObjectId myBlockID;
public BlockNameID(string strBlockName, ObjectId strBlockID)
{
this.myBlockName = strBlockName;
this.myBlockID = strBlockID;
}
public string BlockName
{
get
{
return myBlockName;
}
}
public ObjectId BlockID
{
get
{
return myBlockID;
}
}
public override string ToString()
{
return this.BlockName + " - " + this.BlockID;
}
}
}