Check out my GitHub.
here is an quick example.
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
namespace stackclass
{
/// <summary>
/// Description of MyClass.
/// </summary>
public class MyClass : BlockTableRecord
{
public string BlkRecordName {get;set;}
public MyClass(BlockTableRecord _RecordParam)
{
BlkRecordName = _RecordParam.Name;
}
public MyClass(){}
}
public static class MyClassInstanciator
{
public static void Main(string[] args)
{
var AcDoc = Application.DocumentManager.MdiActiveDocument;
using (var Trans = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()) {
var BlkTable = Trans.GetObject(AcDoc.Database.BlockTableId,OpenMode.ForRead) as BlockTable;
foreach (var BlkRcrdId in BlkTable) {
var _blkRcrd = Trans.GetObject(BlkRcrdId,OpenMode.ForRead) as BlockTableRecord;
var MyClassRcd = new MyClass(_blkRcrd);
// or
var _MyClassRcd = new MyClass();
_MyClassRcd.BlkRecordName = _blkRcrd.Name;
}
}
}
}
Update:
based on the way you have your class structured you are either going to have to instantiate a new version of your class and then assign the Id = 2;
or you are going to have to add the static class modifier and then have a property named Id
.
example:
public static class MyClass : BlockTableRecord
{
public static string Id {get;set;}
}
public static class UsingStaticClass
{
public static void setIdWithValue(string[] args)
{
MyClass.Id = 2;
}
public static void RetrieveIdValue()
{
var someOtherValue = MyClass.Id;
}
}
Is this what you mean?
Additional Update:
Your code usage here is not right.
foreach (ObjectId obj in bt)
{
BlockTableRecord btr = obj.GetObject(OpenMode.ForRead) as BlockTableRecord;
}
In a foreach while inside of a AutoCad.Database variable is an objectId as you have obj
as in the loop.
in the example above you are trying to grab the Object using a property of the the BlockTableRecord. your Code should be like this and should be inside of a transaction
foreach(ObjectId ObjId in BlkTbl)
{
var BlkRcd = Transaction.GetObject(ObjId,OpenMode.ForRead) as BlockTableRecord;
}
you need to use the transaction to grab any object from the database.