Exemplo n.º 1
4
        private static void WriteDxfFile()
        {
            DxfDocument dxf = new DxfDocument();

            //arc
            Arc arc = new Arc(new Vector3(10, 10, 0), 10, 45, 135);
            arc.Layer = new Layer("arc");
            arc.Layer.Color.Index = 1;
            dxf.AddEntity(arc);

            //xData sample
            XData xdata = new XData(new ApplicationRegistry("netDxf"));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, 0));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, 0));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, 0));
            xdata.XDataRecord.Add(XDataRecord.CloseControlString);

            XData xdata2 = new XData(new ApplicationRegistry("other application"));
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata2.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.String, "string record"));
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.Real, 15.5));
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.Int32, 350));
            xdata2.XDataRecord.Add(XDataRecord.CloseControlString);

            //circle
            Vector3 extrusion = new Vector3(1, 1, 1);
            Vector3 centerWCS = new Vector3(1, 1, 1);
            Vector3 centerOCS = MathHelper.Transform(centerWCS,
                                                        extrusion,
                                                        CoordinateSystem.World,
                                                        CoordinateSystem.Object);

            Circle circle = new Circle(centerOCS, 5);
            circle.Layer = new Layer("circle with spaces");
            circle.Layer.Color=AciColor.Yellow;
            circle.LineType = LineType.Dashed;
            circle.Normal = extrusion;
            circle.XData.Add(xdata);
            circle.XData.Add(xdata2);

            dxf.AddEntity(circle);

            //points
            Point point1 = new Point(new Vector3(-3, -3, 0));
            point1.Layer = new Layer("point");
            point1.Color = new AciColor(30);
            Point point2 = new Point(new Vector3(1, 1, 1));
            point2.Layer = point1.Layer;
            point2.Layer.Color.Index = 9;
            point2.Normal = new Vector3(1, 1, 1);
            dxf.AddEntity(point1);
            dxf.AddEntity(point2);

            //3dface
            Face3d face3D = new Face3d(new Vector3(-5, -5, 5),
                                        new Vector3(5, -5, 5),
                                        new Vector3(5, 5, 5),
                                        new Vector3(-5, 5, 5));
            face3D.Layer = new Layer("3dface");
            face3D.Layer.Color.Index = 3;
            dxf.AddEntity(face3D);
            
            //polyline
            LwPolylineVertex polyVertex;
            List<LwPolylineVertex> polyVertexes = new List<LwPolylineVertex>();
            polyVertex = new LwPolylineVertex(new Vector2(-50, -50));
            polyVertex.StartWidth = 2;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(50, -50));
            polyVertex.StartWidth = 1;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(50, 50));
            polyVertex.Bulge = 1;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(-50, 50));
            polyVertexes.Add(polyVertex);
            LwPolyline polyline2d = new LwPolyline(polyVertexes, true);
            polyline2d.Layer = new Layer("polyline2d");
            polyline2d.Layer.Color.Index = 5;
            polyline2d.Normal = new Vector3(1, 1, 1);
            polyline2d.Elevation = 100.0f;
            dxf.AddEntity(polyline2d);

            //lightweight polyline
            LwPolylineVertex lwVertex;
            List<LwPolylineVertex> lwVertexes = new List<LwPolylineVertex>();
            lwVertex = new LwPolylineVertex(new Vector2(-25, -25));
            lwVertex.StartWidth = 2;
            lwVertexes.Add(lwVertex);
            lwVertex = new LwPolylineVertex(new Vector2(25, -25));
            lwVertex.StartWidth = 1;
            lwVertexes.Add(lwVertex);
            lwVertex = new LwPolylineVertex(new Vector2(25, 25));
            lwVertex.Bulge = 1;
            lwVertexes.Add(lwVertex);
            lwVertex = new LwPolylineVertex(new Vector2(-25, 25));
            lwVertexes.Add(lwVertex);
            LwPolyline lwPolyline = new LwPolyline(lwVertexes, true);
            lwPolyline.Layer = new Layer("lwpolyline");
            lwPolyline.Layer.Color.Index = 5;
            lwPolyline.Normal = new Vector3(1, 1, 1);
            lwPolyline.Elevation = 100.0f;
            dxf.AddEntity(lwPolyline);

            // polyfaceMesh
            List<PolyfaceMeshVertex> meshVertexes = new List<PolyfaceMeshVertex>
                                                    {
                                                        new PolyfaceMeshVertex(0, 0, 0),
                                                        new PolyfaceMeshVertex(10, 0, 0),
                                                        new PolyfaceMeshVertex(10, 10, 0),
                                                        new PolyfaceMeshVertex(5, 15, 0),
                                                        new PolyfaceMeshVertex(0, 10, 0)
                                                    };
            List<PolyfaceMeshFace> faces = new List<PolyfaceMeshFace>
                                                {
                                                    new PolyfaceMeshFace(new short[] {1, 2, -3}),
                                                    new PolyfaceMeshFace(new short[] {-1, 3, -4}),
                                                    new PolyfaceMeshFace(new short[] {-1, 4, 5})
                                                };

            PolyfaceMesh mesh = new PolyfaceMesh(meshVertexes, faces);
            mesh.Layer = new Layer("polyfacemesh");
            mesh.Layer.Color.Index = 104;
            dxf.AddEntity(mesh);

            //line
            Line line = new Line(new Vector3(0, 0, 0), new Vector3(10, 10, 10));
            line.Layer = new Layer("line");
            line.Layer.Color.Index = 6;
            dxf.AddEntity(line);

            //3d polyline
            PolylineVertex vertex;
            List<PolylineVertex> vertexes = new List<PolylineVertex>();
            vertex = new PolylineVertex(new Vector3(-50, -50, 0));
            vertexes.Add(vertex);
            vertex = new PolylineVertex(new Vector3(50, -50, 10));
            vertexes.Add(vertex);
            vertex = new PolylineVertex(new Vector3(50, 50, 25));
            vertexes.Add(vertex);
            vertex = new PolylineVertex(new Vector3(-50, 50, 50));
            vertexes.Add(vertex);
            Polyline polyline = new Polyline(vertexes, true);
            polyline.Layer = new Layer("polyline3d");
            polyline.Layer.Color.Index = 24;
            dxf.AddEntity(polyline);

            //block definition
            Block block = new Block("TestBlock");
            block.Entities.Add(new Line(new Vector3(-5, -5, 5), new Vector3(5, 5, 5)));
            block.Entities.Add(new Line(new Vector3(5, -5, 5), new Vector3(-5, 5, 5)));
           
            //insert
            Insert insert = new Insert(block, new Vector3(5, 5, 5));
            insert.Layer = new Layer("insert");
            insert.Layer.Color.Index = 4;
            dxf.AddEntity(insert);

            //text
            TextStyle style=new TextStyle("True type font","Arial.ttf");
            Text text = new Text("Hello world!", Vector3.Zero, 10.0f,style);
            text.Layer = new Layer("text");
            text.Layer.Color.Index = 8;
            text.Alignment = TextAlignment.TopRight;
            dxf.AddEntity(text);

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            dxf.Save("AutoCad2010.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save("AutoCad2007.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2004;
            dxf.Save("AutoCad2004.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2000;
            dxf.Save("AutoCad2000.dxf");
            dxf = DxfDocument.Load("AutoCad2000.dxf");
            dxf.Save("AutoCad2000 result.dxf");
        }
Exemplo n.º 2
1
        /// <inheritdoc/>
        void Core2D.Interfaces.IProjectExporter.Save(string path, Core2D.Project.XProject project)
        {
            _outputPath = System.IO.Path.GetDirectoryName(path);
            var dxf = new DxfDocument(DxfVersion.AutoCad2010);

            Add(dxf, project);

            dxf.Save(path);
            ClearCache(isZooming: false);
        }
Exemplo n.º 3
1
        public static void ModifyingXData()
        {
            Line line = new Line(Vector2.Zero, Vector2.UnitX);

            ApplicationRegistry appReg = new ApplicationRegistry("netDxf");
            XData xdata = new XData(appReg);
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "Length"));
            line.XData.Add(xdata);

            DxfDocument doc = new DxfDocument();
            doc.AddEntity(line);

            // modifying existing extended data
            line.XData[appReg.Name].XDataRecord.Add(new XDataRecord(XDataCode.Real, Vector3.Distance(line.StartPoint, line.EndPoint)));

            // adding new extended data entry to an existing entity
            ApplicationRegistry appReg2 = new ApplicationRegistry("newXData");
            XData xdata2 = new XData(appReg2);
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.String, "XData entries"));
            line.XData.Add(xdata2);

            Debug.Assert(ReferenceEquals(line.XData[appReg2.Name].ApplicationRegistry, doc.ApplicationRegistries[appReg2.Name]));

            // deleting existing extended data
            line.XData.Remove(appReg.Name);

            // we can also change the name of the application registry name
            doc.ApplicationRegistries["newXData"].Name = "netDxfRenamed";

            doc.Save("xData.dxf");

            doc = DxfDocument.Load("xData.dxf");
        }
Exemplo n.º 4
1
        private static void Polyline()
        {
            DxfDocument dxf = new DxfDocument();
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            Polyline poly = new Polyline();
            poly.Vertexes.Add(new PolylineVertex(0, 0, 0));
            poly.Vertexes.Add(new PolylineVertex(10, 10, 0));
            poly.Vertexes.Add(new PolylineVertex(20, 0, 0));
            poly.Vertexes.Add(new PolylineVertex(30, 10, 0));
            dxf.AddEntity(poly);

            dxf.Save("polyline.dxf");

        }
Exemplo n.º 5
1
        private static void Dxf2000()
        {
            DxfDocument dxf = new DxfDocument();
            //line
            Line line = new Line(new Vector3(0, 0, 0), new Vector3(5, 5, 5));
            line.Layer = new Layer("line");
            line.Layer.Color.Index = 6;

            dxf.AddEntity(line);

            dxf.Save("test2000.dxf");
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="container"></param>
        public void Save(string path, Core2D.Container container)
        {
            _outputPath = System.IO.Path.GetDirectoryName(path);
            var doc = new DxfDocument(DxfVersion.AutoCad2010);

            Add(doc, container);
            doc.Save(path);
            ClearCache(isZooming: false);
        }
Exemplo n.º 7
0
        protected override Task _save()
        {
            RecordSet layer = _layer as RecordSet;

            layer.Position           = transform.position.ToPoint();
            layer.Transform.Position = Vector3.zero;
            layer.Transform.Rotate   = transform.rotation;
            layer.Transform.Scale    = transform.localScale;
            EditableMesh[] meshes = GetComponentsInChildren <EditableMesh>();
            string         ex     = Path.GetExtension(layer.Source).ToLower();

            features = new List <DMesh3>();
            foreach (EditableMesh mesh in meshes)
            {
                features.Add(mesh.GetMesh());
            }
            if (ex == ".obj")
            {
                List <WriteMesh> wmeshes = new List <WriteMesh>();
                foreach (DMesh3 dmesh in features)
                {
                    DMesh3 mesh = new DMesh3(dmesh);
                    foreach (int idx in mesh.VertexIndices())
                    {
                        Vector3d vtx = mesh.GetVertex(idx);
                        mesh.SetVertex(idx, new Vector3d(vtx.x, vtx.z, vtx.y));
                    }
                    wmeshes.Add(new WriteMesh(mesh, ""));
                }
                saveObj(layer.Source, wmeshes);
            }
            if (ex == ".dxf")
            {
                DXF.DxfDocument          doc       = new DXF.DxfDocument();
                CoordinateTransformation transform = null;
                if (GetCrs() != null)
                {
                    transform = new CoordinateTransformation(AppState.instance.mapProj, GetCrs());
                }
                foreach (DMesh3 dmesh in features)
                {
                    foreach (Index3i tri in dmesh.Triangles())
                    {
                        DXF.Vector3 v1 = dmesh.GetVertex(tri.a).ToDxfVector3(transform);
                        DXF.Vector3 v2 = dmesh.GetVertex(tri.b).ToDxfVector3(transform);
                        DXF.Vector3 v3 = dmesh.GetVertex(tri.c).ToDxfVector3(transform);
                        doc.AddEntity(new Face3d(v1, v2, v3));
                    }
                }
                using (Stream stream = File.Open(layer.Source, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) {
                    doc.Save(stream);
                    stream.Close();
                }
            }
            return(Task.CompletedTask);
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        void IProjectExporter.Save(string path, IProjectContainer project)
        {
            _outputPath = System.IO.Path.GetDirectoryName(path);
            var dxf = new DXF.DxfDocument(DXFH.DxfVersion.AutoCad2010);

            Add(dxf, project);

            dxf.Save(path);
            ClearCache(isZooming: false);
        }
Exemplo n.º 9
0
        /// <inheritdoc/>
        public void Save(Stream stream, IProjectContainer project)
        {
            if (stream is FileStream fileStream)
            {
                _outputPath = System.IO.Path.GetDirectoryName(fileStream.Name);
            }
            else
            {
                _outputPath = string.Empty;
            }

            var dxf = new DXF.DxfDocument(DXFH.DxfVersion.AutoCad2010);

            Add(dxf, project);

            dxf.Save(stream);
            ClearCache(isZooming: false);
        }
Exemplo n.º 10
0
        static void DxfKitTimeline()
        {
            var dxf = new netDxf.DxfDocument();

            var ents = dxf.DrawTimeline(new List <(DateTime from, DateTime to)>()
            {
                (new DateTime(2000, 1, 1), new DateTime(2001, 7, 31)),
                (new DateTime(2004, 2, 1), new DateTime(2007, 4, 28)),
                (new DateTime(2007, 4, 1), new DateTime(2008, 12, 31)),
                (new DateTime(2008, 8, 1), new DateTime(2012, 10, 31)),
                (new DateTime(2012, 11, 1), new DateTime(2015, 10, 31)),
                (new DateTime(2016, 4, 1), new DateTime(2017, 12, 31))
            });

            dxf.AddEntities(ents);

            dxf.Viewport.ShowGrid = false;
            dxf.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "timeline.dxf"), isBinary: true);
        }
Exemplo n.º 11
0
        public static void DimensionUserText()
        {
            DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010);

            Vector3 p2 = new Vector3(0, 0, 0);
            Vector3 p1 = new Vector3(5, 0, 0);

            Line line1 = new Line(p1, p2)
            {
                Layer = new Layer("Reference line")
                {
                    Color = AciColor.Green
                }
            };
            dxf.AddEntity(line1);

            DimensionStyle style = new DimensionStyle("MyStyle");

            double offset = 0.75;
            LinearDimension dim = new LinearDimension(line1, offset, 0, style);
            dim.UserText = null;    // 5.00 (this is the default behavior)
            dxf.AddEntity(dim);

            dim = new LinearDimension(line1, 2 * offset, 0, style);
            dim.UserText = string.Empty;    // 5.00 (same behavior as null)
            dxf.AddEntity(dim);

            dim = new LinearDimension(line1, 3 * offset, 0, style);
            dim.UserText = " ";    // No dimension text will be drawn (one blank space)
            dxf.AddEntity(dim);

            dim = new LinearDimension(line1, 4 * offset, 0, style);
            dim.UserText = "<>";    // 5.00 (the characters <> will be substituted with the style.DIMPOST property)
            dxf.AddEntity(dim);

            dim = new LinearDimension(line1, 5 * offset, 0, style);
            dim.UserText = "Length: <> mm"; // Length: 5.00 mm (the characters <> will be substituted with the style.DIMPOST property)
            dxf.AddEntity(dim);

            dim = new LinearDimension(line1, 6 * offset, 0, style);
            dim.UserText = "User text"; // User text
            dxf.AddEntity(dim);

            dxf.Save("DimensionUserText.dxf");

        }
Exemplo n.º 12
0
        private static void Face3d()
        {

            DxfDocument dxf = new DxfDocument();

            Face3d face3d = new Face3d();
            face3d.FirstVertex = new Vector3(0, 0, 0);
            face3d.SecondVertex = new Vector3(1, 0, 0);
            face3d.ThirdVertex = new Vector3(1, 1, 0);
            face3d.FourthVertex = new Vector3(0, 1, 0);
            dxf.AddEntity(face3d);

            dxf.Save("face.dxf");
            dxf = DxfDocument.Load("face.dxf");
            dxf.Save("face return.dxf");

        }
Exemplo n.º 13
0
        public static void DimensionsLinearAndAngularUnits()
        {
            DimensionStyle style = new DimensionStyle("MyStyle")
            {
                // DIMDEC defines the number of decimal places.
                // For Architectural and Fractional units the minimum fraction is defined by 1/2^DIMDEC.
                DIMDEC = 4,
                DIMFRAC = FractionFormatType.Horizontal,
                DIMLUNIT = LinearUnitType.Engineering,
                SuppressLinearTrailingZeros = true,
                SuppressZeroFeet = false,
                SuppressZeroInches = false,
                DIMLFAC = 10.0,
                // the round off to nearest DIMRND is applied to the linear dimension measurement after applying the scale DIMLFAC
                DIMRND = 0.025,
                DIMADEC = 2,
                DIMAUNIT = AngleUnitType.DegreesMinutesSeconds
            };

            Layer layer = new Layer("Layer1") { Color = AciColor.Blue };

            Vector3 p1 = new Vector3(0, 0, 0);
            Vector3 p2 = new Vector3(21.2548, 0, 0);

            LinearDimension dim = new LinearDimension(p1, p2, 4, 0, style);

            Vector2 s1 = new Vector2(-2, 2);
            Vector2 s2 = new Vector2(2, -2);

            Vector2 e1 = new Vector2(-1, -3);
            Vector2 e2 = new Vector2(1, 3);

            Line line1 = new Line(s1, s2) { Layer = layer };
            Line line2 = new Line(e1, e2) { Layer = layer };
            Angular2LineDimension dim1 = new Angular2LineDimension(line2, line1, 4, style);

            DxfDocument doc = new DxfDocument();
            doc.AddEntity(dim);
            doc.AddEntity(dim1);
            doc.Save("DimensionsLinearAndAngularUnits.dxf");

            DxfDocument dxf = DxfDocument.Load("DimensionsLinearAndAngularUnits.dxf");
        }
Exemplo n.º 14
0
        private static void HatchTest3()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.Vertexes[2].Bulge = 1;
            poly.IsClosed = true;

            Ellipse ellipse = new Ellipse(Vector3.Zero, 16, 10);
            ellipse.Rotation = 0;
            ellipse.StartAngle = 0;
            ellipse.EndAngle = 180;

            LwPolyline poly2 = new LwPolyline();
            poly2.Vertexes.Add(new LwPolylineVertex(-8, 0));
            poly2.Vertexes.Add(new LwPolylineVertex(0, -4));
            poly2.Vertexes.Add(new LwPolylineVertex(8, 0));

            //Arc arc = new Arc(Vector3.Zero,8,180,0);
            //Line line =new Line(new Vector3(8,0,0), new Vector3(-8,0,0));

            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly2, ellipse})
                                                                            };

            Hatch hatch = new Hatch(HatchPattern.Line, boundary, true);
            hatch.Pattern.Angle = 45;
            //dxf.AddEntity(poly);
            //dxf.AddEntity(ellipse);
            ////dxf.AddEntity(arc);
            ////dxf.AddEntity(line);
            //dxf.AddEntity(poly2);
            dxf.AddEntity(hatch);


            dxf.Save("hatchTest3.dxf");
        }
Exemplo n.º 15
0
        private static void WriteNestedInsert()
        {
            // nested blocks
            DxfDocument dxf = new DxfDocument();
            
            Block nestedBlock = new Block("Nested block");
            Circle circle = new Circle(Vector3.Zero, 5);
            circle.Layer = new Layer("circle");
            circle.Layer.Color.Index = 2;
            nestedBlock.Entities.Add(circle);
            
            AttributeDefinition attdef = new AttributeDefinition("NewAttribute");
            attdef.Prompt = "InfoText";
            attdef.Alignment = TextAlignment.MiddleCenter;
            nestedBlock.AttributeDefinitions.Add(attdef);

            Insert nestedInsert = new Insert(nestedBlock, new Vector3(0, 0, 0)); // the position will be relative to the position of the insert that nest it
            nestedInsert.Attributes[0].Value = 24;

            Insert nestedInsert2 = new Insert(nestedBlock, new Vector3(-20, 0, 0)); // the position will be relative to the position of the insert that nest it
            nestedInsert2.Attributes[0].Value = -20;

            Block block = new Block("MyBlock");
            block.Entities.Add(new Line(new Vector3(-5, -5, 0), new Vector3(5, 5, 0)));
            block.Entities.Add(new Line(new Vector3(5, -5, 0), new Vector3(-5, 5, 0)));
            block.Entities.Add(nestedInsert);
            block.Entities.Add(nestedInsert2);

            Insert insert = new Insert(block, new Vector3(5, 5, 5));
            insert.Layer = new Layer("insert");

            dxf.AddEntity(insert);
            //dxf.AddEntity(circle); // this is not allowed the circle is already part of a block

            dxf.Save("nested insert.dxf");
            dxf = DxfDocument.Load("nested insert.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            dxf.Save("nested insert copy.dxf");

        }
Exemplo n.º 16
0
        private static void LwPolyline()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(0, 0));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(20, 0));
            poly.Vertexes.Add(new LwPolylineVertex(30, 10));
            poly.SetConstantWidth(2);
            //poly.IsClosed = true;
            dxf.AddEntity(poly);

            dxf.Save("lwpolyline.dxf");

            dxf = DxfDocument.Load("lwpolyline.dxf");
        }
Exemplo n.º 17
0
        private static void HatchTest4()
        {
            DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010);

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.IsClosed = true;

            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>() { new HatchBoundaryPath(new List<EntityObject>()) }; ;
            //List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath> {new HatchBoundaryPath(new List<Entity> {poly})};
            HatchGradientPattern pattern = new HatchGradientPattern(AciColor.Yellow, AciColor.Blue, HatchGradientPatternType.Linear);
            pattern.Origin = new Vector2(120, -365);
            Hatch hatch = new Hatch(pattern, boundary, true);
            dxf.AddEntity(hatch);
            dxf.AddEntity(hatch.CreateBoundary(true));
            
            dxf.Save("HatchTest4.dxf");
            dxf = DxfDocument.Load("HatchTest4.dxf");
            dxf.Save("HatchTest4 copy.dxf");

        }
Exemplo n.º 18
0
        /// <summary>
        /// Processes a tlog to get the offsets - creates dxf of data
        /// </summary>
        /// <param name="fn">Filename</param>
        /// <returns>Offsets</returns>
        public static double[] getOffsets(string fn, int throttleThreshold = 0)
        {
            // based off tridge's work
            string logfile = fn;

            // old method
            float minx = 0;
            float maxx = 0;
            float miny = 0;
            float maxy = 0;
            float minz = 0;
            float maxz = 0;

            // this is for a dxf
            Polyline3dVertex vertex;
            List<Polyline3dVertex> vertexes = new List<Polyline3dVertex>();

            // data storage
            Tuple<float, float, float> offset = new Tuple<float, float, float>(0, 0, 0);
            List<Tuple<float, float, float>> data = new List<Tuple<float, float, float>>();

            Hashtable filter = new Hashtable();

            // track data to use
            bool useData = false;

            log.Info("Start log: " + DateTime.Now);

                MAVLink mine = new MAVLink();
                try
                {
                    mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.Read));
                }
                catch (Exception ex) { log.Debug(ex.ToString()); CustomMessageBox.Show("Log Can not be opened. Are you still connected?"); return new double[] {0}; }

                mine.logreadmode = true;

                mine.MAV.packets.Initialize(); // clear

                // gather data
                while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length)
                {
                    byte[] packetraw = mine.readPacket();

                    var packet = mine.DebugPacket(packetraw, false);

                    // this is for packets we dont know about
                    if (packet == null)
                        continue;

                    if (packet.GetType() == typeof(MAVLink.mavlink_vfr_hud_t))
                    {
                        if (((MAVLink.mavlink_vfr_hud_t)packet).throttle >= throttleThreshold)
                        {
                            useData = true;
                        }
                        else
                        {
                            useData = false;
                        }

                    }

                    if (packet.GetType() == typeof(MAVLink.mavlink_sensor_offsets_t))
                    {
                        offset = new Tuple<float, float, float>(
                            ((MAVLink.mavlink_sensor_offsets_t)packet).mag_ofs_x,
                            ((MAVLink.mavlink_sensor_offsets_t)packet).mag_ofs_y,
                            ((MAVLink.mavlink_sensor_offsets_t)packet).mag_ofs_z);
                    }
                    else if (packet.GetType() == typeof(MAVLink.mavlink_raw_imu_t) && useData)
                    {
                        int div = 20;

                        // fox dxf
                        vertex = new Polyline3dVertex(new Vector3f(
                            ((MAVLink.mavlink_raw_imu_t)packet).xmag - offset.Item1,
                            ((MAVLink.mavlink_raw_imu_t)packet).ymag - offset.Item2,
                            ((MAVLink.mavlink_raw_imu_t)packet).zmag - offset.Item3)
                            );
                        vertexes.Add(vertex);

                        // for old method
                        setMinorMax(((MAVLink.mavlink_raw_imu_t)packet).xmag - offset.Item1, ref minx, ref maxx);
                        setMinorMax(((MAVLink.mavlink_raw_imu_t)packet).ymag - offset.Item2, ref miny, ref maxy);
                        setMinorMax(((MAVLink.mavlink_raw_imu_t)packet).zmag - offset.Item3, ref minz, ref maxz);

                        // for new lease sq
                        string item = (int)(((MAVLink.mavlink_raw_imu_t)packet).xmag / div) + "," +
                            (int)(((MAVLink.mavlink_raw_imu_t)packet).ymag / div) + "," +
                            (int)(((MAVLink.mavlink_raw_imu_t)packet).zmag / div);

                        if (filter.ContainsKey(item))
                        {
                            filter[item] = (int)filter[item] + 1;

                            if ((int)filter[item] > 3)
                                continue;
                        }
                        else
                        {
                            filter[item] = 1;
                        }

                        data.Add(new Tuple<float, float, float>(
                            ((MAVLink.mavlink_raw_imu_t)packet).xmag - offset.Item1,
                            ((MAVLink.mavlink_raw_imu_t)packet).ymag - offset.Item2,
                            ((MAVLink.mavlink_raw_imu_t)packet).zmag - offset.Item3));

                    }

                }

                log.Info("Log Processed " + DateTime.Now);

                Console.WriteLine("Extracted " + data.Count + " data points");
                Console.WriteLine("Current offset: " + offset);

                mine.logreadmode = false;
                mine.logplaybackfile.Close();
                mine.logplaybackfile = null;

                if (data.Count < 10)
                {
                    CustomMessageBox.Show("Log does not contain enough data");
                    throw new Exception("Not Enough Data");
                }

                data.Sort(
                    delegate(Tuple<float, float, float> d1, Tuple<float, float, float> d2)
                    {
                        // get distance from 0,0,0
                        double ans1 = Math.Sqrt(d1.Item1 * d1.Item1 + d1.Item2 * d1.Item2+ d1.Item3 * d1.Item3);
                        double ans2 = Math.Sqrt(d2.Item1 * d2.Item1 + d2.Item2 * d2.Item2+ d2.Item3 * d2.Item3);
                        if (ans1 > ans2)
                            return 1;
                        if (ans1 < ans2)
                            return -1;
                        return 0;
                    }
                    );

                data.RemoveRange(data.Count - (data.Count / 16), data.Count / 16);

                double[] x = LeastSq(data);

                System.Console.WriteLine("Old Method {0} {1} {2}", -(maxx + minx) / 2, -(maxy + miny) / 2, -(maxz + minz) / 2);

                log.Info("Least Sq Done " + DateTime.Now);

                // create a dxf for those who want to "see" the calibration
                DxfDocument dxf = new DxfDocument();

                Polyline3d polyline = new Polyline3d(vertexes, true);
                polyline.Layer = new Layer("polyline3d");
                polyline.Layer.Color.Index = 24;
                dxf.AddEntity(polyline);

                Point pnt = new Point(new Vector3f(-offset.Item1, -offset.Item2, -offset.Item3));
                pnt.Layer = new Layer("old offset");
                pnt.Layer.Color.Index = 22;
                dxf.AddEntity(pnt);

                pnt = new Point(new Vector3f(-(float)x[0], -(float)x[1], -(float)x[2]));
                pnt.Layer = new Layer("new offset");
                pnt.Layer.Color.Index = 21;
                dxf.AddEntity(pnt);

                dxf.Save("magoffset.dxf", DxfVersion.AutoCad2000);

                log.Info("dxf Done " + DateTime.Now);

                Array.Resize<double>(ref x, 3);

                return x;
        }
Exemplo n.º 19
0
        private static void HatchTest2()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.Vertexes[2].Bulge = 1;
            poly.IsClosed = true;

            Circle circle = new Circle(Vector3.Zero, 5);

            Ellipse ellipse = new Ellipse(Vector3.Zero,16,10);
            ellipse.Rotation = 30;
            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{circle}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{ellipse})
                                                                            };

            Hatch hatch = new Hatch(HatchPattern.Line, boundary, false);
            hatch.Pattern.Angle = 150;
            hatch.Pattern.Scale = 5;
            //hatch.Normal = new Vector3(1,1,1);
            //hatch.Elevation = 23;
            //dxf.AddEntity(poly);
            //dxf.AddEntity(circle);
            //dxf.AddEntity(ellipse);
            dxf.AddEntity(hatch);
            hatch.CreateBoundary(true);
            dxf.Save("hatchTest2.dxf");
            dxf = DxfDocument.Load("hatchTest2.dxf");
            dxf.Save("hatchTest2 copy.dxf");
        }
Exemplo n.º 20
0
        private static void WritePolyline3d()
        {
            DxfDocument dxf = new DxfDocument();

            List<PolylineVertex> vertexes = new List<PolylineVertex>{
                                                                        new PolylineVertex(0, 0, 0), 
                                                                        new PolylineVertex(10, 0, 10), 
                                                                        new PolylineVertex(10, 10, 20), 
                                                                        new PolylineVertex(0, 10, 30)
                                                                        };

            Polyline poly = new Polyline(vertexes, true);

            XData xdata = new XData(new ApplicationRegistry("netDxf"));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "netDxf polyline3d"));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.Int16, poly.Vertexes.Count));
            xdata.XDataRecord.Add(XDataRecord.CloseControlString);

            poly.XData.Add(xdata);

            dxf.AddEntity(poly);

            dxf.Save("polyline.dxf");           
        }
Exemplo n.º 21
0
        private static void SolidEntity()
        {
            // The solid vertexes are expressed in OCS (object coordinate system)
            // Now they are stored as Vector2 to force all vertexes to lay on a plane, this is similar as how the LwPolyline works.
            // The Z coordinate is controlled by the elevation property of the Solid.
            Vector2 a = new Vector2(-1, -1);
            Vector2 b = new Vector2(1, -1);
            Vector2 c = new Vector2(-1, 1);
            Vector2 d = new Vector2(1, 1);

            Solid solid = new Solid(a, b, c, d);
            solid.Normal = new Vector3(1, 1, 0);

            solid.Elevation = 2;

            DxfDocument doc = new DxfDocument();
            doc.AddEntity(solid);
            doc.Save("SolidEntity.dxf");
        }
Exemplo n.º 22
0
        private static void TraceEntity()
        {
            // The Trace entity behaves exactly as the Solid, they have the same properties and have the same graphical representation.
            // They are a different entity since in AutoCad they show as two distinct types of objects.
            // In any case, it is recommended to always use the Solid in its place.
            Vector2 a = new Vector2(-1, -1);
            Vector2 b = new Vector2(1, -1);
            Vector2 c = new Vector2(-1, 1);
            Vector2 d = new Vector2(1, 1);

            Trace trace = new Trace(a, b, c, d);
            trace.Normal = new Vector3(1, 1, 0);
            trace.Elevation = 2;

            DxfDocument doc = new DxfDocument();
            doc.AddEntity(trace);
            doc.Save("TraceEntity.dxf");
        }
Exemplo n.º 23
0
        private static void Ellipse()
        {
           
            DxfDocument dxf = new DxfDocument();

            //Line line = new Line(new Vector3(0, 0, 0), new Vector3(2 * Math.Cos(Math.PI / 4),2 * Math.Cos(Math.PI / 4), 0));

            //dxf.AddEntity(line);

            //Line line2 = new Line(new Vector3(0, 0, 0), new Vector3(0, -2, 0));
            //dxf.AddEntity(line2);

            //Arc arc=new Arc(Vector3.Zero,2,45,270);
            //dxf.AddEntity(arc);

            Ellipse ellipse = new Ellipse(new Vector3(2,2,0), 5,3);
            ellipse.Rotation = 30;
            ellipse.Normal=new Vector3(1,1,1);
            ellipse.Thickness = 2;
            dxf.AddEntity(ellipse);

            Ellipse ellipseArc = new Ellipse(new Vector3(2, 10, 0), 5, 3);
            ellipseArc.StartAngle = -45;
            ellipseArc.EndAngle = 45;
            dxf.AddEntity(ellipseArc);

            dxf.Save("ellipse.dxf");
            dxf = new DxfDocument();
            dxf = DxfDocument.Load("ellipse.dxf");

            DxfDocument load = DxfDocument.Load("test ellipse.dxf");
            load.Save("saved test ellipse.dxf");

        }
Exemplo n.º 24
0
        private static void WritePolyfaceMesh()
        {
            DxfDocument dxf = new DxfDocument();


            List<PolyfaceMeshVertex> vertexes = new List<PolyfaceMeshVertex>
                                                    {
                                                        new PolyfaceMeshVertex(0, 0, 0),
                                                        new PolyfaceMeshVertex(10, 0, 0),
                                                        new PolyfaceMeshVertex(10, 10, 0),
                                                        new PolyfaceMeshVertex(5, 15, 0),
                                                        new PolyfaceMeshVertex(0, 10, 0)
                                                    };
            List<PolyfaceMeshFace> faces = new List<PolyfaceMeshFace>
                                                {
                                                    new PolyfaceMeshFace(new short[] {1, 2, -3}),
                                                    new PolyfaceMeshFace(new short[] {-1, 3, -4}),
                                                    new PolyfaceMeshFace(new short[] {-1, 4, 5})
                                                };

            PolyfaceMesh mesh = new PolyfaceMesh(vertexes, faces);
            dxf.AddEntity(mesh);

            dxf.Save("mesh.dxf");
        }
Exemplo n.º 25
0
        private static void Solid()
        {

            DxfDocument dxf = new DxfDocument();

            Solid solid = new Solid();
            solid.FirstVertex=new Vector2(0,0);
            solid.SecondVertex  = new Vector2(1, 0);
            solid.ThirdVertex  = new Vector2(0, 1);
            solid.FourthVertex  = new Vector2(1, 1);
            dxf.AddEntity(solid);

            dxf.Save("solid.dxf");
            //dxf = DxfDocument.Load("solid.dxf");
            //dxf.Save("solid.dxf");

        }
Exemplo n.º 26
0
        public static void ModifyingBlockProperties()
        {
            DxfDocument doc = new DxfDocument();
            doc.DrawingVariables.InsUnits = DrawingUnits.Centimeters;
            Line existingLine = new Line(new Vector2(-10, 10), new Vector2(10, -10));
            doc.AddEntity(existingLine);

            AttributeDefinition attDef4 = new AttributeDefinition("MyAttribute4");
            attDef4.Value = "MyValue4";
            attDef4.Alignment = TextAlignment.TopCenter;
            Block block = new Block("MyBlock", null, new List<AttributeDefinition>{attDef4});
            block.Record.Units = DrawingUnits.Millimeters;

            // this is incorrect we cannot add an entity that belongs to a document when the block does not belong to anyone.
            //block.Entities.Add(existingLine);
            doc.Blocks.Add(block);
            // when the block and the entity that is being added belong to the same document, the entity will be removed from its current layout and added to the block
            // you cannot add an entity that belongs to a different document or block. Clone it instead.
            block.Entities.Add(existingLine);

            // now we can modify the block properties even if it has been already added to the document
            Line line = new Line(new Vector2(-10, -10), new Vector2(10, 10));

            // when new entities that do not belong to anyone are added to an existing block, they will also be added to the document
            block.Entities.Add(line);

            DxfDocument doc2 = new DxfDocument();
            Circle circle = new Circle(Vector2.Zero, 5);
            doc2.AddEntity(circle);

            // this is incorrect the circle already belongs to another document
            //block.Entities.Add(circle);
            // we need to clone it first
            Circle circle2 = (Circle) circle.Clone();
            circle2.Radius = 2.5;
            block.Entities.Add(circle2);

            //you could also remove circle2 from doc2 and add it to the block
            doc2.RemoveEntity(circle);
            block.Entities.Add(circle);

            AttributeDefinition attDef = new AttributeDefinition("MyAttribute1");
            attDef.Value = "MyValue1";
            block.AttributeDefinitions.Add(attDef);

            // the same that is applicable to entities is also true to attribute definitions
            AttributeDefinition attDef2 = new AttributeDefinition("MyAttribute2");
            attDef2.Value = "MyValue2";
            attDef2.Alignment = TextAlignment.BaselineRight;
            block.AttributeDefinitions.Add(attDef2);

            Insert ins = new Insert(block);
            doc.AddEntity(ins);

            // if the insert has been added to a document, any new attribute definitions added to the block will not be reflected in the insert
            // this mimics the behavior in AutoCad
            AttributeDefinition attDef3 = new AttributeDefinition("MyAttribute3");
            attDef3.Value = "MyValue3";
            attDef3.Alignment = TextAlignment.TopCenter;
            block.AttributeDefinitions.Add(attDef3);
            ins.Rotation = 30;

            // to update the insert attributes call the method Sync, this method will also call the method TransformAttributes
            ins.Sync();

            // the ins2 will have all three attributes
            Insert ins2 = new Insert(block, new Vector2(20,0));
            doc.AddEntity(ins2);

            doc.Save("Test.dxf");

            block.Name = "MyBlockRenamed";

            doc.Save("BlockRename.dxf");

            doc = Test("BlockRename.dxf");
        }
Exemplo n.º 27
0
        static void Main(string[] args)
        {
            var dxf = new netDxf.DxfDocument();

            var rValues1 = new List <double>()
            {
                1, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9,
                4.7, 5.6, 6.8, 8.2, 10, 12, 15, 18, 22,
                27, 33, 39, 47, 56, 68, 82, 100,
                120, 150, 180, 220, 270, 330, 390, 470,
                560, 680, 820,

                1e3, 1.2e3, 1.5e3, 1.8e3, 2.2e3, 2.7e3,
                3.3e3, 3.9e3, 4.7e3, 5.5e3, 6.8e3, 7.2e3, 8.2e3, 10e3, 12e3,
                15e3, 18e3, 22e3, 27e3, 33e3, 39e3, 47e3, 56e3, 82e3, 100e3,
                120e3, 150e3, 180e3, 220e3, 270e3, 330e3, 380e3,
                470e3, 560e3, 680e3, 820e3,

                1e6, 1.2e6, 1.5e6, 1.8e6, 2.2e6, 2.7e6, 3.3e6, 3.9e6, 4.7e6,
                5.6e6, 6.8e6, 8.2e6, 10e6
            };

            var rValues2 = new List <double>()
            {
                7.5, 75, 200, 510,

                2e3, 3e3, 5.1e3, 7.5e3, 20e3, 51e3, 68e3, 75e3, 200e3, 510e3, 750e3,

                2e6
            };

            var rValues3 = new List <double>()
            {
                1.2, 1.5, 1.8, 2.7, 3.3, 3.9, 6.8, 12, 18, 560, 820,
                1.2e3, 1.8e3, 2.7e3, 12e3, 18e3, 27e3, 270e3, 380e3,
                5.6e3, 300e3,
                1.2e6, 1.8e6, 2.2e6, 2.7e6, 3.9e6, 6.8e6, 8.2e6
            };

            var DATA = rValues1.Union(rValues2).Union(rValues3).OrderBy(w => w).ToList();

            var PAGE_W = 210d;
            var PAGE_H = 297d;

            var LABEL_W = 25d;
            var LABEL_H = 20d;

            var MARGINS_LTRB = new[] { 10d, 10d, 10d, 10d };

            var COLS = Math.Truncate((PAGE_W - MARGINS_LTRB[0] - MARGINS_LTRB[2]) / LABEL_W);
            var ROWS = Math.Truncate((PAGE_H - MARGINS_LTRB[1] - MARGINS_LTRB[3]) / LABEL_H);

            var XORIGIN = 0d;
            var YORIGIN = 0d;

            var TXT_HEIGHT = 5d;
            var OHM_HEIGHT = 3d;
            var TXT_FONT   = new netDxf.Tables.TextStyle("Ubuntu Condensed", netDxf.Tables.FontStyle.Regular);

            var x = XORIGIN;
            var y = YORIGIN;

            int page = 0;

            for (int i = 0; i < DATA.Count; ++i)
            {
                var r = DATA[i];

                var prefix = "";
                if (r >= 1e6)
                {
                    prefix = " M";
                    r     /= 1e6;
                }
                else if (r >= 1e3)
                {
                    prefix = " k";
                    r     /= 1e3;
                }

                var txt = Invariant($"\\H{TXT_HEIGHT};{r}{prefix} \\H{OHM_HEIGHT};Ω");

                {
                    var ent = new LwPolyline(new[]
                    {
                        new Vector2(x, y),
                        new Vector2(x + LABEL_W, y),
                        new Vector2(x + LABEL_W, y + LABEL_H),
                        new Vector2(x, y + LABEL_H),
                    }, isClosed: true);
                    dxf.AddEntity(ent);
                }

                {
                    var ent = new MText(txt)
                    {
                        Position        = new Vector3(x + LABEL_W / 2, y + LABEL_H / 2, 0),
                        Height          = TXT_HEIGHT,
                        RectangleWidth  = LABEL_W,
                        AttachmentPoint = MTextAttachmentPoint.MiddleCenter,
                        Style           = TXT_FONT
                    };
                    dxf.AddEntity(ent);
                }

                x += LABEL_W;
                if ((i + 1) % COLS == 0)
                {
                    y += LABEL_H;

                    if (y >= ROWS * LABEL_H)
                    {
                        y = YORIGIN;
                        ++page;
                    }
                    x = XORIGIN + page * (PAGE_W + LABEL_W);
                }
            }

            dxf.Save("output.dxf", isBinary: false);

            Process.Start(new ProcessStartInfo("output.dxf")
            {
                UseShellExecute = true
            });
        }
Exemplo n.º 28
0
        public static void ModifyingMLineStyles()
        {
            DxfDocument doc = new DxfDocument(DxfVersion.AutoCad2010);
            doc.DrawingVariables.LtScale = 10;

            List<Vector2> vertexes = new List<Vector2>
                                            {
                                                new Vector2(0, 0),
                                                new Vector2(0, 150),
                                                new Vector2(150, 150),
                                                new Vector2(150, 0)
                                            };

            MLine mline = new MLine(vertexes);
            mline.Scale = 20;
            mline.Justification = MLineJustification.Zero;

            MLineStyle style = new MLineStyle("MyStyle", "Personalized style.");
            style.Elements.Add(new MLineStyleElement(0.25));
            style.Elements.Add(new MLineStyleElement(-0.25));
         
            // if we add new elements directly to the list we need to sort the list,
            style.Elements.Sort();           
            style.Flags = MLineStyleFlags.EndInnerArcsCap | MLineStyleFlags.EndRoundCap | MLineStyleFlags.StartInnerArcsCap | MLineStyleFlags.StartRoundCap;

            // AutoCad2000 dxf version does not support true colors for MLineStyle elements
            style.Elements[0].Color = new AciColor(180, 230, 147);
           
            doc.AddEntity(mline);

            // change the multi line style after it has been added to the document
            mline.Style = style;
            Debug.Assert(ReferenceEquals(mline.Style, doc.MlineStyles[mline.Style.Name]), "Reference not equals.");

            // VERY IMPORTANT: We have modified the MLine after setting its vertexes so we need to manually call this method.
            // It is also necessary when manually editing the vertex distances.
            mline.Update();
            
            // the line type will be automatically added to the document
            foreach (MLineStyleElement e in style.Elements)
            {
                // making changes after the MLineStyle has been added to the document
                e.LineType = LineType.Dashed;
                Debug.Assert(ReferenceEquals(e.LineType, doc.LineTypes[e.LineType.Name]), "Reference not equals.");
            }

            MLine copy = (MLine) mline.Clone();
            copy.Scale = 100;
            doc.AddEntity(copy);
            // once the entity has been added to the document, changing its style requires that the new style is also present in the document.
            copy.Style = doc.MlineStyles["standard"];
            // VERY IMPORTANT: We have modified the MLine after setting its vertexes so we need to manually call this method.
            // It is also necessary when manually editing the vertex distances.
            copy.Update();

            doc.Save("ModifyingMLineStyle.dxf");
            Test("ModifyingMLineStyle.dxf");
        }
Exemplo n.º 29
0
		public void WriteProject(WaveguideDesignerProjectData project)
			{
			if( project == null ) return;

			Type type;
			EntityObject obj = null;
			DxfDocument doc = new DxfDocument();

			doc.Name = project.Name;

			Layer dxfLayer;
			LayerData layerData;
			foreach( VirtualLayer vLayer in project.VirtualGraphics.Layers )
				{
				layerData = null;
				foreach( LayerData tmp in project.Layers )
					if( tmp.VirtualLayer == vLayer )
						{
						layerData = tmp;
						break;
						}
				if( layerData == null ) continue;
				dxfLayer = new Layer( layerData.Name );
				dxfLayer.Color.Index = (short)layerData.LayerNumber;
				doc.Layers.Add( dxfLayer );

				foreach( VirtualShapeBase vShape in vLayer.Shapes )
					{
					type = vShape.GetType();
					if( type == typeof( VirtualRectangle ) )
						{
						VirtualRectangle rect = (VirtualRectangle)vShape;
						Polyline dxfrect = new Polyline();
						obj = new Polyline();
						dxfrect.IsClosed = true;
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X, rect.Location.Y, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X + rect.Size.W, rect.Location.Y, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X + rect.Size.W, rect.Location.Y + rect.Size.H, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X, rect.Location.Y + rect.Size.H, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X, rect.Location.Y, 0 ) );
						obj = dxfrect;
						}
					else if( type == typeof( VirtualPolygon ) )
						{
						VirtualPolygon poly = (VirtualPolygon)vShape;
						Polyline dxfpoly = new Polyline();
						dxfpoly.IsClosed = true;
						foreach( PointD p in poly.Vertices )
							dxfpoly.Vertexes.Add( conv( p ) );
						dxfpoly.Vertexes.Add( conv( poly.Vertices[0] ) );
						obj = dxfpoly;
						}
					else if( type == typeof( VirtualEllipse ) )
						{
						VirtualEllipse elli = (VirtualEllipse)vShape;
						Ellipse dxfelli = new Ellipse();
						dxfelli.Center = new netDxf.Vector3( elli.Center.X, elli.Center.Y, 0 );
						dxfelli.StartAngle = 0;
						dxfelli.EndAngle = 360;
						dxfelli.MajorAxis = Math.Max( elli.Radius.W, elli.Radius.H );
						dxfelli.MinorAxis = Math.Min( elli.Radius.W, elli.Radius.H );
						dxfelli.Rotation = elli.Radius.W >= elli.Radius.H ? 0 : 90;
						obj = dxfelli;
						}
					else if( type == typeof( VirtualPie ) )
						{
						VirtualPie pie = (VirtualPie)vShape;
						Ellipse dxfelli = new Ellipse();
						dxfelli.Center = new netDxf.Vector3( pie.Center.X, pie.Center.Y, 0 );
						dxfelli.StartAngle = pie.StartAngle;
						dxfelli.EndAngle = pie.EndAngle;
						dxfelli.MajorAxis = Math.Max( pie.Radius.W, pie.Radius.H );
						dxfelli.MinorAxis = Math.Min( pie.Radius.W, pie.Radius.H );
						dxfelli.Rotation = pie.Radius.W >= pie.Radius.H ? 0 : 90;
						obj = dxfelli;
						}
					else obj = null;

					if( obj == null )
						continue;
					obj.Layer = dxfLayer;
					doc.AddEntity( obj );
					}
				}

			doc.Save( FileName );
			}
Exemplo n.º 30
0
        public static void ModifyingDimensionGeometryAndStyle()
        {
            DimensionStyle style = new DimensionStyle("MyStyle");
            Vector3 p1 = new Vector3(-2.5, 0, 0);
            Vector3 p2 = new Vector3(2.5, 0, 0);

            LinearDimension dim = new LinearDimension(p1, p2, 4, 0, style);

            // This is illegal. Trying to rebuild the dimension block before it has been added to a document will throw an exception
            //dim.RebuildBlock();

            DxfDocument doc = new DxfDocument();
            doc.AddEntity(dim);

            // modifying the dimension style
            dim.Style.DIMBLK = DimensionArrowhead.ArchitecturalTick;
            // if we make any change to the dimension style, we need to manually call the RebuildBlock method to reflect the new changes
            // since we will also modify the geometry of the dimension we will rebuild the block latter
            //dim.RebuildBlock();

            // the same kind of procedure needs to be done when modifying the geometry of a dimension
            dim.FirstReferencePoint = new Vector3(-5.0, 0, 0);
            dim.SecondReferencePoint = new Vector3(5.0, 0, 0);
            // now that all necessary changes has been made, we will rebuild the block.
            // this is an expensive operation, use it only when need it.

            dim.Style.DIMBLK = DimensionArrowhead.Box;
            dim.Style.DIMBLK = DimensionArrowhead.ArchitecturalTick;
            Debug.Assert(ReferenceEquals(dim.Style.DIMBLK, doc.Blocks[dim.Style.DIMBLK.Name]), "References are not equal.");
            Debug.Assert(ReferenceEquals(style.DIMBLK, doc.Blocks[style.DIMBLK.Name]), "References are not equal.");
            //dim.Style.DIMBLK = null;

            // VERY IMPORTANT: If any change is made to the dimension geometry and/or its style, we need to rebuild the drawing representation
            // so the dimension block will reflect the new changes. This is only necessary for dimension that already belongs to a document.
            // This process is automatically called when a new dimension is added to a document.
            dim.Update();
            Debug.Assert(ReferenceEquals(dim.Block, doc.Blocks[dim.Block.Name]));

            doc.Save("dimension.dxf");
            Test("dimension.dxf");

        }
Exemplo n.º 31
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="path"></param>
 /// <param name="container"></param>
 public void Save(string path, Test2d.Container container)
 {
     _outputPath = System.IO.Path.GetDirectoryName(path);
     var doc = new DxfDocument(DxfVersion.AutoCad2010);
     Add(doc, container);
     doc.Save(path);
     ClearCache(isZooming: false);
 }
Exemplo n.º 32
0
        public static void ModifyingGroups()
        {
            Line line1 = new Line(new Vector2(0, 0), new Vector2(100, 100));
            line1.Color = AciColor.Red;
            Line line2 = new Line(new Vector2(100, 0), new Vector2(200, 100));
            line2.Color = AciColor.Yellow;
            Line line3 = new Line(new Vector2(200, 0), new Vector2(300, 100));
            line3.Color = AciColor.Magenta;

            DxfDocument doc = new DxfDocument();

            Block blk = new Block("MyBlock");
            blk.Entities.Add(line1);
            Insert ins = new Insert(blk);
            doc.AddEntity(ins);

            doc.AddEntity(line2);

            Layout layout = new Layout("Layout1");
            doc.Layouts.Add(layout);
            doc.ActiveLayout = layout.Name;
            doc.AddEntity(line3);
            
            // group
            Group group = new Group("MyGroup");
            doc.Groups.Add(group);

            // the Add method will also add the entities contained in a group to the document (in the active layout).
            doc.Groups.Add(group);

            // when the group belongs to a document, all entities must belong to the same document.
            // even if it does not sound very useful, a group can contain entities that belongs to different layouts and even blocks.
            group.Entities.Add(line1);
            group.Entities.Add(line2);
            group.Entities.Add(line3);

            Line line4 = new Line(new Vector2(300, 0), new Vector2(400, 100));
            line4.Color = AciColor.Blue;
            // if a new entity, that does not belong to any document, is added to the group, it will be added to the group document active layout.
            doc.ActiveLayout = Layout.ModelSpaceName;
            group.Entities.Add(line4);

            Line line5 = new Line(new Vector2(400, 0), new Vector2(500, 100));
            line5.Color = AciColor.Green;
            DxfDocument doc2 = new DxfDocument();
            doc2.AddEntity(line5);

            // this is illegal, line5 belongs to another document.
            //group.Entities.Add(line5);
            // you need to clone the entity before adding it to the group. This is also the common practice to copy entities between documents.
            group.Entities.Add((EntityObject) line5.Clone());

            // remember removing a group only deletes it from the collection not the entities
            //doc.Groups.Remove(group);
            doc.Save("group.dxf");

            doc = DxfDocument.Load("group.dxf");
        }
Exemplo n.º 33
0
        private static void NestedBlock()
        {
            Block blockMM = new Block("BlockMM");
            blockMM.Record.Units = DrawingUnits.Millimeters;
            AttributeDefinition attDefMM = new AttributeDefinition("MyAttributeMM");
            attDefMM.Height = 1.0;
            attDefMM.Value = "This is block mm";
            blockMM.AttributeDefinitions.Add(attDefMM);
            Line line1MM = new Line(Vector2.Zero, Vector2.UnitX);
            blockMM.Entities.Add(line1MM);
            Insert insMM = new Insert(blockMM);
            insMM.TransformAttributes();

            Block blockCM = new Block("BlockCM");
            blockCM.Record.Units = DrawingUnits.Centimeters;
            AttributeDefinition attDefCM = new AttributeDefinition("MyAttributeCM");
            attDefCM.Height = 1.0;
            attDefCM.Value = "This is block cm";
            blockCM.AttributeDefinitions.Add(attDefCM);
            Line line1CM = new Line(Vector2.Zero, Vector2.UnitY);
            blockCM.Entities.Add(line1CM);
            blockCM.Entities.Add(insMM);
            Insert insCM = new Insert(blockCM);

            DxfDocument doc = new DxfDocument();
            doc.DrawingVariables.InsUnits = DrawingUnits.Meters;
            //doc.AddEntity(insMM);
            doc.AddEntity(insCM);

            doc.Save("test.dxf");
        }
Exemplo n.º 34
0
        private static void SpeedTest()
        {
            Stopwatch crono = new Stopwatch();
            const int numLines = (int)1e6; // create # lines
            string layerName = "MyLayer";
            float totalTime=0;
            
            List<EntityObject> lines = new List<EntityObject>(numLines);
            DxfDocument dxf = new DxfDocument();

            crono.Start();
            for (int i = 0; i < numLines; i++)
            {
                    //line
                Line line = new Line(new Vector3(0, i, 0), new Vector3(5, i, 0));
                line.Layer = new Layer(layerName);
                line.Layer.Color.Index = 6;
                lines.Add(line);
            }
            Console.WriteLine("Time creating entities : " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Reset();

            crono.Start();
            dxf.AddEntity(lines);
            Console.WriteLine("Time adding entities to document : " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Reset();

            crono.Start();
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2000;
            dxf.Save("speedtest (netDxf 2000).dxf");
            Console.WriteLine("Time saving file 2000 : " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Reset();

            crono.Start();
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2000;
            dxf.Save("speedtest (binary netDxf 2000).dxf", true);
            Console.WriteLine("Time saving binary file 2000 : " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Reset();

            crono.Start();
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            dxf.Save("speedtest (netDxf 2010).dxf");
            Console.WriteLine("Time saving file 2010 : " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Reset();


            crono.Start();
            dxf = DxfDocument.Load("speedtest (netDxf 2000).dxf");
            Console.WriteLine("Time loading file 2000: " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Stop();
            crono.Reset();

            crono.Start();
            dxf = DxfDocument.Load("speedtest (binary netDxf 2000).dxf");
            Console.WriteLine("Time loading binary file 2000: " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Stop();
            crono.Reset();

            crono.Start();
            dxf = DxfDocument.Load("speedtest (netDxf 2010).dxf");
            Console.WriteLine("Time loading file 2010: " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Stop();
            crono.Reset();

            Console.WriteLine("Total time : " + totalTime / 1000.0f);
            Console.ReadLine();

        }
        static void doDXF(List<Polyline3dVertex> vertexes, double[] x)
        {
            // create a dxf for those who want to "see" the calibration
            DxfDocument dxf = new DxfDocument();

            Polyline3d polyline = new Polyline3d(vertexes, true);
            polyline.Layer = new Layer("polyline3d");
            polyline.Layer.Color.Index = 24;
            dxf.AddEntity(polyline);

            var pnt = new Point(new Vector3f(-(float) x[0], -(float) x[1], -(float) x[2]));
            pnt.Layer = new Layer("new offset");
            pnt.Layer.Color.Index = 21;
            dxf.AddEntity(pnt);

            dxf.Save("magoffset.dxf", DxfVersion.AutoCad2000);

            log.Info("dxf Done " + DateTime.Now);
        }
Exemplo n.º 36
0
        public static void ModifyingDocumentEntities()
        {
            Layer layer1 = new Layer("layer1");
            Layer layer2 = new Layer("layer2");
            Layer layer3 = new Layer("layer3");

            LineType lineType1 = LineType.Dot;
            LineType lineType2 = LineType.Dashed;

            Line line = new Line(Vector2.Zero, Vector2.UnitX);
            line.Layer = layer1;
            line.LineType = lineType1;

            DxfDocument doc = new DxfDocument();
            doc.AddEntity(line);

            // if the layer does not exist in the document it will be added automatically
            line.Layer = layer2;
            Debug.Assert(ReferenceEquals(line.Layer, doc.Layers[line.Layer.Name]), "References are not equal.");

            // you can always add it first
            doc.Layers.Add(layer3);
            // layer3 is defined in the document
            line.Layer = layer3;
            Debug.Assert(ReferenceEquals(line.Layer, doc.Layers[line.Layer.Name]), "References are not equal.");

            // same thing is applicable to line types
            line.LineType = lineType2;
            Debug.Assert(ReferenceEquals(line.LineType, doc.LineTypes[line.LineType.Name]), "References are not equal.");

            doc.Save("entity.dxf");

            // it is also possible to rename table objects
            layer1.Name = "New layer1 name";
            lineType1.Name = "DotDot";

            // this operation is illegal, you cannot rename reserved table objects.
            //doc.Layers[Layer.DefaultName].Name = "NewName";

            doc.Save("test.dxf");
        }