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 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(); }
private static void AppRegUsesAndRemove() { DxfDocument dxf = new DxfDocument(); List<PolylineVertex> vertexes = new List<PolylineVertex>{ new PolylineVertex(0, 0, 0), new PolylineVertex(10, 0, 10), new PolylineVertex(10, 10, 20), new PolylineVertex(0, 10, 30) }; Polyline poly = new Polyline(vertexes, true); XData xdata1 = new XData(new ApplicationRegistry("netDxf")); xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf")); poly.XData.Add(xdata1); dxf.AddEntity(poly); Line line = new Line(new Vector2(10, 5), new Vector2(-10, -5)); ApplicationRegistry myAppReg = new ApplicationRegistry("MyAppReg"); XData xdata2 = new XData(myAppReg); xdata2.XDataRecord.Add(new XDataRecord(XDataCode.Distance, Vector3.Distance(line.StartPoint, line.EndPoint))); line.XData.Add(xdata2); dxf.AddEntity(line); Circle circle = new Circle(Vector3.Zero, 15); XData xdata3 = new XData(myAppReg); xdata3.XDataRecord.Add(new XDataRecord(XDataCode.Real, circle.Radius)); circle.XData.Add(xdata3); dxf.AddEntity(circle); dxf.Save("appreg.dxf"); DxfDocument dxf2 = DxfDocument.Load("appreg.dxf"); // will return false the "MyAppReg" is in use bool ok; ok = dxf.ApplicationRegistries.Remove(myAppReg.Name); dxf.RemoveEntity(line); dxf.RemoveEntity(circle); // "MyAppReg" is not used anymore IList<DxfObject> uses = dxf.ApplicationRegistries.GetReferences(myAppReg.Name); // it is safe to delete it ok = dxf.ApplicationRegistries.Remove(myAppReg.Name); // we can even make a full cleanup dxf.ApplicationRegistries.Clear(); dxf.Save("appreg2.dxf"); }
private static void AddAndRemove() { Layer layer1 = new Layer("layer1") { Color = AciColor.Blue }; Layer layer2 = new Layer("layer2") { Color = AciColor.Green }; Line line = new Line(new Vector2(0, 0), new Vector2(10, 10)); line.Layer = layer1; Circle circle = new Circle(new Vector2(0, 0), 10); circle.Layer = layer2; 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("MyDimStyle"); myStyle.DIMPOST = "<>mm"; AlignedDimension dim1 = new AlignedDimension(p1, p2, offset, myStyle); //text TextStyle style = new TextStyle("MyTextStyle", "Arial.ttf"); Text text = new Text("Hello world!", Vector3.Zero, 10.0f, style) { Layer = new Layer("text") { Color = {Index = 8} } }; text.Alignment = TextAlignment.TopRight; HeaderVariables variables = new HeaderVariables { AcadVer = DxfVersion.AutoCad2004 }; DxfDocument dxf = new DxfDocument(); dxf.AddEntity(new EntityObject[] {line, circle, dim1, text}); dxf.Save("before remove.dxf"); dxf.RemoveEntity(circle); dxf.Save("after remove.dxf"); dxf.AddEntity(circle); dxf.Save("after remove and add.dxf"); dxf.RemoveEntity(dim1); dxf.Save("remove dim.dxf"); dxf.AddEntity(dim1); dxf.Save("add dim.dxf"); DxfDocument dxf2 = DxfDocument.Load("dim block names.dxf"); dxf2.AddEntity(dim1); dxf2.Save("dim block names2.dxf"); }
private static void MLineStyleUsesAndRemove() { DxfDocument dxf = new DxfDocument(); //MLineStyle style = MLineStyle.Default; //dxf.AddMLineStyle(style); List<Vector2> vertexes = new List<Vector2> { new Vector2(0, 0), new Vector2(0, 150), new Vector2(150, 150), new Vector2(150, 0) }; MLine mline = new MLine(vertexes); mline.Scale = 20; mline.Justification = MLineJustification.Zero; //mline.IsClosed = true; MLineStyle style = new MLineStyle("MyStyle", "Personalized style."); style.Elements.Add(new MLineStyleElement(0.25)); style.Elements.Add(new MLineStyleElement(-0.25)); // if we add new elements directly to the list we need to sort the list, style.Elements.Sort(); style.Flags = MLineStyleFlags.EndInnerArcsCap | MLineStyleFlags.EndRoundCap | MLineStyleFlags.StartInnerArcsCap | MLineStyleFlags.StartRoundCap; //style.StartAngle = 25.0; //style.EndAngle = 160.0; // AutoCad2000 dxf version does not support true colors for MLineStyle elements style.Elements[0].Color = new AciColor(180, 230, 147); mline.Style = style; // we have modified the mline after setting its vertexes so we need to manually call this method. // also when manually editting the vertex distances mline.Update(); // we can manually create cuts or gaps in the individual elements that made the multiline. // the cuts are defined as distances from the start point of the element along its direction. mline.Vertexes[0].Distances[0].Add(50); mline.Vertexes[0].Distances[0].Add(100); mline.Vertexes[0].Distances[mline.Style.Elements.Count - 1].Add(50); mline.Vertexes[0].Distances[mline.Style.Elements.Count - 1].Add(100); dxf.AddEntity(mline); dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2004; dxf.Save("MLine.dxf"); DxfDocument dxf2 = DxfDocument.Load("MLine.dxf"); // "MyStyle" is used only once List<DxfObject> uses; uses = dxf.MlineStyles.GetReferences(mline.Style.Name); // if we try to get the LineTypeUses, we will find out that "MyStyle" appears several times, // this is due to that each MLineStyleElement of a MLineStyle has an associated LineType uses = dxf.LineTypes.GetReferences(LineType.ByLayerName); bool ok; ok = dxf.RemoveEntity(mline); // "MyStyle" is not used its reference has been deleted uses = dxf.MlineStyles.GetReferences(mline.Style.Name); // we can safely remove it dxf.MlineStyles.Remove(mline.Style.Name); dxf.Save("MLine2.dxf"); dxf.Layers.Clear(); dxf.Save("MLine2.dxf"); }
private static void TextAndDimensionStyleUsesAndRemove() { DxfDocument dxf = new DxfDocument(); Layer layer1 = new Layer("Layer1"); layer1.Color = AciColor.Blue; layer1.LineType = LineType.Center; Layer layer2 = new Layer("Layer2"); layer2.Color = AciColor.Red; // blocks needs an special attention Layer layer3 = new Layer("Layer3"); layer3.Color = AciColor.Yellow; Circle circle = new Circle(Vector3.Zero, 15); // it is always recommended that all block entities will be located in layer 0, but this is up to the user. circle.Layer = new Layer("circle"); circle.Layer.Color = AciColor.Green; Block block = new Block("MyBlock"); block.Entities.Add(circle); AttributeDefinition attdef = new AttributeDefinition("NewAttribute"); block.AttributeDefinitions.Add(attdef); Insert insert = new Insert(block, new Vector2(5, 5)); insert.Attributes[0].Style = new TextStyle("Arial.ttf"); dxf.AddEntity(insert); dxf.Save("style.dxf"); DxfDocument dxf2; dxf2 = DxfDocument.Load("style.dxf"); dxf.RemoveEntity(circle); Vector3 p1 = new Vector3(0, 0, 0); Vector3 p2 = new Vector3(5, 5, 0); Line line = new Line(p1, p2); dxf.AddEntity(line); DimensionStyle myStyle = new DimensionStyle("MyStyle"); myStyle.DIMTXSTY = new TextStyle("Tahoma.ttf"); myStyle.DIMPOST = "<>mm"; myStyle.DIMDEC = 2; double offset = 7; LinearDimension dimX = new LinearDimension(line, offset, 0.0, myStyle); dimX.Rotation += 30.0; LinearDimension dimY = new LinearDimension(line, offset, 90.0, myStyle); dimY.Rotation += 30.0; dxf.AddEntity(dimX); dxf.AddEntity(dimY); dxf.Save("style2.dxf"); dxf2 = DxfDocument.Load("style2.dxf"); dxf.RemoveEntity(dimX); dxf.RemoveEntity(dimY); bool ok; // we can remove myStyle it was only referenced by dimX and dimY ok = dxf.DimensionStyles.Remove(myStyle.Name); // we cannot remove myStyle.TextStyle since it is in use by the internal blocks created by the dimension entities ok = dxf.Blocks.Remove(dimX.Block.Name); ok = dxf.Blocks.Remove(dimY.Block.Name); // no we can remove the unreferenced textStyle ok = dxf.TextStyles.Remove(myStyle.DIMTXSTY.Name); dxf.Save("style3.dxf"); dxf2 = DxfDocument.Load("style3.dxf"); }
private static void LayerAndLineTypesUsesAndRemove() { DxfDocument dxf = new DxfDocument(); Layer layer1 = new Layer("Layer1"); layer1.Color = AciColor.Blue; layer1.LineType = LineType.Center; Layer layer2 = new Layer("Layer2"); layer2.Color = AciColor.Red; LwPolyline poly = new LwPolyline(); poly.Vertexes.Add(new LwPolylineVertex(0, 0)); poly.Vertexes.Add(new LwPolylineVertex(10, 10)); poly.Vertexes.Add(new LwPolylineVertex(20, 0)); poly.Vertexes.Add(new LwPolylineVertex(30, 10)); poly.Layer = layer1; dxf.AddEntity(poly); Ellipse ellipse = new Ellipse(new Vector3(2, 2, 0), 5, 3); ellipse.Rotation = 30; ellipse.Layer = layer1; dxf.AddEntity(ellipse); Line line = new Line(new Vector2(10, 5), new Vector2(-10, -5)); line.Layer = layer2; line.LineType = LineType.DashDot; dxf.AddEntity(line); bool ok; // this will return false since layer1 is not empty ok = dxf.Layers.Remove(layer1.Name); List<DxfObject> entities = dxf.Layers.GetReferences(layer1.Name); foreach (DxfObject o in entities) { dxf.RemoveEntity(o as EntityObject); } // now this should return true since layer1 is empty ok = dxf.Layers.Remove(layer1.Name); // blocks needs an special attention Layer layer3 = new Layer("Layer3"); layer3.Color = AciColor.Yellow; Circle circle = new Circle(Vector3.Zero, 15); // it is always recommended that all block entities will be located in layer 0, but this is up to the user. circle.Layer = new Layer("circle"); circle.Layer.Color = AciColor.Green; Block block = new Block("MyBlock"); block.Entities.Add(circle); block.Layer = new Layer("blockLayer"); AttributeDefinition attdef = new AttributeDefinition("NewAttribute"); attdef.Layer = new Layer("attDefLayer"); attdef.LineType = LineType.Center; block.AttributeDefinitions.Add(attdef); Insert insert = new Insert(block, new Vector2(5, 5)); insert.Layer = layer3; insert.Attributes[0].Layer = new Layer("attLayer"); insert.Attributes[0].LineType = LineType.Dashed; dxf.AddEntity(insert); dxf.Save("test.dxf"); DxfDocument dxf2 = DxfDocument.Load("test.dxf"); // this list will contain the circle entity List<DxfObject> dxfObjects; dxfObjects = dxf.Layers.GetReferences("circle"); // but we cannot removed since it is part of a block ok = dxf.RemoveEntity(circle); // we need to remove first the block, but to do this we need to make sure there are no references of that block in the document dxfObjects = dxf.Blocks.GetReferences(block.Name); foreach (DxfObject o in dxfObjects) { dxf.RemoveEntity(o as EntityObject); } // now it is safe to remove the block since we do not have more references in the document ok = dxf.Blocks.Remove(block.Name); // now it is safe to remove the layer "circle", the circle entity was removed with the block since it was part of it ok = dxf.Layers.Remove("circle"); // purge all document layers, only empty layers will be removed dxf.Layers.Clear(); // purge all document line types, only line types without references will be removed dxf.LineTypes.Clear(); dxf.Save("test2.dxf"); }
private static void ImageUsesAndRemove() { ImageDefinition imageDef1 = new ImageDefinition("img\\image01.jpg"); Image image1 = new Image(imageDef1, Vector3.Zero, 10, 10); ImageDefinition imageDef2 = new ImageDefinition("img\\image02.jpg"); Image image2 = new Image(imageDef2, new Vector3(0, 220, 0), 10, 10); Image image3 = new Image(imageDef2, image2.Position + new Vector3(280, 0, 0), 10, 10); Block block =new Block("MyImageBlock"); block.Entities.Add(image1); Insert insert = new Insert(block); DxfDocument dxf = new DxfDocument(); dxf.AddEntity(insert); dxf.AddEntity(image2); dxf.AddEntity(image3); dxf.Save("test netDxf.dxf"); dxf.RemoveEntity(insert); dxf.Blocks.Remove(insert.Block.Name); // imageDef1 has no references in the document List<DxfObject> uses = dxf.ImageDefinitions.GetReferences(imageDef1.Name); dxf.Save("test netDxf with unreferenced imageDef.dxf"); dxf = DxfDocument.Load("test netDxf with unreferenced imageDef.dxf"); // once we have removed the insert and then the block that contained image1 we don't have more references to imageDef1 dxf.ImageDefinitions.Remove(imageDef1.Name); dxf.Save("test netDxf with deleted imageDef.dxf"); }
private static void PaperSpace() { // Sample on how to work with Layouts DxfDocument dxf = new DxfDocument(); // A new DxfDocument will create the default "Model" layout that is associated with the ModelSpace block. This layout cannot be erased or renamed. Line line = new Line(new Vector2(0), new Vector2(100)); // The line will be added to the "Model" layout since this is the active one by default. dxf.AddEntity(line); // Create a new Layout, all new layouts will be associated with different PaperSpace blocks, // while there can be only one ModelSpace multiple PaperSpace blocks might exist in the document Layout layout1 = new Layout("Layout1"); // When the layout is added to the list, a new PaperSpace block will be created automatically dxf.Layouts.Add(layout1); // Set this new Layout as the active one. All entities will now be added to this layout. dxf.ActiveLayout = layout1.Name; // Create a viewport, this is the window to the ModelSpace Viewport viewport1 = new Viewport { Width = 100, Height = 100, Center = new Vector3(50, 50, 0), }; // Add it to the "Layout1" since this is the active one dxf.AddEntity(viewport1); // Also add a circle Circle circle = new Circle(new Vector2(150), 25); dxf.AddEntity(circle); // Create a second Layout, add it to the list, and set it as the active one. Layout layout2 = new Layout("Layout2"); dxf.Layouts.Add(layout2); dxf.ActiveLayout = layout2.Name; // viewports might have a non rectangular boundary, in this case we will use an ellipse. Ellipse ellipse = new Ellipse(new Vector2(100), 200, 150); Viewport viewport2 = new Viewport { ClippingBoundary = ellipse, }; // Add the viewport to the document. This will also add the ellipse to the document. dxf.AddEntity(viewport2); Layout layout3 = new Layout("AnyName"); dxf.Layouts.Add(layout3); //layout can also be renamed layout3.Name = "Layout3"; //dxf.Layouts.Remove(layout2.Name); ShowDxfDocumentInformation(dxf); // Save the document as always. dxf.Save("PaperSpace.dxf"); #region CAUTION - This is subject to change in the future, use it with care // You cannot directly remove the ellipse from the document since it has been attached to a viewport bool ok = dxf.RemoveEntity(ellipse); // OK = false // If an entity has been attached to another, its reactor will point to its owner // This information is subject to change in the future to become a list, an entity can be attached to multiple objects; // but at the moment only the viewport clipping boundary make use of this. // This is the way AutoCad also handles hatch and dimension associativity, that I might implement in the future DxfObject reactor = ellipse.Reactors[0]; // in this case reactor points to viewport2 // You need to delete the viewport instead. This deletes the viewport and the ellipse //dxf.RemoveEntity(viewport2); // another way of deleting the ellipse, is first to assign another clipping boundary to the viewport or just set it to null viewport2.ClippingBoundary = null; // now it will be possible to delete the ellipse. This will not delete the viewport. ok = dxf.RemoveEntity(ellipse); // OK = true // Save the document if you want to test the changes dxf.Save("PaperSpace.dxf"); #endregion DxfDocument dxfLoad = DxfDocument.Load("PaperSpace.dxf"); // For every entity you can check its layout // The entity Owner will return the block to which it belongs, it can be a *Model_Space, *Paper_Space, ... or a common block if the entity is part of its geometry. // The block record stores information about the block and one of them is the layout, this mimics the way the dxf stores this information. // Remember only the internal blocks *Model_Space, *Paper_Space, *Paper_Space0, *Paper_Space1, ... have an associated layout, // all other blocks will return null is asked for block.Record.Layout Layout associatedLayout = dxfLoad.Lines[0].Owner.Record.Layout; // or you can get the complete list of entities of a layout foreach (Layout layout in dxfLoad.Layouts) { List<DxfObject> entities = dxfLoad.Layouts.GetReferences(layout.Name); } // You can also remove any layout from the list, except the "Model". // Remember all entities that has been added to this layout will also be removed. // This mimics the behavior in AutoCad, when a layout is deleted all entities in it will also be deleted. dxfLoad.Layouts.Remove(layout1.Name); Layout layout4 = (Layout) layout2.Clone("Layout4"); dxfLoad.Layouts.Add(layout4); ShowDxfDocumentInformation(dxfLoad); dxfLoad.Save("PaperSpace removed.dxf"); }