// (C) Copyright 2022 by kdub
//
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Kdub.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly
: CommandClass
(typeof(AutoCAD2023_ExtensionTest_01
.TestCommands))]
namespace AutoCAD2023_ExtensionTest_01
{
public class TestCommands
{
[CommandMethod("Test01")]
public static void Test01()
{
AddLine_01();
}
[CommandMethod("Test02")]
public static void Test02()
{
AddLine_02();
}
[CommandMethod("Test03")]
public static void Test03()
{
AddLine_03();
}
[CommandMethod("Test04")]
public static void Test04()
{
AddLine_04();
}
/// <summary>
/// Active class and .UsingModelSpace()
/// and Create<T>()>
/// </summary>
internal static void AddLine_04()
{
// could be returned to caller.
var objId = ObjectId.Null;
Active.Database.UsingModelSpace(
(transaction, modelSpace) =>
{
objId = modelSpace.Create<Line>(
transaction, acLine =>
{
acLine
.StartPoint = new Point3d
(500,
800,
0); acLine
.EndPoint = new Point3d
(1200,
600,
0); acLine.ColorIndex = 4;
});
});
}
/// <summary>
/// Active class and .UsingModelSpace()
/// </summary>
internal static void AddLine_03()
{
// Start a transaction
Active.Database.UsingModelSpace(
(transaction, modelSpace) =>
{
// Create a line
using (Line acLine
= new Line
(new Point3d
(500,
700,
0),
new Point3d
(1200,
500,
0))) {
// Add the new object to the block table record and the transaction
acLine.SetDatabaseDefaults();
acLine.ColorIndex = 3;
modelSpace.AppendEntity(acLine);
transaction.AddNewlyCreatedDBObject(acLine, true);
}
});
}
/// <summary>
/// Active class and .UsingTransaction()
/// </summary>
internal static void AddLine_02()
{
// Start a transaction
Active.Database.UsingTransaction(transaction =>
{
// Open the Database Block table
var dbBlockTable = (BlockTable)transaction.GetObject(
Active.Database.BlockTableId, OpenMode.ForRead);
// Open the Block table record Model space
var modelSpaceRecord = (BlockTableRecord)transaction.GetObject(
dbBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// Create a line
using (Line acLine
= new Line
(new Point3d
(500,
600,
0),
new Point3d
(1200,
400,
0))) {
// Add the new object to the block table record and the transaction
acLine.SetDatabaseDefaults();
acLine.ColorIndex = 2;
modelSpaceRecord.AppendEntity(acLine);
transaction.AddNewlyCreatedDBObject(acLine, true);
}
});
}
/// <summary>
/// Traditional code
/// </summary>
internal static void AddLine_01()
{
// Get the current document and database
var document = AcadApp.DocumentManager.MdiActiveDocument;
var database = document.Database;
// Start a transaction
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
// Open the Database Block table
var dbBlockTable = (BlockTable)transaction.GetObject(
database.BlockTableId, OpenMode.ForRead);
// Open the Block table record Model space
var modelSpaceRecord = (BlockTableRecord)transaction.GetObject(
dbBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// Create a line
using (Line acLine
= new Line
(new Point3d
(500,
500,
0),
new Point3d
(1200,
300,
0))) {
// Add the new object to the block table record and the transaction
acLine.SetDatabaseDefaults();
acLine.ColorIndex = 1;
modelSpaceRecord.AppendEntity(acLine);
transaction.AddNewlyCreatedDBObject(acLine, true);
}
// Save the new object to the database
transaction.Commit();
}
}
}
}