Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,685 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
hi.
I want to add an ObservableCollection of Lines and ObservableCollection of arcsegment to Viewport3d.
the goal is to draw arcs based on the Lines so whenever line.x/line.y updated it automatically updates the arcs,
however my question is not about how to draw arcs or lines my question is how to add an ObservableCollection of Lines to Viewport3d.
thanks in advance, any help would be appreciated.
I'm using c#, wpfCore3.1, MS VS2019.
Hi, if you want see line you must create triangles. Try following code:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position="-10,20,10" LookDirection="40,-40,-40" UpDirection="0,0,1"/>
</Viewport3D.Camera>
<ModelVisual3D Content="{Binding MGroup}"/>
</Viewport3D>
public class ViewModel
Hi, if you want see line you must create triangles. try following code:
{
public Model3DGroup MGroup { get => GetData(); }
private ObservableCollection<Line> col = new ObservableCollection<Line>();
private Model3DGroup GetData()
{
// collection of lines
Point pt1 = new Point(0, 0);
Point pt2 = new Point(10, 0);
for (int i = 0; i < 10; i++) col.Add(new Line() { X1 = pt1.X, X2 = pt2.X, Y1 = pt1.Y + i, Y2 = pt2.Y + i });
// create ModelGroup
Model3DGroup gr = new Model3DGroup();
gr.Children.Add(new DirectionalLight(Colors.White, new Vector3D(-1, -1, -3)));
foreach (var item in col)
{
GeometryModel3D mod = AddLine(new Point3D(item.X1, item.Y1, 0), new Point3D(item.X2, item.Y2, 0));
gr.Children.Add(mod);
}
return gr;
}
private GeometryModel3D AddLine(Point3D startPoint, Point3D endPoint)
{
var material = new DiffuseMaterial(new SolidColorBrush(Colors.Red));
var mesh = new MeshGeometry3D();
mesh.Positions.Add(startPoint);
mesh.Positions.Add(endPoint);
mesh.Positions.Add(new Point3D(startPoint.X, startPoint.Y + .1, startPoint.Z));
mesh.Positions.Add(new Point3D(endPoint.X, endPoint.Y + .1, endPoint.Z));
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(0);
return new GeometryModel3D(mesh, material);
}
}
public class MyViewPort
{
private Viewport3D _vPort;
public Viewport3D VPort
{
get => this._vPort;
set
{
if (this._vPort == null)
{
this._vPort = value;
this._vPort.MouseDown += _vPort_MouseDown;
this._vPort.MouseMove += _vPort_MouseMove;
this._vPort.MouseUp += _vPort_MouseUp;
this._vPort.Loaded += _vPort_Loaded;
}
}
}
private AxisAngleRotation3D rot = new AxisAngleRotation3D(new Vector3D(0, 2, 0), 0);
private void _vPort_Loaded(object sender, RoutedEventArgs e)
{
var mod = this._vPort.Children[0] as ModelVisual3D;
mod.Transform = new RotateTransform3D(rot);
}
private Point pt = new Point(double.NaN, double.NaN);
private void _vPort_MouseDown(object sender, MouseButtonEventArgs e) => pt = e.GetPosition(this._vPort);
private void _vPort_MouseMove(object sender, MouseEventArgs e)
{
if (!double.IsNaN(pt.X))
{
Point pt1 = e.GetPosition(this._vPort);
double ang = pt1.X - pt.X;
rot.Angle = ang;
}
}
private void _vPort_MouseUp(object sender, MouseButtonEventArgs e) => pt = new Point(double.NaN, double.NaN);
}