Здравствуйте!
Словил ошибку при попытке смены видимости динамического блока.
System.InvalidOperationException
HResult=0x80131509
Сообщение = Operation is not valid due to the current state of the object.
Источник = Acdbmgd
Трассировка стека:
at Autodesk.AutoCAD.DatabaseServices.Transaction.CheckTopTransaction()
at Autodesk.AutoCAD.DatabaseServices.Transaction.DeleteUnmanagedObject()
at Autodesk.AutoCAD.Runtime.DisposableWrapper.!DisposableWrapper()
at Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose(Boolean A_0)
at Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose()
at DrawingSynchronizer.EntityProcessor.SetDynamicBlkProperty2() in K:\DevProjects\AutoCAD\DrawingSynchronizer\EntityProcessor.cs:line 153
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()
Как это исправить?
[CommandMethod("SetDynamicBlkProperty2")]
public static void SetDynamicBlkProperty2()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false, true);
foreach (ObjectId entity in btr)
{
Entity ent = (Entity)tr.GetObject(entity, OpenMode.ForRead);
if (ent is BlockReference)
{
BlockReference bref = tr.GetObject(entity, OpenMode.ForWrite, false, true) as BlockReference;
if (bref.IsDynamicBlock)
{
BlockTableRecord dynBtr = (BlockTableRecord)tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForWrite, false, true);
var blockNames = GetBlockNames("C:\\Developments\\Lists\\ListOfBlocks.txt");
if (blockNames.Contains(dynBtr.Name) )
{
DynamicBlockReferencePropertyCollection props = bref.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop in props)
{
object[] values = prop.GetAllowedValues();
//Switch Property
if ((prop.PropertyName == "Visibility") && !prop.ReadOnly)
{
if (prop.Value.ToString() == values[0].ToString())
{
prop.Value = values[1];
}
else
{
prop.Value = values[0];
}
}
}
}
}
}
}
tr.Commit();
}
}
public static List < string > GetBlockNames(string path)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = doc.TransactionManager.StartTransaction();
List < string > listOfBlockNames = new List < string >();
string filePath = path;
StreamReader sr = new StreamReader(filePath, Encoding.Default);
string line;
while ( (line = sr.ReadLine()) != null )
{
listOfBlockNames.Add(line);
}
return listOfBlockNames;
}