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

            page.Items.Add(builder.ToPath());
            AssertPageContains(page, expected);
        }
예제 #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
");
        }
예제 #3
0
        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);
        }
예제 #4
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);
        }