private static void LineWidth()
        {
            // the line thickness works as expected, according to the AutoCAD way of doing things
            Line thickLine = new Line(new Vector3(0, 10, 0), new Vector3(10, 20, 0));

            // when you assign a thickness to a line, the result is like a wall, it is like a 3d face whose vertexes are defined by the
            // start and end points of the line and the thickness along the normal of the line.
            thickLine.Thickness = 5;

            // maybe what you are trying to do is create a line with a width (something that we can read it as a line with thickness), the only way to do this is to create a polyline
            // the kind of result you will get if you give a width to a 2d polyline
            // you can only give a width to a vertex of a Polyline or a LightweigthPolyline
            // I am planning to drop support to AutoCAD 12 dxf files, so to define a bidimensional polyline the only way will be to use lightweight polyline
            // (the Polyline class and the LightWeightPolyline are basically the same).
            Polyline       widthLine   = new Polyline();
            PolylineVertex startVertex = new PolylineVertex(new Vector2(0, 0));
            PolylineVertex endVertex   = new PolylineVertex(new Vector2(10, 10));

            widthLine.Vertexes = new List <PolylineVertex> {
                startVertex, endVertex
            };

            // the easy way to give a constant width to a polyline, but you can also give a polyline width by vertex
            // there is a mistake on my part, following the AutoCAD documentation I should have called the PolylineVertex.StartThickness and PolylineVertex.EndThickness as
            // PolylineVertex.StartWidth and PolylineVertex.EndWidth
            // SetConstantWidth is a sort cut that will asign the given value to every start width and end width of every vertex of the polyline
            widthLine.SetConstantWidth(0.5);

            DxfDocument dxf = new DxfDocument();

            // add the entities to the document (both of them to see the difference)
            dxf.AddEntity(thickLine);
            dxf.AddEntity(widthLine);

            dxf.Save("line width.dxf", DxfVersion.AutoCad2000);
        }
        private static void ExplodeTest()
        {
            DxfDocument dxf = new DxfDocument();
            //polyline
            PolylineVertex        polyVertex;
            List <PolylineVertex> polyVertexes = new List <PolylineVertex>();

            polyVertex       = new PolylineVertex(new Vector2(-50, -23.5));
            polyVertex.Bulge = 1.33;
            polyVertexes.Add(polyVertex);
            polyVertex = new PolylineVertex(new Vector2(34.8, -42.7));
            polyVertexes.Add(polyVertex);
            polyVertex       = new PolylineVertex(new Vector2(65.3, 54.7));
            polyVertex.Bulge = -0.47;
            polyVertexes.Add(polyVertex);
            polyVertex = new PolylineVertex(new Vector2(-48.2, 42.5));
            polyVertexes.Add(polyVertex);
            Polyline polyline2d = new Polyline(polyVertexes);

            polyline2d.Layer             = new Layer("polyline2d");
            polyline2d.Layer.Color.Index = 5;
            polyline2d.Normal            = new Vector3(1, 1, 1);
            polyline2d.Elevation         = 100.0f;

            dxf.AddEntity(polyline2d);
            dxf.AddEntity(polyline2d.Explode());

            Polyline polyline = new Polyline(polyline2d.PoligonalVertexes(10, 0.001, 0.001));

            polyline.Normal    = polyline2d.Normal;
            polyline.Elevation = polyline2d.Elevation;
            polyline.IsClosed  = false;
            dxf.AddEntity(polyline);

            dxf.Save("explode.dxf", DxfVersion.AutoCad2000);
        }
示例#3
0
文件: Form1.cs 项目: yunlight/httplib
        private void AddPolylines()
        {
            foreach (Polyline obj in dxf.Polylines)
            {
                PolylineVertex vertexFirst = null;
                foreach (PolylineVertex vertex in obj.Vertexes)
                {
                    if (vertexFirst == null)
                    {
                        vertexFirst = (PolylineVertex)vertex.Clone();
                    }
                    this.pointFs.Add(new PointF((float)vertex.Position.X, ConvertToGdiY(vertex.Position.Y)));
                }
                if (obj.IsClosed && vertexFirst != null)
                {
                    this.pointFs.Add(new PointF((float)vertexFirst.Position.X, ConvertToGdiY(vertexFirst.Position.Y)));
                }

                //if (obj.Flags == PolylineTypeFlags.OpenPolyline || obj.Flags == PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM)
                //{
                //    LightWeightPolyline polygon = (LightWeightPolyline)obj;
                //    PathFigure path = new PathFigure();
                //    float bulge = 0;
                //    System.Windows.Point prePoint = new System.Windows.Point();
                //    System.Windows.Point point = new System.Windows.Point();

                //    path.IsClosed = polygon.IsClosed;

                //    for (int i = 0; i < polygon.Vertexes.Count(); i)
                //    {
                //        var seg = polygon.Vertexes[i];
                //        point = new System.Windows.Point(seg.Location.X, -seg.Location.Y);

                //        if (i == 0)
                //        {
                //            path.StartPoint = point;
                //            prePoint = point;
                //            bulge = seg.Bulge;
                //            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                //        }
                //        else
                //        {
                //            ArcSegment arc = new ArcSegment();
                //            arc.Point = point;

                //            //if (angle != 0)
                //            if (bulge != 0)
                //            {
                //                double angle = 4 * Math.Atan(Math.Abs(bulge)) / Math.PI * 180;
                //                double length = Math.Sqrt((point.X - prePoint.X) * (point.X - prePoint.X)(point.Y - prePoint.Y) * (point.Y - prePoint.Y));
                //                //double radius = length / (Math.Sqrt(2 * (1 - Math.Cos(angle / 180 * Math.PI))));

                //                double radius = Math.Abs(length / (2 * Math.Sin(angle / 360 * Math.PI)));

                //                arc.Size = new System.Windows.Size(radius, radius);
                //                arc.RotationAngle = angle;

                //                arc.SweepDirection = bulge < 0 ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;
                //                arc.IsLargeArc = Math.Abs(bulge) > 1 ? true : false;
                //            }

                //            prePoint = point;
                //            bulge = seg.Bulge;
                //            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                //            path.Segments.Add(arc);

                //        }
                //    }
                //}
            }
        }
        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.Long, 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,
                                                     MathHelper.CoordinateSystem.World,
                                                     MathHelper.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       = new Dictionary <ApplicationRegistry, XData>
            {
                { xdata.ApplicationRegistry, xdata },
                { xdata2.ApplicationRegistry, 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
            PolylineVertex        polyVertex;
            List <PolylineVertex> polyVertexes = new List <PolylineVertex>();

            polyVertex            = new PolylineVertex(new Vector2(-50, -50));
            polyVertex.BeginWidth = 2;
            polyVertexes.Add(polyVertex);
            polyVertex            = new PolylineVertex(new Vector2(50, -50));
            polyVertex.BeginWidth = 1;
            polyVertexes.Add(polyVertex);
            polyVertex       = new PolylineVertex(new Vector2(50, 50));
            polyVertex.Bulge = 1;
            polyVertexes.Add(polyVertex);
            polyVertex = new PolylineVertex(new Vector2(-50, 50));
            polyVertexes.Add(polyVertex);
            Polyline polyline2d = new Polyline(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
            LightWeightPolylineVertex        lwVertex;
            List <LightWeightPolylineVertex> lwVertexes = new List <LightWeightPolylineVertex>();

            lwVertex            = new LightWeightPolylineVertex(new Vector2(-25, -25));
            lwVertex.BeginWidth = 2;
            lwVertexes.Add(lwVertex);
            lwVertex            = new LightWeightPolylineVertex(new Vector2(25, -25));
            lwVertex.BeginWidth = 1;
            lwVertexes.Add(lwVertex);
            lwVertex       = new LightWeightPolylineVertex(new Vector2(25, 25));
            lwVertex.Bulge = 1;
            lwVertexes.Add(lwVertex);
            lwVertex = new LightWeightPolylineVertex(new Vector2(-25, 25));
            lwVertexes.Add(lwVertex);
            LightWeightPolyline lwPolyline = new LightWeightPolyline(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);

            //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
            Polyline3dVertex        vertex;
            List <Polyline3dVertex> vertexes = new List <Polyline3dVertex>();

            vertex = new Polyline3dVertex(new Vector3(-50, -50, 0));
            vertexes.Add(vertex);
            vertex = new Polyline3dVertex(new Vector3(50, -50, 10));
            vertexes.Add(vertex);
            vertex = new Polyline3dVertex(new Vector3(50, 50, 25));
            vertexes.Add(vertex);
            vertex = new Polyline3dVertex(new Vector3(-50, 50, 50));
            vertexes.Add(vertex);
            Polyline3d polyline = new Polyline3d(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.Save("AutoCad2010.dxf", DxfVersion.AutoCad2010);
            dxf.Save("AutoCad2007.dxf", DxfVersion.AutoCad2007);
            dxf.Save("AutoCad2004.dxf", DxfVersion.AutoCad2004);
            dxf.Save("AutoCad2000.dxf", DxfVersion.AutoCad2000);
        }
示例#5
0
    public void CreatePolylinesFromCurrentPoints(bool transparent, float feather, int renderOrder, float depthOffset, float mapScale, Color color)
    {
        foreach (List <float2> polyline in Points)
        {
            //NO INTERPOLATION
            Polyline p = new Polyline(polyline.Count);
            p.transparent     = transparent;
            p.feather         = feather;
            p.renderOrder     = renderOrder;
            p.depthOffset     = depthOffset;
            p.colorMultiplier = color;

            for (int i = 0; i < polyline.Count; i++)
            {
                Vector3 vecA = new Vector3(polyline[i].x * mapScale, 0f, polyline[i].y * mapScale);
                p[i] = new PolylineVertex(vecA, Color.white, 1f);
                PointCount++;
            }

            Polylines.Add(p);

            #region interp
            //WITH CATMULL ROM INTERPOLATION

            /*if (polyline.Count >= 4)
             * {
             *      Polyline p = new Polyline(2 + (polyline.Count - 3) * splineInterpolationPoints);
             *      p.transparent = transparent;
             *      p.feather = feather;
             *      p.renderOrder = renderOrder;
             *      p.depthOffset = depthOffset;
             *
             *      //Create first point directly.
             *      Vector3 startPoint = new Vector3(polyline[0].x * mapScale, 0f, polyline[0].y * mapScale);
             *      p[0] = new PolylineVertex(startPoint, Color.white, 1f);
             *
             *      for (int i = 0; i < polyline.Count - 3; i++)
             *      {
             *              for (int j = 0; j < splineInterpolationPoints; j++)
             *              {
             *                      float2 point = PointOnCatmullRom(polyline[i], polyline[i + 1], polyline[i + 2], polyline[i + 3], (float)j / (float)splineInterpolationPoints);
             *                      Vector3 v = new Vector3(point.x * mapScale, 0f, point.y * mapScale);
             *
             *                      p[1 + (i * splineInterpolationPoints) + j] = new PolylineVertex(v, Color.white, 1f);
             *              }
             *      }
             *
             *      Vector3 endPoint = new Vector3(polyline[polyline.Count - 1].x * mapScale, 0f, polyline[polyline.Count - 1].y * mapScale);
             *      p[p.count - 1] = new PolylineVertex(endPoint, Color.white, 1f);
             *
             *      polylines.Add(p);
             * }
             * else
             * {
             *      //Not enough vertex for corner rounding, so just use direct verts.
             *      Polyline p = new Polyline(polyline.Count);
             *      p.transparent = transparent;
             *      p.feather = feather;
             *      p.renderOrder = renderOrder;
             *      p.depthOffset = depthOffset;
             *
             *      for (int i = 0; i < polyline.Count; i++)
             *      {
             *              Vector3 vecA = new Vector3(polyline[i].x * mapScale, 0.0001f, polyline[i].y * mapScale);
             *              p[i] = new PolylineVertex(vecA, Color.white, 1f);
             *              pointCount++;
             *      }
             *
             *      polylines.Add(p);
             * }*/

            //WITH HERMITE INTERPOLATION

            /*if (polyline.Count >= 4)
             * {
             *      List<Vector2> tempPointList = new List<Vector2>(((polyline.Count - 3) * splineInterpolationPoints) + 2);
             *
             *      //add start point
             *      tempPointList.Add(new Vector2(polyline[0].x, polyline[0].y));
             *
             *      for (int i = 0; i < polyline.Count - 3; i++)
             *      {
             *              for (int j = 0; j < splineInterpolationPoints; j++)
             *              {
             *                      float hermX = Linefy.Utilites.HermiteValue(polyline[i].x, polyline[i + 1].x, polyline[i + 2].x, polyline[i + 3].x, (float)j / (float)splineInterpolationPoints);
             *                      float hermY = Linefy.Utilites.HermiteValue(polyline[i].y, polyline[i + 1].y, polyline[i + 2].y, polyline[i + 3].y, (float)j / (float)splineInterpolationPoints);
             *
             *                      tempPointList.Add(new Vector2(hermX, hermY));
             *              }
             *      }
             *
             *      tempPointList.Add(new Vector2(polyline[polyline.Count - 1].x, polyline[polyline.Count - 1].y));
             *
             *      Polyline p = new Polyline(tempPointList.Count);
             *      p.transparent = transparent;
             *      p.feather = feather;
             *      p.renderOrder = renderOrder;
             *      p.depthOffset = depthOffset;
             *
             *      for (int i = 0; i < tempPointList.Count; i++)
             *      {
             *              Vector3 v3 = new Vector3(tempPointList[i].x * mapScale, 0f, tempPointList[i].y * mapScale);
             *              p[i] = new PolylineVertex(v3, Color.white, 1f);
             *              pointCount++;
             *      }
             *
             *      polylines.Add(p);
             * }
             * else
             * {
             *      Polyline p = new Polyline(polyline.Count);
             *      p.transparent = transparent;
             *      p.feather = feather;
             *      p.renderOrder = renderOrder;
             *      p.depthOffset = depthOffset;
             *
             *      for (int i = 0; i < polyline.Count; i++)
             *      {
             *              Vector3 vecA = new Vector3(polyline[i].x * 100f, 0.0001f, polyline[i].y * 100f);
             *              p[i] = new PolylineVertex(vecA, Color.white, 1f);
             *              pointCount++;
             *      }
             *
             *      polylines.Add(p);
             * }*/
            #endregion
        }
    }