[CommandMethod("lmxInsertBlock")]
public void lmxInsertBlock()
{
Document lxDocument = Application.DocumentManager.MdiActiveDocument;
Database lxDatabase = Application.DocumentManager.MdiActiveDocument.Database;
Editor lxEditor = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectId lxObjectID;
string FileName = "E:/A3.dwg";
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
using (Database lxTempDatabase = new Database(false, true))
{
BlockTable lxBlockTable = lxTransaction.GetObject(lxDatabase.BlockTableId, OpenMode.ForWrite, false) as BlockTable;
string BlockName = FileName.Replace("E:/", "").Replace(".dwg", "");
if (!lxBlockTable.Has(BlockName))
{
lxTempDatabase.ReadDwgFile(FileName, System.IO.FileShare.Read, true, null);
lxObjectID = lxDatabase.Insert(BlockName, lxTempDatabase, true);
}
else
{
lxObjectID = lxBlockTable[BlockName];
}
}
lxTransaction.Commit();
}
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
Point3d lxBlockInsertPoint = Point3d.Origin;
//Point3d lxBlockInsertPoint = new Point3d(0, 0, 0);
BlockTable lxBlockTable = lxTransaction.GetObject(lxDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockReference lxBlockReference = new BlockReference(lxBlockInsertPoint, lxObjectID);
BlockEntityJig lxJig = new BlockEntityJig(lxBlockReference);
lxEditor.Drag(lxJig);
BlockTableRecord lxBlockTableRecord = lxTransaction.GetObject(lxBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
lxBlockTableRecord.AppendEntity(lxJig.GetEntity());
lxTransaction.AddNewlyCreatedDBObject(lxJig.GetEntity(), true);
//lxDocument.TransactionManager.QueueForGraphicsFlush();
lxTransaction.Commit();
}
}
class BlockEntityJig : EntityJig
{
Point3d mCenterPt, mActualPoint;
public BlockEntityJig(BlockReference br) : base(br)
{
mCenterPt = br.Position;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls = UserInputControls.Accept3dCoordinates;
jigOpts.UserInputControls = UserInputControls.NoZeroResponseAccepted;
jigOpts.UserInputControls = UserInputControls.NoNegativeResponseAccepted;
jigOpts.Message = "\nEnter insert point: ";
PromptPointResult dres = prompts.AcquirePoint(jigOpts);
if (mActualPoint == dres.Value)
{
return SamplerStatus.NoChange;
}
else
{
mActualPoint = dres.Value;
}
return SamplerStatus.OK;
}
protected override bool Update()
{
mCenterPt = mActualPoint;
try
{
((BlockReference)Entity).Position = mCenterPt;
}
catch (System.Exception)
{
return false;
}
return true;
}
public Entity GetEntity()
{
return Entity;
}
}