TheSwamp
Code Red => .NET => Topic started by: svb4 on September 30, 2011, 03:29:09 AM
-
Hello,
i have written a little dialog with one button. And this button call a function whicht should insert a aligned dimension. But when I click on the button the dimension doesn't appear.
But when I run the function with a command in the commandline, then it works!
What I have to do that it also works in the dialog?
Here ist the code:
private void button1_Click(object sender, EventArgs e)
{
CreateAlignedDim();
}
public void CreateAlignedDim()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
using (doc.LockDocument())
{
using (BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable)
{
BlockTableRecord btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
Point3d pHilfslinienPunkt1 = new Point3d(1000.0, 0.0, 0.0);
Point3d pHilfslinienPunkt2 = new Point3d(3000.0, 0.0, 0.0);
Point3d pBemaßungslinienpunkt = new Point3d(2000.0, 500.0, 0.0);
AlignedDimension rd = new AlignedDimension(
pHilfslinienPunkt1,
pHilfslinienPunkt2,
pBemaßungslinienpunkt,
"",
db.Dimstyle) as AlignedDimension;
ObjectId objId = btr.AppendEntity(rd);
trans.AddNewlyCreatedDBObject(rd, true);
}
}
trans.Commit();
}
catch (System.Exception ex)
{
}
}
}
thanks and regards,
Patrick
-
I've solved this topic.
If anyone need the solution, here it is:
The problem is, that i placed the document lock after i start the transaction. Now i placed it before i start the transaction and now it works.
It is very simple but it takes me one day ... :ugly:
regards,
Patrick
-
Is LockDocument necessary since your using a transaction?
-
Welcome to theSwamp svb4 :)
thanks for posting back.
Is LockDocument necessary since your using a transaction?
yes.
-
Hi svb4,
You do not need to nest the using (Transaction ...) {...} statement within the using (DocumentLock ...) {...} one. You can write more than one using expression for the same code block.
You do not need to explicitly dispose the BlockTable object (using (BlockTable ...) { ... }). As it is opened with the transaction, it will be automatically disposed with the transaction.
try
{
using (doc.LockDocument())
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable
....
}
}
catch {}
See a more complete example here (http://www.theswamp.org/index.php?topic=39622.msg449104#msg449104).
-
Is LockDocument necessary since your using a transaction?
Further to my original simple response to this query ;
The primary keyword for further searches is 'Application Context'
Code that runs from the Menu, Toolbars, Palettes, and Modeless Forms runs in what is termed application context where the application is NOT aware of which document the code is meant to interact with, unlike commands originating from the command line which will be operating in 'Document Context'.
To make AutoCAD aware of the document the code intends to interact with the calling code should 'lock' the document so it can be targeted by the code instruction.
this is done by using the LockDocument method of the targeted document
IE
using (doc.LockDocument())
{
// do the mojo here
// <..>
} // the using mechanism unlocks the document.
Regards
Kerry
-
TY Kerry for that explanation.