.NET

Reply
Member
mkh927
Posts: 3
Registered: ‎10-21-2006
Message 1 of 4 (73 Views)

Create a block problem?

73 Views, 3 Replies
10-21-2006 03:29 AM
I Selected some Entities,then i want use them to make a block,but it's wrong! why?
my code is blow:
public static ObjectId m_CreateBlock(string m_blockname, Entity[] m_ent)
{
DocumentLock m_doclock= Application.DocumentManager.MdiActiveDocument.LockDocument();

ObjectId m_blockid = new ObjectId();
Database m_db = HostApplicationServices.WorkingDatabase;

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);

for (int i = 0; i <= m_ent.GetUpperBound(0); i++)
{
m_btr.AppendEntity(m_ent);
}
m_tr.Commit();
}

m_doclock.Dispose();

return m_blockid;
}

oh,MyGod!How to Formate this Code? Message was edited by: mkh927
*Alexander Rivilis
Message 2 of 4 (73 Views)

Re: Create a block problem?

10-21-2006 09:23 AM in reply to: mkh927
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.
Member
mkh927
Posts: 3
Registered: ‎10-21-2006
Message 3 of 4 (73 Views)

Re: Create a block problem?

10-21-2006 09:24 PM in reply to: mkh927
Thanks, Alexander Rivilis!
But it's not work properly yet!
I has modifed it to this:
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;
foreach (Entity m_en in m_ent)
{
m_btr.AppendEntity((Entity)m_en.Clone());
}
m_blockid = m_bt.Add(m_btr);
m_tr.AddNewlyCreatedDBObject(m_btr, true);
m_tr.Commit();
}
}
return m_blockid;
}
Member
mkh927
Posts: 3
Registered: ‎10-21-2006
Message 4 of 4 (73 Views)

Re: Create a block problem?

10-21-2006 09:40 PM in reply to: mkh927
I'm sorry! Alexander Rivilis. Your Codes is correct!
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.