Element selectedElement = doc.GetElement(selectedRef);
时间: 2023-08-10 17:03:13 浏览: 47
这段代码是使用 Revit API 中的方法从文档中获取一个元素。其中,`doc` 是 `Document` 类型的变量,表示当前打开的 Revit 文档;`selectedRef` 是 `Reference` 类型的变量,表示一个元素的引用。最终,`selectedElement` 会存储获取到的元素对象。
相关问题
使用bpmnjs在点击选中的节点正下方设置一个bpmn:TextAnnotation,内容填测试
可以通过以下代码在选中节点正下方添加一个bpmn:TextAnnotation元素,内容为“测试”:
```javascript
// 获取选中的节点
var elementRegistry = viewer.get('elementRegistry');
var selectedElement = elementRegistry.get(selection.get());
// 获取选中节点的位置信息
var bbox = selectedElement.getBBox();
// 添加TextAnnotation元素
var modeling = viewer.get('modeling');
var newElementData = {
type: 'bpmn:TextAnnotation',
text: '测试'
};
var newElement = modeling.createShape(newElementData, {
x: bbox.x + bbox.width / 2,
y: bbox.y + bbox.height + 50
}, selectedElement.parent);
// 选中新添加的TextAnnotation元素
selection.select(newElement);
```
其中,`viewer`是BpmnJS的实例,`selection`是当前选中的元素。在上面的代码中,我们首先获取了选中节点的位置信息,然后在其正下方添加了一个TextAnnotation元素,内容为“测试”。最后将新添加的TextAnnotation元素选中,以便用户可以编辑它。
WPF 中向Canvas动态添加控件,该控件要求在被点击的时候有虚线方框包裹控件,用鼠标拖动边框的四条边可以改变边框大小且动态改变控件的大小,但是边框及边框内控件在Canvas上的定位不发生变化。点击Canvas空白处时,取消掉包裹控件的边框
你可以实现一个自定义的控件类,继承自Canvas,然后在该类中实现鼠标事件的处理。在该类中,你需要维护一个当前被包裹的控件的引用,以及该控件的边框。
在鼠标点击事件中,你需要判断当前鼠标点击的位置是否在某个子控件上,如果是,则将该子控件作为当前被包裹的控件,并创建边框并添加到Canvas上。在鼠标移动事件中,你需要判断当前是否在拖动边框,如果是,则改变边框大小,并动态调整当前被包裹的控件的大小。在鼠标释放事件中,你需要将当前被包裹的控件的引用和边框清空。
以下是一个简单的示例代码,可以实现你的需求:
```csharp
public class ResizableCanvas : Canvas
{
private FrameworkElement _selectedElement;
private Rectangle _selectionBorder;
public ResizableCanvas()
{
this.MouseLeftButtonDown += OnMouseLeftButtonDown;
this.MouseLeftButtonUp += OnMouseLeftButtonUp;
this.MouseMove += OnMouseMove;
this.MouseLeave += OnMouseLeave;
}
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// 判断是否在子控件上点击
var element = e.Source as FrameworkElement;
if (element != null)
{
_selectedElement = element;
_selectionBorder = new Rectangle
{
Stroke = Brushes.Black,
StrokeDashArray = new DoubleCollection(new double[] { 4, 4 }),
StrokeThickness = 1,
Fill = Brushes.Transparent
};
this.Children.Add(_selectionBorder);
Canvas.SetLeft(_selectionBorder, Canvas.GetLeft(_selectedElement));
Canvas.SetTop(_selectionBorder, Canvas.GetTop(_selectedElement));
_selectionBorder.Width = _selectedElement.ActualWidth;
_selectionBorder.Height = _selectedElement.ActualHeight;
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
// 判断是否在拖动边框
if (_selectedElement != null && _selectionBorder != null)
{
var pos = e.GetPosition(this);
var dx = pos.X - Canvas.GetLeft(_selectedElement);
var dy = pos.Y - Canvas.GetTop(_selectedElement);
var newWidth = Math.Max(dx, 0);
var newHeight = Math.Max(dy, 0);
_selectionBorder.Width = newWidth;
_selectionBorder.Height = newHeight;
_selectedElement.Width = newWidth;
_selectedElement.Height = newHeight;
}
}
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// 清空选中状态
_selectedElement = null;
if (_selectionBorder != null)
{
this.Children.Remove(_selectionBorder);
_selectionBorder = null;
}
}
private void OnMouseLeave(object sender, MouseEventArgs e)
{
// 取消选中状态
_selectedElement = null;
if (_selectionBorder != null)
{
this.Children.Remove(_selectionBorder);
_selectionBorder = null;
}
}
}
```
在使用该控件时,你可以像使用普通的Canvas一样添加子控件,例如:
```xml
<local:ResizableCanvas>
<Button Content="Button 1" Width="100" Height="50" Canvas.Left="50" Canvas.Top="50"/>
<Button Content="Button 2" Width="100" Height="50" Canvas.Left="150" Canvas.Top="150"/>
</local:ResizableCanvas>
```