As I expected in previous reply, you need to lock
the document when modifying drawing database from modeless form/palette. If you
do it from a modal form, it should work as you do from command line. Do not ask
me why: that is how autodesk implements the drawing database transaction.
So, you need to change your code similar to
this:
//Note, the argument doc, you can get it prior to
calling this method
//from Application.DocumentManager.MdiDocument, as
long as
//it is a Document object. That is, you need to get
a hold on a drawing
//document, to which you intent to modify its
database, and lock it
//during the database change, then release the
lock.
public void MakeChangeToDrawingDatabase(Document
doc)
{
using (DocumentLock
lk=doc.LockDocument())
{
//Add/Modify
your polyline, or whatever entities here
//you can use
try...catch...finally, if you prefer, as your code shown
//but "using"
might be more suitable to wrap a transaction
using
(Transaction
tran=doc.Database.TransactionManager.StartTransaction())
{
//code to add/modify
}
}
}
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I build a class like this:
public class
LithorLayer
{
public void DisplayInModelSpace(Editor
Ed)
{
Polyline PlLay;
BlockTableRecord
btr;
BlockTable bt;
Database Db;
Transaction
trans;
Db = HostApplicationServices.WorkingDatabase;
trans =
Db.TransactionManager.StartTransaction();
try
{
PlLay =
new Polyline(5);
PlLay.AddVertexAt(0, new Point2d(0, 0), 0, -1,
-1);
PlLay.AddVertexAt(1, new Point2d(1, 0), 0, -1,
-1);
PlLay.AddVertexAt(2, new Point2d(1, 1), 0, -1,
-1);
PlLay.AddVertexAt(3, new Point2d(0, 1), 0, -1,
-1);
PlLay.AddVertexAt(4, new Point2d(0, 0), 0, -1, -1);
bt =
(BlockTable)trans.GetObject(Db.BlockTableId, OpenMode.ForRead);
btr =
(BlockTableRecord)trans.GetObject(Db.CurrentSpaceId,
OpenMode.ForWrite);
btr.AppendEntity(PlLay);
trans.AddNewlyCreatedDBObject(PlLay,
true);
trans.Commit();
}
catch
{
Ed.WriteMessage("Error\n");
}
finally
{
trans.Dispose();
}
}
}
and
use it in command like this:
public class
tdcCommands
{
public
tdcCommands()
{
}
[CommandMethod("Pll")]
static
public void Pll()
{
Editor ed =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
LithorLayer
Lop;
Lop = new
LithorLayer();
Lop.DisplayInModelSpace(ed);
}
}
It
work!!!
But when I use like:
private void
button1_Click(object sender, EventArgs e)
{
Editor ed =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
LithorLayer
Lop;
Lop = new
LithorLayer();
Lop.DisplayInModelSpace(ed);
}
with
button1 is a command button on a palette.
it doesn't work
Help
me!! Thanks so much
Edited by: ledungtdc on Feb 4, 2009
9:52 AM