The first snip here is the original code
The second is the reflector view on the IL code in the DLL.
Note that the input tests and return statement have been revised to be in-line.
the
using( DocumentLock docLock = doc.LockDocument() ) {
has been changed to
using (doc.LockDocument())
The using( Transaction tr = db.TransactionManager.StartTransaction() ) {
has been shanged to
Transaction tr = db.TransactionManager.StartTransaction();
... I assume because of the DockLock using statement.
The start of the try statement has been changed.
The finally statement has been added.
ORIGINAL SOURCE
[CommandMethod("GP1", CommandFlags.Session)]
public static void GetPointsFromUser_1()
{
// Get the current database and start the Transaction Manager
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
// Prompt for the start point, Point3D will be in current UCS
pPtOpts.Message = "\nEnter the start point of the line: ";
pPtRes = doc.Editor.GetPoint(pPtOpts);
// Exit if the user presses ESC or cancels the command
if( pPtRes.Status == PromptStatus.Cancel )
return;
//
Point3d ptStart = pPtRes.Value;
// Prompt for the end point, Point3D will be in current UCS
pPtOpts.Message = "\nEnter the end point of the line: ";
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = ptStart;
pPtRes = doc.Editor.GetPoint(pPtOpts);
if( pPtRes.Status == PromptStatus.Cancel )
return;
//
Point3d ptEnd = pPtRes.Value;
// Convert the UCS points to WCS
Point3d ptStartW = ptStart.UcsToWcs();
Point3d ptEndW = ptEnd.UcsToWcs();
// Start a transaction
using( DocumentLock docLock = doc.LockDocument() ) {
using( Transaction tr = db.TransactionManager.StartTransaction() ) {
BlockTable bt;
BlockTableRecord btr;
try {
// Open Model space for write
bt = tr.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
btr = tr.GetObject(bt[ BlockTableRecord.ModelSpace ],
OpenMode.ForWrite) as BlockTableRecord;
// Define the new line using the WCS Ordinates
Line acLine = new Line(ptStartW, ptEndW);
acLine.SetDatabaseDefaults();
// Add the line to the drawing
btr.AppendEntity(acLine);
tr.AddNewlyCreatedDBObject(acLine, true);
// Commit the changes and dispose of the transaction
tr.Commit();
} catch( Autodesk.AutoCAD.Runtime.Exception exx ) {
ed.WriteMessage("\n" + exx.ToString());
}
}
// Zoom to the extents or limits of the drawing
doc.SendStringToExecute("._zoom _all ", true, false, false);
}
}
TRANSLATED IL CODE
[CommandMethod("GP1", CommandFlags.Session)]
public static void GetPointsFromUser_1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointOptions pPtOpts = new PromptPointOptions("") {
Message = "\nEnter the start point of the line: "
};
PromptPointResult pPtRes = doc.Editor.GetPoint(pPtOpts);
if (pPtRes.Status != PromptStatus.Cancel)
{
Point3d ptStart = pPtRes.Value;
pPtOpts.Message = "\nEnter the end point of the line: ";
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = ptStart;
pPtRes = doc.Editor.GetPoint(pPtOpts);
if (pPtRes.Status != PromptStatus.Cancel)
{
Point3d ptEnd = pPtRes.Value;
Point3d ptStartW = ptStart.UcsToWcs();
Point3d ptEndW = ptEnd.UcsToWcs();
using (doc.LockDocument())
{
Transaction tr = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Line acLine = new Line(ptStartW, ptEndW);
acLine.SetDatabaseDefaults();
btr.AppendEntity(acLine);
tr.AddNewlyCreatedDBObject(acLine, true);
tr.Commit();
}
catch (Exception exx)
{
ed.WriteMessage("\n" + exx.ToString());
}
finally
{
if (tr != null)
{
tr.Dispose();
}
}
doc.SendStringToExecute("._zoom _all ", true, false, false);
}
}
}
}