8

I'm trying to find a way that I can store a C# class into an AutoCAD entity as an XRecord. For example, I have the following class:

public class ExampleClass
{
    private int x;
    private int y;

    public ExampleClass(int x, int y)
    {
         this.x = x;
         this.y = y;
    }

    #region Getters and Setters
}

and I create an instance of it:

ExampleClass objToStoreInXRecord = new ExampleClass(3, 5);

What is the best way to store that into an AutoCAD entity's (for example a line in the drawing) XRecord so that I could do something like this:

ExampleClass objRecoveredFromXRecord = GetXRecordFromEntity(Entity e)

Where GetXRecordFromEntity(Entity e) is a helper method I can write that takes an entity e, gets the XRecord of the object I stored earlier, and returns it.

I don't have a very good handle on how XRecord works and how XRecord relates to AutoCAD's Named Object Dictionary (NOD). I've seen implementations where the object to be stored in an XRecord is serialized with a binary formatter and the serialized data is stored in the entity's XRecord but I'm looking for a better way.

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • What is it that you don't like about serializing with a binary formatter? If it's because it's binary that's the issue, you can serialize to Json or XML instead? – Brendan Green Apr 14 '15 at 22:27
  • I use a whole hierarchy of classes and I was hoping to avoid marking them all as serializable – Nick Gilbert Apr 16 '15 at 13:20

2 Answers2

0

Serialize the object and add it to the xrecord. It works perfectly.

Miiir
  • 731
  • 1
  • 9
  • 12
0

Use .NET Serialize is not the supported way in AutoCAD, meaning other apps will not understand that...

Check this discussion at the Autodesk Forum, it may clarify what I mean. Also, there is a small code snippet.

http://forums.autodesk.com/t5/net/binary-serialization-to-xrecord/td-p/5601969

Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44