2

I'm working on being able to serialize C# objects to AutoCAD entities. I have a method that serializes them and I'm trying to call this method from the AutoCAD command line intended to deserialize them.

            [CommandMethod("OpenXRecord", CommandFlags.Modal)]
            public SerializeTest XMLOpen()
            {
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                SerializeTest retval = null;
                XmlSerializer serializer = new XmlSerializer(typeof(SerializeTest));
                using (Transaction tr = db.TransactionManager.StartTransaction())
                using (DocumentLock docLock = doc.LockDocument())
                {
                    PromptSelectionResult acSSPrompt = ed.GetSelection();
                    if (acSSPrompt.Status == PromptStatus.OK)
                    {
                        ObjectId[] ids = acSSPrompt.Value.GetObjectIds();
                        Entity acadObj = tr.GetObject(ids[0], OpenMode.ForWrite) as Entity;
                        if (acadObj == null || acadObj.ExtensionDictionary == ObjectId.Null)
                        {
                            tr.Abort();
                            return retval;
                        }
                        using (DBDictionary dict = tr.GetObject(acadObj.ExtensionDictionary, OpenMode.ForRead, false) as DBDictionary)
                        {
                            if (dict.Contains("KW_PID"))
                            {
                                using (Xrecord xrec = tr.GetObject(dict.GetAt("KW_PID"), OpenMode.ForRead) as Xrecord)
                                {
                                    if (xrec != null)
                                    {
                                        using (ResultBuffer rb = xrec.Data)
                                        {
                                            if (rb != null)
                                            {
                                                using (MemoryStream stream = new MemoryStream())
                                                {
                                                    TypedValue[] tvs = rb.AsArray();
                                                    if (tvs != null)
                                                    {
                                                        if (tvs[0].TypeCode == (short)DxfCode.Text)
                                                        {
                                                            string xmlString = "";
                                                            TextWriter writer = new StreamWriter(stream);
                                                            for (int i = 1; i < tvs.Length; i++)
                                                            {
                                                                if (tvs[i].TypeCode == (short)DxfCode.Text)
                                                                {
                                                                    xmlString = (string)tvs[i].Value; writer.Write(xmlString);
                                                                }
                                                            }
                                                            writer.Flush();
                                                            stream.Position = 0;
                                                            retval = serializer.Deserialize(stream) as SerializeTest;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return retval;
            }

However, when I call it, I'm greeted with this error and have no idea why as it gives no line numbers or useful debug information.

enter image description here

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • hmmm, are you missing a `{` after `using (Transaction tr = db.TransactionManager.StartTransaction())` in posting this listing? –  Apr 16 '15 at 15:53
  • 2
    No, C# syntax lets you stack using statements and then have one sets of { } for all of them – Nick Gilbert Apr 16 '15 at 15:54
  • Neat. _How_ is this run? Is it a plug-in for AutoCAD? If so try attaching Visual Studio to it prior or from your project in Visual Studio, set the Debug **Startup** to AutoCAD and debug it. That will VS will load your dll first (I'm assuming it's a .dll) and debug info prior to launching AC. Same applies if you are launching some AC command-line tool perhaps? –  Apr 16 '15 at 15:55
  • That won't work either, the method never even actually starts – Nick Gilbert Apr 16 '15 at 16:00
  • possible duplicate of [Serialization Code Causes Unhandled Exception](http://stackoverflow.com/questions/24192222/serialization-code-causes-unhandled-exception) –  Apr 16 '15 at 16:05

2 Answers2

1

Figured it out, the method must be void or else it will get mad and throw this error.

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
1

Ideally the command methods would be:

public static void MethodName()

Where the static will make AutoCAD not instantiate the class to call the command. Also, should be void and not accept any parameter

And if you don't Commit() the transaction, AutoCAD will assume Abort(), so you may need to call it by the end of your routine.

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