C#进行Visio二次开发之设备状态跟踪
明察秋毫,很多情况下是非常需要的,例如,如果我们完善了一套系统,能够将四川赈灾的款项的筹集、采购、分发等步骤都能明察秋毫,相信整个世界会安静很多。
同样,对于一个使用Visio进行二次开发的程序来说,背后你需要知道用户增加了那些设备,删除了那些设备,修改了那些设备,这样你才能对整个系统的数据进行有效的控制,否则“赈灾”款项就可能丢失了,呵呵。
那我们应该如何做才能有效的处理这些事件,达到对设备的变更明察秋毫呢?
我前面介绍了一篇文章《C#进行Visio开发的事件处理 》,其中也介绍了各种事件侦听,我们要对设备进行跟踪的话,基本上只需要侦听这几个事件,并对之进行处理即可。
同样,对于一个使用Visio进行二次开发的程序来说,背后你需要知道用户增加了那些设备,删除了那些设备,修改了那些设备,这样你才能对整个系统的数据进行有效的控制,否则“赈灾”款项就可能丢失了,呵呵。
那我们应该如何做才能有效的处理这些事件,达到对设备的变更明察秋毫呢?
我前面介绍了一篇文章《C#进行Visio开发的事件处理 》,其中也介绍了各种事件侦听,我们要对设备进行跟踪的话,基本上只需要侦听这几个事件,并对之进行处理即可。
const string sink = "";
Event newEvent = null;
EventList applicationEvents = eventApplication.EventList;
EventList documentEvents = eventDocument.EventList;
newEvent = documentEvents.AddAdvise(
(unchecked((short)VisEventCodes.visEvtAdd) + (short)VisEventCodes.visEvtShape),
(IVisEventProc)this, sink, "ShapeAdd");
newEvent = documentEvents.AddAdvise(
(short)VisEventCodes.visEvtDel + (short)VisEventCodes.visEvtShape,
(IVisEventProc)this, sink, "ShapeDelete");
newEvent = documentEvents.AddAdvise(
(short)VisEventCodes.visEvtMod + (short)VisEventCodes.visEvtCell,
(IVisEventProc)this, sink, "CellChanged");
Event newEvent = null;
EventList applicationEvents = eventApplication.EventList;
EventList documentEvents = eventDocument.EventList;
newEvent = documentEvents.AddAdvise(
(unchecked((short)VisEventCodes.visEvtAdd) + (short)VisEventCodes.visEvtShape),
(IVisEventProc)this, sink, "ShapeAdd");
newEvent = documentEvents.AddAdvise(
(short)VisEventCodes.visEvtDel + (short)VisEventCodes.visEvtShape,
(IVisEventProc)this, sink, "ShapeDelete");
newEvent = documentEvents.AddAdvise(
(short)VisEventCodes.visEvtMod + (short)VisEventCodes.visEvtCell,
(IVisEventProc)this, sink, "CellChanged");
object IVisEventProc.VisEventProc(short eventCode, object source, int eventId,
int eventSequenceNumber, object subject, object moreInfo)
{
.
switch (eventCode)
{
case (short)VisEventCodes.visEvtShape + unchecked((short)VisEventCodes.visEvtAdd):
eventShape = (Shape)subject;
handleShapeAdd(eventShape);
break;
case (short)VisEventCodes.visEvtDel + (short)VisEventCodes.visEvtShape:
eventShape = (Shape)subject;
handleShapeDelete(eventShape);
break;
case (short)VisEventCodes.visEvtCell + (short)VisEventCodes.visEvtMod:
Visio.Cell cell = (Cell)subject;
if (cell.Name.IndexOf("Prop") >= 0)//限制只执行自定义事件一次
{
eventShape = cell.Shape;
handleCellModify(eventShape);
}
break;
default:
break;
}
return result;
}
int eventSequenceNumber, object subject, object moreInfo)
{
.
switch (eventCode)
{
case (short)VisEventCodes.visEvtShape + unchecked((short)VisEventCodes.visEvtAdd):
eventShape = (Shape)subject;
handleShapeAdd(eventShape);
break;
case (short)VisEventCodes.visEvtDel + (short)VisEventCodes.visEvtShape:
eventShape = (Shape)subject;
handleShapeDelete(eventShape);
break;
case (short)VisEventCodes.visEvtCell + (short)VisEventCodes.visEvtMod:
Visio.Cell cell = (Cell)subject;
if (cell.Name.IndexOf("Prop") >= 0)//限制只执行自定义事件一次
{
eventShape = cell.Shape;
handleCellModify(eventShape);
}
break;
default:
break;
}
return result;
}
以上是对几个特别事件的侦听,我们要实现设备的跟踪,需要在这几个事件中处理相关的设备信息。为了跟踪好设备的相关信息,我们需要定义一个实体类ShapeLogInfo类放置相关的设备信息,如Shape的GUID,Shape的ID,设备状态(添加、修改、删除),Shape的Name,还有就是我们自定义的一个属性“设备类型”。
/// <summary>
/// 形状的类型,即设备类型
/// </summary>
public string ShapeType
/// <summary>
/// 形状ID
/// </summary>
public string ShapeID
/// <summary>
/// 形状的GUID
/// </summary>
public string ShapeGuid
/// <summary>
/// 形状的状态:新增、编辑、删除
/// </summary>
public ShapeStatus ShapeStatus
/// <summary>
/// 形状名称
/// </summary>
public string ShapeName
/// 形状的类型,即设备类型
/// </summary>
public string ShapeType
/// <summary>
/// 形状ID
/// </summary>
public string ShapeID
/// <summary>
/// 形状的GUID
/// </summary>
public string ShapeGuid
/// <summary>
/// 形状的状态:新增、编辑、删除
/// </summary>
public ShapeStatus ShapeStatus
/// <summary>
/// 形状名称
/// </summary>
public string ShapeName
为了更好的管理ShapeLogInfo的相关信息,我们把相同设备类型(ShapeType)的放到一起管理,于是,我们再创建一个ShapeTypeLogInfo类来装载相关的日志信息,具体如下:
/// <summary>
/// 形状类型
/// </summary>
public string ShapeType
/// <summary>
/// 形状类型对应的数据库表名称
/// </summary>
public string ShapeTableName
/// <summary>
/// 该形状类型对应的形状日志对象集合
/// </summary>
public Dictionary<string, ShapeLogInfo> ShapeColloction
/// 形状类型
/// </summary>
public string ShapeType
/// <summary>
/// 形状类型对应的数据库表名称
/// </summary>
public string ShapeTableName
/// <summary>
/// 该形状类型对应的形状日志对象集合
/// </summary>
public Dictionary<string, ShapeLogInfo> ShapeColloction
为了判断是否有相应的ShapeTypeLogInfo和ShapeLogInfo,需要定义几个函数用来维护相关的集合信息,如下所示:
/// <summary>
/// 获取形状对应的类型,如果没有则创建
/// </summary>
private ShapeTypeLogInfo GetShapeType(ShapeLogInfo shape)
/// <summary>
/// 如果集合中有,修改状态;否则添加一个新的
/// </summary>
public void AddLog(ShapeLogInfo shapeLogInfo)
/// <summary>
/// 判断指定的形状是否是在集合中存在
/// </summary>
public bool IsNewShape(ShapeLogInfo shape)
/// <summary>
/// 取当前设备在集合中存储的ShapeLog对象
/// </summary>
public ShapeLogInfo GetShape(string shapeType, string shapeId)
/// 获取形状对应的类型,如果没有则创建
/// </summary>
private ShapeTypeLogInfo GetShapeType(ShapeLogInfo shape)
/// <summary>
/// 如果集合中有,修改状态;否则添加一个新的
/// </summary>
public void AddLog(ShapeLogInfo shapeLogInfo)
/// <summary>
/// 判断指定的形状是否是在集合中存在
/// </summary>
public bool IsNewShape(ShapeLogInfo shape)
/// <summary>
/// 取当前设备在集合中存储的ShapeLog对象
/// </summary>
public ShapeLogInfo GetShape(string shapeType, string shapeId)
完成这些函数后,最后需要做的就是在添加、删除、修改Shape的侦听事件中加入相关的日志就可以了,如在删除设备的时候,我们是这样记录相关信息的
private void visioEventSink_OnShapeDelete(object sender, EventArgs e)
{
Shape shape = (Shape)sender;
string strDeviceType = string.Empty;
string strDeviceName = string.Empty;
string strDeviceID = string.Empty;
strDeviceType = VisioUtility.GetShapeCellValue(shape, "设备类型");
strDeviceID = VisioUtility.GetShapeCellValue(shape, "GUID");
ShapeLogInfo logInfo = new ShapeLogInfo();
logInfo.ShapeType = strDeviceType;
logInfo.ShapeID = shape.NameID;
logInfo.ShapeName = VisioUtility.GetShapeCellValue(shape, "名称");
if (shape.Application.IsUndoingOrRedoing)
{
ShapeLogInfo info = Portal.gc.gOperationLog.GetShape(strDeviceType, shape.NameID);
if (info != null)
{
strDeviceID = info.ShapeGuid;
}
}
logInfo.ShapeGuid = strDeviceID;
logInfo.ShapeStatus = ShapeStatus.DeleteShape;
Portal.gc.gOperationLog.AddLog(logInfo);
}
{
Shape shape = (Shape)sender;
string strDeviceType = string.Empty;
string strDeviceName = string.Empty;
string strDeviceID = string.Empty;
strDeviceType = VisioUtility.GetShapeCellValue(shape, "设备类型");
strDeviceID = VisioUtility.GetShapeCellValue(shape, "GUID");
ShapeLogInfo logInfo = new ShapeLogInfo();
logInfo.ShapeType = strDeviceType;
logInfo.ShapeID = shape.NameID;
logInfo.ShapeName = VisioUtility.GetShapeCellValue(shape, "名称");
if (shape.Application.IsUndoingOrRedoing)
{
ShapeLogInfo info = Portal.gc.gOperationLog.GetShape(strDeviceType, shape.NameID);
if (info != null)
{
strDeviceID = info.ShapeGuid;
}
}
logInfo.ShapeGuid = strDeviceID;
logInfo.ShapeStatus = ShapeStatus.DeleteShape;
Portal.gc.gOperationLog.AddLog(logInfo);
}
其中if (shape.Application.IsUndoingOrRedoing)是为了判断该操作是否为Undo或者Redo导致的操作,如果是,我们获取它之前Shape的GUID即可。
记录了设备的这些修改信息,我们就可以在保存数据的时候,根据这些信息移除相关的关系,添加或者删除相关的设备信息了,而且这些信息,对于我们记录用户的图纸修订记录也是必须要做的事情。
专注于代码生成工具、.Net/.NetCore 框架架构及软件开发,以及各种Vue.js的前端技术应用。著有Winform开发框架/混合式开发框架、微信开发框架、Bootstrap开发框架、ABP开发框架、SqlSugar开发框架等框架产品。
转载请注明出处:撰写人:伍华聪 http://www.iqidi.com
转载请注明出处:撰写人:伍华聪 http://www.iqidi.com