4

I am using the Helixtoolkit.WPF in my C# program. I have imported the NuGet package and it is working perfectly. However, I am wanting to edit one of the files, specifically GridLinesVisual.cs. I want to change how one of the functions in that file operates, but can’t seem to get it working.

The function I need to change begins on line 247 protected override MeshGeometry3D Tessellate()

Here is a link to the file I need to update/change https://searchcode.com/codesearch/view/10564811/

The calling code from my program is grid = new GridLinesVisual3D();

I am not as familiar with C# as I am with C++, but I know that I cannot create a child class to edit this function. I am thinking that an override is the proper way to do this, but I can’t get that working. I created a new file RectGrid.cs and this is what I have in the code:

using HelixToolkit.Wpf;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace Axcro.Helix_Toolkit_Extentions
{
    class RectGrid : GridLinesVisual3D
    {
        protected override MeshGeometry3D Tessellate()
        {
        this.lengthDirection = this.LengthDirection;
        this.lengthDirection.Normalize();
        this.widthDirection = Vector3D.CrossProduct(this.Normal, this.lengthDirection);
        this.widthDirection.Normalize();

        var mesh = new MeshBuilder(true, false);
        double minX = -this.Width / 2;
        double minY = -this.Length / 2;
        double maxX = this.Width / 2;
        double maxY = this.Length / 2;

        double x = minX;
        double eps = this.MinorDistance / 10;
        while (x <= maxX + eps)
        {
            double t = this.Thickness;
            if (IsMultipleOf(x, this.MajorDistance))
            {
                t *= 2;
            }

            this.AddLineX(mesh, x, minY, maxY, t);
            x += this.MinorDistance;
        }

        var m = mesh.ToMesh();
        m.Freeze();
        return m;
        }
    }
} 

This code compiles just fine, but my changes to Tessellate are not showing up. Is using an override the proper way to modify how Tessellate functions or is there a better/easier way to edit it?

For what it is worth, the Tessellate function is creating grid lines in the X and Y directions. I only want grid lines in the Y direction, not the X. So basically I don’t want a grid, I just want lines…

axcro
  • 43
  • 2
  • 1
    If the function is virtual and you are calling an instance of this class, your changes should show up. How are you instantiating your derived class for use? Are you sure it is your version that is getting created? – Steve Mitcham May 20 '15 at 13:20
  • Steve, I am not calling the class RectGrid because Visual Studio says "the type or namespace cannot be found" I am still using grid = new GridLinesVisual3D(); – axcro May 20 '15 at 14:43

2 Answers2

1

Your changes only apply to the RectGrid class. you aren't modifying the behavior of the original class, that isn't how overrides work.

You need to make sure that you add using Axcro.Helix_Toolkit_Extentions; to the top of your file where you instantiate your class. If the RectGrid class is in a different DLL that where it is being used. Make sure to add a reference.

Then you will instantiate your class instance of the GridLinesVisual3D with

grid = new RectGrid();
Steve Mitcham
  • 5,268
  • 1
  • 28
  • 56
0

Below is my code for the class I created in RectGrid.cs

using HelixToolkit.Wpf;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace Axcro.Helix_Toolkit_Extentions
   {
   class RectGrid : GridLinesVisual3D
{
    private Vector3D lengthDirection;
    private Vector3D widthDirection;
    private void AddLineY(MeshBuilder mesh, double y, double minX,     double maxX, double thickness)
    {
        int i0 = mesh.Positions.Count;
        mesh.Positions.Add(this.GetPoint(minX, y + thickness / 2));
        mesh.Positions.Add(this.GetPoint(maxX, y + thickness / 2));
        mesh.Positions.Add(this.GetPoint(maxX, y - thickness / 2));
        mesh.Positions.Add(this.GetPoint(minX, y - thickness / 2));
        mesh.Normals.Add(this.Normal);
        mesh.Normals.Add(this.Normal);
        mesh.Normals.Add(this.Normal);
        mesh.Normals.Add(this.Normal);
        mesh.TriangleIndices.Add(i0);
        mesh.TriangleIndices.Add(i0 + 1);
        mesh.TriangleIndices.Add(i0 + 2);
        mesh.TriangleIndices.Add(i0 + 2);
        mesh.TriangleIndices.Add(i0 + 3);
        mesh.TriangleIndices.Add(i0);
    }
        private Point3D GetPoint(double x, double y)
    {
        return this.Center + this.widthDirection * x +     this.lengthDirection * y;
    }
        private static bool IsMultipleOf(double y, double d)
    {
        double y2 = d * (int)(y / d);
        return (y - y2) < 1e-3;
        }

        protected override MeshGeometry3D Tessellate()
        {
            this.lengthDirection = this.LengthDirection;
            this.lengthDirection.Normalize();
            this.widthDirection = Vector3D.CrossProduct(this.Normal,     this.lengthDirection);
            this.widthDirection.Normalize();

            var mesh = new MeshBuilder(true, false);
            double minX = -this.Width / 2;
            double minY = -this.Length / 2;
            double maxX = this.Width / 2;
            double maxY = this.Length / 2;
            double eps = this.MinorDistance / 10;
            double y = minY;
            while (y <= maxY + eps)
            {
                double t = this.Thickness;
                if (IsMultipleOf(y, this.MajorDistance))
                {
                    t *= 2;
                }

                this.AddLineY(mesh, y, minX, maxX, t);
                y += this.MinorDistance;
            }


            var m = mesh.ToMesh();
            m.Freeze();
            return m;
        }
    }
}

And in the file that called this function I added using Axcro.Helix_Toolkit_Extentions;

And the call to the function is grid = new RectGrid();

I've got my grid, or well lines going in one direction, working great. Thanks for the help!

axcro
  • 43
  • 2