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.Int32, 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, CoordinateSystem.World, 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.Add(xdata); circle.XData.Add(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 LwPolylineVertex polyVertex; List<LwPolylineVertex> polyVertexes = new List<LwPolylineVertex>(); polyVertex = new LwPolylineVertex(new Vector2(-50, -50)); polyVertex.StartWidth = 2; polyVertexes.Add(polyVertex); polyVertex = new LwPolylineVertex(new Vector2(50, -50)); polyVertex.StartWidth = 1; polyVertexes.Add(polyVertex); polyVertex = new LwPolylineVertex(new Vector2(50, 50)); polyVertex.Bulge = 1; polyVertexes.Add(polyVertex); polyVertex = new LwPolylineVertex(new Vector2(-50, 50)); polyVertexes.Add(polyVertex); LwPolyline polyline2d = new LwPolyline(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 LwPolylineVertex lwVertex; List<LwPolylineVertex> lwVertexes = new List<LwPolylineVertex>(); lwVertex = new LwPolylineVertex(new Vector2(-25, -25)); lwVertex.StartWidth = 2; lwVertexes.Add(lwVertex); lwVertex = new LwPolylineVertex(new Vector2(25, -25)); lwVertex.StartWidth = 1; lwVertexes.Add(lwVertex); lwVertex = new LwPolylineVertex(new Vector2(25, 25)); lwVertex.Bulge = 1; lwVertexes.Add(lwVertex); lwVertex = new LwPolylineVertex(new Vector2(-25, 25)); lwVertexes.Add(lwVertex); LwPolyline lwPolyline = new LwPolyline(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); // polyfaceMesh List<PolyfaceMeshVertex> meshVertexes = new List<PolyfaceMeshVertex> { new PolyfaceMeshVertex(0, 0, 0), new PolyfaceMeshVertex(10, 0, 0), new PolyfaceMeshVertex(10, 10, 0), new PolyfaceMeshVertex(5, 15, 0), new PolyfaceMeshVertex(0, 10, 0) }; List<PolyfaceMeshFace> faces = new List<PolyfaceMeshFace> { new PolyfaceMeshFace(new short[] {1, 2, -3}), new PolyfaceMeshFace(new short[] {-1, 3, -4}), new PolyfaceMeshFace(new short[] {-1, 4, 5}) }; PolyfaceMesh mesh = new PolyfaceMesh(meshVertexes, faces); mesh.Layer = new Layer("polyfacemesh"); mesh.Layer.Color.Index = 104; dxf.AddEntity(mesh); //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 PolylineVertex vertex; List<PolylineVertex> vertexes = new List<PolylineVertex>(); vertex = new PolylineVertex(new Vector3(-50, -50, 0)); vertexes.Add(vertex); vertex = new PolylineVertex(new Vector3(50, -50, 10)); vertexes.Add(vertex); vertex = new PolylineVertex(new Vector3(50, 50, 25)); vertexes.Add(vertex); vertex = new PolylineVertex(new Vector3(-50, 50, 50)); vertexes.Add(vertex); Polyline polyline = new Polyline(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.DrawingVariables.AcadVer = DxfVersion.AutoCad2010; dxf.Save("AutoCad2010.dxf"); dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007; dxf.Save("AutoCad2007.dxf"); dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2004; dxf.Save("AutoCad2004.dxf"); dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2000; dxf.Save("AutoCad2000.dxf"); dxf = DxfDocument.Load("AutoCad2000.dxf"); dxf.Save("AutoCad2000 result.dxf"); }
/// <summary> /// Initializes a new instance of the <c>Insert</c> class. /// </summary> /// <param name="block">Insert block definition.</param> /// <param name="position">Insert <see cref="Vector3">point</see> in world coordinates.</param> public Insert(Block block, Vector3 position) : base(EntityType.Insert, DxfObjectCode.Insert) { if (block == null) throw new ArgumentNullException("block"); this.block = block; this.position = position; this.scale = new Vector3(1.0); this.rotation = 0.0; this.endSequence = new EndSequence(); List<Attribute> atts = new List<Attribute>(block.AttributeDefinitions.Count); foreach (AttributeDefinition attdef in block.AttributeDefinitions.Values) { Attribute att = new Attribute(attdef) { Position = attdef.Position + this.position - this.block.Position, Owner = this }; atts.Add(att); } this.attributes = new AttributeCollection(atts); }
protected virtual Block OnBlockChangeEvent(Block oldBlock, Block newBlock) { BlockChangeEventHandler ae = this.BlockChange; if (ae != null) { TableObjectChangeEventArgs<Block> eventArgs = new TableObjectChangeEventArgs<Block>(this.dimblk, newBlock); ae(this, eventArgs); return eventArgs.NewValue; } return newBlock; }
protected virtual Block OnDimensionBlockChangedEvent(Block oldBlock, Block newBlock) { DimensionBlockChangedEventHandler ae = this.DimensionBlockChanged; if (ae != null) { TableObjectChangedEventArgs<Block> eventArgs = new TableObjectChangedEventArgs<Block>(oldBlock, newBlock); ae(this, eventArgs); return eventArgs.NewValue; } return newBlock; }
private EntityObject ExportPath(Path path) { List <EntityObject> entities = new List <EntityObject>(); for (int i = 0; i < path.Curves.Length; i++) { EntityObject curve = GeoObjectToEntity(path.Curves[i] as IGeoObject); if (curve != null) { entities.Add(curve); } } netDxf.Blocks.Block block = new netDxf.Blocks.Block(GetNextAnonymousBlockName(), entities); doc.Blocks.Add(block); return(new netDxf.Entities.Insert(block)); }
/// <summary> /// Initializes a new instance of the <c>Dimension</c> class. /// </summary> protected Dimension(DimensionType type) : base(EntityType.Dimension, DxfObjectCode.Dimension) { this.definitionPoint = Vector3.Zero; this.midTextPoint = Vector3.Zero; this.dimensionType = type; this.attachmentPoint = MTextAttachmentPoint.MiddleCenter; this.lineSpacingStyle = MTextLineSpacingStyle.AtLeast; this.lineSpacing = 1.0; this.block = null; this.style = DimensionStyle.Default; this.userText = null; this.elevation = 0.0; this.styleOverrides = new DimensionStyleOverrideDictionary(); this.styleOverrides.BeforeAddItem += this.StyleOverrides_BeforeAddItem; this.styleOverrides.AddItem += this.StyleOverrides_AddItem; this.styleOverrides.BeforeRemoveItem += this.StyleOverrides_BeforeRemoveItem; this.styleOverrides.RemoveItem += this.StyleOverrides_RemoveItem; }
/// <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="Vector3d">point</see>.</param> public Insert(Block block, Vector3d insertionPoint) : base(DxfObjectCode.Insert) { if (block == null) throw new ArgumentNullException("block"); this.block = block; this.insertionPoint = insertionPoint; this.scale = new Vector3d(1.0f, 1.0f, 1.0f); this.rotation = 0.0f; this.normal = Vector3d.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 EntityObject ExportBlock(GeoObject.Block blk) { List <EntityObject> entities = new List <EntityObject>(); for (int i = 0; i < blk.Children.Count; i++) { EntityObject entity = GeoObjectToEntity(blk.Child(i)); if (entity != null) { entities.Add(entity); } } string name = blk.Name; if (name == null || doc.Blocks.Contains(name) || !TableObject.IsValidName(name)) { name = GetNextAnonymousBlockName(); } netDxf.Blocks.Block block = new netDxf.Blocks.Block(name, entities); doc.Blocks.Add(block); return(new netDxf.Entities.Insert(block)); }
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); }
private static void AlignedDimensionDrawing() { DxfDocument dxf = new DxfDocument(); double offset = -0.9; Vector3 p1 = new Vector3(1, 2, 0); Vector3 p2 = new Vector3(2, 6, 0); Line line1 = new Line(p1, p2); Vector3 l1; Vector3 l2; MathHelper.OffsetLine(line1.StartPoint, line1.EndPoint, line1.Normal, offset, out l1, out l2); DimensionStyle myStyle = new DimensionStyle("MyStyle"); myStyle.DIMPOST = "<>mm"; AlignedDimension dim1 = new AlignedDimension(p1, p2, offset, myStyle); Vector3 p3 = p1 + new Vector3(4, 0, 0); Vector3 p4 = p2 + new Vector3(4, 0, 0); Line line2 = new Line(p3, p4); AlignedDimension dim2 = new AlignedDimension(p3, p4, -offset, myStyle); Vector2 perp = Vector2.Perpendicular(new Vector2(p2.X, p2.Y) - new Vector2(p1.X, p1.Y)); //dim.Normal = -new Vector3(perp.X, perp.Y, 0.0) ; XData xdata = new XData(new ApplicationRegistry("other application")); xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf")); xdata.XDataRecord.Add(XDataRecord.OpenControlString); xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "Aligned Dimension")); xdata.XDataRecord.Add(new XDataRecord(XDataCode.Real, 15.5)); xdata.XDataRecord.Add(new XDataRecord(XDataCode.Int32, 350)); xdata.XDataRecord.Add(XDataRecord.CloseControlString); dim1.XData.Add(xdata); //dxf.AddEntity(line1); //dxf.AddEntity(line2); //dxf.AddEntity(dim1); //dxf.AddEntity(dim2); Block block = new Block("DimensionBlock"); block.Entities.Add(line1); block.Entities.Add(line2); block.Entities.Add(dim1); block.Entities.Add(dim2); Insert insert = new Insert(block); dxf.AddEntity(insert); dxf.Save("aligned dimension.dxf"); dxf = DxfDocument.Load("aligned dimension.dxf"); }
private static void WriteInsert() { // nested blocks DxfDocument dxf = new DxfDocument(); Block nestedBlock = new Block("Nested block"); nestedBlock.Entities.Add(new Line(new Vector3(-5, -5, 0), new Vector3(5, 5, 0))); nestedBlock.Entities.Add(new Line(new Vector3(5, -5, 0), new Vector3(-5, 5, 0))); Insert nestedInsert = new Insert(nestedBlock, new Vector3(0, 0, 0)); // the position will be relative to the position of the insert that nest it Circle circle = new Circle(Vector3.Zero, 5); circle.Layer = new Layer("circle"); circle.Layer.Color.Index = 2; Block block = new Block("MyBlock"); block.Entities.Add(circle); block.Entities.Add(nestedInsert); Insert insert = new Insert(block, new Vector3(5, 5, 5)); insert.Layer = new Layer("insert"); dxf.AddEntity(insert); dxf.Save("insert.dxf"); dxf = DxfDocument.Load("insert.dxf"); }
private Block ReadBlock(ref CodeValuePair code) { Layer layer = null; string name = string.Empty; string handle = string.Empty; Vector3d basePoint = Vector3d.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 = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: basePoint.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: basePoint.X = double.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; }
/// <summary> /// Initializes a new instance of the <c>Insert</c> class. /// </summary> /// <param name="block">Insert block definition.</param> /// <param name="position">Insert <see cref="Vector2">position</see> in world coordinates.</param> public Insert(Block block, Vector2 position) : this(block, new Vector3(position.X, position.Y, 0.0)) { }
private static void RemoveBlock() { DxfDocument dxf = new DxfDocument(); Block block = new Block("MyBlock"); Line line1 = new Line(new Vector3(-5, -5, 0), new Vector3(5, 5, 0)); Line line2 = new Line(new Vector3(5, -5, 0), new Vector3(-5, 5, 0)); block.Entities.Add(line1); block.Entities.Add(line2); Insert insert = new Insert(block); dxf.AddEntity(insert); bool ok; // line1 is used by block and cannot be removed (ok = false) ok = dxf.RemoveEntity(line1); // block is used by insert and cannot be removed (ok = false) ok = dxf.Blocks.Remove(block); // it is safe to remove insert, it doesn't belong to anybody (ok = true) ok = dxf.RemoveEntity(insert); // it is safe to remove block, it doesn't belong to anybody (ok = true) // at the same time, all entities that were part of the block have been also removed ok = dxf.Blocks.Remove(block); // obj is null the line1 does not exist in the document, the block was removed DxfObject obj = dxf.GetObjectByHandle(line1.Handle); Console.WriteLine("Press a key..."); Console.ReadKey(); }
public static void ModifyingGroups() { Line line1 = new Line(new Vector2(0, 0), new Vector2(100, 100)); line1.Color = AciColor.Red; Line line2 = new Line(new Vector2(100, 0), new Vector2(200, 100)); line2.Color = AciColor.Yellow; Line line3 = new Line(new Vector2(200, 0), new Vector2(300, 100)); line3.Color = AciColor.Magenta; DxfDocument doc = new DxfDocument(); Block blk = new Block("MyBlock"); blk.Entities.Add(line1); Insert ins = new Insert(blk); doc.AddEntity(ins); doc.AddEntity(line2); Layout layout = new Layout("Layout1"); doc.Layouts.Add(layout); doc.ActiveLayout = layout.Name; doc.AddEntity(line3); // group Group group = new Group("MyGroup"); doc.Groups.Add(group); // the Add method will also add the entities contained in a group to the document (in the active layout). doc.Groups.Add(group); // when the group belongs to a document, all entities must belong to the same document. // even if it does not sound very useful, a group can contain entities that belongs to different layouts and even blocks. group.Entities.Add(line1); group.Entities.Add(line2); group.Entities.Add(line3); Line line4 = new Line(new Vector2(300, 0), new Vector2(400, 100)); line4.Color = AciColor.Blue; // if a new entity, that does not belong to any document, is added to the group, it will be added to the group document active layout. doc.ActiveLayout = Layout.ModelSpaceName; group.Entities.Add(line4); Line line5 = new Line(new Vector2(400, 0), new Vector2(500, 100)); line5.Color = AciColor.Green; DxfDocument doc2 = new DxfDocument(); doc2.AddEntity(line5); // this is illegal, line5 belongs to another document. //group.Entities.Add(line5); // you need to clone the entity before adding it to the group. This is also the common practice to copy entities between documents. group.Entities.Add((EntityObject) line5.Clone()); // remember removing a group only deletes it from the collection not the entities //doc.Groups.Remove(group); doc.Save("group.dxf"); doc = DxfDocument.Load("group.dxf"); }
public static void ModifyingBlockProperties() { DxfDocument doc = new DxfDocument(); doc.DrawingVariables.InsUnits = DrawingUnits.Centimeters; Line existingLine = new Line(new Vector2(-10, 10), new Vector2(10, -10)); doc.AddEntity(existingLine); AttributeDefinition attDef4 = new AttributeDefinition("MyAttribute4"); attDef4.Value = "MyValue4"; attDef4.Alignment = TextAlignment.TopCenter; Block block = new Block("MyBlock", null, new List<AttributeDefinition>{attDef4}); block.Record.Units = DrawingUnits.Millimeters; // this is incorrect we cannot add an entity that belongs to a document when the block does not belong to anyone. //block.Entities.Add(existingLine); doc.Blocks.Add(block); // when the block and the entity that is being added belong to the same document, the entity will be removed from its current layout and added to the block // you cannot add an entity that belongs to a different document or block. Clone it instead. block.Entities.Add(existingLine); // now we can modify the block properties even if it has been already added to the document Line line = new Line(new Vector2(-10, -10), new Vector2(10, 10)); // when new entities that do not belong to anyone are added to an existing block, they will also be added to the document block.Entities.Add(line); DxfDocument doc2 = new DxfDocument(); Circle circle = new Circle(Vector2.Zero, 5); doc2.AddEntity(circle); // this is incorrect the circle already belongs to another document //block.Entities.Add(circle); // we need to clone it first Circle circle2 = (Circle) circle.Clone(); circle2.Radius = 2.5; block.Entities.Add(circle2); //you could also remove circle2 from doc2 and add it to the block doc2.RemoveEntity(circle); block.Entities.Add(circle); AttributeDefinition attDef = new AttributeDefinition("MyAttribute1"); attDef.Value = "MyValue1"; block.AttributeDefinitions.Add(attDef); // the same that is applicable to entities is also true to attribute definitions AttributeDefinition attDef2 = new AttributeDefinition("MyAttribute2"); attDef2.Value = "MyValue2"; attDef2.Alignment = TextAlignment.BaselineRight; block.AttributeDefinitions.Add(attDef2); Insert ins = new Insert(block); doc.AddEntity(ins); // if the insert has been added to a document, any new attribute definitions added to the block will not be reflected in the insert // this mimics the behavior in AutoCad AttributeDefinition attDef3 = new AttributeDefinition("MyAttribute3"); attDef3.Value = "MyValue3"; attDef3.Alignment = TextAlignment.TopCenter; block.AttributeDefinitions.Add(attDef3); ins.Rotation = 30; // to update the insert attributes call the method Sync, this method will also call the method TransformAttributes ins.Sync(); // the ins2 will have all three attributes Insert ins2 = new Insert(block, new Vector2(20,0)); doc.AddEntity(ins2); doc.Save("Test.dxf"); block.Name = "MyBlockRenamed"; doc.Save("BlockRename.dxf"); doc = Test("BlockRename.dxf"); }
private static void NestedBlock() { Block blockMM = new Block("BlockMM"); blockMM.Record.Units = DrawingUnits.Millimeters; AttributeDefinition attDefMM = new AttributeDefinition("MyAttributeMM"); attDefMM.Height = 1.0; attDefMM.Value = "This is block mm"; blockMM.AttributeDefinitions.Add(attDefMM); Line line1MM = new Line(Vector2.Zero, Vector2.UnitX); blockMM.Entities.Add(line1MM); Insert insMM = new Insert(blockMM); insMM.TransformAttributes(); Block blockCM = new Block("BlockCM"); blockCM.Record.Units = DrawingUnits.Centimeters; AttributeDefinition attDefCM = new AttributeDefinition("MyAttributeCM"); attDefCM.Height = 1.0; attDefCM.Value = "This is block cm"; blockCM.AttributeDefinitions.Add(attDefCM); Line line1CM = new Line(Vector2.Zero, Vector2.UnitY); blockCM.Entities.Add(line1CM); blockCM.Entities.Add(insMM); Insert insCM = new Insert(blockCM); DxfDocument doc = new DxfDocument(); doc.DrawingVariables.InsUnits = DrawingUnits.Meters; //doc.AddEntity(insMM); doc.AddEntity(insCM); doc.Save("test.dxf"); }
private static void WriteImage() { ImageDefinition imageDefinition = new ImageDefinition("img\\image01.jpg"); Image image = new Image(imageDefinition, Vector3.Zero, 10, 10); XData xdata1 = new XData(new ApplicationRegistry("netDxf")); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "xData image position")); xdata1.XDataRecord.Add(XDataRecord.OpenControlString); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, image.Position.X)); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, image.Position.Y)); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, image.Position.Z)); xdata1.XDataRecord.Add(XDataRecord.CloseControlString); image.XData.Add(xdata1); //image.Normal = new Vector3(1, 1, 1); //image.Rotation = 30; // you can pass a name that must be unique for the image definiton, by default it will use the file name without the extension ImageDefinition imageDef2 = new ImageDefinition("img\\image02.jpg", "MyImage"); Image image2 = new Image(imageDef2, new Vector3(0, 150, 0), 10, 10); Image image3 = new Image(imageDef2, new Vector3(150, 150, 0), 10, 10); // clipping boundary definition in local coordinates ClippingBoundary clip = new ClippingBoundary(100, 100, 500, 300); image.ClippingBoundary = clip; // set to null to restore the default clipping boundary (full image) image.ClippingBoundary = null; // images can be part of a block definition Block block = new Block("ImageBlock"); block.Entities.Add(image2); block.Entities.Add(image3); Insert insert = new Insert(block, new Vector3(0, 100, 0)); DxfDocument dxf = new DxfDocument(); dxf.AddEntity(image); //dxf.AddEntity(image2); //dxf.AddEntity(image3); dxf.AddEntity(insert); dxf.Save("image.dxf"); dxf = DxfDocument.Load("image.dxf"); dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010; dxf.Save("test.dxf"); //dxf.RemoveEntity(image2); //dxf.Save("image2.dxf"); //dxf.RemoveEntity(image3); //dxf.RemoveEntity(image); //dxf.Save("image3.dxf"); }
private static void WriteNestedInsert() { // nested blocks DxfDocument dxf = new DxfDocument(); Block nestedBlock = new Block("Nested block"); Circle circle = new Circle(Vector3.Zero, 5); circle.Layer = new Layer("circle"); circle.Layer.Color.Index = 2; nestedBlock.Entities.Add(circle); AttributeDefinition attdef = new AttributeDefinition("NewAttribute"); attdef.Prompt = "InfoText"; attdef.Alignment = TextAlignment.MiddleCenter; nestedBlock.AttributeDefinitions.Add(attdef); Insert nestedInsert = new Insert(nestedBlock, new Vector3(0, 0, 0)); // the position will be relative to the position of the insert that nest it nestedInsert.Attributes[0].Value = 24; Insert nestedInsert2 = new Insert(nestedBlock, new Vector3(-20, 0, 0)); // the position will be relative to the position of the insert that nest it nestedInsert2.Attributes[0].Value = -20; Block block = new Block("MyBlock"); block.Entities.Add(new Line(new Vector3(-5, -5, 0), new Vector3(5, 5, 0))); block.Entities.Add(new Line(new Vector3(5, -5, 0), new Vector3(-5, 5, 0))); block.Entities.Add(nestedInsert); block.Entities.Add(nestedInsert2); Insert insert = new Insert(block, new Vector3(5, 5, 5)); insert.Layer = new Layer("insert"); dxf.AddEntity(insert); //dxf.AddEntity(circle); // this is not allowed the circle is already part of a block dxf.Save("nested insert.dxf"); dxf = DxfDocument.Load("nested insert.dxf"); dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010; dxf.Save("nested insert copy.dxf"); }
private void WriteBlock(Block block, Layout layout) { Debug.Assert(this.activeSection == DxfObjectCode.BlocksSection); string name = this.EncodeNonAsciiCharacters(block.Name); string blockLayer = this.EncodeNonAsciiCharacters(block.Layer.Name); this.chunk.Write(0, block.CodeName); this.chunk.Write(5, block.Handle); this.chunk.Write(330, block.Owner.Handle); this.chunk.Write(100, SubclassMarker.Entity); if (layout != null) this.chunk.Write(67, layout.IsPaperSpace ? (short) 1 : (short) 0); this.chunk.Write(8, blockLayer); this.chunk.Write(100, SubclassMarker.BlockBegin); if (block.IsXRef) this.chunk.Write(1, this.EncodeNonAsciiCharacters(block.XrefFile)); this.chunk.Write(2, name); this.chunk.Write(70, (short)block.Flags); this.chunk.Write(10, block.Origin.X); this.chunk.Write(20, block.Origin.Y); this.chunk.Write(30, block.Origin.Z); this.chunk.Write(3, name); foreach (AttributeDefinition attdef in block.AttributeDefinitions.Values) { this.WriteEntityCommonCodes(attdef, null); this.WriteAttributeDefinition(attdef); } if (layout == null) { foreach (EntityObject entity in block.Entities) { this.WriteEntity(entity, null); } } else { this.WriteEntity(layout.Viewport, layout); List<DxfObject> entities = this.doc.Layouts.GetReferences(layout); foreach (DxfObject entity in entities) { this.WriteEntity(entity as EntityObject, layout); } } // EndBlock entity this.chunk.Write(0, block.End.CodeName); this.chunk.Write(5, block.End.Handle); this.chunk.Write(330, block.Owner.Handle); this.chunk.Write(100, SubclassMarker.Entity); this.chunk.Write(8, blockLayer); this.chunk.Write(100, SubclassMarker.BlockEnd); }
/// <summary> /// Initializes a new instance of the <c>Insert</c> class. /// </summary> /// <param name="block">Insert <see cref="Block">block definition</see>.</param> public Insert(Block block) : this(block, Vector3.Zero) { }
private Attribute ReadAttribute(Block block, bool isBlockEntity = false) { string handle = null; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; Linetype linetype = Linetype.ByLayer; Lineweight lineweight = Lineweight.ByLayer; double linetypeScale = 1.0; bool isVisible = true; Transparency transparency = Transparency.ByLayer; AttributeFlags flags = AttributeFlags.Visible; Vector3 firstAlignmentPoint = Vector3.Zero; Vector3 secondAlignmentPoint = Vector3.Zero; TextStyle style = TextStyle.Default; double height = 0.0; double widthFactor = 0.0; double obliqueAngle = 0.0; short horizontalAlignment = 0; short verticalAlignment = 0; double rotation = 0.0; Vector3 normal = Vector3.UnitZ; // DxfObject codes this.chunk.Next(); while (this.chunk.Code != 100) { switch (this.chunk.Code) { case 0: throw new Exception(string.Format("Premature end of entity {0} definition.", DxfObjectCode.Attribute)); case 5: handle = this.chunk.ReadHex(); this.chunk.Next(); break; default: this.chunk.Next(); break; } } // AcDbEntity common codes this.chunk.Next(); while (this.chunk.Code != 100) { switch (this.chunk.Code) { case 0: throw new Exception(string.Format("Premature end of entity {0} definition.", DxfObjectCode.Attribute)); case 8: // layer code string layerName = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); layer = this.GetLayer(layerName); this.chunk.Next(); break; case 62: // ACI color code if (!color.UseTrueColor) color = AciColor.FromCadIndex(this.chunk.ReadShort()); this.chunk.Next(); break; case 440: //transparency transparency = Transparency.FromAlphaValue(this.chunk.ReadInt()); this.chunk.Next(); break; case 420: // the entity uses true color color = AciColor.FromTrueColor(this.chunk.ReadInt()); this.chunk.Next(); break; case 6: // type line code string linetypeName = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); linetype = this.GetLinetype(linetypeName); this.chunk.Next(); break; case 370: // line weight code lineweight = (Lineweight) this.chunk.ReadShort(); this.chunk.Next(); break; case 48: // line type scale linetypeScale = this.chunk.ReadDouble(); this.chunk.Next(); break; case 60: //object visibility isVisible = this.chunk.ReadShort() == 0; this.chunk.Next(); break; default: this.chunk.Next(); break; } } string atttag = null; AttributeDefinition attdef = null; object value = null; this.chunk.Next(); while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 2: atttag = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); // seems that some programs might export insert entities with attributes which definitions are not defined in the block // if it is not present the insert attribute will have a null definition if (!isBlockEntity) block.AttributeDefinitions.TryGetValue(atttag, out attdef); this.chunk.Next(); break; case 1: value = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); this.chunk.Next(); break; case 70: flags = (AttributeFlags) this.chunk.ReadShort(); this.chunk.Next(); break; case 10: firstAlignmentPoint.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 20: firstAlignmentPoint.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 30: firstAlignmentPoint.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 11: secondAlignmentPoint.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 21: secondAlignmentPoint.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 31: secondAlignmentPoint.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 7: string styleName = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); style = this.GetTextStyle(styleName); this.chunk.Next(); break; case 40: height = this.chunk.ReadDouble(); this.chunk.Next(); break; case 41: widthFactor = this.chunk.ReadDouble(); this.chunk.Next(); break; case 50: rotation = this.chunk.ReadDouble(); this.chunk.Next(); break; case 51: obliqueAngle = MathHelper.NormalizeAngle(this.chunk.ReadDouble()); if (obliqueAngle > 180) obliqueAngle -= 360; if (obliqueAngle < -85.0 || obliqueAngle > 85.0) obliqueAngle = 0.0; this.chunk.Next(); break; case 72: horizontalAlignment = this.chunk.ReadShort(); this.chunk.Next(); break; case 74: verticalAlignment = this.chunk.ReadShort(); this.chunk.Next(); break; case 210: normal.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 220: normal.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 230: normal.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; default: this.chunk.Next(); break; } } TextAlignment alignment = ObtainAlignment(horizontalAlignment, verticalAlignment); Vector3 ocsBasePoint = alignment == TextAlignment.BaselineLeft ? firstAlignmentPoint : secondAlignmentPoint; Vector3 wcsBasePoint = MathHelper.Transform(ocsBasePoint, normal, CoordinateSystem.Object, CoordinateSystem.World); return new Attribute { Handle = handle, Color = color, Layer = layer, Linetype = linetype, Lineweight = lineweight, LinetypeScale = linetypeScale, Transparency = transparency, IsVisible = isVisible, Definition = attdef, Tag = atttag, Position = wcsBasePoint, Normal = normal, Alignment = alignment, Value = value, Flags = flags, Style = style, Height = height, WidthFactor = MathHelper.IsZero(widthFactor) ? style.WidthFactor : widthFactor, ObliqueAngle = obliqueAngle, Rotation = rotation }; }
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 Block ReadBlock() { Debug.Assert(this.chunk.ReadString() == DxfObjectCode.BeginBlock); BlockRecord blockRecord; Layer layer = Layer.Default; string name = string.Empty; string handle = string.Empty; string xrefFile = string.Empty; BlockTypeFlags type = BlockTypeFlags.None; Vector3 basePoint = Vector3.Zero; List<EntityObject> entities = new List<EntityObject>(); List<AttributeDefinition> attDefs = new List<AttributeDefinition>(); this.chunk.Next(); while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 1: xrefFile = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); this.chunk.Next(); break; case 5: handle = this.chunk.ReadHex(); this.chunk.Next(); break; case 8: string layerName = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); layer = this.GetLayer(layerName); this.chunk.Next(); break; case 2: name = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString()); this.chunk.Next(); break; case 70: type = (BlockTypeFlags) this.chunk.ReadShort(); this.chunk.Next(); break; case 10: basePoint.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 20: basePoint.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 30: basePoint.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 3: //I don't know the reason of these duplicity since code 2 also contains the block name //The program EASE exports code 3 with an empty string (use it or don't use it but do NOT mix information) //name = dxfPairInfo.Value; this.chunk.Next(); break; default: this.chunk.Next(); break; } } // read block entities while (this.chunk.ReadString() != DxfObjectCode.EndBlock) { EntityObject entity = this.ReadEntity(true); if (entity != null) { AttributeDefinition attDef = entity as AttributeDefinition; if (attDef != null) attDefs.Add(attDef); else entities.Add(entity); } } // read the end block object until a new element is found this.chunk.Next(); string endBlockHandle = string.Empty; while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 5: endBlockHandle = this.chunk.ReadHex(); this.chunk.Next(); break; case 8: // the EndBlock layer and the Block layer are the same this.chunk.Next(); break; default: this.chunk.Next(); break; } } if (!this.blockRecords.TryGetValue(name, out blockRecord)) throw new Exception(string.Format("The block record {0} is not defined.", name)); Block block; if (type.HasFlag(BlockTypeFlags.XRef)) { block = new Block(name, xrefFile) { Handle = handle, Owner = blockRecord, Origin = basePoint, Layer = layer, Flags = type, }; } else { block = new Block(name, false, null, null) { Handle = handle, Owner = blockRecord, Origin = basePoint, Layer = layer, Flags = type, }; } block.End.Handle = endBlockHandle; if (name.StartsWith(Block.DefaultPaperSpaceName, StringComparison.OrdinalIgnoreCase)) { // the dxf is not consistent with the way they handle entities that belong to different paper spaces. // While the entities of *Paper_Space block are stored in the ENTITIES section as the *Model_Space, // the list of entities in *Paper_Space# are stored in the block definition itself. // As all this entities do not need an insert entity to have a visual representation, // they will be stored in the global entities lists together with the rest of the entities of *Model_Space and *Paper_Space foreach (EntityObject entity in entities) this.entityList.Add(entity, blockRecord.Handle); // this kind of blocks do not store attribute definitions } else { // add attribute definitions foreach (AttributeDefinition attDef in attDefs) { // AutoCAD allows duplicate tags in attribute definitions, but this library does not having duplicate tags is not recommended in any way, // since there will be no way to know which is the definition associated to the insert attribute if (!block.AttributeDefinitions.ContainsTag(attDef.Tag)) block.AttributeDefinitions.Add(attDef); } // block entities for post processing (MLines and Images references other objects (MLineStyle and ImageDefinition) that will be defined later this.blockEntities.Add(block, entities); } return block; }
/// <summary> /// Rebuilds the block definition of the actual dimension. /// </summary> /// <remarks> /// This method needs to be manually called to reflect any change made to the dimension style.<br /> /// The dimension must belong to a document. /// </remarks> public void Update() { if (this.block == null) throw new ArgumentException("The dimension does not belong to a document."); Block newBlock = this.BuildBlock(this.block.Name); this.block = this.OnDimensionBlockChangedEvent(this.block, newBlock); }
/// <summary> /// Creates a block from the content of a <see cref="DxfDocument">document</see>. /// </summary> /// <param name="doc">A <see cref="DxfDocument">DxfDocument</see> instance.</param> /// <param name="name">Name of the new block.</param> /// <returns>The block build from the <see cref="DxfDocument">document</see> content.</returns> /// <remarks>Only the entities contained in ModelSpace will make part of the block.</remarks> public static Block Create(DxfDocument doc, string name) { Block block = new Block(name); block.position = doc.DrawingVariables.InsBase; block.Record.Units = doc.DrawingVariables.InsUnits; List<DxfObject> entities = doc.Layouts.GetReferences(Layout.ModelSpaceName); foreach (DxfObject dxfObject in entities) { Object clone = ((EntityObject)dxfObject).Clone(); AttributeDefinition attdef = clone as AttributeDefinition; if (attdef != null) block.AttributeDefinitions.Add(attdef); EntityObject entity = clone as EntityObject; if (entity != null) block.Entities.Add(entity); } return block; }
/// <summary> /// Initializes a new instance of the <c>Dimension</c> class. /// </summary> protected Dimension(DimensionType type) : base(EntityType.Dimension, DxfObjectCode.Dimension) { this.definitionPoint = Vector3.Zero; this.midTextPoint = Vector3.Zero; this.dimensionType = type; this.attachmentPoint = MTextAttachmentPoint.MiddleCenter; this.lineSpacingStyle = MTextLineSpacingStyle.AtLeast; this.lineSpacing = 1.0; this.block = null; this.style = DimensionStyle.Default; this.userText = null; }
private static void WriteNoInsertBlock() { Line line = new Line(new Vector3(0, 0, 0), new Vector3(100, 100, 0)); Line line2 = new Line(new Vector3(0, 100, 0), new Vector3(100, 0, 0)); // create the block definition Block block = new Block("MyBlock"); // add the entities that you want in the block block.Entities.Add(line); block.Entities.Add(line2); // create the document DxfDocument dxf = new DxfDocument(); // add the block definition to the block table list (this is the function that was private in earlier versions, check the changelog.txt) dxf.Blocks.Add(block); // and save file, no visible entities will appear if you try to open the drawing but the block will be there dxf.Save("Block definiton.dxf"); }
/// <summary> /// Rebuilds the block definition of the actual dimension. /// </summary> /// <remarks> /// This method needs to be manually called to reflect any change made to the dimension style.<br /> /// The dimension must belong to a document. /// </remarks> public void Update() { if (this.block == null) return; Block newBlock = this.BuildBlock(this.block.Name); this.block = this.OnDimensionBlockChangedEvent(this.block, newBlock); }
private static void Angular2LineDimensionDrawing() { double offset = 7.5; Line line1 = new Line(new Vector2(1, 2), new Vector2(6, 0)); Line line2 = new Line(new Vector2(2, 1), new Vector2(4,5)); Angular2LineDimension dim = new Angular2LineDimension(line1, line2, offset); DxfDocument dxf = new DxfDocument(); //dxf.AddEntity(line1); //dxf.AddEntity(line2); //dxf.AddEntity(dim); Block block = new Block("DimensionBlock"); block.Entities.Add(line1); block.Entities.Add(line2); block.Entities.Add(dim); Insert insert = new Insert(block); dxf.AddEntity(insert); dxf.Save("angular 2 line dimension.dxf"); dxf = DxfDocument.Load("angular 2 line dimension.dxf"); dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010; dxf.Save("angular 2 line dimension.dxf"); }
/// <summary> /// Creates a new Block that is a copy of the current instance. /// </summary> /// <param name="newName">Block name of the copy.</param> /// <returns>A new Block that is a copy of this instance.</returns> public override TableObject Clone(string newName) { Block copy = new Block(newName) { Description = this.description, Flags = this.flags, Layer = (Layer)this.layer.Clone(), Position = this.position }; foreach (EntityObject e in this.entities) { copy.entities.Add((EntityObject)e.Clone()); } foreach (AttributeDefinition a in this.attributes.Values) { copy.attributes.Add((AttributeDefinition) a.Clone()); } return copy; }
internal DimensionStyle(string name, bool checkName) : base(name, DxfObjectCode.DimStyle, checkName) { if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name), "The dimension style name should be at least one character long."); this.reserved = name.Equals(DefaultName, StringComparison.OrdinalIgnoreCase); // dimension lines this.dimclrd = AciColor.ByBlock; this.dimltype = LineType.ByBlock; this.dimlwd = Lineweight.ByBlock; this.dimdli = 0.38; this.dimdle = 0.0; // extension lines this.dimclre = AciColor.ByBlock; this.dimltex1 = LineType.ByBlock; this.dimltex2 = LineType.ByBlock; this.dimlwe = Lineweight.ByBlock; this.dimse1 = false; this.dimse2 = false; this.dimexo = 0.0625; this.dimexe = 0.18; // symbols and arrows this.dimasz = 0.18; this.dimcen = 0.09; this.dimsah = false; this.dimldrblk = null; this.dimblk = null; this.dimblk1 = null; this.dimblk2 = null; // text this.dimtxsty = TextStyle.Default; this.dimclrt = AciColor.ByBlock; this.dimtxt = 0.18; this.dimtad = 1; this.dimjust = 0; this.dimgap = 0.09; // fit this.dimscale = 1.0; // primary units this.dimdec = 2; this.dimadec = 0; this.dimpost = "<>"; this.dimtih = 0; this.dimtoh = 0; this.dimdsep = '.'; this.dimlfac = 1.0; this.dimaunit = AngleUnitType.DecimalDegrees; this.dimlunit = LinearUnitType.Decimal; this.dimfrac = FractionFormatType.Horizontal; this.suppressLinearLeadingZeros = false; this.suppressLinearTrailingZeros = false; this.suppressAngularLeadingZeros = false; this.suppressAngularTrailingZeros = false; this.suppressZeroFeet = true; this.suppressZeroInches = true; this.dimrnd = 0.0; }