Ejemplo n.º 1
0
        private Ellipse ReadEllipse(ref CodeValuePair code)
        {
            var ellipse = new Ellipse();
            Vector3f center = Vector3f.Zero;
            Vector3f axisPoint = Vector3f.Zero;
            Vector3f normal = Vector3f.UnitZ;
            float ratio = 0;
            Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>();

            code = this.ReadCodePair();
            while (code.Code != 0)
            {
                switch (code.Code)
                {
                    case 5:
                        ellipse.Handle = code.Value;
                        code = this.ReadCodePair();
                        break;
                    case 8: //layer code
                        ellipse.Layer = this.GetLayer(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 62: //aci color code
                        ellipse.Color = new AciColor(short.Parse(code.Value));
                        code = this.ReadCodePair();
                        break;
                    case 6: //type line code
                        ellipse.LineType = this.GetLineType(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 10:
                        center.X = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 20:
                        center.Y = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 30:
                        center.Z = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 11:
                        axisPoint.X = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 21:
                        axisPoint.Y = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 31:
                        axisPoint.Z = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 40:
                        ratio = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 41:
                        ellipse.StartAngle = (float) (double.Parse(code.Value)*MathHelper.RadToDeg);
                        code = this.ReadCodePair();
                        break;
                    case 42:
                        ellipse.EndAngle = (float) (double.Parse(code.Value)*MathHelper.RadToDeg);
                        code = this.ReadCodePair();
                        break;
                    case 210:
                        normal.X = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 220:
                        normal.Y = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 230:
                        normal.Z = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 1001:
                        XData xDataItem = this.ReadXDataRecord(code.Value, ref code);
                        xData.Add(xDataItem.ApplicationRegistry, xDataItem);
                        break;
                    default:
                        if (code.Code >= 1000 && code.Code <= 1071)
                            throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file,
                                                                         "The extended data of an entity must start with the application registry code " + this.fileLine);
                        code = this.ReadCodePair();
                        break;
                }
            }

            Vector3d ocsAxisPoint = MathHelper.Transform((Vector3d) axisPoint,
                                                         (Vector3d) normal,
                                                         MathHelper.CoordinateSystem.World,
                                                         MathHelper.CoordinateSystem.Object);
            double rotation = (float) Vector2d.AngleBetween(Vector2d.UnitX, new Vector2d(ocsAxisPoint.X, ocsAxisPoint.Y));

            ellipse.MajorAxis = 2*axisPoint.Modulus();
            ellipse.MinorAxis = ellipse.MajorAxis*ratio;
            ellipse.Rotation = (float) (rotation*MathHelper.RadToDeg);
            ellipse.Center = center;
            ellipse.Normal = normal;
            ellipse.XData = xData;
            return ellipse;
        }
Ejemplo n.º 2
0
        private void WriteEllipseAsPolyline(Ellipse ellipse)
        {
            //we will draw the ellipse as a polyline, it is not supported in AutoCad12 dxf files
            this.WriteCodePair(0, DxfObjectCode.Polyline);

            this.WriteEntityCommonCodes(ellipse);

            //closed polyline
            this.WriteCodePair(70, 1);

            //dummy point
            this.WriteCodePair(10, 0.0f);
            this.WriteCodePair(20, 0.0f);
            this.WriteCodePair(30, ellipse.Center.Z);

            this.WriteCodePair(39, ellipse.Thickness);

            this.WriteCodePair(210, ellipse.Normal.X);
            this.WriteCodePair(220, ellipse.Normal.Y);
            this.WriteCodePair(230, ellipse.Normal.Z);

            //Obsolete; formerly an “entities follow flag” (optional; ignore if present)
            //but its needed to load the dxf file in AutoCAD
            this.WriteCodePair(66, "1");

            this.WriteXData(ellipse.XData);

            List<Vector2f> points = ellipse.PolygonalVertexes(ellipse.CurvePoints);
            foreach (Vector2f v in points)
            {
                this.WriteCodePair(0, DxfObjectCode.Vertex);
                this.WriteCodePair(8, ellipse.Layer);
                this.WriteCodePair(70, 0);
                this.WriteCodePair(10, v.X);
                this.WriteCodePair(20, v.Y);
            }
            this.WriteCodePair(0, StringCode.EndSequence);
        }
Ejemplo n.º 3
0
        private static void Ellipse()
        {
            DxfDocument dxf = new DxfDocument();

            Line line = new Line(new Vector3f(0, 0, 0), new Vector3f((float)(2 * Math.Cos(Math.PI / 4)), (float)(2 * Math.Cos(Math.PI / 4)), 0));
            dxf.AddEntity(line);

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

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

            // ellipses are saved as polylines
            Ellipse ellipse = new Ellipse(new Vector3f(2,2,0), 5,3);
            ellipse.Rotation = 30;
            ellipse.Normal=new Vector3f(1,1,1);
            ellipse.Thickness = 2;
            dxf.AddEntity(ellipse);

            dxf.Save("ellipse.dxf", DxfVersion.AutoCad2000);
            dxf = new DxfDocument();
            dxf.Load("ellipse.dxf");
        }
Ejemplo n.º 4
0
        private void WriteEllipse(Ellipse ellipse)
        {
            if (this.activeSection != StringCode.EntitiesSection && !this.isBlockEntities)
            {
                throw new InvalidDxfSectionException(this.activeSection, this.file);
            }

            if (this.version == DxfVersion.AutoCad12)
            {
                this.WriteEllipseAsPolyline(ellipse);
                return;
            }

            this.WriteCodePair(0, ellipse.CodeName);
            this.WriteCodePair(5, ellipse.Handle);
            this.WriteCodePair(100, SubclassMarker.Entity);
            this.WriteEntityCommonCodes(ellipse);
            this.WriteCodePair(100, SubclassMarker.Ellipse);

            this.WriteCodePair(10, ellipse.Center.X);
            this.WriteCodePair(20, ellipse.Center.Y);
            this.WriteCodePair(30, ellipse.Center.Z);

            float sine = (float) (0.5*ellipse.MajorAxis*Math.Sin(ellipse.Rotation*MathHelper.DegToRad));
            float cosine = (float) (0.5*ellipse.MajorAxis*Math.Cos(ellipse.Rotation*MathHelper.DegToRad));
            Vector3d axisPoint = MathHelper.Transform((Vector3d) new Vector3f(cosine, sine, 0),
                                                      (Vector3d) ellipse.Normal,
                                                      MathHelper.CoordinateSystem.Object,
                                                      MathHelper.CoordinateSystem.World);

            this.WriteCodePair(11, axisPoint.X);
            this.WriteCodePair(21, axisPoint.Y);
            this.WriteCodePair(31, axisPoint.Z);

            this.WriteCodePair(210, ellipse.Normal.X);
            this.WriteCodePair(220, ellipse.Normal.Y);
            this.WriteCodePair(230, ellipse.Normal.Z);

            this.WriteCodePair(40, ellipse.MinorAxis/ellipse.MajorAxis);
            this.WriteCodePair(41, ellipse.StartAngle*MathHelper.DegToRad);
            this.WriteCodePair(42, ellipse.EndAngle*MathHelper.DegToRad);

            this.WriteXData(ellipse.XData);
        }