如何提取Visio图中XML格式的数据

guoyangchun2006 2007-12-14 06:13:15
用Visio2003画的Visio图可以保存为vdx扩展名的格式,这种格式的Visio图可以用记事本打开看到它XML格式的数据,请问我如何提取和分析这些数据,并对这些数据进行解码成为C#可以编程开发的数据格式,望哪位高人给与指点,在下非常感激,愿意奉献我所有的分数,以表本人之诚意!非常之急!
...全文
1046 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
time_is_life 2009-10-05
  • 打赏
  • 举报
回复
如何使用C#进行Visio二次开发
Visio Drawing Control控件使用
Visio开发必备
Visio SDK 2007
VisSDK.chm
Visio Code Samples Library.chm
Visio文档操作
查看ShapeSheet属性及帮助内容
宏的录制
Visio的几个重要对象
1、Application
2、Window (Application.ActiveWindow)
3、Document (Application.ActiveDocument)
4、Master、Shape

Visio XML格式文件分析
Master格式

Pages/Shapes格式

图纸的XML文档中,Master后面的Shapes集合中只有一个Shape对象。图纸的Shapes集合有多个对象,每个对象的NameU和Name值可能不一样,一般使用NameU。
Visio基本操作的实现
VisApplication = this.ctrlDrawing.Document.Application;
VisWindow = VisApplication.ActiveWindow;
VisDocument = VisApplication.ActiveDocument;

//Settings
VisApplication.Settings.ShowShapeSearchPane = false; //显示搜索形状窗体
VisApplication.Settings.EnableAutoConnect = false; //自动链接(2007默认)
VisApplication.Settings.StencilBackgroundColor = 10070188; //vbGrayText

//文档各种显示
VisApplication.DoCmd((short)VisUICmds.visCmdViewRulers);
VisApplication.DoCmd((short)VisUICmds.visCmdViewGrid);
VisApplication.DoCmd((short)VisUICmds.ShowGuides);
VisApplication.DoCmd((short)VisUICmds.ShowConnectPoints);
VisApplication.DoCmd((short)VisUICmds.ShowPageBreaks);

//各种窗口
VisApplication.DoCmd((short)VisUICmds.visCmdShapesWindow);
VisApplication.DoCmd((short)VisUICmds.visCmdPanZoom);
VisApplication.DoCmd((short)VisUICmds.visCmdCustProp);
VisApplication.DoCmd((short)VisUICmds.visCmdSizePos);

SendKeys.Send("^(x)");
//VisApplication.DoCmd((short)VisUICmds.visCmdUFEditCut);
SendKeys.Send("{DELETE}");
//VisApplication.DoCmd((short)VisUICmds.visCmdUFEditClear);
SendKeys.Send("^+(p)");
//VisApplication.DoCmd(VisUICmds.visCmdFormatPainter);
SendKeys.Send("^(z)");
//VisApplication.DoCmd(VisUICmds.visCmdEditUndo);

调用工具条对象、菜单对象的方法
Application.CommandBars
Microsoft.Office.Core.CommandBars共享Office对象模型
使用CommandBar代替UIObject
CommandBar对象中,菜单及工具条是同一个东西。CommandBar、CommandBarButton、CommandBarComboBox、CommandBarControl和CommandBarPopup。
示例:执行视图中的工具条的所有按钮事件。
Microsoft.Office.Core.CommandBars commandBars;
commandBars = (Microsoft.Office.Core.CommandBars)VisApplication.CommandBars;

foreach (Microsoft.Office.Core.CommandBarControl control in commandBars["View"].Controls)
{
Microsoft.Office.Core.CommandBarButton button =
control as Microsoft.Office.Core.CommandBarButton;
if (button != null)
{
button.Execute();
}
}

StringBuilder sb = new StringBuilder();
foreach (Microsoft.Office.Core.CommandBar bar in commandBars)
{
sb.Append(string.Format("CommandBar Name:{0}\r\n", bar.Name));
foreach(Microsoft.Office.Core.CommandBarControl control in bar.Controls)
{
Microsoft.Office.Core.CommandBarButton button = control as
Microsoft.Office.Core.CommandBarButton;
if(button != null)
{
sb.Append(string.Format("Button Name:{0} \r\n", button.Caption));
}
}
}
Form2 frm = new Form2();
frm.txtContent.Text = sb.ToString();
frm.Show();

short flags = (short)VisOpenSaveArgs.visOpenDocked | (short)VisOpenSaveArgs.visOpenRO;
StencilOpenEx(wndVisio.Application, flags);

/**////
/// 打开模具的公共方法
///
/// 按引用调用的VisioApplication对象
/// 打开的模式
private void StencilOpenEx(Application visApp, short flags)
{
List stencilList = GetStencils();
string stencilFileName;

foreach(string stencil in stencilList)
{
stencilFileName = GetStencilsFileName(stencil);
if(!string.IsNullOrEmpty(stencilFileName))
{
visApp.Documents.OpenEx(Portal.gc.gStencileFileBasePath + stencilFileName, flags);
}
}
}

//关闭模具文件
visApp.Documents["Switch.vss"].Close();
visApp.Documents["Span.vss"].Close();
visApp.Documents["Line.vss"].Close();
visApp.Documents["Label.vss"].Close();
visApp.Documents["Construct.vss"].Close();
visApp.Documents["Monitor.vss"].Close();
Visio Shape的属性操作
StringToFormulaForString、FormulaStringToString函数
访问属性
设置属性
添加属性
//列出模具组
this.cmbStencilGroup.Items.Clear();
List stencilGroups = stencil.GetStencils();
foreach (string group in stencilGroups)
{
this.cmbStencilGroup.Items.Add(group);
}

//根据模具组列出模具
string stencilName = stencil.GetStencilsFileName(this.cmbStencilGroup.Text);
this.cmbStencil.Items.Clear();
string tempName;
foreach (Master master in visApp.Documents[stencilName].Masters)
{
tempName = master.Name;
if (!stencil.IsExcludeItem(tempName))
{
this.cmbStencil.Items.Add(tempName);
}
}

//根据模具,获取对应的属性集合,遍历属性集合,列出属性名称
string stencilName = stencil.GetStencilsFileName(this.cmbStencilGroup.Text);
string masterName = this.cmbStencil.Text;
Visio.Shape shape = visApp.Documents[stencilName].Masters[masterName].Shapes[1];
if (shape != null)
{
List propInfos = property.GetPropertyCollection(shape);
foreach (StencilPropertyInfo info in propInfos)
{
this.cmbProperty.Items.Add(info.Name);
}
}

//根据模具、模具属性,列出对应的属性信息
string stencilName = stencil.GetStencilsFileName(this.cmbStencilGroup.Text);
string masterName = this.cmbStencil.Text;
Visio.Shape shape = visApp.Documents[stencilName].Masters[masterName].Shapes[1];
StencilPropertyInfo info = property.GetProperty(shape, this.cmbProperty.Text);
if (info != null)
{
this.txtName.Text = info.Name;//属性名称
this.txtValue.Text = info.Value;//属性值
this.txtFormat.Text = info.Format;//属性格式
this.txtSortKey.Text = info.Sort;//属性的排序
this.txtPrompt.Text = info.Prompt;//属性的提示信息
}

//根据模具,获取属性对象集合
public List GetPropertyCollection(Visio.Shape shape)
{
List list = new List();
StencilPropertyInfo propertyInfo;
Visio.Cell shapeCell;
short shortSectionProp = (short)VisSectionIndices.visSectionProp;

if (shape != null)
{
for (short i = 0; i < shape.get_RowCount(shortSectionProp) - 1; i++ )
{
if (shape.get_CellsSRCExists(shortSectionProp, i,
(short)VisCellIndices.visCustPropsLabel, 0) != 0)
{
propertyInfo = new StencilPropertyInfo();

shapeCell = shape.get_CellsSRC(shortSectionProp, i,
(short)VisCellIndices.visCustPropsLabel);
propertyInfo.Name = VisioUtility.FormulaStringToString(shapeCell.RowNameU);

shapeCell = shape.get_CellsSRC(shortSectionProp, i,
(short)VisCellIndices.visCustPropsPrompt);
propertyInfo.Prompt = VisioUtility.FormulaStringToString(shapeCell.FormulaU);

shapeCell = shape.get_CellsSRC(shortSectionProp, i,
(short)VisCellIndices.visCustPropsFormat);
propertyInfo.Format = VisioUtility.FormulaStringToString(shapeCell.FormulaU);

shapeCell = shape.get_CellsSRC(shortSectionProp, i,
(short)VisCellIndices.visCustPropsValue);
propertyInfo.Value = VisioUtility.FormulaStringToString(shapeCell.FormulaU);

shapeCell = shape.get_CellsSRC(shortSectionProp, i,
(short)VisCellIndices.visCustPropsSortKey);
propertyInfo.Sort = VisioUtility.FormulaStringToString(shapeCell.FormulaU);

//shapeCell = shape.get_CellsSRC(shortSectionProp, i,
(short)VisCellIndices.visCustPropsType);
//propertyInfo.PropType = VisioUtility.FormulaStringToString(shapeCell.FormulaU);

//shapeCell = shape.get_CellsSRC(shortSectionProp, i,
(short)VisCellIndices.visCustPropsInvis);
//propertyInfo.InVisible = VisioUtility.FormulaStringToString(shapeCell.FormulaU);

//..
list.Add(propertyInfo);
}
}
}

return list;
}

//根据模具和属性名称,获取属性对象信息
public StencilPropertyInfo GetProperty(Visio.Shape shape, string propertyName)
{
List list = GetPropertyCollection(shape);
StencilPropertyInfo propertyInfo = null;
foreach(StencilPropertyInfo tempInfo in list)
{
if (tempInfo.Name == propertyName)
{
propertyInfo = tempInfo;
break;
}
}

return propertyInfo;
}
guoyangchun2006 2007-12-17
  • 打赏
  • 举报
回复
多谢各位高人来指点,在此多谢各位了,都来顶顶哦!
wsj1983920 2007-12-14
  • 打赏
  • 举报
回复
友情帮顶一下~~~

111,082

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧