예제 #1
0
        private void DrawWavyLine(Wpg.WordprocessingGroup wordprocessingGroup, Rect cmlExtents, Point bondStart, Point bondEnd)
        {
            UInt32Value id           = UInt32Value.FromUInt32((uint)m_ooxmlId++);
            string      bondLineName = "WavyLine" + id;

            Vector bondVector  = bondEnd - bondStart;
            int    noOfWiggles = (int)Math.Ceiling(bondVector.Length / BondOffset());

            if (noOfWiggles < 1)
            {
                noOfWiggles = 1;
            }

            double wiggleLength = bondVector.Length / noOfWiggles;

            Vector originalWigglePortion = bondVector;

            originalWigglePortion.Normalize();
            originalWigglePortion *= wiggleLength / 2;

            Matrix toLeft = new Matrix();

            toLeft.Rotate(-60);
            Matrix toRight = new Matrix();

            toRight.Rotate(60);
            Vector leftVector  = originalWigglePortion * toLeft;
            Vector rightVector = originalWigglePortion * toRight;

            List <Point>         allpoints    = new List <Point>();
            List <List <Point> > allTriangles = new List <List <Point> >();
            List <Point>         triangle     = new List <Point>();

            Point lastPoint = bondStart;

            allpoints.Add(lastPoint);
            triangle.Add(lastPoint);
            for (int i = 0; i < noOfWiggles; i++)
            {
                Point leftPoint = lastPoint + leftVector;
                allpoints.Add(leftPoint);
                triangle.Add(leftPoint);

                Point midPoint = lastPoint + originalWigglePortion;
                allpoints.Add(midPoint);
                triangle.Add(midPoint);
                allTriangles.Add(triangle);
                triangle = new List <Point>();
                triangle.Add(midPoint);

                Point rightPoint = lastPoint + originalWigglePortion + rightVector;
                allpoints.Add(rightPoint);
                triangle.Add(rightPoint);

                lastPoint += originalWigglePortion * 2;
                allpoints.Add(lastPoint);
                triangle.Add(lastPoint);
                allTriangles.Add(triangle);
                triangle = new List <Point>();
                triangle.Add(lastPoint);
            }

            double minX = double.MaxValue;
            double maxX = double.MinValue;
            double minY = double.MaxValue;
            double maxY = double.MinValue;

            foreach (Point p in allpoints)
            {
                maxX = Math.Max(p.X + cmlExtents.Left, maxX);
                minX = Math.Min(p.X + cmlExtents.Left, minX);
                maxY = Math.Max(p.Y + cmlExtents.Top, maxY);
                minY = Math.Min(p.Y + cmlExtents.Top, minY);
            }

            Rect   newExtents = new Rect(minX, minY, maxX - minX, maxY - minY);
            double xOffset    = cmlExtents.Left - newExtents.Left;
            double yOffset    = cmlExtents.Top - newExtents.Top;

            Int64Value width  = OoXmlHelper.ScaleCmlToEmu(newExtents.Width);
            Int64Value height = OoXmlHelper.ScaleCmlToEmu(newExtents.Height);
            Int64Value top    = OoXmlHelper.ScaleCmlToEmu(newExtents.Top);
            Int64Value left   = OoXmlHelper.ScaleCmlToEmu(newExtents.Left);

            Wps.WordprocessingShape        shape = new Wps.WordprocessingShape();
            Wps.NonVisualDrawingProperties nonVisualDrawingProperties = new Wps.NonVisualDrawingProperties()
            {
                Id = id, Name = bondLineName
            };
            Wps.NonVisualDrawingShapeProperties nonVisualDrawingShapeProperties = new Wps.NonVisualDrawingShapeProperties();

            Wps.ShapeProperties shapeProperties = new Wps.ShapeProperties();

            A.Transform2D transform2D = new A.Transform2D();
            A.Offset      offset      = new A.Offset {
                X = left, Y = top
            };
            A.Extents extents = new A.Extents {
                Cx = width, Cy = height
            };

            transform2D.Append(offset);
            transform2D.Append(extents);

            A.CustomGeometry  customGeometry  = new A.CustomGeometry();
            A.AdjustValueList adjustValueList = new A.AdjustValueList();
            A.Rectangle       rectangle       = new A.Rectangle {
                Left = "l", Top = "t", Right = "r", Bottom = "b"
            };

            A.PathList pathList = new A.PathList();

            A.Path path = new A.Path {
                Width = width, Height = height
            };

            A.MoveTo moveTo = new A.MoveTo();
            A.Point  point1 = new A.Point {
                X = OoXmlHelper.ScaleCmlToEmu(bondStart.X + xOffset).ToString(), Y = OoXmlHelper.ScaleCmlToEmu(bondStart.Y + yOffset).ToString()
            };
            moveTo.Append(point1);
            path.Append(moveTo);

            // Render as Curves
            foreach (var tri in allTriangles)
            {
                A.CubicBezierCurveTo cubicBezierCurveTo = new A.CubicBezierCurveTo();
                foreach (var p in tri)
                {
                    A.Point point = new A.Point {
                        X = OoXmlHelper.ScaleCmlToEmu(p.X + xOffset).ToString(), Y = OoXmlHelper.ScaleCmlToEmu(p.Y + yOffset).ToString()
                    };
                    cubicBezierCurveTo.Append(point);
                }
                path.Append(cubicBezierCurveTo);
            }

            // Render as Straight Lines
            //foreach (var p in allpoints)
            //{
            //    A.LineTo lineTo = new A.LineTo();
            //    A.Point point = new A.Point { X = OoXmlHelper.ScaleCmlToEmu(p.X + xOffset).ToString(), Y = OoXmlHelper.ScaleCmlToEmu(p.Y + yOffset).ToString() };
            //    lineTo.Append(point);
            //    path1.Append(lineTo);
            //}

            pathList.Append(path);

            customGeometry.Append(adjustValueList);
            customGeometry.Append(rectangle);
            customGeometry.Append(pathList);

            A.Outline outline = new A.Outline {
                Width = OoXmlHelper.ACS_LINE_WIDTH_EMUS, CapType = A.LineCapValues.Round
            };

            A.SolidFill solidFill = new A.SolidFill();

            A.RgbColorModelHex rgbColorModelHex = new A.RgbColorModelHex {
                Val = "000000"
            };
            solidFill.Append(rgbColorModelHex);

            outline.Append(solidFill);

            shapeProperties.Append(transform2D);
            shapeProperties.Append(customGeometry);
            shapeProperties.Append(outline);

            OoXmlHelper.AppendShapeStyle(shape, nonVisualDrawingProperties, nonVisualDrawingShapeProperties, shapeProperties);
            wordprocessingGroup.Append(shape);
        }
예제 #2
0
        private void DrawWavyLine(Wpg.WordprocessingGroup wordprocessingGroup1, Rect extents, Point bondStart, Point bondEnd)
        {
            UInt32Value bondLineId   = UInt32Value.FromUInt32((uint)m_ooxmlId++);
            string      bondLineName = "WavyLine" + bondLineId;

            Vector bondVector  = bondEnd - bondStart;
            int    noOfWiggles = (int)Math.Ceiling(bondVector.Length / BondOffset());

            if (noOfWiggles < 1)
            {
                noOfWiggles = 1;
            }

            double wiggleLength = bondVector.Length / noOfWiggles;

            Debug.WriteLine($"v.Length: {bondVector.Length} noOfWiggles: {noOfWiggles}");

            Vector originalWigglePortion = bondVector;

            originalWigglePortion.Normalize();
            originalWigglePortion *= wiggleLength / 2;

            Matrix toLeft = new Matrix();

            toLeft.Rotate(-60);
            Matrix toRight = new Matrix();

            toRight.Rotate(60);
            Vector leftVector  = originalWigglePortion * toLeft;
            Vector rightVector = originalWigglePortion * toRight;

            List <Point>         allpoints    = new List <Point>();
            List <List <Point> > allTriangles = new List <List <Point> >();
            List <Point>         triangle     = new List <Point>();

            Point lastPoint = bondStart;

            allpoints.Add(lastPoint);
            triangle.Add(lastPoint);
            for (int i = 0; i < noOfWiggles; i++)
            {
                Point leftPoint = lastPoint + leftVector;
                allpoints.Add(leftPoint);
                triangle.Add(leftPoint);

                Point midPoint = lastPoint + originalWigglePortion;
                allpoints.Add(midPoint);
                triangle.Add(midPoint);
                allTriangles.Add(triangle);
                triangle = new List <Point>();
                triangle.Add(midPoint);

                Point rightPoint = lastPoint + originalWigglePortion + rightVector;
                allpoints.Add(rightPoint);
                triangle.Add(rightPoint);

                lastPoint += originalWigglePortion * 2;
                allpoints.Add(lastPoint);
                triangle.Add(lastPoint);
                allTriangles.Add(triangle);
                triangle = new List <Point>();
                triangle.Add(lastPoint);
            }

            double minX = double.MaxValue;
            double maxX = double.MinValue;
            double minY = double.MaxValue;
            double maxY = double.MinValue;

            foreach (Point p in allpoints)
            {
                maxX = Math.Max(p.X + extents.Left, maxX);
                minX = Math.Min(p.X + extents.Left, minX);
                maxY = Math.Max(p.Y + extents.Top, maxY);
                minY = Math.Min(p.Y + extents.Top, minY);
            }

            Rect   newExtents = new Rect(minX, minY, maxX - minX, maxY - minY);
            double xOffset    = extents.Left - newExtents.Left;
            double yOffset    = extents.Top - newExtents.Top;

            Int64Value width  = OoXmlHelper.ScaleCmlToEmu(newExtents.Width);
            Int64Value height = OoXmlHelper.ScaleCmlToEmu(newExtents.Height);
            Int64Value top    = OoXmlHelper.ScaleCmlToEmu(newExtents.Top);
            Int64Value left   = OoXmlHelper.ScaleCmlToEmu(newExtents.Left);

            Wps.WordprocessingShape        wordprocessingShape1        = new Wps.WordprocessingShape();
            Wps.NonVisualDrawingProperties nonVisualDrawingProperties1 = new Wps.NonVisualDrawingProperties()
            {
                Id = bondLineId, Name = bondLineName
            };
            Wps.NonVisualDrawingShapeProperties nonVisualDrawingShapeProperties1 = new Wps.NonVisualDrawingShapeProperties();

            Wps.ShapeProperties shapeProperties1 = new Wps.ShapeProperties();

            A.Transform2D transform2D1 = new A.Transform2D();
            A.Offset      offset2      = new A.Offset()
            {
                X = left, Y = top
            };
            A.Extents extents2 = new A.Extents()
            {
                Cx = width, Cy = height
            };

            transform2D1.Append(offset2);
            transform2D1.Append(extents2);

            A.CustomGeometry  customGeometry1  = new A.CustomGeometry();
            A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
            A.Rectangle       rectangle1       = new A.Rectangle()
            {
                Left = "l", Top = "t", Right = "r", Bottom = "b"
            };

            A.PathList pathList1 = new A.PathList();

            A.Path path1 = new A.Path()
            {
                Width = width, Height = height
            };

            A.MoveTo moveTo1 = new A.MoveTo();
            A.Point  point1  = new A.Point()
            {
                X = OoXmlHelper.ScaleCmlToEmu(bondStart.X + xOffset).ToString(), Y = OoXmlHelper.ScaleCmlToEmu(bondStart.Y + yOffset).ToString()
            };
            moveTo1.Append(point1);
            path1.Append(moveTo1);

            //Curves
            foreach (var tri in allTriangles)
            {
                A.CubicBezierCurveTo cubicBezierCurveTo = new A.CubicBezierCurveTo();
                foreach (var p in tri)
                {
                    A.Point point = new A.Point()
                    {
                        X = OoXmlHelper.ScaleCmlToEmu(p.X + xOffset).ToString(), Y = OoXmlHelper.ScaleCmlToEmu(p.Y + yOffset).ToString()
                    };
                    cubicBezierCurveTo.Append(point);
                }
                path1.Append(cubicBezierCurveTo);
            }

            //// Straight Lines
            //foreach (var p in allpoints)
            //{
            //    A.LineTo lineTo = new A.LineTo();
            //    A.Point point = new A.Point() { X = OoXmlHelper.ScaleCmlToEmu(p.X + xOffset).ToString(), Y = OoXmlHelper.ScaleCmlToEmu(p.Y + yOffset).ToString() };
            //    lineTo.Append(point);
            //    path1.Append(lineTo);
            //}

            pathList1.Append(path1);

            customGeometry1.Append(adjustValueList1);
            customGeometry1.Append(rectangle1);
            customGeometry1.Append(pathList1);

            A.Outline outline1 = new A.Outline()
            {
                Width = 9525, CapType = A.LineCapValues.Round
            };

            A.SolidFill solidFill1 = new A.SolidFill();

            A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex()
            {
                Val = "000000"
            };
            A.Alpha alpha1 = new A.Alpha()
            {
                Val = new Int32Value()
                {
                    InnerText = "100%"
                }
            };

            rgbColorModelHex1.Append(alpha1);

            solidFill1.Append(rgbColorModelHex1);

            outline1.Append(solidFill1);

            shapeProperties1.Append(transform2D1);
            shapeProperties1.Append(customGeometry1);
            shapeProperties1.Append(outline1);

            Wps.ShapeStyle  shapeStyle1    = new Wps.ShapeStyle();
            A.LineReference lineReference1 = new A.LineReference()
            {
                Index = (UInt32Value)0U
            };
            A.FillReference fillReference1 = new A.FillReference()
            {
                Index = (UInt32Value)0U
            };
            A.EffectReference effectReference1 = new A.EffectReference()
            {
                Index = (UInt32Value)0U
            };
            A.FontReference fontReference1 = new A.FontReference()
            {
                Index = A.FontCollectionIndexValues.Minor
            };

            shapeStyle1.Append(lineReference1);
            shapeStyle1.Append(fillReference1);
            shapeStyle1.Append(effectReference1);
            shapeStyle1.Append(fontReference1);
            Wps.TextBodyProperties textBodyProperties1 = new Wps.TextBodyProperties();

            wordprocessingShape1.Append(nonVisualDrawingProperties1);
            wordprocessingShape1.Append(nonVisualDrawingShapeProperties1);
            wordprocessingShape1.Append(shapeProperties1);
            wordprocessingShape1.Append(shapeStyle1);
            wordprocessingShape1.Append(textBodyProperties1);

            wordprocessingGroup1.Append(wordprocessingShape1);
        }