Beispiel #1
0
        public void AssertPathBuilderContains(PdfPathBuilder builder, string expected)
        {
            var page = PdfPage.NewLetter();

            page.Items.Add(builder.ToPath());
            AssertPageContains(page, expected);
        }
Beispiel #2
0
        public void VerifyEllipseReallyCloseToQuadrants1And4()
        {
            var el = new PdfEllipse(
                new PdfPoint(272.0, 396.0, 0.0),
                new PdfMeasurement(340.0, PdfMeasurementType.Point),
                new PdfMeasurement(339.99999999999994, PdfMeasurementType.Point),
                rotationAngle: 0.0,
                startAngle: 4.712388980384712,
                endAngle: 1.5707963267949143);

            var page    = PdfPage.NewLetter();
            var builder = new PdfPathBuilder()
            {
                el
            };

            page.Items.Add(builder.ToPath());
            var file = new PdfFile();

            file.Pages.Add(page);

            AssertPathItemContains(el, @"
612.00 396.00 m
612.00 583.78 459.78 736.00 272.00 736.00 c
272.00 736.00 m
272.00 736.00 272.00 736.00 272.00 736.00 c
272.00 56.00 m
459.78 56.00 612.00 208.22 612.00 396.00 c
");
        }
        public PdfFile Convert(DxfFile source, DxfToPdfConverterOptions options)
        {
            // adapted from https://github.com/ixmilia/bcad/blob/main/src/IxMilia.BCad.FileHandlers/Plotting/Pdf/PdfPlotter.cs
            CreateTransformations(source.ActiveViewPort, options, out Matrix4 scale, out Matrix4 affine);
            var pdf  = new PdfFile();
            var page = new PdfPage(options.PageWidth, options.PageHeight);

            pdf.Pages.Add(page);

            var builder = new PdfPathBuilder();

            foreach (var layer in source.Layers)
            {
                foreach (var entity in source.Entities.Where(e => e.Layer == layer.Name))
                {
                    TryConvertEntity(entity, layer, affine, scale, builder, page);
                    // if that failed, emit some diagnostic hint? Callback?
                }
            }

            if (builder.Items.Count > 0)
            {
                page.Items.Add(builder.ToPath());
            }

            return(pdf);
        }
Beispiel #4
0
        public void AssertPathItemContains(IPdfPathItem pathItem, string expected)
        {
            var builder = new PdfPathBuilder()
            {
                pathItem
            };

            AssertPathBuilderContains(builder, expected);
        }
Beispiel #5
0
        public void VerifyFillTest()
        {
            var builder = new PdfPathBuilder()
            {
                new PdfLine(
                    new PdfPoint(PdfMeasurement.Points(0.0), PdfMeasurement.Points(0.0)),
                    new PdfPoint(PdfMeasurement.Points(1.0), PdfMeasurement.Points(1.0))),
                new PdfLine(
                    new PdfPoint(PdfMeasurement.Points(2.0), PdfMeasurement.Points(2.0)),
                    new PdfPoint(PdfMeasurement.Points(3.0), PdfMeasurement.Points(3.0)),
                    state: new PdfStreamState(strokeWidth: PdfMeasurement.Points(1.1))),
                new PdfLine(
                    new PdfPoint(PdfMeasurement.Points(4.0), PdfMeasurement.Points(4.0)),
                    new PdfPoint(PdfMeasurement.Points(5.0), PdfMeasurement.Points(5.0)),
                    state: new PdfStreamState(nonStrokeColor: new PdfColor(1.0, 0.0, 0.0))),
                new PdfLine(
                    new PdfPoint(PdfMeasurement.Points(6.0), PdfMeasurement.Points(6.0)),
                    new PdfPoint(PdfMeasurement.Points(7.0), PdfMeasurement.Points(7.0)),
                    state: new PdfStreamState(nonStrokeColor: new PdfColor(0.0, 1.0, 0.0), strokeWidth: PdfMeasurement.Points(2.2)))
            };

            AssertPathBuilderContains(builder, @"
0 w
0 0 0 RG
0 0 0 rg
0.00 0.00 m
1.00 1.00 l
S
1.1 w
2.00 2.00 m
3.00 3.00 l
S
0 w
1 0 0 rg
4.00 4.00 m
5.00 5.00 l
S
2.2 w
0 1 0 rg
6.00 6.00 m
7.00 7.00 l
S
");
        }
Beispiel #6
0
        public void VerifyPageLinesTest()
        {
            var builder = new PdfPathBuilder()
            {
                new PdfLine(
                    new PdfPoint(PdfMeasurement.Zero, PdfMeasurement.Zero),
                    new PdfPoint(PageWidth, PageHeight)),
                new PdfLine(
                    new PdfPoint(PageWidth, PdfMeasurement.Zero),
                    new PdfPoint(PdfMeasurement.Zero, PageHeight))
            };

            AssertPathBuilderContains(builder, @"
0.00 0.00 m
612.00 792.00 l
612.00 0.00 m
0.00 792.00 l
");
        }
        private static bool TryConvertEntity(DxfEntity entity, DxfLayer layer, Matrix4 affine, Matrix4 scale, PdfPathBuilder builder, PdfPage page)
        {
            switch (entity)
            {
            case DxfText text:
                // TODO flush path builder and recreate
                page.Items.Add(ConvertText(text, layer, affine, scale));
                return(true);

            case DxfLine line:
                Add(ConvertLine(line, layer, affine), builder);
                return(true);

            case DxfModelPoint point:
                Add(ConvertPoint(point, layer, affine, scale), builder);
                return(true);

            case DxfArc arc:
                Add(ConvertArc(arc, layer, affine, scale), builder);
                return(true);

            case DxfCircle circle:
                Add(ConvertCircle(circle, layer, affine, scale), builder);
                return(true);

            case DxfLwPolyline lwPolyline:
                Add(ConvertPolyline(lwPolyline, layer, affine, scale), builder);
                return(true);

            default:
                return(false);
            }

            void Add(IEnumerable <IPdfPathItem> items, PdfPathBuilder b)
            {
                foreach (IPdfPathItem item in items)
                {
                    b.Add(item);
                }
            }
        }
Beispiel #8
0
        public override void Plot(IWorkspace workspace)
        {
            var file = new PdfFile();
            var font = new PdfFontType1(PdfFontType1Type.Helvetica);

            foreach (var pageViewModel in ViewModel.Pages)
            {
                var projectedEntities = ProjectionHelper.ProjectTo2D(
                    workspace.Drawing,
                    pageViewModel.ViewPort,
                    pageViewModel.ViewWidth,
                    pageViewModel.ViewHeight,
                    ProjectionStyle.OriginBottomLeft);
                var page = new PdfPage(
                    new PdfMeasurement(pageViewModel.ViewWidth, PdfMeasurementType.Inch),
                    new PdfMeasurement(pageViewModel.ViewHeight, PdfMeasurementType.Inch));
                file.Pages.Add(page);
                var builder = new PdfPathBuilder();
                void AddPathItemToPage(IPdfPathItem pathItem)
                {
                    builder.Add(pathItem);
                }

                void AddStreamItemToPage(PdfStreamItem streamItem)
                {
                    if (builder.Items.Count > 0)
                    {
                        page.Items.Add(builder.ToPath());
                        builder = new PdfPathBuilder();
                    }

                    page.Items.Add(streamItem);
                }

                foreach (var group in projectedEntities.GroupBy(e => e.OriginalLayer).OrderBy(l => l.Key.Name))
                {
                    var layer = group.Key;
                    foreach (var entity in group)
                    {
                        var scale = 1.0;
                        switch (entity.Kind)
                        {
                        case EntityKind.Arc:
                            var arc = (ProjectedArc)entity;
                            scale = arc.RadiusX / arc.OriginalArc.Radius;
                            AddPathItemToPage(new PdfArc(
                                                  arc.Center.ToPdfPoint(PdfMeasurementType.Inch),
                                                  new PdfMeasurement(arc.RadiusX, PdfMeasurementType.Inch),
                                                  arc.StartAngle * MathHelper.DegreesToRadians,
                                                  arc.EndAngle * MathHelper.DegreesToRadians,
                                                  state: new PdfStreamState(
                                                      strokeColor: (arc.OriginalArc.Color ?? layer.Color ?? AutoColor).ToPdfColor(),
                                                      strokeWidth: new PdfMeasurement(ApplyScaleToThickness(arc.OriginalArc.Thickness, scale), PdfMeasurementType.Inch))));
                            break;

                        case EntityKind.Circle:
                            var circle = (ProjectedCircle)entity;
                            scale = circle.RadiusX / circle.OriginalCircle.Radius;
                            AddPathItemToPage(new PdfCircle(
                                                  circle.Center.ToPdfPoint(PdfMeasurementType.Inch),
                                                  new PdfMeasurement(circle.RadiusX, PdfMeasurementType.Inch),
                                                  state: new PdfStreamState(
                                                      strokeColor: (circle.OriginalCircle.Color ?? layer.Color ?? AutoColor).ToPdfColor(),
                                                      strokeWidth: new PdfMeasurement(ApplyScaleToThickness(circle.OriginalCircle.Thickness, scale), PdfMeasurementType.Inch))));
                            break;

                        case EntityKind.Line:
                            var line = (ProjectedLine)entity;
                            scale = (line.P2 - line.P1).Length / (line.OriginalLine.P2 - line.OriginalLine.P1).Length;
                            AddPathItemToPage(new PdfLine(
                                                  line.P1.ToPdfPoint(PdfMeasurementType.Inch),
                                                  line.P2.ToPdfPoint(PdfMeasurementType.Inch),
                                                  state: new PdfStreamState(
                                                      strokeColor: (line.OriginalLine.Color ?? layer.Color ?? AutoColor).ToPdfColor(),
                                                      strokeWidth: new PdfMeasurement(ApplyScaleToThickness(line.OriginalLine.Thickness, scale), PdfMeasurementType.Inch))));
                            break;

                        case EntityKind.Text:
                            var text = (ProjectedText)entity;
                            AddStreamItemToPage(
                                new PdfText(
                                    text.OriginalText.Value,
                                    font,
                                    new PdfMeasurement(text.Height, PdfMeasurementType.Inch),
                                    text.Location.ToPdfPoint(PdfMeasurementType.Inch),
                                    state: new PdfStreamState(
                                        nonStrokeColor: (text.OriginalText.Color ?? layer.Color ?? AutoColor).ToPdfColor())));
                            break;

                        default:
                            // TODO:
                            break;
                        }
                    }
                }

                if (builder.Items.Count > 0)
                {
                    page.Items.Add(builder.ToPath());
                }
            }

            file.Save(ViewModel.Stream);
        }