024集——CAD 动态显示图形——ed.Redraw(ent)实现(CAD—C#二次开发入门)
cad二开中提交事务前可动态显示实体图形,方法如下:
主代码:
public static void Redraw(this Editor ed, Entity? ent = null)
{
using (ent?.ForWrite())
{
ent?.Redraw(BrightEntity.RecordGraphicsModified | BrightEntity.RecomputeDimensionBlock | BrightEntity.Draw | BrightEntity.MoveZero);
ed.Redraw(BrightEditor.UpdateScreen);
}
Autodesk.AutoCAD.ApplicationServices.TransactionManager transactionManager = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.TransactionManager;
transactionManager.QueueForGraphicsFlush();
transactionManager.FlushGraphics();
//Application.DoEvents();//目前不可用
}
public static void Redraw(this Editor ed, BrightEditor bright)
{
if ((bright & BrightEditor.UpdateScreen) == BrightEditor.UpdateScreen)
{
ed.UpdateScreen();
}
if ((bright & BrightEditor.Regen) == BrightEditor.Regen)
{
ed.Regen();
}
if ((bright & BrightEditor.SelectionClean) == BrightEditor.SelectionClean)
{
ed.SetImpliedSelection(new ObjectId[0]);
}
if ((bright & BrightEditor.ViewportsFrom) == BrightEditor.ViewportsFrom)
{
ed.UpdateTiledViewportsFromDatabase();
}
if ((bright & BrightEditor.ViewportsIn) == BrightEditor.ViewportsIn)
{
ed.UpdateTiledViewportsInDatabase();
}
}
public static void Redraw(this Entity ent, BrightEntity bright)
{
if ((bright & BrightEntity.RecordGraphicsModified) == BrightEntity.RecordGraphicsModified)
{
ent.RecordGraphicsModified(setModified: true);
}
if ((bright & BrightEntity.RecomputeDimensionBlock) == BrightEntity.RecomputeDimensionBlock && ent is Dimension dimension)
{
dimension.RecomputeDimensionBlock(forceUpdate: true);
}
if ((bright & BrightEntity.Draw) == BrightEntity.Draw)
{
ent.Draw();
}
if ((bright & BrightEntity.Highlight) == BrightEntity.Highlight)
{
ent.Highlight();
}
if ((bright & BrightEntity.Unhighlight) == BrightEntity.Unhighlight)
{
ent.Unhighlight();
}
if ((bright & BrightEntity.VisibleTrue) == BrightEntity.VisibleTrue)
{
ent.Visible = true;
}
if ((bright & BrightEntity.VisibleFalse) == BrightEntity.VisibleFalse)
{
ent.Visible = false;
}
if ((bright & BrightEntity.MoveZero) == BrightEntity.MoveZero)
{
//ent.Move(Point3d.Origin, Point3d.Origin);
EditEntityTools.MoveEntity(ent, Point3d.Origin, Point3d.Origin);
}
}
上述代码含有封装函数,需要调用以下方法、字段:
[Flags]
public enum BrightEntity
{
RecordGraphicsModified = 1,
RecomputeDimensionBlock = 2,
Draw = 4,
Highlight = 8,
Unhighlight = 0x10,
VisibleTrue = 0x20,
VisibleFalse = 0x40,
MoveZero = 0x80
}
[Flags]
public enum BrightEditor
{
UpdateScreen = 1,
Regen = 2,
SelectionClean = 4,
ViewportsFrom = 8,
ViewportsIn = 0x10
}
public static T CloneEx<T>(this T ent) where T : RXObject
{
if (!(ent.Clone() is T result))
{
throw new ArgumentException("CloneEx克隆出错");
}
return result;
}
public static void ForWrite<T>(this T obj, Action<T> action) where T : DBObject
{
bool isNotifyEnabled = obj.IsNotifyEnabled;
bool isWriteEnabled = obj.IsWriteEnabled;
if (isNotifyEnabled)
{
obj.UpgradeFromNotify();
}
else if (!isWriteEnabled)
{
obj.UpgradeOpen();
}
action?.Invoke(obj);
if (isNotifyEnabled)
{
obj.DowngradeToNotify(isWriteEnabled);
}
else if (!isWriteEnabled)
{
obj.DowngradeOpen();
}
}
public static UpgradeOpenManager ForWrite(this DBObject obj)
{
return new UpgradeOpenManager(obj);
}