private IPolyline ReadPolyline(ref CodeValuePair code) { string handle = string.Empty; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; PolylineTypeFlags flags = PolylineTypeFlags.OpenPolyline; float elevation = 0.0f; float thickness = 0.0f; Vector3f normal = Vector3f.UnitZ; List<Vertex> vertexes = new List<Vertex>(); Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); //int numVertexes = -1; //int numFaces = -1; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; code = this.ReadCodePair(); break; case 8: layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: lineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 30: elevation = float.Parse(code.Value); code = this.ReadCodePair(); break; case 39: thickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 70: flags = (PolylineTypeFlags) (int.Parse(code.Value)); code = this.ReadCodePair(); break; case 71: //this field might not exist for polyface meshes, we cannot depend on it //numVertexes = int.Parse(code.Value); code = this.ReadCodePair(); code = this.ReadCodePair(); break; case 72: //this field might not exist for polyface meshes, we cannot depend on it //numFaces = int.Parse(code.Value); 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; } } //begin to read the vertex list if (code.Value != DxfObjectCode.Vertex) throw new DxfEntityException(DxfObjectCode.Polyline, this.file, "Vertex not found in line " + this.fileLine); while (code.Value != StringCode.EndSequence) { if (code.Value == DxfObjectCode.Vertex) { Debug.Assert(code.Code == 0); Vertex vertex = this.ReadVertex(ref code); vertexes.Add(vertex); } } // read the end end sequence object until a new element is found if (code.Value != StringCode.EndSequence) throw new DxfEntityException(DxfObjectCode.Polyline, this.file, "End sequence entity not found in line " + this.fileLine); code = this.ReadCodePair(); string endSequenceHandle = string.Empty; Layer endSequenceLayer = layer; while (code.Code != 0) { switch (code.Code) { case 5: endSequenceHandle = code.Value; code = this.ReadCodePair(); break; case 8: endSequenceLayer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; default: code = this.ReadCodePair(); break; } } IPolyline pol; bool isClosed = false; if ((flags & PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM) == PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM) { isClosed = true; } //to avoid possible error between the vertex type and the polyline type //the polyline type will decide which information to use from the read vertex if ((flags & PolylineTypeFlags.Polyline3D) == PolylineTypeFlags.Polyline3D) { List<Polyline3dVertex> polyline3dVertexes = new List<Polyline3dVertex>(); foreach (Vertex v in vertexes) { Polyline3dVertex vertex = new Polyline3dVertex { Color = v.Color, Layer = v.Layer, LineType = v.LineType, Location = v.Location, Handle = v.Handle }; vertex.XData = v.XData; polyline3dVertexes.Add(vertex); } ////posible error avoidance, the polyline is marked as polyline3d code:(70,8) but the vertex is marked as PolylineVertex code:(70,0) //if (v.Type == EntityType.PolylineVertex) //{ // Polyline3dVertex polyline3dVertex = new Polyline3dVertex(((PolylineVertex)v).Location.X, ((PolylineVertex)v).Location.Y,0); // polyline3dVertexes.Add(polyline3dVertex); //} //else //{ // polyline3dVertexes.Add((Polyline3dVertex)v); //} //} pol = new Polyline3d(polyline3dVertexes, isClosed) { Handle = handle }; ((Polyline3d) pol).EndSequence.Handle = endSequenceHandle; ((Polyline3d) pol).EndSequence.Layer = endSequenceLayer; } else if ((flags & PolylineTypeFlags.PolyfaceMesh) == PolylineTypeFlags.PolyfaceMesh) { //the vertex list created contains vertex and face information List<PolyfaceMeshVertex> polyfaceVertexes = new List<PolyfaceMeshVertex>(); List<PolyfaceMeshFace> polyfaceFaces = new List<PolyfaceMeshFace>(); foreach (Vertex v in vertexes) { if ((v.Flags & (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh)) == (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh)) { PolyfaceMeshVertex vertex = new PolyfaceMeshVertex { Color = v.Color, Layer = v.Layer, LineType = v.LineType, Location = v.Location, Handle = v.Handle }; vertex.XData = xData; polyfaceVertexes.Add(vertex); } else if ((v.Flags & (VertexTypeFlags.PolyfaceMeshVertex)) == (VertexTypeFlags.PolyfaceMeshVertex)) { PolyfaceMeshFace vertex = new PolyfaceMeshFace { Color = v.Color, Layer = v.Layer, LineType = v.LineType, VertexIndexes = v.VertexIndexes, Handle = v.Handle }; vertex.XData = xData; polyfaceFaces.Add(vertex); } //if (v.Type == EntityType.PolyfaceMeshVertex) //{ // polyfaceVertexes.Add((PolyfaceMeshVertex) v); //} //else if (v.Type == EntityType.PolyfaceMeshFace) //{ // polyfaceFaces.Add((PolyfaceMeshFace) v); //} //else //{ // throw new EntityDxfException(v.Type.ToString(), this.file, "Error in vertex type."); //} } pol = new PolyfaceMesh(polyfaceVertexes, polyfaceFaces) { Handle = handle }; ((PolyfaceMesh) pol).EndSequence.Handle = endSequenceHandle; ((PolyfaceMesh) pol).EndSequence.Layer = endSequenceLayer; } else { List<PolylineVertex> polylineVertexes = new List<PolylineVertex>(); foreach (Vertex v in vertexes) { PolylineVertex vertex = new PolylineVertex { Location = new Vector2f(v.Location.X, v.Location.Y), BeginThickness = v.BeginThickness, Bulge = v.Bulge, Color = v.Color, EndThickness = v.EndThickness, Layer = v.Layer, LineType = v.LineType, Handle = v.Handle }; vertex.XData = xData; ////posible error avoidance, the polyline is marked as polyline code:(70,0) but the vertex is marked as Polyline3dVertex code:(70,32) //if (v.Type==EntityType.Polyline3dVertex) //{ // PolylineVertex polylineVertex = new PolylineVertex(((Polyline3dVertex)v).Location.X, ((Polyline3dVertex)v).Location.Y); // polylineVertexes.Add(polylineVertex); //} //else //{ // polylineVertexes.Add((PolylineVertex) v); //} polylineVertexes.Add(vertex); } pol = new Polyline(polylineVertexes, isClosed) { Thickness = thickness, Elevation = elevation, Normal = normal, Handle = handle }; ((Polyline) pol).EndSequence.Handle = endSequenceHandle; ((Polyline) pol).EndSequence.Layer = endSequenceLayer; } pol.Color = color; pol.Layer = layer; pol.LineType = lineType; pol.XData = xData; return pol; }
private static void WriteDxfFile() { DxfDocument dxf = new DxfDocument(); //arc Arc arc = new Arc(new Vector3f(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("SharpDxf")); xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with SharpDxf")); 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 SharpDxf")); 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 Vector3f extrusion = new Vector3f(1, 1, 1); Vector3f centerWCS = new Vector3f(1, 1, 1); Vector3d centerOCS = MathHelper.Transform((Vector3d) centerWCS, (Vector3d)extrusion, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object); Circle circle = new Circle((Vector3f) 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 Vector3f(-3, -3, 0)); point1.Layer = new Layer("point"); point1.Color = new AciColor(30); Point point2 = new Point(new Vector3f(1, 1, 1)); point2.Layer = point1.Layer; point2.Layer.Color.Index = 9; point2.Normal = new Vector3f(1, 1, 1); dxf.AddEntity(point1); dxf.AddEntity(point2); //3dface Face3d face3D = new Face3d(new Vector3f(-5, -5, 5), new Vector3f(5, -5, 5), new Vector3f(5, 5, 5), new Vector3f(-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 Vector2f(-50, -50)); polyVertex.BeginThickness = 2; polyVertexes.Add(polyVertex); polyVertex = new PolylineVertex(new Vector2f(50, -50)); polyVertex.BeginThickness = 1; polyVertexes.Add(polyVertex); polyVertex = new PolylineVertex(new Vector2f(50, 50)); polyVertex.Bulge = 1; polyVertexes.Add(polyVertex); polyVertex = new PolylineVertex(new Vector2f(-50, 50)); polyVertexes.Add(polyVertex); Polyline polyline2d = new Polyline(polyVertexes, true); polyline2d.Layer = new Layer("polyline2d"); polyline2d.Layer.Color.Index = 5; polyline2d.Normal = new Vector3f(1, 1, 1); polyline2d.Elevation = 100.0f; dxf.AddEntity(polyline2d); //lightweight polyline LightWeightPolylineVertex lwVertex; List<LightWeightPolylineVertex> lwVertexes = new List<LightWeightPolylineVertex>(); lwVertex = new LightWeightPolylineVertex(new Vector2f(-25, -25)); lwVertex.BeginThickness = 2; lwVertexes.Add(lwVertex); lwVertex = new LightWeightPolylineVertex(new Vector2f(25, -25)); lwVertex.BeginThickness = 1; lwVertexes.Add(lwVertex); lwVertex = new LightWeightPolylineVertex(new Vector2f(25, 25)); lwVertex.Bulge = 1; lwVertexes.Add(lwVertex); lwVertex = new LightWeightPolylineVertex(new Vector2f(-25, 25)); lwVertexes.Add(lwVertex); LightWeightPolyline lwPolyline = new LightWeightPolyline(lwVertexes, true); lwPolyline.Layer = new Layer("lwpolyline"); lwPolyline.Layer.Color.Index = 5; lwPolyline.Normal = new Vector3f(1, 1, 1); lwPolyline.Elevation = 100.0f; dxf.AddEntity(lwPolyline); //line Line line = new Line(new Vector3f(0, 0, 0), new Vector3f(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 Vector3f(-50, -50, 0)); vertexes.Add(vertex); vertex = new Polyline3dVertex(new Vector3f(50, -50, 10)); vertexes.Add(vertex); vertex = new Polyline3dVertex(new Vector3f(50, 50, 25)); vertexes.Add(vertex); vertex = new Polyline3dVertex(new Vector3f(-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 Vector3f(-5, -5, 5), new Vector3f(5, 5, 5))); block.Entities.Add(new Line(new Vector3f(5, -5, 5), new Vector3f(-5, 5, 5))); //insert Insert insert = new Insert(block, new Vector3f(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!", Vector3f.Zero, 10.0f,style); text.Layer = new Layer("text"); text.Layer.Color.Index = 8; text.Alignment = TextAlignment.TopRight; dxf.AddEntity(text); dxf.Save("AutoCad2007.dxf", DxfVersion.AutoCad2007); dxf.Save("AutoCad2004.dxf", DxfVersion.AutoCad2004); dxf.Save("AutoCad2000.dxf", DxfVersion.AutoCad2000); dxf.Save("AutoCad12.dxf", DxfVersion.AutoCad12); }
/// <summary> /// Converts the ellipse in a Polyline. /// </summary> /// <param name="precision">Number of vertexes generated.</param> /// <returns>A new instance of <see cref="Polyline">Polyline</see> that represents the ellipse.</returns> public Polyline ToPolyline(int precision) { List<Vector2f> vertexes = this.PolygonalVertexes(precision); Vector3d ocsCenter = MathHelper.Transform((Vector3d) this.center, (Vector3d)this.normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object); Polyline poly = new Polyline { Color = this.color, Layer = this.layer, LineType = this.lineType, Normal = this.normal, Elevation = (float)ocsCenter.Z, Thickness=this.thickness }; poly.XData=this.xData; foreach (Vector2f v in vertexes) { poly.Vertexes.Add(new PolylineVertex((float) (v.X + ocsCenter.X), (float) (v.Y + ocsCenter.Y))); } if (this.IsFullEllipse) poly.IsClosed = true; return poly; }
private static void Polyline() { DxfDocument dxf = new DxfDocument(); Polyline poly = new Polyline(); poly.Vertexes.Add(new PolylineVertex(0,0)); poly.Vertexes.Add(new PolylineVertex(10, 10)); poly.Vertexes.Add(new PolylineVertex(20, 0)); poly.Vertexes.Add(new PolylineVertex(30, 10)); dxf.AddEntity(poly); dxf.Save("polyline.dxf", DxfVersion.AutoCad2000); }
private void WritePolyline2d(Polyline polyline) { if (this.activeSection != StringCode.EntitiesSection && !this.isBlockEntities) { throw new InvalidDxfSectionException(this.activeSection, this.file); } this.WriteCodePair(0, polyline.CodeName); this.WriteCodePair(100, SubclassMarker.Entity); this.WriteEntityCommonCodes(polyline); this.WriteCodePair(5, polyline.Handle); this.WriteCodePair(100, SubclassMarker.Polyline); this.WriteCodePair(70, (int) polyline.Flags); //dummy point this.WriteCodePair(10, 0.0); this.WriteCodePair(20, 0.0); this.WriteCodePair(30, polyline.Elevation); this.WriteCodePair(39, polyline.Thickness); this.WriteCodePair(210, polyline.Normal.X); this.WriteCodePair(220, polyline.Normal.Y); this.WriteCodePair(230, polyline.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(polyline.XData); foreach (PolylineVertex v in polyline.Vertexes) { this.WriteCodePair(0, v.CodeName); this.WriteCodePair(5, v.Handle); this.WriteCodePair(100, SubclassMarker.Entity); this.WriteCodePair(8, v.Layer); this.WriteCodePair(100, SubclassMarker.Vertex); this.WriteCodePair(100, SubclassMarker.PolylineVertex); this.WriteCodePair(70, (int) v.Flags); this.WriteCodePair(10, v.Location.X); this.WriteCodePair(20, v.Location.Y); this.WriteCodePair(40, v.BeginThickness); this.WriteCodePair(41, v.EndThickness); this.WriteCodePair(42, v.Bulge); this.WriteXData(v.XData); } this.WriteCodePair(0, polyline.EndSequence.CodeName); this.WriteCodePair(5, polyline.EndSequence.Handle); this.WriteCodePair(100, SubclassMarker.Entity); this.WriteCodePair(8, polyline.EndSequence.Layer); }