/// <summary> /// Initializes a new instance of the <c>Insert</c> class. /// </summary> /// <param name="block">Insert block definition.</param> /// <param name="insertionPoint">Insert <see cref="Vector3f">point</see>.</param> public Insert(Block block, Vector3f insertionPoint) : base(DxfObjectCode.Insert) { if (block == null) throw new ArgumentNullException("block"); this.block = block; this.insertionPoint = insertionPoint; this.scale = new Vector3f(1.0f, 1.0f, 1.0f); this.rotation = 0.0f; this.normal = Vector3f.UnitZ; this.layer = Layer.Default; this.color = AciColor.ByLayer; this.lineType = LineType.ByLayer; this.attributes = new List<Attribute>(); foreach (AttributeDefinition attdef in block.Attributes.Values) { this.attributes.Add(new Attribute(attdef)); } this.endSequence = new EndSequence(); }
private Block ReadBlock(ref CodeValuePair code) { Layer layer = null; string name = string.Empty; string handle = string.Empty; Vector3f basePoint = Vector3f.Zero; List<IEntityObject> entities = new List<IEntityObject>(); Dictionary<string, AttributeDefinition> attdefs = new Dictionary<string, AttributeDefinition>(); code = this.ReadCodePair(); while (code.Value != StringCode.EndBlock) { switch (code.Code) { case 5: handle = code.Value; code = this.ReadCodePair(); break; case 8: layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 2: name = code.Value; code = this.ReadCodePair(); break; case 10: basePoint.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 20: basePoint.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 30: basePoint.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 3: name = code.Value; code = this.ReadCodePair(); break; case 0: // entity IEntityObject entity; entity = this.ReadBlockEntity(ref code); if (entity != null) if (entity.Type == EntityType.AttributeDefinition) attdefs.Add(((AttributeDefinition) entity).Id, (AttributeDefinition) entity); else entities.Add(entity); break; default: code = this.ReadCodePair(); break; } } // read the end bloc object until a new element is found code = this.ReadCodePair(); string endBlockHandle = string.Empty; Layer endBlockLayer = layer; while (code.Code != 0) { switch (code.Code) { case 5: endBlockHandle = code.Value; code = this.ReadCodePair(); break; case 8: endBlockLayer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; default: code = this.ReadCodePair(); break; } } Block block = new Block(name) { BasePoint = basePoint, Layer = layer, Entities = entities, Attributes = attdefs, Handle = handle, }; block.End.Handle = endBlockHandle; block.End.Layer = endBlockLayer; return block; }
private Attribute ReadAttribute(Block block, ref CodeValuePair code) { string handle = string.Empty; AttributeDefinition attdef = null; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; Object value = null; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; break; case 2: attdef = block.Attributes[code.Value]; break; case 1: value = code.Value; break; case 8: //layer code layer = this.GetLayer(code.Value); break; case 62: //aci color code color = new AciColor(short.Parse(code.Value)); break; case 6: //type line code lineType = this.GetLineType(code.Value); break; } code = this.ReadCodePair(); } return new Attribute(attdef) { Color = color, Layer = layer, LineType = lineType, Value = value, Handle = handle }; }
private static void BlockAttributes() { DxfDocument dxf = new DxfDocument( ); Block block = new Block("BlockWithAttributes"); block.Layer = new Layer("BlockSample"); AttributeDefinition attdef = new AttributeDefinition("NewAttribute"); attdef.Text = "InfoText"; attdef.BasePoint = new Vector3f(1, 1, 1); attdef.Style.IsVertical = true; attdef.Rotation = 45; block.Attributes.Add(attdef.Id, attdef); block.Entities.Add(new Line(new Vector3f(-5, -5, 0), new Vector3f(5, 5, 0))); block.Entities.Add(new Line(new Vector3f(5, -5, 0), new Vector3f(-5, 5, 0))); Insert insert = new Insert(block, new Vector3f(5, 5, 5)); insert.Layer = new Layer("insert"); insert.Rotation = 45; insert.Layer.Color.Index = 4; insert.Attributes[0].Value = 24; Insert insert2 = new Insert(block, new Vector3f(-5, -5, -5)); insert2.Attributes[0].Value = 34; XData xdata1 = new XData(new ApplicationRegistry("SharpDxf")); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with SharpDxf")); xdata1.XDataRecord.Add(XDataRecord.OpenControlString); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, 0)); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, 0)); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, 0)); xdata1.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); insert.XData = new Dictionary<ApplicationRegistry, XData> { {xdata1.ApplicationRegistry, xdata1}, }; dxf.AddEntity(insert); dxf.AddEntity(insert2); Circle circle = new Circle(Vector3f.Zero, 5); circle.Layer = new Layer("circle"); circle.Layer.Color.Index = 2; circle.XData = new Dictionary<ApplicationRegistry, XData> { {xdata2.ApplicationRegistry, xdata2}, }; dxf.AddEntity(circle); dxf.Save("Block with attributes.dxf", DxfVersion.AutoCad2000); dxf.Load("Block with attributes.dxf"); dxf.Save("Block with attributes result.dxf", DxfVersion.AutoCad2000); // both results must be equal only the handles might be different }
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> /// Determines if a specified block exists in the table. /// </summary> /// <param name="block">Block to locate.</param> /// <returns>True if the specified block exists or false in any other case.</returns> public bool ContainsBlock(Block block) { return this.blocks.ContainsKey(block.Name); }
public void WriteBlock(Block block, List<IEntityObject> entityObjects) { if (this.version == DxfVersion.AutoCad12) if (block.Name == "*Model_Space" || block.Name == "*Paper_Space") return; if (this.activeSection != StringCode.BlocksSection) { throw new InvalidDxfSectionException(this.activeSection, this.file); } this.WriteCodePair(0, block.CodeName); this.WriteCodePair(5, block.Handle); this.WriteCodePair(100, SubclassMarker.Entity); this.WriteCodePair(8, block.Layer); this.WriteCodePair(100, SubclassMarker.BlockBegin); this.WriteCodePair(2, block); //flags if (block.Attributes.Count == 0) { this.WriteCodePair(70, 0); } else { this.WriteCodePair(70, 2); } this.WriteCodePair(10, block.BasePoint.X); this.WriteCodePair(20, block.BasePoint.Y); this.WriteCodePair(30, block.BasePoint.Z); this.WriteCodePair(3, block); foreach (AttributeDefinition attdef in block.Attributes.Values) { this.WriteAttributeDefinition(attdef); } //block entities, if version is AutoCad12 we will write the converted entities this.isBlockEntities = true; foreach (IEntityObject entity in entityObjects) { this.WriteEntity(entity); } this.isBlockEntities = false; this.WriteBlockEnd(block.End); }