private void AddElement3D(MeshBuilder bldr, Element3D elm)
        {
            if (elm.Nodes.Length == 8)
            {
                //brick

                var p = new Func <Node, Point>(i => i.Location);

                var sides = new int[][]
                {
                    new int[] { 1, 2, 6 }, new int[] { 1, 5, 6 },
                    new int[] { 2, 6, 3 }, new int[] { 7, 6, 3 },
                    new int[] { 4, 3, 7 }, new int[] { 4, 8, 7 },

                    new int[] { 1, 4, 8 }, new int[] { 1, 5, 8 },
                    new int[] { 5, 6, 7 }, new int[] { 5, 8, 7 },
                    new int[] { 1, 2, 3 }, new int[] { 1, 4, 3 },
                };



                foreach (var side in sides)
                {
                    bldr.AddTriangle(p(elm.Nodes[side[0] - 1]), p(elm.Nodes[side[1] - 1]), p(elm.Nodes[side[2] - 1]));
                }
            }
        }
        private void AddElement2d(MeshBuilder bldr, Element2D elm)
        {
            if (elm.Nodes.Length == 4)
            {
                var p1 = elm.Nodes[0].Location;
                var p2 = elm.Nodes[1].Location;
                var p3 = elm.Nodes[2].Location;
                var p4 = elm.Nodes[3].Location;


                bldr.AddTriangle(p1, p2, p3);

                bldr.AddTriangle(p1, p3, p4);
            }


            if (elm.Nodes.Length == 3)
            {
                var p1 = elm.Nodes[0].Location;
                var p2 = elm.Nodes[1].Location;
                var p3 = elm.Nodes[2].Location;


                bldr.AddTriangle(p1, p2, p3);
            }
        }
        private void AddRigidElement(MeshBuilder bldr, RigidElement elm)
        {
            PolygonYz section = null;

            if (elm.Nodes.Count(i => !ReferenceEquals(i, null)) < 2)
            {
                return;
            }

            var r = ElementVisualThickness / 2;

            var cnt = elm.CentralNode;

            if (cnt == null)
            {
                cnt = elm.Nodes.First(i => !ReferenceEquals(i, null));

                /*
                 * for (int i = 0; i < elm.Nodes.Count; i++)
                 * {
                 *  for (int j = 0; j < elm.Nodes.Count; j++)
                 *  {
                 *      if(i==j)
                 *          continue;
                 *
                 *      var st = elm.Nodes[i].Location.ToPoint3D();
                 *      var en = elm.Nodes[j].Location.ToPoint3D();
                 *
                 *      bldr.AddPipe(st, en, ElementVisualThickness / 2, ElementVisualThickness / 1.9, 4);
                 *  }
                 * }
                 */
            }
            //else
            {
                for (var i = 0; i < elm.Nodes.Count; i++)
                {
                    if (ReferenceEquals(elm.Nodes[i], null))
                    {
                        continue;
                    }

                    if (ReferenceEquals(elm.Nodes[i], cnt))
                    {
                        continue;
                    }

                    var st = elm.Nodes[i].Location.ToPoint3D();
                    var en = cnt.Location.ToPoint3D();

                    bldr.AddPipe(st, en, ElementVisualThickness / 2, ElementVisualThickness / 1.9, 4);
                }
            }
        }
        private void AddSpringElement(MeshBuilder bldr, Spring1D elm)
        {
            PolygonYz section = null;

            var r = ElementVisualThickness / 2;

            if (elm.StartNode.Location.Equals(elm.EndNode.Location))
            {
                bldr.AddSphere(new Point3D(elm.StartNode.Location.X,
                                           elm.StartNode.Location.Y,
                                           elm.StartNode.Location.Z), ElementVisualThickness * 3);
            }
        }
        private void AddCstElement(MeshBuilder bldr, CstElement elm)
        {
            PolygonYz section = null;

            var r = ElementVisualThickness / 2;


            var p1 = elm.Nodes[0].Location;
            var p2 = elm.Nodes[1].Location;
            var p3 = elm.Nodes[2].Location;


            bldr.AddTriangle(p1, p3, p2);
        }
        private void AddTetrahedronElement(MeshBuilder bldr, Tetrahedral elm)
        {
            PolygonYz section = null;

            var r = ElementVisualThickness / 2;


            var p1 = elm.Nodes[0].Location;
            var p2 = elm.Nodes[1].Location;
            var p3 = elm.Nodes[2].Location;
            var p4 = elm.Nodes[3].Location;


            bldr.AddTriangle(p1, p3, p4);
            bldr.AddTriangle(p3, p2, p4);
            bldr.AddTriangle(p1, p2, p4);
            bldr.AddTriangle(p1, p2, p3);
        }
        private void AddTrussElement(MeshBuilder bldr, TrussElement2Node elm)
        {
            PolygonYz section = null;

            var r = ElementVisualThickness / 2;


            if (elm.UseOverridedProperties)
            {
                section = new PolygonYz(
                    new PointYZ(-r, -r),
                    new PointYZ(-r, r),
                    new PointYZ(r, r),
                    new PointYZ(r, -r),
                    new PointYZ(-r, -r));
            }
            else
            {
                section = elm.Geometry;
            }


            for (var i = 0; i < section.Count - 1; i++)
            {
                var v1 = new Vector(0, section[i].Y, section[i].Z);
                var v2 = new Vector(0, section[i + 1].Y, section[i + 1].Z);

                var p1 = elm.StartNode.Location + elm.TransformLocalToGlobal(v1);
                var p2 = elm.StartNode.Location + elm.TransformLocalToGlobal(v2);

                var v = elm.EndNode.Location - elm.StartNode.Location;

                if (Math.Abs(v.Z) < 0.01)
                {
                    Guid.NewGuid();
                }

                var p3 = p1 + v;
                var p4 = p2 + v;

                bldr.AddTriangle(p1, p3, p2);
                bldr.AddTriangle(p4, p2, p3);
            }
        }
        /// <summary>
        /// Updates the UI.
        /// </summary>
        public void UpdateUi()
        {
            MainViewport.Children.Clear();

            if (!this.IsLoaded)
            {
                return;
            }

            if (ModelToVisualize == null)
            {
                return;
            }

            if (!ModelToVisualize.Nodes.Any())
            {
                return;
            }


            if (ElementVisualThickness == 0)
            {
                ElementVisualThickness = GetSmartElementThichness();
            }

            #region Adding elements

            var sb = new StringBuilder();


            if (ShowElements)
            {
                foreach (var elm in ModelToVisualize.Elements)
                {
                    var builder = new MeshBuilder(false, false);

                    if (elm is FrameElement2Node)
                    {
                        AddFrameElement(builder, elm as FrameElement2Node);
                    }
                    else if (elm is ConcentratedMass)
                    {
                        AddMassElement(builder, elm as ConcentratedMass);
                    }
                    else if (elm is TrussElement2Node)
                    {
                        AddTrussElement(builder, elm as TrussElement2Node);
                    }
                    else if (elm is DktElement)
                    {
                        AddDktElement(builder, elm as DktElement);
                    }
                    else if (elm is Tetrahedral)
                    {
                        AddTetrahedronElement(builder, elm as Tetrahedral);
                    }
                    else if (elm is TriangleFlatShell)
                    {
                        AddFlatshellElement(builder, elm as TriangleFlatShell);
                    }
                    else if (elm is CstElement)
                    {
                        AddCstElement(builder, elm as CstElement);
                    }
                    else if (elm is Element2D)
                    {
                        AddElement2d(builder, elm as Element2D);
                    }
                    else if (elm is Element3D)
                    {
                        AddElement3D(builder, elm as Element3D);
                    }
                    else if (elm is Spring1D)
                    {
                        AddSpringElement(builder, elm as Spring1D);
                    }

                    var gradient = new LinearGradientBrush();
                    //TODO: to be done like this: http://waldoscode.blogspot.de/2014/11/helix-3d-toolkit-well-viewer-part-2.html


                    gradient.GradientStops.Add(new GradientStop(Colors.Blue, 0));
                    gradient.GradientStops.Add(new GradientStop(Colors.Cyan, 0.2));
                    gradient.GradientStops.Add(new GradientStop(Colors.Green, 0.4));
                    gradient.GradientStops.Add(new GradientStop(Colors.Yellow, 0.6));
                    gradient.GradientStops.Add(new GradientStop(Colors.Red, 0.8));
                    gradient.GradientStops.Add(new GradientStop(Colors.White, 1));


                    var mesh = builder.ToMesh(true);

                    var material = MaterialHelper.CreateMaterial(gradient, null, null, 1, 0);

                    var mygeometry = new GeometryModel3D(mesh, material)
                    {
                        BackMaterial = material
                    };

                    var modelElement = new ModelUIElement3D();
                    modelElement.Model = mygeometry;


                    BindMouseEvents(modelElement, elm);
                    // var myModelVisual3D = new ModelVisual3D();
                    //myModelVisual3D.Content = modelGroup;


                    MainViewport.Children.Add(modelElement);
                }
            }

            #endregion

            #region Adding nodes

            if (ShowNodes)
            {
                foreach (var nde in ModelToVisualize.Nodes)
                {
                    var builder = new MeshBuilder(false, false);

                    AddNode(builder, nde);

                    var gradient = new LinearGradientBrush();
                    //to be done like this: http://waldoscode.blogspot.de/2014/11/helix-3d-toolkit-well-viewer-part-2.html

                    gradient.GradientStops.Add(new GradientStop(Colors.Blue, 0));
                    gradient.GradientStops.Add(new GradientStop(Colors.Cyan, 0.2));
                    gradient.GradientStops.Add(new GradientStop(Colors.Green, 0.4));
                    gradient.GradientStops.Add(new GradientStop(Colors.Yellow, 0.6));
                    gradient.GradientStops.Add(new GradientStop(Colors.Red, 0.8));
                    gradient.GradientStops.Add(new GradientStop(Colors.White, 1));


                    var mesh = builder.ToMesh(true);

                    var material = MaterialHelper.CreateMaterial(Brushes.Crimson);

                    var mygeometry = new GeometryModel3D(mesh, material)
                    {
                        BackMaterial = material
                    };

                    var modelElement = new ModelUIElement3D();
                    modelElement.Model = mygeometry;

                    BindMouseEvents(modelElement, nde);
                    // var myModelVisual3D = new ModelVisual3D();
                    //myModelVisual3D.Content = modelGroup;


                    MainViewport.Children.Add(modelElement);
                }
            }

            #endregion

            #region Adding rigid elements

            if (ShowRigidElements)
            {
                foreach (var elm in ModelToVisualize.RigidElements)
                {
                    var builder = new MeshBuilder(false, false);

                    AddRigidElement(builder, elm);

                    var gradient = new LinearGradientBrush();
                    //to be done like this: http://waldoscode.blogspot.de/2014/11/helix-3d-toolkit-well-viewer-part-2.html

                    gradient.GradientStops.Add(new GradientStop(Colors.Blue, 0));
                    gradient.GradientStops.Add(new GradientStop(Colors.Cyan, 0.2));
                    gradient.GradientStops.Add(new GradientStop(Colors.Green, 0.4));
                    gradient.GradientStops.Add(new GradientStop(Colors.Yellow, 0.6));
                    gradient.GradientStops.Add(new GradientStop(Colors.Red, 0.8));
                    gradient.GradientStops.Add(new GradientStop(Colors.White, 1));


                    var mesh = builder.ToMesh(true);

                    var material = MaterialHelper.CreateMaterial(Brushes.GreenYellow, Brushes.Green, Brushes.Green, 1,
                                                                 100);


                    var mygeometry = new GeometryModel3D(mesh, material)
                    {
                        BackMaterial = material
                    };

                    var modelElement = new ModelUIElement3D();
                    modelElement.Model = mygeometry;

                    BindMouseEvents(modelElement, elm);
                    // var myModelVisual3D = new ModelVisual3D();
                    //myModelVisual3D.Content = modelGroup;


                    MainViewport.Children.Add(modelElement);
                }
            }

            #endregion

            if (sb.Length != 0)
            {
                MessageBox.Show(sb.ToString());
            }


            MainViewport.Children.Add(new DefaultLights());
        }
 private void AddNode(MeshBuilder bldr, Node nde)
 {
     bldr.AddSphere(new Point3D(nde.Location.X, nde.Location.Y, nde.Location.Z),
                    ElementVisualThickness * 2, 20, 20);
 }
Exemplo n.º 10
0
 private void AddMassElement(MeshBuilder bldr, ConcentratedMass elm)
 {
     bldr.AddCone(elm.Nodes[0].Location.ToPoint3D(), new Vector3D(0, 0, 1), 0, ElementVisualThickness * 2,
                  10 * ElementVisualThickness, true, true, 5);
 }