Esempio n. 1
0
        public bool WriteExportFile(string filePath, string fileExt)
        {
            try
            {
                // instantiate filter
                PicFilter filter = (_showCotations ? PicFilter.FilterNone : PicFilter.FilterCotation) & PicFilter.FilterNoZeroEntities;
                // get bounding box
                Pic.Factory2D.PicVisitorBoundingBox visitorBoundingBox = new Pic.Factory2D.PicVisitorBoundingBox();
                _factory.ProcessVisitor(visitorBoundingBox, filter);
                Pic.Factory2D.Box2D box = visitorBoundingBox.Box;
                // add margins : 5 % of the smallest value among height
                box.AddMarginHorizontal(box.Width * 0.01);
                box.AddMarginVertical(box.Height * 0.01);

                string author     = Application.ProductName + " (" + Application.CompanyName + ")";
                string title      = System.IO.Path.GetFileNameWithoutExtension(filePath);
                string fileFormat = fileExt.TrimStart('.').ToLower();

                byte[] byteArray;
                // get file content
                if ("des" == fileFormat)
                {
                    using (Pic.Factory2D.PicVisitorDesOutput visitor = new Pic.Factory2D.PicVisitorDesOutput())
                    {
                        visitor.Author = author;
                        _factory.ProcessVisitor(visitor, filter);
                        byteArray = visitor.GetResultByteArray();
                    }
                }
                else if ("dxf" == fileFormat)
                {
                    using (Pic.Factory2D.PicVisitorDxfOutput visitor = new Pic.Factory2D.PicVisitorDxfOutput())
                    {
                        visitor.Author = author;
                        _factory.ProcessVisitor(visitor, filter);
                        byteArray = visitor.GetResultByteArray();
                    }
                }
                else if ("pdf" == fileFormat)
                {
                    using (Pic.Factory2D.PicGraphicsPdf graphics = new PicGraphicsPdf(box))
                    {
                        graphics.Author = author;
                        graphics.Title  = title;
                        _factory.Draw(graphics, filter);
                        byteArray = graphics.GetResultByteArray();
                    }
                }
                else if ("ai" == fileFormat || "cf2" == fileFormat)
                {
                    using (Pic.Factory2D.PicVisitorDiecutOutput visitor = new PicVisitorDiecutOutput(fileExt))
                    {
                        visitor.Author = author;
                        visitor.Title  = title;
                        _factory.ProcessVisitor(visitor, filter);
                        byteArray = visitor.GetResultByteArray();
                    }
                }
                else
                {
                    throw new Exception("Invalid file format: " + fileFormat);
                }
                // write byte array to stream
                using (System.IO.FileStream fstream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                    fstream.Write(byteArray, 0, byteArray.GetLength(0));

                return(true);
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("Exception: {0}", ex.ToString()));
                return(false);
            }
        }
Esempio n. 2
0
        public byte[] GetExportFile(string fileExt, ParameterStack stack)
        {
            // build factory
            PicFactory factory = new PicFactory();

            Component.CreateFactoryEntities(factory, stack);
            if (_reflectionX)
            {
                factory.ProcessVisitor(new PicVisitorTransform(Transform2D.ReflectionX));
            }
            if (_reflectionY)
            {
                factory.ProcessVisitor(new PicVisitorTransform(Transform2D.ReflectionY));
            }
            // remove existing quotations
            if (!_showCotationCode)
            {
                factory.Remove(PicFilter.FilterCotation);
            }
            // build auto quotations
            if (_showCotationAuto)
            {
                PicAutoQuotation.BuildQuotation(factory);
            }

            // instantiate filter
            PicFilter filter = !PicFilter.FilterCotation
                               & !(new PicFilterLineType(PicGraphics.LT.LT_COTATION) | new PicFilterLineType(PicGraphics.LT.LT_AXIS))
                               & PicFilter.FilterNoZeroEntities;

            // get bounding box
            PicVisitorBoundingBox visitorBoundingBox = new PicVisitorBoundingBox();

            factory.ProcessVisitor(visitorBoundingBox, filter);
            Box2D box = visitorBoundingBox.Box;

            // add margins : 5 % of the smallest value among height
            box.AddMarginHorizontal(box.Width * 0.05);
            box.AddMarginVertical(box.Height * 0.05);

            string author     = Application.ProductName + " (" + Application.CompanyName + ")";
            string title      = Application.ProductName + " export";
            string fileFormat = fileExt.StartsWith(".") ? fileExt.Substring(1) : fileExt;

            fileFormat = fileFormat.ToLower();

            // get file content
            if (string.Equals("des", fileFormat))
            {
                PicVisitorDesOutput visitor = new PicVisitorDesOutput {
                    Author = author
                };
                factory.ProcessVisitor(visitor, filter);
                return(visitor.GetResultByteArray());
            }
            else if (string.Equals("dxf", fileFormat))
            {
                PicVisitorOutput visitor = new PicVisitorDxfOutput {
                    Author = author
                };
                factory.ProcessVisitor(visitor, filter);
                return(visitor.GetResultByteArray());
            }
#if PDFSharp
            else if (string.Equals("pdf", fileFormat))
            {
                PicGraphicsPdf graphics = new PicGraphicsPdf(box)
                {
                    Author = author,
                    Title  = title
                };
                factory.Draw(graphics, filter);
                return(graphics.GetResultByteArray());
            }
#endif
            else if (string.Equals("ai", fileFormat) || string.Equals("cf2", fileFormat))
            {
                PicVisitorDiecutOutput visitor = new PicVisitorDiecutOutput(fileFormat)
                {
                    Author = author,
                    Title  = title
                };
                factory.ProcessVisitor(visitor, filter);
                return(visitor.GetResultByteArray());
            }
            else
            {
                throw new Exception("Invalid file format:" + fileFormat);
            }
        }