.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create a block problem?

3 REPLIES 3
Reply
Message 1 of 4
mkh927
325 Views, 3 Replies

Create a block problem?

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
3 REPLIES 3
Message 2 of 4
Anonymous
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.
Message 3 of 4
mkh927
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;
}
Message 4 of 4
mkh927
in reply to: mkh927

I'm sorry! Alexander Rivilis. Your Codes is correct!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost