using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
public class MyCommands
{
public void WriteXrecord(Document doc, string dictName, string key, ResultBuffer resbuf)
{
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary NOD =
(DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
DBDictionary dict;
if (NOD.Contains(dictName))
{
dict = (DBDictionary)tr.GetObject(NOD.GetAt(dictName), OpenMode.ForWrite);
}
else
{
dict = new DBDictionary();
NOD.UpgradeOpen();
NOD.SetAt(dictName, dict);
tr.AddNewlyCreatedDBObject(dict, true);
}
Xrecord xRec = new Xrecord();
xRec.Data = resbuf;
dict.SetAt(key, xRec);
tr.AddNewlyCreatedDBObject(xRec, true);
tr.Commit();
}
}
public ResultBuffer ReadXrecord(Document doc, string dictName, string key)
{
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary NOD =
(DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
if (!NOD.Contains(dictName))
return null;
DBDictionary dict = tr.GetObject(NOD.GetAt(dictName), OpenMode.ForRead) as DBDictionary;
if (dict == null || !dict.Contains(key))
return null;
Xrecord xRec = tr.GetObject(dict.GetAt(key), OpenMode.ForRead) as Xrecord;
if (xRec == null)
return null;
return xRec.Data;
}
}
public void WriteDouble(Document doc, string acDictName, string acKey, double var)
{
ResultBuffer acResultBuffer = new ResultBuffer();
acResultBuffer.Add(new TypedValue((int)DxfCode.ExtendedDataReal, var));
WriteXrecord(doc, acDictName, acKey, acResultBuffer);
}
public double ReadXDouble(Document doc, string acDictName, string acKey, double acDefault)
{
ResultBuffer acResultBuffer = new ResultBuffer();
acResultBuffer = ReadXrecord(doc, acDictName, acKey);
if (acResultBuffer != null)
return (double)acResultBuffer.AsArray()[0].Value;
else return acDefault;
}
[CommandMethod("savevar", CommandFlags.Modal)]
public void MyCommand1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
double var = ReadXDouble(doc, "dist", "key", 5.555);
PromptDoubleOptions pdo = new PromptDoubleOptions("Введите число");
pdo.DefaultValue = var;
PromptDoubleResult pdr = doc.Editor.GetDouble(pdo);
if (pdr.Status != PromptStatus.OK) return;
WriteDouble(doc, "dist_1", "key_1", pdr.Value);
Application.ShowAlertDialog("Переменная сохранена");
}
[CommandMethod("readvar", CommandFlags.Modal)]
public void MyCommand2()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
double var = ReadXDouble(doc, "dist_1", "key_1", 5.555);
Application.ShowAlertDialog("Сохраненное число = " + var);
}
}