.NET

Reply
Member
3 Posts
0 Kudos
Registered: ‎10-21-2006
Post 1 of 4

Create a block problem?

74 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
Post 2 of 4

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
3 Posts
0 Kudos
Registered: ‎10-21-2006
Post 3 of 4

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
3 Posts
0 Kudos
Registered: ‎10-21-2006
Post 4 of 4

Re: Create a block problem?

10-21-2006 09:40 PM in reply to: mkh927
I'm sorry! Alexander Rivilis. Your Codes is correct!
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!