C# 在图表屏幕上居中显示DSL形状
首先,对不起我的英语 我正在开发一个DSL。在主窗口中,我有两个面板:形状列表和图表本身。我想,单击列表中的项目时,将其形状居中显示在屏幕上(图表面板) 我不想将形状移动到图表的中心。我想滚动图表以使形状居中C# 在图表屏幕上居中显示DSL形状,c#,dsl,visual-studio-sdk,dsl-tools,vssdk,C#,Dsl,Visual Studio Sdk,Dsl Tools,Vssdk,首先,对不起我的英语 我正在开发一个DSL。在主窗口中,我有两个面板:形状列表和图表本身。我想,单击列表中的项目时,将其形状居中显示在屏幕上(图表面板) 我不想将形状移动到图表的中心。我想滚动图表以使形状居中 valueListAssociation.LocateInDiagram(); 另一种解释方法是:当发生验证错误时(底部的错误列表窗口),当单击错误时,它会将无效形状集中在屏幕上,对吗?这就是我想要的 起点: private void symbolsListBox_SelectedInd
valueListAssociation.LocateInDiagram();
另一种解释方法是:当发生验证错误时(底部的错误列表窗口),当单击错误时,它会将无效形状集中在屏幕上,对吗?这就是我想要的
起点:
private void symbolsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var listBox = sender as ListBox;
var symbol = listBox.SelectedItem as Symbol;
var compartment = PresentationViewsSubject.GetPresentation(symbol)
.FirstOrDefault() as SymbolCompartmentShape;
var diagram = docView.CurrentDiagram;
//Next step, center the shape
//How? God please help me!!!
}
图像示例总是更好!!所以…
这里是我想要的一个示例图像
下面是预期结果的示例图像
您可以在图表中找到形状并选择它。如有必要,图表将自动使其可见 这是我使用的代码。请注意,这是从ModelElement(而不是形状)开始的 现在扩展方法:
public static bool LocateInDiagram(this ModelElement element)
{
// Validation
SmartGuard.NotNull(() => element, element);
// Get the diagram view
DiagramView diagramView = element.GetActiveDiagramView();
if (diagramView != null)
{
// Select it
return diagramView.SelectModelElement(element);
}
// Default result
return false;
}
public static DiagramView GetActiveDiagramView(this ModelElement element)
{
// Validation
SmartGuard.NotNull(() => element, element);
// Get the shape that corresponds to this model element
ShapeElement shapeElement = element.GetShapeElement();
if (shapeElement != null)
{
// Result
return shapeElement.GetActiveDiagramView();
}
// Default result
return null;
}
public static ShapeElement GetShapeElement(this ModelElement element)
{
// Validation
SmartGuard.NotNull(() => element, element);
// Get the first shape
// If the model element is in a compartment the result will be null
ShapeElement shape = element.GetFirstShapeElement();
if (shape == null)
{
// If the element is in a compartment, try to get the parent model element to select that
ModelElement parentElement = element.GetCompartmentElementFirstParentElement();
if (parentElement != null)
{
shape = parentElement.GetFirstShapeElement();
}
}
// Result
return shape;
}
private static ShapeElement GetFirstShapeElement(this ModelElement element)
{
// Presentation elements
LinkedElementCollection<PresentationElement> presentations = PresentationViewsSubject.GetPresentation(element);
foreach (PresentationElement presentation in presentations)
{
ShapeElement shapeElement = presentation as ShapeElement;
if (shapeElement != null)
{
return shapeElement;
}
}
// Default result
return null;
}
private static ModelElement GetCompartmentElementFirstParentElement(this ModelElement modelElement)
{
// Get the domain class associated with model element.
DomainClassInfo domainClass = modelElement.GetDomainClass();
if (domainClass != null)
{
// A element is only considered to be in a compartment if it participates in only 1 embedding relationship
// This might be wrong for some models
if (domainClass.AllEmbeddedByDomainRoles.Count == 1)
{
DomainRoleInfo roleInfo = domainClass.AllEmbeddedByDomainRoles[0];
if (roleInfo != null)
{
// Get a collection of all the links to this model element
// Since this is in a compartment there should be at least one
// There can be only one.
ReadOnlyCollection<ElementLink> links = roleInfo.GetElementLinks(modelElement);
if (links.Count == 1)
{
// Get the model element participating in the link that isn't the current one
// That will be the parent
// Probably there is a better way to achieve the same result
foreach (ModelElement linkedElement in links[0].LinkedElements)
{
if (!modelElement.Equals(linkedElement))
{
return linkedElement;
}
}
}
}
}
}
// Default result
return null;
}
public static DiagramView GetActiveDiagramView(this ShapeElement shape)
{
// Validation
SmartGuard.NotNull(() => shape, shape);
// Result
if (shape.Diagram != null)
{
return shape.Diagram.ActiveDiagramView;
}
// Default result
return null;
}
public static bool SelectModelElement(this DiagramView diagramView, ModelElement modelElement, bool ensureVisible)
{
// Validation
SmartGuard.NotNull(() => diagramView, diagramView);
SmartGuard.NotNull(() => modelElement, modelElement);
// Get the shape element that corresponds to the model element
ShapeElement shapeElement = modelElement.GetPresentation<ShapeElement>();
if (shapeElement != null)
{
// Make sure the shape element is visible (because connectors can be hidden)
if (!shapeElement.IsVisible)
{
shapeElement.Show();
}
// Create a diagram item for this shape element and select it
diagramView.SelectDiagramItem(new DiagramItem(shapeElement), ensureVisible);
return true;
}
// If the model element does not have a shape, try to cast it IModelElementCompartmented
IModelElementCompartmented compartmentedModelElement = modelElement as IModelElementCompartmented;
if (compartmentedModelElement != null)
{
// Get the parent
IModelElementWithCompartments parentModelElement = compartmentedModelElement.ParentModelElement;
if (parentModelElement != null)
{
// Get the compartment that stores the model element
ElementListCompartment compartment = parentModelElement.GetCompartment(compartmentedModelElement.CompartmentName);
if (compartment == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.RES_Error_CannotFindCompartment, compartmentedModelElement.CompartmentName));
}
// Expand the compartment?
if (!compartment.IsExpanded)
{
using (Transaction trans = modelElement.Store.TransactionManager.BeginTransaction("IsExpanded"))
{
compartment.IsExpanded = true;
trans.Commit();
}
}
// Find the model element in the compartment
int index = compartment.Items.IndexOf(modelElement);
if (index >= 0)
{
// Create a diagram item and select it
diagramView.SelectDiagramItem(new DiagramItem(compartment, compartment.ListField, new ListItemSubField(index)), ensureVisible);
// Result OK
return true;
}
}
}
// Default result
return false;
}
public static bool LocateInDiagram(此ModelElement元素)
{
//验证
SmartGuard.NotNull(()=>元素,元素);
//获取图表视图
DiagramView DiagramView=元素。GetActiveDiagramView();
如果(diagramView!=null)
{
//选择它
返回diagramView。选择ModelElement(元素);
}
//默认结果
返回false;
}
公共静态DiagramView GetActiveDiagramView(此ModelElement元素)
{
//验证
SmartGuard.NotNull(()=>元素,元素);
//获取与此模型图元对应的形状
ShapeElement ShapeElement=element.GetShapeElement();
if(shapeElement!=null)
{
//结果
返回shapeElement.GetActiveDiagramView();
}
//默认结果
返回null;
}
公共静态ShapeElement GetShapeElement(此ModelElement元素)
{
//验证
SmartGuard.NotNull(()=>元素,元素);
//得到第一个形状
//如果模型元素位于隔室中,则结果将为空
ShapeElement shape=element.GetFirstShapeElement();
if(shape==null)
{
//如果图元位于隔间中,请尝试让父模型图元选择该隔间
ModelElement parentElement=element.getPartitionElementFirstParentElement();
if(parentElement!=null)
{
shape=parentElement.GetFirstShapeElement();
}
}
//结果
返回形状;
}
私有静态ShapeElement GetFirstShapeElement(此ModelElement元素)
{
//表示元素
LinkedElementCollection presentations=PresentationViewsObject.GetPresentation(元素);
foreach(演示文稿中的PresentationElement演示文稿)
{
ShapeElement=表示为ShapeElement;
if(shapeElement!=null)
{
返回形状元素;
}
}
//默认结果
返回null;
}
私有静态ModelElement GetPartitionElementFirstParentElement(此ModelElement ModelElement)
{
//获取与模型元素关联的域类。
DomainClassInfo domainClass=modelElement.GetDomainClass();
if(domainClass!=null)
{
//只有当一个元素只参与一个嵌入关系时,它才被认为是在一个隔间中
//对于某些型号来说,这可能是错误的
if(domainClass.AllEmbeddedByDomainRoles.Count==1)
{
DomainRoleInfo roleInfo=domainClass.AllEmbeddedByDomainRoles[0];
if(roleInfo!=null)
{
//获取指向此模型元素的所有链接的集合
//因为这是在一个隔间里,所以至少应该有一个
//只能有一个。
ReadOnlyCollection links=roleInfo.GetElementLinks(modelElement);
如果(links.Count==1)
{
//获取参与非当前链接的模型元素
//那将是父母
//也许有更好的方法可以达到同样的效果
foreach(链接[0]中的ModelElement linkedElement.LinkedElements)
{
如果(!modelement.Equals(linkedElement))
{
返回链接删除;
}
}
}
}
}
}
//默认结果
返回null;
}
公共静态图表视图GetActiveDiagramView(此形状元素形状)
{
//验证
SmartGuard.NotNull(()=>shape,shape);
//结果
如果(shape.Diagram!=null)
{
返回shape.Diagram.ActiveDiagramView;
}
//默认结果
返回null;
}
公共静态bool SelectModelElement(此图视图DiagramView、ModelElement、bool可修改)
{
//验证
SmartGuard.NotNull(()=>diagramView,diagramView);
SmartGuard.NotNull(()=>modelement,modelement);
//获取与模型元素对应的形状元素
ShapeElement ShapeElement=modelement.GetPresentation();
if(shapeElement!=null)
{
//确保图形元素可见(因为可以隐藏连接符)
如果(!shapeElement.IsVisible)
{
shapeElement.Show();
}
//为此形状元素创建一个图表项并将其选中
diagramView。选择DiagramItem(新DiagramItem(shapeElement),确保可修改;
返回true;
}
//如果模型图元没有形状,请尝试将其强制转换为IModelementCharacterized
IModelementChairated ChairatedModelElement=作为IModelementChairated的模型元素;
if(分隔模型元素!=null)
{
//找到父母
IModelementWithCarries parentModelElement=分隔模型元素