2

I am new to WPF, and am trying to build a 3d viewport with helix toolkit. The viewport, grid etc show up as expected, And I add a simple tube. Now, i want to use a new function to update the Transform of the tube, based on user input, but I cannot get it to work.

Where am i going wrong here?

Thank you.

.xaml

  <UserControl x:Class="WPFUserControl.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf" 
             mc:Ignorable="d"
             d:DesignHeight="480" d:DesignWidth="640">
    <Grid>
        <!-- The HelixViewport3D supports camera manipulation, and can be used just like the Viewport3D -->
        <HelixToolkit:HelixViewport3D ZoomExtentsWhenLoaded="True" Name="MainViewPort">

            <HelixToolkit:SunLight/>

            <ModelVisual3D Content="{Binding Model}"/>

            <HelixToolkit:GridLinesVisual3D Width="40" Length="40" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>

        </HelixToolkit:HelixViewport3D>
    </Grid>
</UserControl>

.xaml.cs

using HelixToolkit.Wpf;

namespace WPFUserControl
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public TubeVisual3D tube1 = new TubeVisual3D();

        public UserControl1()
        {

            InitializeComponent();
            DataContext = this;
            Setup();

        }


        public void SetTranslation(double xx) //this is called from an external app.
        {

             Console.Write(xx); //This prints as expected.

            //TranslateTransform3D Trans = new TranslateTransform3D(new Vector3D(xx, 0, 0));     
          //  tube1.Transform = Trans;  //this does not work.

        }

        public void Setup()
        {             
            int tubeDiameter = 5;
            tube1.Path = new Point3DCollection();
            tube1.Path.Add(new Point3D(-15, 0, 0));
            tube1.Path.Add(new Point3D(15, 0, 0));
            tube1.Diameter = tubeDiameter;
            tube1.Fill = Brushes.Red;
            tube1.IsPathClosed = false;

            MainViewPort.Children.Add(tube1);

        }

    }
}
anti
  • 3,011
  • 7
  • 36
  • 86
  • 1
    You have to be on the Dispatcher Thread. Try Dispatcher.Invoke( – egse Jul 22 '16 at 11:53
  • Thanks for your response! So in my SetTranslation function, I add something like: `Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { tube1.Transform = Trans; }));` Does that look right? – anti Jul 22 '16 at 12:20
  • 1
    As far as I can remember UserControl has a Dispatcher Member. So you don't need the Application.Current. But this should do it as well – egse Jul 22 '16 at 12:22
  • Ok great. Thank you again for your help. – anti Jul 22 '16 at 12:24
  • So I have added the above code to the function, (without Application.Current) and i get this error: 'The calling thread cannot access this object because a different thread owns it.' – anti Jul 22 '16 at 17:50
  • I have: `Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { tube1.Transform = Trans; }));` I have also tried: `this.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { tube1.Transform = Trans; }));` Am I going wrong somewhere? thanks again. – anti Jul 22 '16 at 18:14
  • 1
    Create the TranslationTransform inside the curly brakets: Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { TranslateTransform3D Trans = new TranslateTransform3D(new Vector3D(xx, 0, 0)); tube1.Transform = Trans; })); – egse Jul 25 '16 at 05:48
  • perfect. Thank you again. – anti Jul 25 '16 at 08:30

0 Answers0