using Earthblend.Nts.Common;
using Earthblend.Nts.Common.MeshGenerators;
using Earthblend.Nts.Common.MeshGenerators.Base;
using Earthblend.Tts.Common.Core.Behaviors;
using Earthblend.Tts.Common.Core.CustomEventArgs;
using Earthblend.Tts.Common.Core.Enumerations;
using Earthblend.Tts.Common.Core.Extensions;
using Earthblend.Tts.Common.Core.Extentions;
using Earthblend.Tts.Common.Core.Members;
using Earthblend.Tts.Common.Core.MeshGenerators;
using Earthblend.Tts.Common.Core.Services;
using Earthblend.Tts.Common.Core.Statics;
using Earthblend.Tts.Common.MeshGenerators;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using VirtualCanvasTestCode.Enumerations;
using VirtualCanvasTestCode.MeshGenerators;

namespace VirtualCanvasTestCode.Services
{
    public class Model3DGroupRenderService : CustomBindableBase
    {

        private void MeshChanged(object sender, MeshEventArgs e)
        {
            //NodeBypassRender();
        }

        public void UpdateMeshTypeTargets
        (
            Mesh3DType meshType
          , VirtualWorld3DPluginDataItem affectedItem
          , GeometryModel3D geometryModel3D
          , IEnumerable<TargetDefinition> checkTargets
          , TranslateTransform3D translateTransform3D
        )
        {
            #region Arrow

            if (meshType == Mesh3DType.Arrow)
            {
                ArrowMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is ArrowMeshGenerator))
                {
                    meshGenerator = new ArrowMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is ArrowMeshGenerator)
                {
                    meshGenerator = (ArrowMeshGenerator)affectedItem.Shape;
                }
                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    UpdateTargets<ArrowMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            else if (meshType == Mesh3DType.DoubleArrow)
            {
                DoubleArrowMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is DoubleArrowMeshGenerator))
                {
                    meshGenerator = new DoubleArrowMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is DoubleArrowMeshGenerator)
                {
                    meshGenerator = (DoubleArrowMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    UpdateTargets<DoubleArrowMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }

                if (meshGenerator != null) geometryModel3D.Geometry = meshGenerator.Geometry;

                meshGenerator = null;
            }

            #region BoxMeshGenerator

            else if (meshType == Mesh3DType.Box)
            {
                BoxMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is BoxMeshGenerator))
                {
                    meshGenerator = new BoxMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is BoxMeshGenerator)
                {
                    meshGenerator = (BoxMeshGenerator)affectedItem.Shape;
                }

                meshGenerator.MeshChanged -= MeshChanged;
                meshGenerator.MeshChanged += MeshChanged;

                //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;

                if (checkTargets.Any())
                {
                    UpdateTargets<BoxMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region SkewBoxMeshGenerator

            else if (meshType == Mesh3DType.SkewBox)
            {
                SkewBoxMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is SkewBoxMeshGenerator))
                {
                    meshGenerator = new SkewBoxMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                    //meshGenerator.SkewBoxMeshGeneratorMeshChanged -= MeshGenerator_SkewBoxMeshGeneratorMeshChanged;
                    //meshGenerator.SkewBoxMeshGeneratorMeshChanged += MeshGenerator_SkewBoxMeshGeneratorMeshChanged;
                }
                else if (affectedItem.Shape is SkewBoxMeshGenerator)
                {
                    meshGenerator = (SkewBoxMeshGenerator)affectedItem.Shape;
                    //meshGenerator.SkewBoxMeshGeneratorMeshChanged -= MeshGenerator_SkewBoxMeshGeneratorMeshChanged;
                    //meshGenerator.SkewBoxMeshGeneratorMeshChanged += MeshGenerator_SkewBoxMeshGeneratorMeshChanged;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    UpdateTargets<SkewBoxMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Cone

            else if (meshType == Mesh3DType.Cone)
            {
                ConeMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is ConeMeshGenerator))
                {
                    meshGenerator = new ConeMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is ConeMeshGenerator)
                {
                    meshGenerator = (ConeMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    UpdateTargets<ConeMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }

                if (geometryModel3D != null && meshGenerator != null) geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Cylinder

            else if (meshType == Mesh3DType.Cylinder)
            {
                CylinderMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is CylinderMeshGenerator))
                {
                    meshGenerator = new CylinderMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is CylinderMeshGenerator)
                {
                    meshGenerator = (CylinderMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    UpdateTargets<CylinderMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }

                if (geometryModel3D != null && meshGenerator != null) geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Dodecahedron

            else if (meshType == Mesh3DType.Dodecahedron)
            {
                DodecahedronMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is DodecahedronMeshGenerator))
                {
                    meshGenerator = new DodecahedronMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is DodecahedronMeshGenerator)
                {
                    meshGenerator = (DodecahedronMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    //meshGenerator.MeshChanged -= MeshChanged;
                    //meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Dodecahedron Targets");
                    UpdateTargets<DodecahedronMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Ellipsoid

            else if (meshType == Mesh3DType.Ellipsoid)
            {
                EllipsoidMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is EllipsoidMeshGenerator))
                {
                    meshGenerator = new EllipsoidMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is EllipsoidMeshGenerator)
                {
                    meshGenerator = (EllipsoidMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Ellipsoid Targets");
                    UpdateTargets<EllipsoidMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Hollow Cylinder

            else if (meshType == Mesh3DType.HollowCylinder)
            {
                HollowCylinderMeshGenerator meshGenerator = null;

                if (affectedItem.Shape == null || !(affectedItem.Shape is HollowCylinderMeshGenerator))
                {
                    meshGenerator = new HollowCylinderMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is HollowCylinderMeshGenerator)
                {
                    meshGenerator = (HollowCylinderMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Hollow Cylinder Targets");
                    UpdateTargets<HollowCylinderMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Icosahedron

            else if (meshType == Mesh3DType.Icosahedron)
            {
                IcosahedronMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is IcosahedronMeshGenerator))
                {
                    meshGenerator = new IcosahedronMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is IcosahedronMeshGenerator)
                {
                    meshGenerator = (IcosahedronMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    //meshGenerator.MeshChanged -= MeshChanged;
                    //meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Icosahedron Targets");
                    UpdateTargets<IcosahedronMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Octahedron

            else if (meshType == Mesh3DType.Octahedron)
            {
                OctahedronMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is OctahedronMeshGenerator))
                {
                    meshGenerator = new OctahedronMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is OctahedronMeshGenerator)
                {
                    meshGenerator = (OctahedronMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    //meshGenerator.MeshChanged -= MeshChanged;
                    //meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Octahedron Targets");
                    UpdateTargets<OctahedronMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region RibbonText

            else if (meshType == Mesh3DType.RibbonText)
            {
                RibbonTextMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is RibbonTextMeshGenerator))
                {
                    meshGenerator = new RibbonTextMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is RibbonTextMeshGenerator)
                {
                    meshGenerator = (RibbonTextMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    //meshGenerator.MeshChanged -= MeshChanged;
                    //meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                meshGenerator.FontDepth = affectedItem.FontDepth;
                meshGenerator.FontSize = affectedItem.TextFontSize;
                meshGenerator.Text = affectedItem.Name;
                meshGenerator.GenerateMesh();
                if (checkTargets.Any())
                {
                    //SendMessage("Updating Ribbon Text Targets");
                    UpdateTargets<RibbonTextMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Pipe

            else if (meshType == Mesh3DType.Pipe)
            {
                PipeMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is PipeMeshGenerator))
                {
                    meshGenerator = new PipeMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is PipeMeshGenerator)
                {
                    meshGenerator = (PipeMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Planar Text Targets");
                    UpdateTargets<PipeMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Hose

            else if (meshType == Mesh3DType.Hose)
            {
                HoseMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is HoseMeshGenerator))
                {
                    meshGenerator = new HoseMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is HoseMeshGenerator)
                {
                    meshGenerator = (HoseMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Planar Text Targets");
                    UpdateTargets<HoseMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region PlanarText

            //else if (meshType == Mesh3DType.PlanarText)
            //{
            //    PlanarTextMeshGenerator meshGenerator = null;
            //    if (affectedItem.Shape == null || !(affectedItem.Shape is PlanarTextMeshGenerator))
            //    {
            //        meshGenerator = new PlanarTextMeshGenerator();
            //        affectedItem.Shape = meshGenerator;
            //    }
            //    else if (affectedItem.Shape is PlanarTextMeshGenerator)
            //    {
            //        meshGenerator = (PlanarTextMeshGenerator)affectedItem.Shape;
            //    }

            //    if (meshGenerator != null)
            //    {
            //        //meshGenerator.MeshChanged -= MeshChanged;
            //        //meshGenerator.MeshChanged += MeshChanged;

            //        meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
            //        meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
            //    }

            //    meshGenerator.FontSize = affectedItem.TextFontSize;
            //    meshGenerator.Text = affectedItem.Name;
            //    meshGenerator.GenerateMesh();
            //    if (checkTargets.Any())
            //    {
            //        //SendMessage("Updating Planar Text Targets");
            //        UpdateTargets<PlanarTextMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
            //    }
            //    geometryModel3D.Geometry = meshGenerator.Geometry;
            //    meshGenerator = null;
            //}

            #endregion

            #region Sphere

            else if (meshType == Mesh3DType.Sphere)
            {
                SphereMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is SphereMeshGenerator))
                {
                    meshGenerator = new SphereMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is SphereMeshGenerator)
                {
                    meshGenerator = (SphereMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Sphere Targets");
                    UpdateTargets<SphereMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Tetrahedron

            else if (meshType == Mesh3DType.Tetrahedron)
            {
                TetrahedronMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is TetrahedronMeshGenerator))
                {
                    meshGenerator = new TetrahedronMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is TetrahedronMeshGenerator)
                {
                    meshGenerator = (TetrahedronMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    //meshGenerator.MeshChanged -= MeshChanged;
                    //meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Tetrahedron Targets");
                    UpdateTargets<TetrahedronMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Torus

            else if (meshType == Mesh3DType.Torus)
            {
                TorusMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is TorusMeshGenerator))
                {
                    meshGenerator = new TorusMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is TorusMeshGenerator)
                {
                    meshGenerator = (TorusMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    //meshGenerator.MeshChanged -= MeshChanged;
                    //meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Torus Targets");
                    UpdateTargets<TorusMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Traveling Torus

            else if (meshType == Mesh3DType.TravelingTorus)
            {
                TravelingTorusMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is TravelingTorusMeshGenerator))
                {
                    meshGenerator = new TravelingTorusMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is TravelingTorusMeshGenerator)
                {
                    meshGenerator = (TravelingTorusMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Torus Targets");
                    UpdateTargets<TravelingTorusMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Tube

            else if (meshType == Mesh3DType.Tube)
            {
                TubeMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is TubeMeshGenerator))
                {
                    meshGenerator = new TubeMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is TubeMeshGenerator)
                {
                    meshGenerator = (TubeMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    //meshGenerator.MeshChanged -= MeshChanged;
                    //meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    //SendMessage("Updating Tube Targets");
                    UpdateTargets<TubeMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #region Wedge

            else if (meshType == Mesh3DType.Wedge)
            {
                WedgeMeshGenerator meshGenerator = null;
                if (affectedItem.Shape == null || !(affectedItem.Shape is WedgeMeshGenerator))
                {
                    meshGenerator = new WedgeMeshGenerator();
                    affectedItem.Shape = meshGenerator;
                }
                else if (affectedItem.Shape is WedgeMeshGenerator)
                {
                    meshGenerator = (WedgeMeshGenerator)affectedItem.Shape;
                }

                if (meshGenerator != null)
                {
                    meshGenerator.MeshChanged -= MeshChanged;
                    meshGenerator.MeshChanged += MeshChanged;

                    //meshGenerator.LastActionChanged -= MeshGenerator_LastActionChanged;
                    //meshGenerator.LastActionChanged += MeshGenerator_LastActionChanged;
                }

                if (checkTargets.Any())
                {
                    UpdateTargets<WedgeMeshGenerator>(meshGenerator, affectedItem, translateTransform3D, checkTargets);
                }
                geometryModel3D.Geometry = meshGenerator.Geometry;
                meshGenerator = null;
            }

            #endregion

            #endregion
        }

        private void UpdateTargets<T>
(
    T t
   , VirtualWorld3DPluginDataItem dataItem
   , TranslateTransform3D translateTransform3D
   , IEnumerable<TargetDefinition> checkTargets) where T : MeshGeneratorBase
        {
            foreach (var targetDefinition in checkTargets)
            {
                if (dataItem.Id == targetDefinition.TargetId)
                {
                    if (targetDefinition.TargetField == "PosX") dataItem.Ttx = Convert.ToDouble(targetDefinition.VirtualResult);
                    else if (targetDefinition.TargetField == "PosY") dataItem.Tty = Convert.ToDouble(targetDefinition.VirtualResult);
                    else if (targetDefinition.TargetField == "PosZ") dataItem.Ttz = Convert.ToDouble(targetDefinition.VirtualResult);
                    else if (targetDefinition.TargetField == "Width")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Box":
                                var boxMeshGenerator = dataItem.Shape as BoxMeshGenerator;
                                if (boxMeshGenerator != null) boxMeshGenerator.Width = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Height")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Box":
                                var boxMeshGenerator = dataItem.Shape as BoxMeshGenerator;
                                if (boxMeshGenerator != null) boxMeshGenerator.Height = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Depth")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Box":
                                var boxMeshGenerator = dataItem.Shape as BoxMeshGenerator;
                                if (boxMeshGenerator != null) boxMeshGenerator.Depth = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Radius")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Radius = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Length")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Length = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }

                    else if (targetDefinition.TargetField == "Point1X")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Point1X = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Point1Y")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Point1Y = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Point1Z")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Point1Z = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Point2X")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Point2X = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Point2Y")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Point2Y = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                    else if (targetDefinition.TargetField == "Point2Z")
                    {
                        switch (dataItem.MeshType)
                        {
                            case "Cylinder":
                                var cylinderMeshGenerator = dataItem.Shape as CylinderMeshGenerator;
                                if (cylinderMeshGenerator != null) cylinderMeshGenerator.Point2Z = Convert.ToDouble(targetDefinition.VirtualResult);
                                break;
                        }
                    }
                }
            }
        }

        private ObservableCollection<VirtualWorld3DPluginDataItem> _virtualWorldDataItems = new ObservableCollection<VirtualWorld3DPluginDataItem>();
        public ObservableCollection<VirtualWorld3DPluginDataItem> VirtualWorldDataItems { get { return _virtualWorldDataItems; } set { if (_virtualWorldDataItems == value) return; SetProperty(ref _virtualWorldDataItems, value); } }

        private ObservableCollection<TargetDefinition> _targetDefinitions = new ObservableCollection<TargetDefinition>();
        public ObservableCollection<TargetDefinition> TargetDefinitions { get { return _targetDefinitions; } set { if (_targetDefinitions == value) return; SetProperty(ref _targetDefinitions, value); } }

        private ObservableCollection<VirtualCanvasPluginDataItem> _virtualCanvasPluginDataItems = new ObservableCollection<VirtualCanvasPluginDataItem>();
        public ObservableCollection<VirtualCanvasPluginDataItem> VirtualCanvasPluginDataItems { get { return _virtualCanvasPluginDataItems; } set { if (_virtualCanvasPluginDataItems == value) return; SetProperty(ref _virtualCanvasPluginDataItems, value); } }


        private RenderMode _selectedRenderMode = RenderMode.Default;
        public Render3DMode SelectedRender3DMode { get { return _selectedRender3DMode; } set { if (_selectedRender3DMode == value) return; SetProperty(ref _selectedRender3DMode, value); } }
        
        
        private Render3DMode _selectedRender3DMode = Render3DMode.Default;
        public RenderMode SelectedRenderMode
        {
            get { return _selectedRenderMode; }
            set
            {
                if (_selectedRenderMode == value) return;
                SetProperty(ref _selectedRenderMode, value);
            }
        }

        //public IShellViewModel ShellViewModel = null;
        private TargetApplicationMode _targetMode = TargetApplicationMode.On;
        public TargetApplicationMode TargetMode
        {
            get { return _targetMode; }
            set { SetProperty(ref _targetMode, value); 
                //if (TargetMode == TargetApplicationMode.On) 
                    //CompileSourceCodeCommandIcExecuted(null);
                    }
        }
        public Model3DGroup Axis { get; set; } = null;
        public Model3DGroup SelectionAugments { get; set; } = null;
        //private IEnumerable<TargetDefinition> TargetDefinitions { get; set; } = null;
        public WorldRenderMode Render3DType { get; set; } = WorldRenderMode.Flat;

        public Model3DGroupRenderService()
        {

        }

        public Model3DGroupRenderService
            (
                 //ref IShellViewModel shellViewModel
                 Model3DGroup axis
               , Model3DGroup selectionAugments
               , ActionType lastActionType
               , ref IEnumerable<TargetDefinition> targetDefinitions
            )
        {
            //ShellViewModel = shellViewModel;
            Axis = axis;
            SelectionAugments = selectionAugments;
            TargetDefinitions = targetDefinitions.ToObservableCollection();
        }

        public void RenderSelection(IEnumerable<VirtualWorld3DPluginDataItem> renderItems, Boolean unSelect = false)
        {
            foreach (var dataItem in renderItems)
            {
                if (dataItem.Model != null) (dataItem.Model as GeometryModel3D).Material = new DiffuseMaterial(ApplyImage(dataItem, unSelect));
            }
        }

        private Brush ApplyImage(VirtualWorld3DPluginDataItem dataItem, Boolean unSelect = false)
        {
            if (unSelect == true) { }

            Brush geometryBrush = new ImageBrush();
            switch (dataItem.BrushType)
            {
                case MeshBrushType.Texture:
                    //if (ShellViewModel.SelectedWorldDataItem == null) ShellViewModel.Rsd.VirtualWorldDataItems.Where(c => c.NodeId == ShellViewModel.Rsd.ActiveWorldProjectNode.Id).FirstOrDefault();

                    switch (SelectedRenderMode)
                    {
                        case RenderMode.GroupHighlight:
                            switch (Render3DType)
                            {
                                case WorldRenderMode.Flat:
                                    if (dataItem.Group == dataItem.Group && unSelect != true)
                                    {
                                        geometryBrush = new SolidColorBrush(Colors.Yellow);
                                    }
                                    else
                                    {
                                        ((ImageBrush)geometryBrush).ImageSource = ImageUtil.MemoryImages[dataItem.FullImageName];
                                    }
                                    break;
                                //case WorldRenderMode.Hierarchy:
                                //    if (SelectedWorldNode != null)
                                //    {
                                //        if (dataItem.ParentNode.Id == SelectedWorldNode.Id && unSelect != true)
                                //        {
                                //            geometryBrush = new SolidColorBrush(Colors.Yellow);
                                //        }
                                //        else
                                //        {
                                //            ((ImageBrush)geometryBrush).ImageSource = ImageUtil.MemoryImages[dataItem.FullImageName];
                                //        }
                                //    }
                                //    else if (unSelect == true)
                                //    {
                                //        //ShellViewModel.SendMessage("Cannot apply unselect 3d image: " + dataItem.Id + "  - no 3d object is selected!");
                                //    }
                                //    break;
                            }
                            break;
                        case RenderMode.ObjectHighlight:
                            if (dataItem.Id == dataItem.Id)
                            {
                                geometryBrush = new SolidColorBrush(Colors.Yellow);
                            }
                            else
                            {
                                ((ImageBrush)geometryBrush).ImageSource = ImageUtil.MemoryImages[dataItem.FullImageName];
                            }
                            break;
                        case RenderMode.Equation:
                            //if (TargetDefinitions != null)
                            //{
                            //    var targetDefinitions = TargetDefinitions.Where(c => c.TargetId == dataItem.Id && c.EquationId == SelectedVirtualEquationElement.EquationId);
                            //    if (targetDefinitions.Any())
                            //    {
                            //        geometryBrush = new SolidColorBrush(Colors.Yellow);
                            //    }
                            //    else
                            //    {
                            //        geometryBrush = new SolidColorBrush(Colors.SeaGreen);
                            //    }
                            //}
                            break;
                        case RenderMode.Default:
                            ((ImageBrush)geometryBrush).ImageSource = ImageUtil.MemoryImages[dataItem.FullImageName];
                            break;
                        default:
                            ((ImageBrush)geometryBrush).ImageSource = ImageUtil.MemoryImages[dataItem.FullImageName];
                            break;
                    }

                    break;
                case MeshBrushType.Solid:
                    ColorConverter colorConverter = new ColorConverter();
                    geometryBrush = new SolidColorBrush((Color)colorConverter.ConvertFromInvariantString(dataItem.MeshColor));
                    colorConverter = null;
                    break;
            }

            return geometryBrush;
        }

        public Model3DGroup Render(Guid targetProjectNodeId, IEnumerable<VirtualWorld3DPluginDataItem> renderItems)
        {
            Model3DGroup model3DGroup = new Model3DGroup();

            switch (SelectedRender3DMode)
            {
                case Render3DMode.Default:

                    //var virtualCanvasItems = CanvasItems.Where(c => c.SymbolType == MathSymbolType.DynamicMesh && c.IsEnabled == true);
                    //if (virtualCanvasItems.Any())
                    //{
                    //    try
                    //    {
                            //Clear virtual objects
                            //var virtualDataItems = VirtualWorldDataItems.Where(c => c.IsVirtual == true);

                            //foreach (var virtualDataItem in virtualDataItems)
                            //{
                            //    MainModel3DGroup.Children.Remove(virtualDataItem.Model);
                            //}

                            //VirtualWorldDataItems.RemoveRange(virtualDataItems);

                            //List<VirtualWorld3DPluginDataItem> virtualItems = new List<VirtualWorld3DPluginDataItem>();

                            //foreach (var virtualcanvasItem in virtualCanvasItems)
                            //{

                            //    foreach (var item in ResultsXml.OrderBy(c => c.Sequence))
                            //    {
                            //        var itm = new VirtualWorld3DPluginDataItem()
                            //        {
                            //            Ttx = item.Ttx,
                            //            Tty = item.Tty,
                            //            Ttz = item.Ttz,
                            //            IsVirtual = true,
                            //            NodeId = ActiveWorldProjectNode.Id,
                            //            BrushType = MeshBrushType.Texture,
                            //            TextOffsetPosY = .55D,
                            //            Text = item.Text,
                            //            TextFontSize = .75,
                            //            FontDepth = .25
                            //        };

                            //        if (itm.Text == "1") itm.TextOffsetPosZ += .1D;

                            //        virtualItems.Add(itm);
                            //    }

                            //}

                            //VirtualWorldDataItems.AddRange(virtualItems);

                            //var partitionSets = virtualItems.Partition(2);
                            //CreateConnectors(partitionSets);
                            //var partitionSets2 = virtualItems.Skip(1).Take(virtualItems.Count - 2).Partition(2);
                            //CreateConnectors(partitionSets2);
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        //SendMessage("Error generating virtual objects: " + ex.Message);
                    //    }
                    //}

                    break;
                case Render3DMode.Hierarchy: break;
            }

            if (renderItems == null)
            {
                renderItems = VirtualWorldDataItems.Where(c => c.NodeId == targetProjectNodeId);
            }

            switch (Render3DType)
            {
                case WorldRenderMode.Flat:
                    RenderFlat(renderItems.ToList(), model3DGroup);
                    break;
                //case WorldRenderMode.Hierarchy:
                //    RenderHierarchy(World3DRootNode.ChildNodes, model3DGroup);
                //    break;
            }

            return model3DGroup;
        }

        //private void CreateConnectors(IEnumerable<IList<VirtualWorld3DPluginDataItem>> partitionSets)
        //{
        //    foreach (var partition in partitionSets)
        //    {
        //        var point1 = partition[0];
        //        if (partition.Count == 2)
        //        {
        //            var point2 = partition[1];

        //            var connectorDataItem = new VirtualWorld3DPluginDataItem()
        //            {
        //                IsVirtual = true,
        //                NodeId = ActiveWorldProjectNode.Id,
        //                BrushType = MeshBrushType.Solid,
        //                MeshColor = "Yellow",
        //                TextOffsetPosY = .55D,
        //                TextFontSize = 1,
        //                FontDepth = .25,
        //                MeshType = "Cylinder"
        //            };

        //            CylinderMeshGenerator connectorMeshGenerator = new CylinderMeshGenerator();
        //            connectorDataItem.Shape = connectorMeshGenerator;

        //            connectorMeshGenerator.Point1X = point1.Ttx;
        //            connectorMeshGenerator.Point1Y = point1.Tty;
        //            connectorMeshGenerator.Point1Z = point1.Ttz;

        //            connectorMeshGenerator.Point2X = point2.Ttx;
        //            connectorMeshGenerator.Point2Y = point2.Tty;
        //            connectorMeshGenerator.Point2Z = point2.Ttz;
        //            connectorMeshGenerator.Radius = .15;
        //            connectorMeshGenerator.Slices = 36;

        //            VirtualWorldDataItems.Add(connectorDataItem);
        //        }
        //    }
        //}

        private Mesh3DType UpdateShape(VirtualWorld3DPluginDataItem dataItem)
        {
            Mesh3DType mesh3DType = dataItem.MeshType.ToEnum<Mesh3DType>();

            switch (mesh3DType)
            {
                case Mesh3DType.Arrow: if (!(dataItem.Shape is ArrowMeshGenerator)) dataItem.Shape = new ArrowMeshGenerator(); break;
                case Mesh3DType.Box: if (!(dataItem.Shape is BoxMeshGenerator)) dataItem.Shape = new BoxMeshGenerator(); break;
                case Mesh3DType.Cone: if (!(dataItem.Shape is ConeMeshGenerator)) dataItem.Shape = new ConeMeshGenerator(); break;
                case Mesh3DType.Cylinder: if (!(dataItem.Shape is CylinderMeshGenerator)) dataItem.Shape = new CylinderMeshGenerator(); break;
                case Mesh3DType.Dodecahedron: if (!(dataItem.Shape is DodecahedronMeshGenerator)) dataItem.Shape = new DodecahedronMeshGenerator(); break;
                case Mesh3DType.DoubleArrow: if (!(dataItem.Shape is DoubleArrowMeshGenerator)) dataItem.Shape = new DoubleArrowMeshGenerator(); break;
                case Mesh3DType.Icosahedron: if (!(dataItem.Shape is IcosahedronMeshGenerator)) dataItem.Shape = new IcosahedronMeshGenerator(); break;
                case Mesh3DType.Ellipsoid: if (!(dataItem.Shape is EllipsoidMeshGenerator)) dataItem.Shape = new EllipsoidMeshGenerator(); break;
                case Mesh3DType.HollowCylinder: if (!(dataItem.Shape is HollowCylinderMeshGenerator)) dataItem.Shape = new HollowCylinderMeshGenerator(); break;
                case Mesh3DType.Octahedron: if (!(dataItem.Shape is OctahedronMeshGenerator)) dataItem.Shape = new OctahedronMeshGenerator(); break;
                case Mesh3DType.Pipe: if (!(dataItem.Shape is PipeMeshGenerator)) dataItem.Shape = new PipeMeshGenerator(); break;
                //case Mesh3DType.Plane: if (!(dataItem.Shape is PlaneMeshGenerator)) dataItem.Shape = new PlaneMeshGenerator(); break;
                case Mesh3DType.RibbonText: if (!(dataItem.Shape is RibbonTextMeshGenerator)) dataItem.Shape = new RibbonTextMeshGenerator(); break;
                case Mesh3DType.SkewBox: if (!(dataItem.Shape is SkewBoxMeshGenerator)) dataItem.Shape = new SkewBoxMeshGenerator(); break;
                case Mesh3DType.Sphere: if (!(dataItem.Shape is SphereMeshGenerator)) dataItem.Shape = new SphereMeshGenerator(); break;
                case Mesh3DType.Tetrahedron: if (!(dataItem.Shape is TetrahedronMeshGenerator)) dataItem.Shape = new TetrahedronMeshGenerator(); break;
                case Mesh3DType.Torus: if (!(dataItem.Shape is TorusMeshGenerator)) dataItem.Shape = new TorusMeshGenerator(); break;
                case Mesh3DType.TravelingTorus: if (!(dataItem.Shape is TravelingTorusMeshGenerator)) dataItem.Shape = new TravelingTorusMeshGenerator(); break;
                case Mesh3DType.Tube: if (!(dataItem.Shape is TubeMeshGenerator)) dataItem.Shape = new TubeMeshGenerator(); break;
                case Mesh3DType.Wedge: if (!(dataItem.Shape is WedgeMeshGenerator)) dataItem.Shape = new WedgeMeshGenerator(); break;
            }

            return mesh3DType;
        }

        private Model3DGroup RenderHierarchy(IEnumerable<VirtualWorld3DPluginDataItem> childNodes, Model3DGroup model3DGroup)
        {
            foreach (var dataItem in childNodes)
            {
                Model3DGroup currentNodeModel3DGroup = null;

                switch (dataItem.NodeType)
                {
                    #region Group

                    case WorldNodeType.Group:
                        Transform3DGroup transform3DGroupH = null;
                        currentNodeModel3DGroup = new Model3DGroup();

                        if (transform3DGroupH == null) transform3DGroupH = new Transform3DGroup();
                        TranslateTransform3D tt = new TranslateTransform3D() { OffsetX = dataItem.Ttx, OffsetY = dataItem.Tty, OffsetZ = dataItem.Ttz };
                        transform3DGroupH.Children.Add(tt);

                        #region Rotate Transform

                        if (dataItem.Rtx != 0D)
                        {
                            if (transform3DGroupH == null) transform3DGroupH = new Transform3DGroup();

                            RotateTransform3D groupRotateTransform3DX = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                            AxisAngleRotation3D groupAxisAngleRotation3DX = new AxisAngleRotation3D() { Axis = new Vector3D(1, 0, 0), Angle = dataItem.Rtx };
                            groupRotateTransform3DX.Rotation = groupAxisAngleRotation3DX;
                            transform3DGroupH.Children.Add(groupRotateTransform3DX);
                        }

                        if (dataItem.Rty != 0D)
                        {
                            if (transform3DGroupH == null) transform3DGroupH = new Transform3DGroup();

                            RotateTransform3D groupRotateTransform3DY = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                            AxisAngleRotation3D groupAxisAngleRotation3DY = new AxisAngleRotation3D() { Axis = new Vector3D(0, 1, 0), Angle = dataItem.Rty };
                            groupRotateTransform3DY.Rotation = groupAxisAngleRotation3DY;
                            transform3DGroupH.Children.Add(groupRotateTransform3DY);
                        }

                        if (dataItem.Rtz != 0D)
                        {
                            if (transform3DGroupH == null) transform3DGroupH = new Transform3DGroup();

                            RotateTransform3D groupRotateTransform3DZ = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                            AxisAngleRotation3D groupAxisAngleRotation3DZ = new AxisAngleRotation3D() { Axis = new Vector3D(0, 0, 1), Angle = dataItem.Rtz };
                            groupRotateTransform3DZ.Rotation = groupAxisAngleRotation3DZ;
                            transform3DGroupH.Children.Add(groupRotateTransform3DZ);
                        }

                        #endregion

                        if (currentNodeModel3DGroup != null)
                        {
                            currentNodeModel3DGroup.Transform = transform3DGroupH;
                            dataItem.TransformGroup = transform3DGroupH;
                        }

                        break;

                    #endregion

                    #region Object //Adds to model3DGroup

                    case WorldNodeType.Object:
                        GeometryModel3D geometryModel3D = new GeometryModel3D();
                        Mesh3DType mesh3DType = UpdateShape(dataItem);
                        IEnumerable<TargetDefinition> checkTargets = null;
                        if (TargetDefinitions != null) checkTargets = TargetDefinitions.Where(c => c.TargetId == dataItem.Id);
                        if (dataItem.BrushType == MeshBrushType.Texture && !ImageUtil.MemoryImages.ContainsKey(dataItem.FullImageName))
                        {
                            ImageUtil.MemoryImages.Add(dataItem.FullImageName, new BitmapImage(new Uri(dataItem.FullImageName, UriKind.Absolute)));
                        }

                        Brush geometryBrush = ApplyImage(dataItem);
                        if (dataItem.Shape != null)
                        {
                            geometryModel3D.Geometry = (dataItem.Shape as MeshGeneratorBase).Geometry;
                            geometryModel3D.Material = new DiffuseMaterial(geometryBrush);
                        }

                        Transform3DGroup transform3DGroup = null;

                        TranslateTransform3D translateTransform3D = null;

                        //if (dataItem.Ttx != 0D || dataItem.Tty != 0D || dataItem.Ttz != 0D)
                        //{
                        if (transform3DGroup == null) transform3DGroup = new Transform3DGroup();
                        translateTransform3D = new TranslateTransform3D(dataItem.Ttx, dataItem.Tty, dataItem.Ttz);
                        transform3DGroup.Children.Add(translateTransform3D);
                        //}

                        #region Rotate Transform 3D

                        if (dataItem.Rtx != 0D)
                        {
                            if (transform3DGroup == null) transform3DGroup = new Transform3DGroup();
                            RotateTransform3D rotateTransform3DX = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                            AxisAngleRotation3D axisAngleRotation3DX = new AxisAngleRotation3D() { Axis = new Vector3D(1, 0, 0), Angle = dataItem.Rtx };
                            rotateTransform3DX.Rotation = axisAngleRotation3DX;
                            transform3DGroup.Children.Add(rotateTransform3DX);
                        }

                        if (dataItem.Rty != 0D)
                        {
                            if (transform3DGroup == null) transform3DGroup = new Transform3DGroup();
                            RotateTransform3D rotateTransform3DY = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                            AxisAngleRotation3D axisAngleRotation3DY = new AxisAngleRotation3D() { Axis = new Vector3D(0, 1, 0), Angle = dataItem.Rty };
                            rotateTransform3DY.Rotation = axisAngleRotation3DY;
                            transform3DGroup.Children.Add(rotateTransform3DY);
                        }

                        if (dataItem.Rtz != 0D)
                        {
                            if (transform3DGroup == null) transform3DGroup = new Transform3DGroup();
                            RotateTransform3D rotateTransform3DZ = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                            AxisAngleRotation3D axisAngleRotation3DZ = new AxisAngleRotation3D() { Axis = new Vector3D(0, 0, 1), Angle = dataItem.Rtz };
                            rotateTransform3DZ.Rotation = axisAngleRotation3DZ;
                            transform3DGroup.Children.Add(rotateTransform3DZ);
                        }

                        #endregion

                        if (transform3DGroup != null)
                        {
                            geometryModel3D.Transform = transform3DGroup;
                            dataItem.TransformGroup = transform3DGroup;
                        }

                        geometryModel3D.SetValue(ObjectBinder.DataItemProperty, dataItem);

                        if (checkTargets != null) UpdateMeshTypeTargets(mesh3DType, dataItem, geometryModel3D, checkTargets, translateTransform3D);





                        dataItem.Model = geometryModel3D;
                        model3DGroup.Children.Add(geometryModel3D);
                        break;

                        #endregion 
                }

                if (dataItem.ChildNodes.Any())
                {
                    if (currentNodeModel3DGroup == null)
                    {
                        dataItem.ObjectModel3DGroup = model3DGroup;
                        model3DGroup.Children.Add(RenderHierarchy(dataItem.ChildNodes, new Model3DGroup()));
                    }
                    else
                    {
                        dataItem.ObjectModel3DGroup = currentNodeModel3DGroup;
                        model3DGroup.Children.Add(RenderHierarchy(dataItem.ChildNodes, currentNodeModel3DGroup));
                    }
                }
            }

            return model3DGroup;
        }

        private void RenderFlat(IEnumerable<VirtualWorld3DPluginDataItem> renderItems, Model3DGroup model3DGroup)
        {
            foreach (var dataItem in renderItems)
            {
                GeometryModel3D geometryModel3D = new GeometryModel3D();
                Mesh3DType mesh3DType = UpdateShape(dataItem);
                IEnumerable<TargetDefinition> checkTargets = null;
                if (TargetDefinitions != null) checkTargets = TargetDefinitions.Where(c => c.TargetId == dataItem.Id);

                if (dataItem.BrushType == MeshBrushType.Texture && !ImageUtil.MemoryImages.ContainsKey(dataItem.FullImageName))
                {
                    //if(File.Exists(dataItem.FullImageName))
                    //    ImageUtil.MemoryImages.Add(dataItem.FullImageName, new BitmapImage(new Uri(dataItem.FullImageName, UriKind.Absolute)));
                    //else
                    //{
                        FileInfo fileInfo = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                        var executingAssembly = fileInfo.DirectoryName + @"\";
                        fileInfo = null;

                        String embeddedResourcePath = executingAssembly + @"\Resources\Images\" + dataItem.ImageName;

                        if (File.Exists(embeddedResourcePath))
                        {
                            ImageUtil.MemoryImages.Add(dataItem.FullImageName, new BitmapImage(new Uri(embeddedResourcePath, UriKind.Absolute)));
                        }
                    //}
                }

                Brush geometryBrush = ApplyImage(dataItem);

                if (dataItem.Shape != null)
                {
                    geometryModel3D.Geometry = (dataItem.Shape as MeshGeneratorBase).Geometry;
                    geometryModel3D.Material = new DiffuseMaterial(geometryBrush);

                    var meshGeneratorBase = dataItem.Shape as MeshGeneratorBase;


                    Transform3DGroup transform3DGroup = new Transform3DGroup();
                    TranslateTransform3D translateTransform3D = new TranslateTransform3D(dataItem.Ttx, dataItem.Tty, dataItem.Ttz);
                    transform3DGroup.Children.Add(translateTransform3D);


                    if (meshGeneratorBase != null)
                    {
                        meshGeneratorBase.SupportLegacyPositioning = false; // ShellViewModel.Rsd.SupportLegacyPositioning;

                        if (meshGeneratorBase.ScaleX != 1D || meshGeneratorBase.ScaleY != 1D || meshGeneratorBase.ScaleZ != 1D)
                        {
                            ScaleTransform3D scaleTransform3D = new ScaleTransform3D(meshGeneratorBase.ScaleX, meshGeneratorBase.ScaleY, meshGeneratorBase.ScaleZ);
                            transform3DGroup.Children.Add(scaleTransform3D);
                        }
                    }

                    //Temporary measure unti trouble shooting can be performed
                    if (dataItem.MeshType == "Torus")
                    {
                        translateTransform3D.OffsetY = 2.75;
                    }

                    #region Rotate Transform 3D

                    if (dataItem.Rtx != 0D)
                    {
                        RotateTransform3D rotateTransform3DX = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                        AxisAngleRotation3D axisAngleRotation3DX = new AxisAngleRotation3D() { Axis = new Vector3D(1, 0, 0), Angle = dataItem.Rtx };
                        rotateTransform3DX.Rotation = axisAngleRotation3DX;
                        transform3DGroup.Children.Add(rotateTransform3DX);
                    }

                    if (dataItem.Rty != 0D)
                    {
                        RotateTransform3D rotateTransform3DY = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                        AxisAngleRotation3D axisAngleRotation3DY = new AxisAngleRotation3D() { Axis = new Vector3D(0, 1, 0), Angle = dataItem.Rty };
                        rotateTransform3DY.Rotation = axisAngleRotation3DY;
                        transform3DGroup.Children.Add(rotateTransform3DY);
                    }

                    if (dataItem.Rtz != 0D)
                    {
                        RotateTransform3D rotateTransform3DZ = new RotateTransform3D() { CenterX = dataItem.Ttx, CenterY = dataItem.Tty, CenterZ = dataItem.Ttz };
                        AxisAngleRotation3D axisAngleRotation3DZ = new AxisAngleRotation3D() { Axis = new Vector3D(0, 0, 1), Angle = dataItem.Rtz };
                        rotateTransform3DZ.Rotation = axisAngleRotation3DZ;
                        transform3DGroup.Children.Add(rotateTransform3DZ);
                    }

                    #endregion

                    geometryModel3D.Transform = transform3DGroup;
                    geometryModel3D.SetValue(ObjectBinder.DataItemProperty, dataItem);
                    dataItem.TransformGroup = transform3DGroup;

                    if (checkTargets.Any())
                        UpdateMeshTypeTargets(mesh3DType, dataItem, geometryModel3D, checkTargets, translateTransform3D);
                            

                    dataItem.Model = geometryModel3D;
                    model3DGroup.Children.Add(geometryModel3D);
                }
            }
        }

        #region Translate Transform Private Methods

        //public void Tnx()
        //{
        //    switch (ShellViewModel.Rsd.Render3DType)
        //    {
        //        case WorldRenderMode.Hierarchy:
        //            switch (ShellViewModel.Rsd.SelectedWorldNode.NodeType)
        //            {
        //                case WorldNodeType.Object:

        //                    var ttH = ShellViewModel.Rsd.SelectedWorldNode.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                    if (ttH.Any())
        //                    {
        //                        var ttx = ttH.FirstOrDefault();
        //                        ShellViewModel.Rsd.SelectedWorldNode.Ttx += VwMovementValue;
        //                        ttx.OffsetX = Rsd.SelectedWorldNode.Ttx;
        //                        ShellViewModel.LastActionType = ActionType.TPX;
        //                    }
        //                    else
        //                    {
        //                        ShellViewModel.SendMessage("Cannot locate translate transforn for Tnx");
        //                    }
        //                    break;
        //                case WorldNodeType.Group:
        //                    foreach (var node in ShellViewModel.Rsd.SelectedWorldNode.ChildNodes)
        //                    {
        //                        var ttG = node.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                        if (ttG.Any())
        //                        {
        //                            var ttx = ttG.FirstOrDefault();
        //                            node.Ttx += ShellViewModel.VwMovementValue;
        //                            ttx.OffsetX = node.Ttx;
        //                            ShellViewModel.LastActionType = ActionType.TPX;
        //                        }
        //                    }
        //                    break;
        //            }

        //            break;
        //        case WorldRenderMode.Flat:
        //            switch (ShellViewModel.SelectedRenderMode)
        //            {
        //                case RenderMode.GroupHighlight:
        //                    if (ShellViewModel.SelectedWorldDataItem != null)
        //                    {
        //                        var groupItems = ShellViewModel.Rsd.VirtualWorldDataItems.Where(c => c.Group == ShellViewModel.SelectedWorldDataItem.Group);

        //                        foreach (var group in groupItems)
        //                        {
        //                            foreach (var transform in group.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                            {
        //                                var ttGroup = transform as TranslateTransform3D;
        //                                group.Ttx += ShellViewModel.VwMovementValue;
        //                                ttGroup.OffsetX = group.Ttx;
        //                                ShellViewModel.LastActionType = ActionType.TNX;
        //                            }
        //                        }
        //                        //RenderSelectionAugments();
        //                    }

        //                    break;

        //                case RenderMode.ObjectHighlight:
        //                case RenderMode.Default:
        //                    if (ShellViewModel.SelectedWorldDataItem != null)
        //                    {
        //                        switch (ShellViewModel.MovementMode)
        //                        {
        //                            case Object3DMovementMode.Object:
        //                                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var tt = transform as TranslateTransform3D;
        //                                    ShellViewModel.SelectedWorldDataItem.Ttx += ShellViewModel.VwMovementValue;
        //                                    tt.OffsetX = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                                    ShellViewModel.LastActionType = ActionType.TNX;
        //                                }
        //                                break;
        //                            case Object3DMovementMode.Text:
        //                                ShellViewModel.SelectedWorldDataItem.TextOffsetPosX += ShellViewModel.VwMovementValue; ShellViewModel.Render();
        //                                break;
        //                        }
        //                    }
        //                    break;
        //            }
        //            break;
        //    }
        //}
        //public void Tpx()
        //{
        //    switch (ShellViewModel.Rsd.Render3DType)
        //    {
        //        case WorldRenderMode.Hierarchy:

        //            switch (ShellViewModel.Rsd.SelectedWorldNode.NodeType)
        //            {
        //                case WorldNodeType.Object:

        //                    var ttH = ShellViewModel.Rsd.SelectedWorldNode.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                    if (ttH.Any())
        //                    {
        //                        var ttx = ttH.FirstOrDefault();
        //                        if (ttx != null)
        //                        {
        //                            ShellViewModel.Rsd.SelectedWorldNode.Ttx -= ShellViewModel.VwMovementValue;
        //                            ttx.OffsetX = ShellViewModel.Rsd.SelectedWorldNode.Ttx;
        //                            ShellViewModel.LastActionType = ActionType.TPX;
        //                        }
        //                    }
        //                    else
        //                    {
        //                        ShellViewModel.SendMessage("Cannot locate translate transforn for Tpx");
        //                    }
        //                    break;
        //                case WorldNodeType.Group:
        //                    foreach (var node in ShellViewModel.Rsd.SelectedWorldNode.ChildNodes)
        //                    {
        //                        var ttG = node.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                        if (ttG.Any())
        //                        {
        //                            var ttx = ttG.FirstOrDefault();
        //                            if (ttx != null)
        //                            {
        //                                node.Ttx -= ShellViewModel.VwMovementValue;
        //                                ttx.OffsetX = node.Ttx;
        //                                ShellViewModel.LastActionType = ActionType.TPX;
        //                            }
        //                        }
        //                    }
        //                    break;
        //            }
        //            break;
        //        case WorldRenderMode.Flat:
        //            if (ShellViewModel.SelectedWorldDataItem != null)
        //            {
        //                switch (ShellViewModel.SelectedRenderMode)
        //                {
        //                    case RenderMode.GroupHighlight:
        //                        var groupItems = ShellViewModel.Rsd.VirtualWorldDataItems.Where(c => c.Group == ShellViewModel.SelectedWorldDataItem.Group);

        //                        foreach (var group in groupItems)
        //                        {
        //                            if (group.TransformGroup != null)
        //                                foreach (var transform in group.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var ttGroup = transform as TranslateTransform3D;
        //                                    group.Ttx -= ShellViewModel.VwMovementValue;
        //                                    ttGroup.OffsetX = group.Ttx;
        //                                    ShellViewModel.LastActionType = ActionType.TPX;
        //                                }
        //                        }

        //                        //RenderSelectionAugments();
        //                        break;
        //                    case RenderMode.ObjectHighlight:
        //                    case RenderMode.Default:
        //                        switch (ShellViewModel.MovementMode)
        //                        {
        //                            case Object3DMovementMode.Object:
        //                                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var tt = transform as TranslateTransform3D;
        //                                    ShellViewModel.SelectedWorldDataItem.Ttx -= ShellViewModel.VwMovementValue;
        //                                    tt.OffsetX = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                                    ShellViewModel.LastActionType = ActionType.TPX;
        //                                }
        //                                break;
        //                            case Object3DMovementMode.Text:
        //                                ShellViewModel.SelectedWorldDataItem.TextOffsetPosX -= ShellViewModel.VwMovementValue; ShellViewModel.Render();
        //                                break;
        //                        }

        //                        break;
        //                }
        //            }
        //            break;
        //    }


        //}

        //public void Tny()
        //{
        //    switch (ShellViewModel.Rsd.Render3DType)
        //    {
        //        case WorldRenderMode.Hierarchy:
        //            switch (ShellViewModel.Rsd.SelectedWorldNode.NodeType)
        //            {
        //                case WorldNodeType.Object:
        //                    var ttH = ShellViewModel.Rsd.SelectedWorldNode.TransformGroup.Children.OfType<TranslateTransform3D>();
        //                    if (ttH.Any())
        //                    {
        //                        var ttx = ttH.FirstOrDefault();
        //                        ShellViewModel.Rsd.SelectedWorldNode.Tty += ShellViewModel.VwMovementValue;
        //                        ttx.OffsetY = ShellViewModel.Rsd.SelectedWorldNode.Tty;
        //                        ShellViewModel.LastActionType = ActionType.TPY;
        //                    }
        //                    break;
        //                case WorldNodeType.Group:
        //                    foreach (var node in ShellViewModel.Rsd.SelectedWorldNode.ChildNodes)
        //                    {
        //                        var ttG = node.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                        if (ttG.Any())
        //                        {
        //                            var tty = ttG.FirstOrDefault();
        //                            node.Tty += ShellViewModel.VwMovementValue;
        //                            tty.OffsetY = node.Tty;
        //                            ShellViewModel.LastActionType = ActionType.TPY;
        //                        }
        //                    }
        //                    break;
        //            }
        //            break;
        //        case WorldRenderMode.Flat:
        //            if (ShellViewModel.SelectedWorldDataItem != null)
        //            {
        //                switch (ShellViewModel.SelectedRenderMode)
        //                {
        //                    case RenderMode.GroupHighlight:
        //                        var groupItems = ShellViewModel.Rsd.VirtualWorldDataItems.Where(c => c.Group == ShellViewModel.SelectedWorldDataItem.Group);

        //                        foreach (var group in groupItems)
        //                        {
        //                            if (group.TransformGroup != null)
        //                                foreach (var transform in group.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var ttGroup = transform as TranslateTransform3D;
        //                                    group.Tty += ShellViewModel.VwMovementValue;
        //                                    ttGroup.OffsetY = group.Tty;
        //                                    ShellViewModel.LastActionType = ActionType.TPY;
        //                                }
        //                        }

        //                        //RenderSelectionAugments();
        //                        break;
        //                    case RenderMode.ObjectHighlight:
        //                    case RenderMode.Default:
        //                        switch (ShellViewModel.MovementMode)
        //                        {
        //                            case Object3DMovementMode.Object:
        //                                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var tt = transform as TranslateTransform3D;
        //                                    ShellViewModel.SelectedWorldDataItem.Tty += ShellViewModel.VwMovementValue;
        //                                    tt.OffsetY = ShellViewModel.SelectedWorldDataItem.Tty;
        //                                    ShellViewModel.LastActionType = ActionType.TNY;
        //                                }
        //                                break;
        //                            case Object3DMovementMode.Text:
        //                                ShellViewModel.SelectedWorldDataItem.TextOffsetPosY += ShellViewModel.VwMovementValue; ShellViewModel.Render();
        //                                break;
        //                        }

        //                        break;
        //                }
        //            }
        //            break;
        //    }
        //}
        //public void Tpy()
        //{
        //    switch (ShellViewModel.Rsd.Render3DType)
        //    {
        //        case WorldRenderMode.Hierarchy:
        //            switch (ShellViewModel.Rsd.SelectedWorldNode.NodeType)
        //            {
        //                case WorldNodeType.Object:
        //                    var ttH = ShellViewModel.Rsd.SelectedWorldNode.TransformGroup.Children.OfType<TranslateTransform3D>();
        //                    if (ttH.Any())
        //                    {
        //                        var ttx = ttH.FirstOrDefault();
        //                        ShellViewModel.Rsd.SelectedWorldNode.Tty -= ShellViewModel.VwMovementValue;
        //                        ttx.OffsetY = ShellViewModel.Rsd.SelectedWorldNode.Tty;
        //                        ShellViewModel.LastActionType = ActionType.TPY;
        //                    }
        //                    break;
        //                case WorldNodeType.Group:
        //                    foreach (var node in ShellViewModel.Rsd.SelectedWorldNode.ChildNodes)
        //                    {
        //                        var ttG = node.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                        if (ttG.Any())
        //                        {
        //                            var tty = ttG.FirstOrDefault();
        //                            node.Tty -= ShellViewModel.VwMovementValue;
        //                            tty.OffsetY = node.Tty;
        //                            ShellViewModel.LastActionType = ActionType.TPY;
        //                        }
        //                    }
        //                    break;
        //            }
        //            break;
        //        case WorldRenderMode.Flat:
        //            if (ShellViewModel.SelectedWorldDataItem != null)
        //            {
        //                switch (ShellViewModel.SelectedRenderMode)
        //                {
        //                    case RenderMode.GroupHighlight:
        //                        var groupItems = ShellViewModel.Rsd.VirtualWorldDataItems.Where(c => c.Group == ShellViewModel.SelectedWorldDataItem.Group);

        //                        foreach (var group in groupItems)
        //                        {
        //                            if (group.TransformGroup != null)
        //                                foreach (var transform in group.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var ttGroup = transform as TranslateTransform3D;
        //                                    group.Tty -= ShellViewModel.VwMovementValue;
        //                                    ttGroup.OffsetY = group.Tty;
        //                                    ShellViewModel.LastActionType = ActionType.TPY;
        //                                }
        //                        }
        //                        break;
        //                    case RenderMode.ObjectHighlight:
        //                    case RenderMode.Default:
        //                        switch (ShellViewModel.MovementMode)
        //                        {
        //                            case Object3DMovementMode.Object:
        //                                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var tt = transform as TranslateTransform3D;
        //                                    ShellViewModel.SelectedWorldDataItem.Tty -= ShellViewModel.VwMovementValue;
        //                                    tt.OffsetY = ShellViewModel.SelectedWorldDataItem.Tty;
        //                                    ShellViewModel.LastActionType = ActionType.TPY;
        //                                }
        //                                break;
        //                            case Object3DMovementMode.Text:
        //                                ShellViewModel.SelectedWorldDataItem.TextOffsetPosY -= ShellViewModel.VwMovementValue; ShellViewModel.Render();
        //                                break;
        //                        }
        //                        break;
        //                }
        //            }
        //            break;

        //    }
        //}

        //public void Tnz()
        //{
        //    switch (ShellViewModel.Rsd.Render3DType)
        //    {
        //        case WorldRenderMode.Hierarchy:
        //            switch (ShellViewModel.Rsd.SelectedWorldNode.NodeType)
        //            {
        //                case WorldNodeType.Object:
        //                    var ttH = ShellViewModel.Rsd.SelectedWorldNode.TransformGroup.Children.OfType<TranslateTransform3D>();
        //                    if (ttH.Any())
        //                    {
        //                        var ttz = ttH.FirstOrDefault();
        //                        ShellViewModel.Rsd.SelectedWorldNode.Ttz += ShellViewModel.VwMovementValue;
        //                        ttz.OffsetZ = ShellViewModel.Rsd.SelectedWorldNode.Ttz;
        //                        ShellViewModel.LastActionType = ActionType.TPZ;
        //                    }
        //                    break;
        //                case WorldNodeType.Group:
        //                    foreach (var node in ShellViewModel.Rsd.SelectedWorldNode.ChildNodes)
        //                    {
        //                        var ttG = node.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                        if (ttG.Any())
        //                        {
        //                            var ttz = ttG.FirstOrDefault();
        //                            node.Ttz += ShellViewModel.VwMovementValue;
        //                            ttz.OffsetZ = node.Ttz;
        //                            ShellViewModel.LastActionType = ActionType.TPZ;
        //                        }
        //                    }
        //                    break;
        //            }
        //            break;
        //        case WorldRenderMode.Flat:

        //            if (ShellViewModel.SelectedWorldDataItem != null)
        //            {
        //                switch (ShellViewModel.SelectedRenderMode)
        //                {
        //                    case RenderMode.GroupHighlight:
        //                        var groupItems = ShellViewModel.Rsd.VirtualWorldDataItems.Where(c => c.Group == ShellViewModel.SelectedWorldDataItem.Group);

        //                        foreach (var group in groupItems)
        //                        {
        //                            if (group.TransformGroup != null)
        //                                foreach (var transform in group.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var ttGroup = transform as TranslateTransform3D;
        //                                    group.Ttz += ShellViewModel.VwMovementValue;
        //                                    ttGroup.OffsetZ = group.Ttz;
        //                                    ShellViewModel.LastActionType = ActionType.TPZ;
        //                                }
        //                        }

        //                        //RenderSelectionAugments();
        //                        break;
        //                    case RenderMode.ObjectHighlight:
        //                    case RenderMode.Default:
        //                        switch (ShellViewModel.MovementMode)
        //                        {
        //                            case Object3DMovementMode.Object:
        //                                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var tt = transform as TranslateTransform3D;
        //                                    ShellViewModel.SelectedWorldDataItem.Ttz += ShellViewModel.VwMovementValue;
        //                                    tt.OffsetZ = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                                    ShellViewModel.LastActionType = ActionType.TNZ;
        //                                }
        //                                break;
        //                            case Object3DMovementMode.Text:
        //                                ShellViewModel.SelectedWorldDataItem.TextOffsetPosZ += ShellViewModel.VwMovementValue; ShellViewModel.Render();
        //                                break;
        //                        }

        //                        break;
        //                }
        //            }
        //            break;
        //    }
        //}
        //public void Tpz()
        //{
        //    switch (ShellViewModel.Rsd.Render3DType)
        //    {
        //        case WorldRenderMode.Hierarchy:
        //            switch (ShellViewModel.Rsd.SelectedWorldNode.NodeType)
        //            {
        //                case WorldNodeType.Object:
        //                    var ttH = ShellViewModel.Rsd.SelectedWorldNode.TransformGroup.Children.OfType<TranslateTransform3D>();
        //                    if (ttH.Any())
        //                    {
        //                        var ttz = ttH.FirstOrDefault();
        //                        ShellViewModel.Rsd.SelectedWorldNode.Ttz -= ShellViewModel.VwMovementValue;
        //                        ttz.OffsetZ = ShellViewModel.Rsd.SelectedWorldNode.Ttz;
        //                        ShellViewModel.LastActionType = ActionType.TPZ;
        //                    }
        //                    break;
        //                case WorldNodeType.Group:
        //                    foreach (var node in ShellViewModel.Rsd.SelectedWorldNode.ChildNodes)
        //                    {
        //                        var ttG = node.TransformGroup.Children.OfType<TranslateTransform3D>();

        //                        if (ttG.Any())
        //                        {
        //                            var ttz = ttG.FirstOrDefault();
        //                            node.Ttz -= ShellViewModel.VwMovementValue;
        //                            ttz.OffsetZ = node.Ttz;
        //                            ShellViewModel.LastActionType = ActionType.TPZ;
        //                        }
        //                    }
        //                    break;
        //            }
        //            break;
        //        case WorldRenderMode.Flat:
        //            if (ShellViewModel.SelectedWorldDataItem != null)
        //            {
        //                switch (ShellViewModel.SelectedRenderMode)
        //                {
        //                    case RenderMode.GroupHighlight:
        //                        var groupItems = ShellViewModel.Rsd.VirtualWorldDataItems.Where(c => c.Group == ShellViewModel.SelectedWorldDataItem.Group);

        //                        foreach (var group in groupItems)
        //                        {
        //                            if (group.TransformGroup != null)
        //                                foreach (var transform in group.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var ttGroup = transform as TranslateTransform3D;
        //                                    group.Ttz -= ShellViewModel.VwMovementValue;
        //                                    ttGroup.OffsetZ = group.Ttz;
        //                                    ShellViewModel.LastActionType = ActionType.TPZ;
        //                                }
        //                        }

        //                        break;
        //                    case RenderMode.ObjectHighlight:
        //                    case RenderMode.Default:
        //                        switch (ShellViewModel.MovementMode)
        //                        {
        //                            case Object3DMovementMode.Object:
        //                                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<TranslateTransform3D>())
        //                                {
        //                                    var tt = transform as TranslateTransform3D;
        //                                    ShellViewModel.SelectedWorldDataItem.Ttz -= ShellViewModel.VwMovementValue;
        //                                    tt.OffsetZ = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                                    ShellViewModel.LastActionType = ActionType.TPZ;
        //                                }
        //                                break;
        //                            case Object3DMovementMode.Text:
        //                                ShellViewModel.SelectedWorldDataItem.TextOffsetPosZ -= ShellViewModel.VwMovementValue; ShellViewModel.Render();
        //                                break;
        //                        }
        //                        break;
        //                }
        //            }
        //            break;
        //    }
        //}

        #endregion

        //#region Rotate Transform Methods

        //public void Rnx()
        //{
        //    if (ShellViewModel.SelectedWorldDataItem != null)
        //    {
        //        switch (ShellViewModel.MovementMode)
        //        {
        //            case Object3DMovementMode.Object:
        //                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<RotateTransform3D>())
        //                {
        //                    var rt = transform as RotateTransform3D;
        //                    var axis = rt.Rotation as AxisAngleRotation3D;
        //                    if (axis.Axis == new Vector3D(1, 0, 0))
        //                    {
        //                        if (ShellViewModel.SelectedWorldDataItem.AutoCenter == true)
        //                        {
        //                            ShellViewModel.SelectedWorldDataItem.Ctx = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                            ShellViewModel.SelectedWorldDataItem.Cty = ShellViewModel.SelectedWorldDataItem.Tty;
        //                            ShellViewModel.SelectedWorldDataItem.Ctz = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                        }

        //                        rt.CenterX = ShellViewModel.SelectedWorldDataItem.Ctx; rt.CenterY = ShellViewModel.SelectedWorldDataItem.Cty; rt.CenterZ = ShellViewModel.SelectedWorldDataItem.Ctz;
        //                        ShellViewModel.SelectedWorldDataItem.Rtx -= ShellViewModel.VwMovementValue;
        //                        axis.Angle = ShellViewModel.SelectedWorldDataItem.Rtx;
        //                        ShellViewModel.LastActionType = ActionType.RNX;
        //                    }
        //                }
        //                break;
        //            case Object3DMovementMode.Text:
        //                ShellViewModel.SelectedWorldDataItem.TextRotationX -= ShellViewModel.VwMovementValue;
        //                ShellViewModel.Render();
        //                break;
        //        }
        //    }
        //}

        //public void Rpx()
        //{
        //    if (ShellViewModel.SelectedWorldDataItem != null)
        //    {
        //        switch (ShellViewModel.MovementMode)
        //        {
        //            case Object3DMovementMode.Object:
        //                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<RotateTransform3D>())
        //                {
        //                    var rt = transform as RotateTransform3D;
        //                    var axis = rt.Rotation as AxisAngleRotation3D;
        //                    if (axis.Axis == new Vector3D(1, 0, 0))
        //                    {
        //                        if (ShellViewModel.SelectedWorldDataItem.AutoCenter == true)
        //                        {
        //                            ShellViewModel.SelectedWorldDataItem.Ctx = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                            ShellViewModel.SelectedWorldDataItem.Cty = ShellViewModel.SelectedWorldDataItem.Tty;
        //                            ShellViewModel.SelectedWorldDataItem.Ctz = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                        }

        //                        rt.CenterX = ShellViewModel.SelectedWorldDataItem.Ctx; rt.CenterY = ShellViewModel.SelectedWorldDataItem.Cty; rt.CenterZ = ShellViewModel.SelectedWorldDataItem.Ctz;
        //                        ShellViewModel.SelectedWorldDataItem.Rtx += ShellViewModel.VwMovementValue;
        //                        axis.Angle = ShellViewModel.SelectedWorldDataItem.Rtx;
        //                        ShellViewModel.LastActionType = ActionType.RPX;
        //                    }


        //                }
        //                break;
        //            case Object3DMovementMode.Text:
        //                ShellViewModel.SelectedWorldDataItem.TextRotationX += ShellViewModel.VwMovementValue;
        //                ShellViewModel.Render();
        //                break;
        //        }
        //    }
        //}

        //public void Rny()
        //{
        //    if (ShellViewModel.SelectedWorldDataItem != null)
        //    {
        //        switch (ShellViewModel.MovementMode)
        //        {
        //            case Object3DMovementMode.Object:
        //                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<RotateTransform3D>())
        //                {
        //                    var rt = transform as RotateTransform3D;
        //                    var axis = rt.Rotation as AxisAngleRotation3D;
        //                    if (axis.Axis == new Vector3D(0, 1, 0))
        //                    {
        //                        if (ShellViewModel.SelectedWorldDataItem.AutoCenter == true)
        //                        {
        //                            ShellViewModel.SelectedWorldDataItem.Ctx = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                            ShellViewModel.SelectedWorldDataItem.Cty = ShellViewModel.SelectedWorldDataItem.Tty;
        //                            ShellViewModel.SelectedWorldDataItem.Ctz = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                        }

        //                        rt.CenterX = ShellViewModel.SelectedWorldDataItem.Ctx; rt.CenterY = ShellViewModel.SelectedWorldDataItem.Cty; rt.CenterZ = ShellViewModel.SelectedWorldDataItem.Ctz;
        //                        ShellViewModel.SelectedWorldDataItem.Rty -= ShellViewModel.VwMovementValue;
        //                        axis.Angle = ShellViewModel.SelectedWorldDataItem.Rty;
        //                        ShellViewModel.LastActionType = ActionType.RNY;
        //                    }
        //                }
        //                break;
        //            case Object3DMovementMode.Text:
        //                ShellViewModel.SelectedWorldDataItem.TextRotationY -= ShellViewModel.VwMovementValue;
        //                ShellViewModel.Render();
        //                break;
        //        }

        //    }
        //}

        //public void Rpy()
        //{
        //    if (ShellViewModel.SelectedWorldDataItem != null)
        //    {
        //        switch (ShellViewModel.MovementMode)
        //        {
        //            case Object3DMovementMode.Object:
        //                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<RotateTransform3D>())
        //                {
        //                    var rt = transform as RotateTransform3D;
        //                    var axis = rt.Rotation as AxisAngleRotation3D;
        //                    if (axis.Axis == new Vector3D(0, 1, 0))
        //                    {
        //                        if (ShellViewModel.SelectedWorldDataItem.AutoCenter == true)
        //                        {
        //                            ShellViewModel.SelectedWorldDataItem.Ctx = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                            ShellViewModel.SelectedWorldDataItem.Cty = ShellViewModel.SelectedWorldDataItem.Tty;
        //                            ShellViewModel.SelectedWorldDataItem.Ctz = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                        }

        //                        rt.CenterX = ShellViewModel.SelectedWorldDataItem.Ctx; rt.CenterY = ShellViewModel.SelectedWorldDataItem.Cty; rt.CenterZ = ShellViewModel.SelectedWorldDataItem.Ctz;
        //                        ShellViewModel.SelectedWorldDataItem.Rty += ShellViewModel.VwMovementValue;
        //                        axis.Angle = ShellViewModel.SelectedWorldDataItem.Rty;
        //                        ShellViewModel.LastActionType = ActionType.RPY;
        //                    }
        //                }
        //                break;
        //            case Object3DMovementMode.Text:
        //                ShellViewModel.SelectedWorldDataItem.TextRotationY += ShellViewModel.VwMovementValue;
        //                ShellViewModel.Render();
        //                break;
        //        }
        //    }
        //}

        //public void Rnz()
        //{
        //    if (ShellViewModel.SelectedWorldDataItem != null)
        //    {
        //        switch (ShellViewModel.MovementMode)
        //        {
        //            case Object3DMovementMode.Object:
        //                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<RotateTransform3D>())
        //                {
        //                    var rt = transform as RotateTransform3D;
        //                    var axis = rt.Rotation as AxisAngleRotation3D;
        //                    if (axis.Axis == new Vector3D(0, 0, 1))
        //                    {
        //                        if (ShellViewModel.SelectedWorldDataItem.AutoCenter == true)
        //                        {
        //                            ShellViewModel.SelectedWorldDataItem.Ctx = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                            ShellViewModel.SelectedWorldDataItem.Cty = ShellViewModel.SelectedWorldDataItem.Tty;
        //                            ShellViewModel.SelectedWorldDataItem.Ctz = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                        }

        //                        rt.CenterX = ShellViewModel.SelectedWorldDataItem.Ctx; rt.CenterY = ShellViewModel.SelectedWorldDataItem.Cty; rt.CenterZ = ShellViewModel.SelectedWorldDataItem.Ctz;
        //                        ShellViewModel.SelectedWorldDataItem.Rtz -= ShellViewModel.VwMovementValue;
        //                        axis.Angle = ShellViewModel.SelectedWorldDataItem.Rtz;
        //                        ShellViewModel.LastActionType = ActionType.RNZ;
        //                    }
        //                }
        //                break;
        //            case Object3DMovementMode.Text:
        //                ShellViewModel.SelectedWorldDataItem.TextRotationZ -= ShellViewModel.VwMovementValue;
        //                ShellViewModel.Render();
        //                break;
        //        }
        //    }
        //}

        //public void Rpz()
        //{
        //    if (ShellViewModel.SelectedWorldDataItem != null)
        //    {
        //        switch (ShellViewModel.MovementMode)
        //        {
        //            case Object3DMovementMode.Object:
        //                foreach (var transform in ShellViewModel.SelectedWorldDataItem.TransformGroup.Children.OfType<RotateTransform3D>())
        //                {
        //                    var rt = transform as RotateTransform3D;
        //                    var axis = rt.Rotation as AxisAngleRotation3D;
        //                    if (axis.Axis == new Vector3D(0, 0, 1))
        //                    {
        //                        if (ShellViewModel.SelectedWorldDataItem.AutoCenter == true)
        //                        {
        //                            ShellViewModel.SelectedWorldDataItem.Ctx = ShellViewModel.SelectedWorldDataItem.Ttx;
        //                            ShellViewModel.SelectedWorldDataItem.Cty = ShellViewModel.SelectedWorldDataItem.Tty;
        //                            ShellViewModel.SelectedWorldDataItem.Ctz = ShellViewModel.SelectedWorldDataItem.Ttz;
        //                        }

        //                        rt.CenterX = ShellViewModel.SelectedWorldDataItem.Ctx; rt.CenterY = ShellViewModel.SelectedWorldDataItem.Cty; rt.CenterZ = ShellViewModel.SelectedWorldDataItem.Ctz;
        //                        ShellViewModel.SelectedWorldDataItem.Rtz += ShellViewModel.VwMovementValue;
        //                        axis.Angle = ShellViewModel.SelectedWorldDataItem.Rtz;
        //                        ShellViewModel.LastActionType = ActionType.RPZ;
        //                    }
        //                }
        //                break;
        //            case Object3DMovementMode.Text:
        //                ShellViewModel.SelectedWorldDataItem.TextRotationZ += ShellViewModel.VwMovementValue;
        //                ShellViewModel.Render();
        //                break;
        //        }
        //    }
        //}

        //#endregion      
    }
}
