将 Visio 形状的 x、y 位置设置为模板文件 C# 中的图形
Setting x,y position of Visio shape to a graph from a template file C#
我有一个模板文件 (.vsdx),其中包含一个带有固定 x 轴和 y 轴的图表,我将其加载到新的 Visio 文档中。我已经设法在 Visio 文档中插入一个形状,但它没有根据图形的 x 轴和 y 轴定位。
示例:将坐标为 0,0 的 vshape 设置为文档的左下角边缘。
到目前为止我有以下代码:
//decalre and initialize Visio objects
var vApp = new Visio.Application();
Visio.Document vDoc, vStencil;
Visio.Page vPage;
Visio.Shape vToShape, vFromShape, vConnector;
Visio.Master vConnectorMaster, vFlowChartMaster;
double dblXLocation;
double dblYLocation;
Visio.Cell vBeginCell, vEndCell;
int iCount;
string TEMPLATEPATH = @"C:\temp\TestProject\testTemplate.vsdx";
//Change this constant to match your choice of location and file name.
string SAVENEWFILE = @"C:\temp\TestProject\testFile.vsdx";
vFlowChartMaster = vStencil.Masters[aryValues[0, 0]];
dblXLocation = 1;
dblYLocation = 1;
vToShape = vPage.Drop(vFlowChartMaster,
dblXLocation, dblYLocation);
vToShape.Text = "Test";
vDoc.Pages[1].Name = "Flowchart Example";
try
{
//Delete the previous version of the file.
//Kill(SAVENEWFILE);
File.Delete(SAVENEWFILE);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
vDoc.SaveAs(SAVENEWFILE);
vDoc.Close();
vApp.Quit();
vDoc = null;
vApp = null;
GC.Collect();
加载到 Visio 文档的图表是 here
好的,感谢更新评论。在这种情况下,这里有一个快速示例。我创建了一个绘图,其中包含一个基本的 'Graph' 主形状,它定义了一个原点,还有一个 'Dot' 主形状,它只是一个小圆圈,可以作为 dta 标记。
代码 (using LINQPad) 查找 Graph master 的第一个实例,然后查找 'known' 单元格(由您定义)以获取原点。然后相对于图形原点放置两个 'Dot' 形状。
图形形状如下所示:
[注意 - 您可以在 X 或 Y 单元格中引用 PNT 类型,Visio 将提取相应的 X 或 Y 坐标]
void Main()
{
var vApp = MyExtensions.GetRunningVisio();
var vPag = vApp.ActivePage;
var graphShp = vPag.Shapes.Cast<Visio.Shape>()
.FirstOrDefault(s => s.Master?.Name == "Graph");
if (graphShp != null)
{
var dotMst = vPag.Document.Masters["Dot"];
//Get x / y back as a named tuple
var origin = GetGraphOrigin(graphShp);
//Green fill is the default defined in the master
var greenDotShp = vPag.Drop(dotMst, origin.x, origin.y);
//Use offest based on graph origin
var redDotOffsetX = -0.5;
var redDotOffsetY = 0.25;
var redDotShp = vPag.Drop(dotMst, origin.x + redDotOffsetX, origin.y + redDotOffsetY);
redDotShp.CellsU["FillForegnd"].FormulaU = "RGB(200,40,40)";
}
}
private (double x, double y) GetGraphOrigin(Visio.Shape targetShp)
{
const string originX = "User.OriginOnPageX";
const string originY = "User.OriginOnPageY";
if (targetShp == null)
{
throw new ArgumentNullException();
}
if (targetShp.CellExistsU[originX, (short)Visio.VisExistsFlags.visExistsAnywhere] != 0
&& targetShp.CellExistsU[originY, (short)Visio.VisExistsFlags.visExistsAnywhere] != 0)
{
return (x: targetShp.CellsU[originX].ResultIU,
y: targetShp.CellsU[originY].ResultIU);
}
return default;
}
因此,如果您 运行 这段代码,您最终应该得到这样的结果(假设您从上面描述的 a 绘图开始):
所以有很多方法可以解决这个问题,但可能您需要一些方法或阅读图形形状中原点的位置,然后使用它来定位您的 'dot' 形状。
我有一个模板文件 (.vsdx),其中包含一个带有固定 x 轴和 y 轴的图表,我将其加载到新的 Visio 文档中。我已经设法在 Visio 文档中插入一个形状,但它没有根据图形的 x 轴和 y 轴定位。 示例:将坐标为 0,0 的 vshape 设置为文档的左下角边缘。
到目前为止我有以下代码:
//decalre and initialize Visio objects
var vApp = new Visio.Application();
Visio.Document vDoc, vStencil;
Visio.Page vPage;
Visio.Shape vToShape, vFromShape, vConnector;
Visio.Master vConnectorMaster, vFlowChartMaster;
double dblXLocation;
double dblYLocation;
Visio.Cell vBeginCell, vEndCell;
int iCount;
string TEMPLATEPATH = @"C:\temp\TestProject\testTemplate.vsdx";
//Change this constant to match your choice of location and file name.
string SAVENEWFILE = @"C:\temp\TestProject\testFile.vsdx";
vFlowChartMaster = vStencil.Masters[aryValues[0, 0]];
dblXLocation = 1;
dblYLocation = 1;
vToShape = vPage.Drop(vFlowChartMaster,
dblXLocation, dblYLocation);
vToShape.Text = "Test";
vDoc.Pages[1].Name = "Flowchart Example";
try
{
//Delete the previous version of the file.
//Kill(SAVENEWFILE);
File.Delete(SAVENEWFILE);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
vDoc.SaveAs(SAVENEWFILE);
vDoc.Close();
vApp.Quit();
vDoc = null;
vApp = null;
GC.Collect();
加载到 Visio 文档的图表是 here
好的,感谢更新评论。在这种情况下,这里有一个快速示例。我创建了一个绘图,其中包含一个基本的 'Graph' 主形状,它定义了一个原点,还有一个 'Dot' 主形状,它只是一个小圆圈,可以作为 dta 标记。
代码 (using LINQPad) 查找 Graph master 的第一个实例,然后查找 'known' 单元格(由您定义)以获取原点。然后相对于图形原点放置两个 'Dot' 形状。
图形形状如下所示:
[注意 - 您可以在 X 或 Y 单元格中引用 PNT 类型,Visio 将提取相应的 X 或 Y 坐标]
void Main()
{
var vApp = MyExtensions.GetRunningVisio();
var vPag = vApp.ActivePage;
var graphShp = vPag.Shapes.Cast<Visio.Shape>()
.FirstOrDefault(s => s.Master?.Name == "Graph");
if (graphShp != null)
{
var dotMst = vPag.Document.Masters["Dot"];
//Get x / y back as a named tuple
var origin = GetGraphOrigin(graphShp);
//Green fill is the default defined in the master
var greenDotShp = vPag.Drop(dotMst, origin.x, origin.y);
//Use offest based on graph origin
var redDotOffsetX = -0.5;
var redDotOffsetY = 0.25;
var redDotShp = vPag.Drop(dotMst, origin.x + redDotOffsetX, origin.y + redDotOffsetY);
redDotShp.CellsU["FillForegnd"].FormulaU = "RGB(200,40,40)";
}
}
private (double x, double y) GetGraphOrigin(Visio.Shape targetShp)
{
const string originX = "User.OriginOnPageX";
const string originY = "User.OriginOnPageY";
if (targetShp == null)
{
throw new ArgumentNullException();
}
if (targetShp.CellExistsU[originX, (short)Visio.VisExistsFlags.visExistsAnywhere] != 0
&& targetShp.CellExistsU[originY, (short)Visio.VisExistsFlags.visExistsAnywhere] != 0)
{
return (x: targetShp.CellsU[originX].ResultIU,
y: targetShp.CellsU[originY].ResultIU);
}
return default;
}
因此,如果您 运行 这段代码,您最终应该得到这样的结果(假设您从上面描述的 a 绘图开始):
所以有很多方法可以解决这个问题,但可能您需要一些方法或阅读图形形状中原点的位置,然后使用它来定位您的 'dot' 形状。