我正致力于将C#对象序列化为AutoCAD实体。我有一个序列化它们的方法,我试图从用于反序列化它们的AutoCAD命令行调用此方法。
[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;
}
然而,当我打电话给它时,我已经接受了这个错误,并且不知道为什么它没有提供行号或有用的调试信息。
答案 0 :(得分:1)
想出来,该方法必须是无效的,否则它会生气并抛出此错误。
答案 1 :(得分:1)
理想情况下,命令方法是:
public static void MethodName()
静态将使AutoCAD不实例化类以调用命令。此外,应该是void
并且不接受任何参数
如果您没有提交()该事务,AutoCAD将假设中止(),因此您可能需要在例程结束时调用它。