Exemplo n.º 1
0
        public Form1()
        {
            InitializeComponent();


            FSvgDoc = new SvgDocument
            {
                Width = 500,
                Height = 500
            };

            FSvgDoc.ViewBox = new SvgViewBox(-250, -250, 500, 500);

            var group = new SvgGroup();
            FSvgDoc.Children.Add(group);

            group.Children.Add(new SvgCircle
            {
                Radius = 100,
                Fill = new SvgColourServer(Color.Red),
                Stroke = new SvgColourServer(Color.Black),
                StrokeWidth = 2
            });

            var stream = new MemoryStream();
            FSvgDoc.Write(stream);
            textBox1.Text = Encoding.UTF8.GetString(stream.GetBuffer());

            pictureBox1.Image = FSvgDoc.Draw();
        }
        private void AddChild(SvgElement element)
        {
            var group = _currentImage.Children.GetSvgElementOf<SvgGroup>();
            if (group == null)
            {
                group = new SvgGroup();
                _currentImage.Children.Add(group);
            }

            group.Children.Add(element);
        }
Exemplo n.º 3
0
 public Layer()
 {
     iGroup = new SvgGroup();
     iGroup.ID = "visual";
 }
        //Does not update dynamic blocks
        public static void exportblockstosvg()
        {
            DocumentCollection docCol   = Application.DocumentManager;
            Database           localDb  = docCol.MdiActiveDocument.Database;
            Document           doc      = docCol.MdiActiveDocument;
            CivilDocument      civilDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            try
            {
                using (Transaction tx = localDb.TransactionManager.StartTransaction())
                //using (Database symbolerDB = new Database(false, true))
                {
                    try
                    {
                        System.Data.DataTable fjvKomponenter = IntersectUtilities.CsvReader.ReadCsvToDataTable(
                            @"X:\AutoCAD DRI - 01 Civil 3D\FJV Komponenter.csv", "FjvKomponenter");

                        //symbolerDB.ReadDwgFile(@"X:\0371-1158 - Gentofte Fase 4 - Dokumenter\01 Intern\" +
                        //                       @"02 Tegninger\01 Autocad\Autocad\01 Views\0.0 Fælles\Symboler.dwg",
                        //                       System.IO.FileShare.Read, true, "");

                        BlockTable bt = tx.GetObject(localDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                        foreach (oid Oid in bt)
                        {
                            BlockTableRecord btr = tx.GetObject(Oid, OpenMode.ForRead) as BlockTableRecord;

                            if (ReadStringParameterFromDataTable(btr.Name, fjvKomponenter, "Navn", 0) != null)// &&
                            //btr.Name == "DN32 90gr twin")
                            {
                                Extents3d bbox = new Extents3d();
                                bbox.AddBlockExtents(btr);

                                float width  = ts(Math.Abs(bbox.MaxPoint.X - bbox.MinPoint.X));
                                float height = ts(Math.Abs(bbox.MaxPoint.Y - bbox.MinPoint.Y));

                                //PP = None, NP = Vertical flip, PN = Horizontal flip, NN = Vertical and Horizontal flip
                                //NN is the same as rotating 180°
                                Flip[] flips = new Flip[4] {
                                    Flip.PP, Flip.PN, Flip.NN, Flip.NP
                                };
                                string[] flipNames = new string[] { "_PP", "_PN", "_NN", "_NP" };

                                for (int i = 0; i < flips.Length; i++)
                                {
                                    Svg.SvgDocument svg = new Svg.SvgDocument()
                                    {
                                        Width   = width,
                                        Height  = height,
                                        ViewBox = new Svg.SvgViewBox(ts(bbox.MinPoint.X),
                                                                     ts(-bbox.MaxPoint.Y),
                                                                     width, height)
                                    };

                                    var group = new Svg.SvgGroup();
                                    svg.Children.Add(group);
                                    //WCS ORIGO transform matrix
                                    double[] dMatrix = new double[16] {
                                        1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
                                    };
                                    Matrix3d transform = new Matrix3d(dMatrix);
                                    DrawOrDiscardEntity(btr, tx, transform, group);//, upscale);
                                    AddTransforms(group, flips[i]);

                                    svg.Write(@"X:\AutoCAD DRI - 01 Civil 3D\Svg\"
                                              + btr.Name + flipNames[i] + ".svg");
                                }
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        tx.Abort();
                        ed.WriteMessage(ex.Message);
                        throw;
                    }

                    tx.Commit();
                };
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }
        public static void DrawOrDiscardEntity(BlockReference br, Transaction tx, Svg.SvgGroup group)//, int upscale)
        {
            BlockTableRecord btr = tx.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;

            DrawOrDiscardEntity(btr, tx, br.BlockTransform, group);//, upscale);
        }
        public static void DrawOrDiscardEntity(BlockTableRecord btr, Transaction tx,
                                               Matrix3d transform, Svg.SvgGroup group)//, int upscale)
        {
            foreach (oid oid in btr)
            {
                switch (oid.ObjectClass.Name)
                {
                case "AcDbLine":
                    //prdDbg(oid.ObjectClass.Name);
                    Line line = oid.Go <Line>(tx);
                    using (Line newLine = new Line(line.StartPoint, line.EndPoint))
                    {
                        newLine.TransformBy(transform);
                        SvgLine sline = new Svg.SvgLine
                        {
                            StartX      = ts(newLine.StartPoint.X),
                            StartY      = ts(-newLine.StartPoint.Y),
                            EndX        = ts(newLine.EndPoint.X),
                            EndY        = ts(-newLine.EndPoint.Y),
                            StrokeWidth = ts(0.1),
                            Stroke      = new Svg.SvgColourServer(System.Drawing.Color.Black)
                        };
                        //if (flip != Flip.PP)
                        //{
                        //    sline.Transforms = AddTransforms(flip,
                        //        sline.StartX + sline.EndX,
                        //        sline.StartY + sline.EndY);
                        //}
                        group.Children.Add(sline);
                        //mSpc.AppendEntity(newLine);
                        //tx.AddNewlyCreatedDBObject(newLine, true);
                    }
                    break;

                case "AcDbPolyline":
                    //prdDbg(oid.ObjectClass.Name);
                    Polyline pline = oid.Go <Polyline>(tx);
                    using (Polyline newPline = new Polyline(pline.NumberOfVertices))
                    {
                        for (int i = 0; i < pline.NumberOfVertices; i++)
                        {
                            newPline.AddVertexAt(i, pline.GetPoint2dAt(i), 0, 0, 0);
                        }
                        newPline.TransformBy(transform);
                        Extents3d          bbox = newPline.GeometricExtents; //Prepare for Svg.Transforms
                        SvgPointCollection pcol = new SvgPointCollection();
                        for (int i = 0; i < newPline.NumberOfVertices; i++)
                        {
                            Point2d p2d = newPline.GetPoint2dAt(i);
                            pcol.Add(ts(p2d.X));
                            pcol.Add(ts(-p2d.Y));
                        }
                        SvgPolyline sPline = new SvgPolyline();
                        sPline.Points = pcol;
                        if (pline.NumberOfVertices == 2)
                        {
                            sPline.StrokeWidth = ts(0.1);
                            sPline.Stroke      = new SvgColourServer(System.Drawing.Color.Black);
                        }
                        else
                        {
                            sPline.Fill = new SvgColourServer(System.Drawing.Color.Black);
                        }
                        //if (flip != Flip.PP)
                        //{
                        //    sPline.Transforms = AddTransforms(flip,
                        //    ts(bbox.MinPoint.X + bbox.MaxPoint.X),
                        //    ts(bbox.MinPoint.Y + bbox.MaxPoint.Y));
                        //}
                        group.Children.Add(sPline);
                    }
                    break;

                case "AcDbCircle":
                    //prdDbg(oid.ObjectClass.Name);
                    Circle circle = oid.Go <Circle>(tx);
                    using (Circle newCircle = new Circle())
                    {
                        newCircle.SetDatabaseDefaults();
                        newCircle.Center = circle.Center;
                        newCircle.Radius = circle.Radius;
                        newCircle.TransformBy(transform);
                        SvgCircle sCircle = new Svg.SvgCircle
                        {
                            CenterX = ts(newCircle.Center.X),
                            CenterY = ts(-newCircle.Center.Y),
                            Radius  = ts(newCircle.Radius),
                            Fill    = new Svg.SvgColourServer(System.Drawing.Color.Black),
                        };
                        //if (flip != Flip.PP)
                        //{
                        //    sCircle.Transforms = AddTransforms(flip,
                        //    2 * sCircle.CenterX,
                        //    2 * sCircle.CenterY);
                        //}
                        group.Children.Add(sCircle);
                        //mSpc.AppendEntity(newCircle);
                        //tx.AddNewlyCreatedDBObject(newCircle, true);
                    }
                    break;

                case "AcDbMText":
                    prdDbg(oid.ObjectClass.Name);
                    MText  mText = oid.Go <MText>(tx);
                    string text  = mText.Contents;
                    using (DBText newText = new DBText())
                    {
                        newText.SetDatabaseDefaults();
                        newText.TextString = text;
                        newText.Position   = mText.Location;
                        newText.Rotation   = mText.Rotation;
                        //newText.TransformBy(transform);
                        SvgText sText = new SvgText(newText.TextString);
                        prdDbg(ts(newText.Position.X).ToString());
                        prdDbg(ts(newText.Position.Y).ToString());
                        sText.X.Add(ts(newText.Position.X));
                        sText.Y.Add(ts(newText.Position.Y + 0.1));
                        sText.FontFamily = "Arial";
                        sText.FontSize   = ts(0.50);
                        prdDbg(ts(newText.Rotation * (180 / Math.PI)).ToString());
                        sText.Rotate = ts(newText.Rotation * (180 / Math.PI)).ToString();
                        sText.Fill   = new SvgColourServer(System.Drawing.Color.Black);
                        group.Children.Add(sText);
                    }
                    break;

                case "AcDbBlockReference":
                    DrawOrDiscardEntity(tx.GetObject(oid, OpenMode.ForRead) as BlockReference, tx, group);    //, upscale);
                    break;

                default:
                    //prdDbg("Not implemented: " + oid.ObjectClass.Name);
                    break;
                }
            }
        }