/// <summary> /// Saves the database of the actual DxfDocument to a dxf ASCII file. /// </summary> /// <param name="file">File name.</param> /// <param name="dxfVersion">Dxf file <see cref="DxfVersion">version</see>.</param> public void Save(string file, DxfVersion dxfVersion) { ReAsignHandlersAndDefaultObjects(); this.fileName = Path.GetFileNameWithoutExtension(file); this.version = dxfVersion; List <Polyline> ellipsePolys = null; List <IPolyline> lwPolys; Dictionary <string, List <IEntityObject> > blockEntities; if (this.version == DxfVersion.AutoCad12) { // since AutoCad dxf Version 12 doesn't support ellipses, we will transform them in polylines ellipsePolys = new List <Polyline>(); foreach (Ellipse ellipse in this.ellipses) { Polyline poly = ellipse.ToPolyline(ellipse.CurvePoints); this.handleCount = poly.AsignHandle(this.handleCount); ellipsePolys.Add(poly); } // since AutoCad dxf Version 12 doesn't support lwpolylines, we will transform them in polylines lwPolys = new List <IPolyline>(); foreach (IPolyline lwPoly in this.polylines) { if (lwPoly is LightWeightPolyline) { Polyline poly = ((LightWeightPolyline)lwPoly).ToPolyline(); this.handleCount = poly.AsignHandle(this.handleCount); lwPolys.Add(poly); } else { lwPolys.Add(lwPoly); } } // since AutoCad dxf Version 12 doesn't support lwpolylines in blocks, we will transform them in polylines blockEntities = new Dictionary <string, List <IEntityObject> >(); foreach (Block block in this.blocks.Values) { blockEntities.Add(block.Name, new List <IEntityObject>()); foreach (IEntityObject entity in block.Entities) { if (entity is LightWeightPolyline) { Polyline poly = ((LightWeightPolyline)entity).ToPolyline(); this.handleCount = poly.AsignHandle(this.handleCount); blockEntities[block.Name].Add(poly); } else { blockEntities[block.Name].Add(entity); } } } } else { // since AutoCad dxf Version 12 doesn't support lwpolylines, we will transform them in polylines lwPolys = new List <IPolyline>(); foreach (IPolyline lwPoly in this.polylines) { if ((lwPoly is Polyline)) { LightWeightPolyline poly = ((Polyline)lwPoly).ToLightWeightPolyline(); this.handleCount = poly.AsignHandle(this.handleCount); lwPolys.Add(poly); } else { lwPolys.Add(lwPoly); } } // since latter AutoCad dxf Version doesn't support polylines in blocks, we will transform them in lightweightpolylines blockEntities = new Dictionary <string, List <IEntityObject> >(); foreach (Block block in this.blocks.Values) { blockEntities.Add(block.Name, new List <IEntityObject>()); foreach (IEntityObject entity in block.Entities) { if (entity is Polyline) { LightWeightPolyline poly = ((Polyline)entity).ToLightWeightPolyline(); this.handleCount = poly.AsignHandle(this.handleCount); blockEntities[block.Name].Add(poly); } else { blockEntities[block.Name].Add(entity); } } } } CultureInfo cultureInfo = CultureInfo.CurrentCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; DxfWriter dxfWriter = new DxfWriter(file, dxfVersion); dxfWriter.Open(); dxfWriter.WriteComment("Dxf file generated by SharpDxf, Copyright(C) 2009 Lomatus, Licensed under LGPL"); //HEADER SECTION dxfWriter.BeginSection(StringCode.HeaderSection); dxfWriter.WriteSystemVariable(new HeaderVariable(SystemVariable.DabaseVersion, StringEnum.GetStringValue(this.version))); dxfWriter.WriteSystemVariable(new HeaderVariable(SystemVariable.HandSeed, Convert.ToString(this.handleCount, 16))); dxfWriter.EndSection(); ////CLASSES SECTION //dxfWriter.BeginSection(StringCode.ClassesSection); //dxfWriter.EndSection(); //TABLES SECTION dxfWriter.BeginSection(StringCode.TablesSection); //viewport tables if (this.version != DxfVersion.AutoCad12) { dxfWriter.BeginTable(StringCode.ViewPortTable); foreach (ViewPort vport in this.viewports.Values) { dxfWriter.WriteViewPort(vport); } dxfWriter.EndTable(); } //line type tables dxfWriter.BeginTable(StringCode.LineTypeTable); foreach (LineType lineType in this.lineTypes.Values) { dxfWriter.WriteLineType(lineType); } dxfWriter.EndTable(); //layer tables dxfWriter.BeginTable(StringCode.LayerTable); foreach (Layer layer in this.layers.Values) { dxfWriter.WriteLayer(layer); } dxfWriter.EndTable(); //text style tables dxfWriter.BeginTable(StringCode.TextStyleTable); foreach (TextStyle style in this.textStyles.Values) { dxfWriter.WriteTextStyle(style); } dxfWriter.EndTable(); //view dxfWriter.BeginTable(StringCode.ViewTable); dxfWriter.EndTable(); //ucs dxfWriter.BeginTable(StringCode.UcsTable); dxfWriter.EndTable(); //registered application tables dxfWriter.BeginTable(StringCode.ApplicationIDTable); foreach (ApplicationRegistry id in this.appRegisterNames.Values) { dxfWriter.RegisterApplication(id); } dxfWriter.EndTable(); //dimension style tables if (this.version != DxfVersion.AutoCad12) { dxfWriter.BeginTable(StringCode.DimensionStyleTable); foreach (DimensionStyle style in this.dimStyles.Values) { dxfWriter.WriteDimensionStyle(style); } dxfWriter.EndTable(); } //block record tables, this table isnot recognized by AutoCad12 if (this.version != DxfVersion.AutoCad12) { dxfWriter.BeginTable(StringCode.BlockRecordTable); foreach (Block block in this.blocks.Values) { dxfWriter.WriteBlockRecord(block.Record); } dxfWriter.EndTable(); } dxfWriter.EndSection(); //End section tables dxfWriter.BeginSection(StringCode.BlocksSection); foreach (Block block in this.blocks.Values) { dxfWriter.WriteBlock(block, blockEntities[block.Name]); } dxfWriter.EndSection(); //End section blocks //ENTITIES SECTION dxfWriter.BeginSection(StringCode.EntitiesSection); #region writting entities foreach (Arc arc in this.arcs) { dxfWriter.WriteEntity(arc); } foreach (Circle circle in this.circles) { dxfWriter.WriteEntity(circle); } // only for version 12 draw polylines instead of ellipses if (this.version == DxfVersion.AutoCad12) { if (ellipsePolys != null) { foreach (Polyline ellipse in ellipsePolys) { dxfWriter.WriteEntity(ellipse); } } } else { foreach (Ellipse ellipse in this.ellipses) { dxfWriter.WriteEntity(ellipse); } } foreach (NurbsCurve nurbsCurve in this.nurbsCurves) { dxfWriter.WriteEntity(nurbsCurve); } foreach (Point point in this.points) { dxfWriter.WriteEntity(point); } foreach (Face3d face in this.faces3d) { dxfWriter.WriteEntity(face); } foreach (Solid solid in this.solids) { dxfWriter.WriteEntity(solid); } foreach (Insert insert in this.inserts) { dxfWriter.WriteEntity(insert); } foreach (Line line in this.lines) { dxfWriter.WriteEntity(line); } // lwpolyline in Acad12 are written as polylines foreach (IPolyline pol in lwPolys) { dxfWriter.WriteEntity(pol); } //foreach (IPolyline polyline in this.polylines) //{ // // avoid write lwpolylines in Acad12 // if (this.version != DxfVersion.AutoCad12 || !(polyline is LightWeightPolyline)) // { // dxfWriter.WriteEntity(polyline); // } //} foreach (Text text in this.texts) { dxfWriter.WriteEntity(text); } #endregion dxfWriter.EndSection(); //End section entities //OBJECTS SECTION dxfWriter.BeginSection(StringCode.ObjectsSection); dxfWriter.WriteDictionary(Dictionary.Default); dxfWriter.EndSection(); dxfWriter.Close(); Thread.CurrentThread.CurrentCulture = cultureInfo; }