You need to lock your document when accessing it from the Application
Context. Or skip using the document object altogether.
[code]
using System;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace GratuitousNameDisplay.BobbyCJones
{
public class CreateAndAppendToDatabaseExample
{
private const string fileName = @"C:\AddCircle.dwg";
[CommandMethod("CreateCircleDwg")]
public void AddCircle()
{
using (Database targetDb = new Database(true, true))
using (Transaction trans =
targetDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(targetDb.BlockTableId,
OpenMode.ForRead);
BlockTableRecord modelSpace =
(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);
Circle newCircle = new Circle();
newCircle.Center = new Point3d(100, 0, 0);
newCircle.Radius = 300;
newCircle.Normal = new Vector3d(0, 0, 1);
modelSpace.AppendEntity(newCircle);
trans.AddNewlyCreatedDBObject(newCircle, true);
trans.Commit();
targetDb.SaveAs(fileName, DwgVersion.Newest);
}
}
}
}
[/code]
--
Bobby C. Jones
wrote in message news:5442633@discussion.autodesk.com...
I want create a new dwg file and append an entity(a circle) in it ,but the
code excute to the line :
"bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForWrite, false);"
,throw an exception:
Autodesk.AutoCAD.Runtime.Exception: eLockViolation
Autodesk.AutoCAD.Runtime.Interop.ThrowExceptionForErrorStatus(Int32
errorStatus)
Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal(AcDbTransactionManager*
pTM, ObjectId id, OpenMode mode, Boolean openErased, Boolean
forceOpenOnLockedLayer)
Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObject(ObjectId
id, OpenMode mode, Boolean openErased)
private void button1_Click(object sender, System.EventArgs e)
{
Database db;
DBTransMan tm;
Transaction myT;
BlockTable bt;
BlockTableRecord btr;
Autodesk.AutoCAD.ApplicationServices.Document doc;
doc=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add("");
db = doc.Database;
tm = db.TransactionManager;
myT = tm.StartTransaction();
bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite, false);
Circle circle;
circle = new Circle();
circle.Center = new Point3d(100,0,0);
circle.Radius = 300;
btr.AppendEntity(circle);
tm.AddNewlyCreatedDBObject(circle, true);
myT.Commit();
if(bt!=null)
bt.Close();
if(btr!=null)
btr.Close();
if(myT!=null)
myT.Dispose();
doc.CloseAndSave(@"d:\111.dwg");
}
I developed in vs.net 2003 and autocad2006,and this code excute in a
ToolPalete.