Hi, mkh927!
[code]
=========Beginning of the citation==============
using System ;
using System.Collections ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.ApplicationServices ;
using Autodesk.AutoCAD.DatabaseServices ;
using Autodesk.AutoCAD.EditorInput ;
[assembly: CommandClass(typeof(ClassLibrary.Class))]
namespace ClassLibrary
{
public class Class
{
public static ObjectId m_CreateBlock(string m_blockname, Entity[] m_ent)
{
ObjectId m_blockid = new ObjectId();
Database m_db = HostApplicationServices.WorkingDatabase;
using (DocumentLock m_doclock= Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using( Transaction m_tr = m_db.TransactionManager.StartTransaction())
{
BlockTable m_bt = (BlockTable)m_tr.GetObject(m_db.BlockTableId, OpenMode.ForWrite, false);
BlockTableRecord m_btr = new BlockTableRecord();
m_btr.Name = m_blockname;
m_blockid = m_bt.Add(m_btr);
m_tr.AddNewlyCreatedDBObject(m_btr, true);
foreach (Entity m_en in m_ent)
{
m_btr.AppendEntity(m_en);
m_tr.AddNewlyCreatedDBObject(m_en, true);
}
m_tr.Commit();
}
}
return m_blockid;
}
///
/// Only for test
///
[CommandMethod("CRT_BLOCK")]
static public void CRT_BLOCK()
{
ArrayList m_ent = new ArrayList();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database m_db = HostApplicationServices.WorkingDatabase;
PromptSelectionResult rs = ed.GetSelection();
if (rs.Status == PromptStatus.OK)
{
SelectionSet ss = rs.Value;
using (Transaction m_tr = m_db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in ss.GetObjectIds())
{
Entity en = m_tr.GetObject(id,OpenMode.ForRead) as Entity;
m_ent.Add(en.Clone());
}
m_tr.Commit();
}
Entity [] a_ent = new Entity[m_ent.Count]; m_ent.CopyTo(a_ent,0);
m_CreateBlock("my_block",a_ent);
}
}
}
}
=========The end of the citation================
[/code]
Best Regards,
Alexander Rivilis.