locked
How to Dynamically Add Object to Viewport? RRS feed

  • Question

  • I have the following code:

        Private Function ParseXAML(ByVal O As Object, ByVal XAML As String) As Object

            Dim newChild As ModelVisual3D
            Dim ThisXAML As String = XAML
            Dim X As XElement

            Try

                X = XElement.Load(New StringReader(ThisXAML))
                Dim thisXAMLReader As New XamlReader
                newChild = CType(XamlReader.Load(X.CreateReader), ModelVisual3D)
                O.Children.Add(newChild)

            Catch ex As Exception

                MessageBox.Show(ex.Message)
                Return Nothing

            End Try

        End Function

        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button5.Click
            Dim X As XElement = _
        <ModelVisual3D
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
            x:Name="World"
            >
            <ModelVisual3D x:Name="AmbientLightContainer">
                <ModelVisual3D.Content>
                    <AmbientLight x:Name="AmbientLight" Color="#FF7F7F7F"/>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="DirectionalLightContainer">
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="DirectionalLight" Color="#FF3F3F3F" Direction="0,0,-1">
                        <DirectionalLight.Transform>
                            <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="3"/>
                        </DirectionalLight.Transform>
                    </DirectionalLight>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="DefaultGroup">
                <ModelVisual3D.Content>
                    <GeometryModel3D x:Name="Suzanne" d:Bounds="-1.36718797683716,-0.851562976837158,-0.984375,2.73437595367432,1.70312595367432,1.96875">
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="0.46875,0.75781298,-0.24218801 0.4375,0.765625,-0.16406301 503 491 364 491 381 493 504 367 493 367 384 379 381 491 379 491 410 493 384 380 493 380 412"/>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial Brush="#FFFFFFFF"/>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
                <ModelVisual3D.Transform>
                    <Transform3DGroup>
                        <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
                        <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1"/>
                        <RotateTransform3D d:EulerAngles="0,0,0"/>
                    </Transform3DGroup>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
        </ModelVisual3D>

            Dim XPath As String

            'XPath = "./ModelVisual3D/ModelVisual3D/ModelVisual3D.Transform/Transform3DGroup/TranslateTransform3D"

            XPath = "//Transform3DGroup/TranslateTransform3D"

            X.XPathSelectElement(XPath).SetAttributeValue("OffsetX", "5")

            'ParseXAML(V, X.ToString)

            'Console.WriteLine(X.ToString)

            Dim reader As XmlReader = X.CreateReader
            Dim nameTable As XmlNameTable = reader.NameTable
            Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nameTable)
            namespaceManager.AddNamespace("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
            Dim child1 As XElement = X.XPathSelectElement("//Transform3DGroup/TranslateTransform3D", namespaceManager)

            child1.SetAttributeValue("OffsetX", "1")

            namespaceManager.RemoveNamespace("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")

            ParseXAML(V, X.ToString)

        End Sub


    And when I try to run it without this import at the beginning:

    Imports <xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

    I get the folloiwng error:

    The tag 'ModelVisual3D' does not exist in XML namespace ''. Line '0' Position '0'.


    at:

    newChild = CType(XamlReader.Load(X.CreateReader), ModelVisual3D)


    And if I add the import then i get the following error:

    Object reference not set to an instance of an object.


    at:

     X.XPathSelectElement(XPath).SetAttributeValue("OffsetX", "5")


    Could someone help me resolve it please?

    Many thanks


    Q
    • Edited by QuddusAli Wednesday, December 17, 2008 3:02 PM
    Saturday, December 6, 2008 12:51 AM

All replies

  • I hope this will help you
    http://www.kindohm.com/technical/WPF3DTutorial.htm
    Prasad - www.beautifulmind.blog.co.in
    Saturday, December 6, 2008 2:28 AM
  • Unfortunately I dont think it helps.
    Saturday, December 6, 2008 2:30 AM
  • Is it allowed to bump a thread in this forum?
    Wednesday, December 17, 2008 2:59 PM
  • Howdy,

    It looks like you are trying to write the XAML in your code file and add it?

    XAML lives in XAML files and not code files.

    If you want to add the objects that your XAML is declaring, you need to create instances of them in code and then add those instances as child elements to the relevant parents.


    So, in XAML where you create a button such as

    <Button Content="Hello"/>

    In code you create

    Dim myButton as Button


    My adivce to you is to get yourself a couple of good books on coding (VB.Net is your choice) as well as a good book on understanding XAML and start to learn and understand what both do from the beginning.

    Hope this helps to give you some direction.

    Good luck

    Cheers

    Expression MVP

    Learn Expression Blend at learnexpressionstudio.com

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Thursday, December 18, 2008 1:49 AM
    Moderator
  • thanks for the reply. The method you are suggesting IMHO is a bit long winded. The code i wrote actually works - except when i try to change a value or add the import statement. if i dont try to modify the xelement then i can successfully add the model to the viewport.

    i might be wrong - but there must be a way to what im trying to do.
    Thursday, December 18, 2008 4:03 PM