Пример #1
3
        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");
        }
Пример #2
2
        private static void AssociativeHatches()
        {
            DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010);

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            // optionally you can the normal of the polyline, by default it is the UnitZ vector
            //poly.Normal = new Vector3(1.0);
            poly.IsClosed = true;


            HatchBoundaryPath boundary = new HatchBoundaryPath(new List<EntityObject> { poly });
            HatchPattern pattern = HatchPattern.Line;
            pattern.Scale = 10;
            pattern.Angle = 45;

            // the hatch boundary can be set in the hatch constructor or it can be added later
            //Hatch hatch = new Hatch(pattern, new[]{boundary}, true);

            Hatch hatch = new Hatch(pattern, true);
            // you will need to manually set the hatch normal to the boundary normal if it is not the UnitZ,
            // to work properly all boundary entities must belong to the same plane
            //hatch.Normal = poly.Normal;

            // the hatch boundary can be set in the hatch constructor or it can be added later, remember hatches with no boundaries will not be saved
            hatch.BoundaryPaths.Add(boundary);
            Circle circle = new Circle(Vector2.Zero, 5);
            // all boundary entities should have the same normal, by default it is the UnitZ
            // the hatch will not handle the normals of the different boundary path, you will have to make sure they all lay on the same plane
            //circle.Normal = poly.Normal;

            hatch.BoundaryPaths.Add(new HatchBoundaryPath(new List<EntityObject> { circle }));
            // when an associative hatch is added to a document the referenced boundary entities will be added too
            dxf.AddEntity(hatch);
            dxf.Save("Hatch.dxf");


            DxfDocument dxf2 = DxfDocument.Load("Hatch.dxf");
            // you can remove boundaries from a hatch
            dxf2.Hatches[0].BoundaryPaths.Remove(dxf2.Hatches[0].BoundaryPaths[1]);
            // and add new ones
            LwPolyline p = new LwPolyline();
            p.Vertexes.Add(new LwPolylineVertex(-20, -20));
            p.Vertexes.Add(new LwPolylineVertex(20, -20));
            p.Vertexes.Add(new LwPolylineVertex(20, 20));
            p.Vertexes.Add(new LwPolylineVertex(-20, 20));
            p.IsClosed = true;
            dxf2.Hatches[0].BoundaryPaths.Add(new HatchBoundaryPath(new List<EntityObject> { p }));
            dxf2.Save("Hatch add and remove boundaries.dxf");


            DxfDocument dxf3 = DxfDocument.Load("Hatch.dxf");
            // unlinking the boundary entities from a hatch will not automatically remove them from the document, you can use the returned list to delete them
            // unlinking the boundary will make the hatch non-associative 
            List<EntityObject> oldBoundary = dxf3.Hatches[0].UnLinkBoundary();
            dxf3.RemoveEntity(oldBoundary);

            // we can recreate the hatch boundary and optionally linking it, thus making it associative,
            // if the hatch is associative and belongs to a document the new entities will also be automatically added to the same document
            List<EntityObject> newBoundary = dxf3.Hatches[0].CreateBoundary(true);

            dxf3.Save("Hatch new contour.dxf");

            DxfDocument dxf4 = DxfDocument.Load("Hatch.dxf");
            // if the hatch is associative, it is possible to modify the entities that make the boundary
            // for non-associative the list of entities will contain zero items
            if (dxf4.Hatches[0].Associative)
            {
                // this will only work for associative hatches
                HatchBoundaryPath path = dxf4.Hatches[0].BoundaryPaths[0];
                LwPolyline entity = (LwPolyline) path.Entities[0];
                entity.Vertexes[2].Position = new Vector2(15, 15);
                // after modifying the boundary entities, it is necessary to rebuild the edges
                path.Update();
                dxf4.Save("Hatch change boundary.dxf");
            }
        }
Пример #3
1
        private static void DimensionNestedBlock()
        {
            DxfDocument dxf = new DxfDocument();

            Vector3 p1 = new Vector3(0, 0, 0);
            Vector3 p2 = new Vector3(5, 5, 0);
            Line line = new Line(p1, p2);

            DimensionStyle myStyle = new DimensionStyle("MyStyle");
            myStyle.DIMPOST = "<>mm";
            myStyle.DIMDEC = 2;
            LinearDimension dim = new LinearDimension(line, 7, 0.0, myStyle);

            Block nestedBlock = new Block("NestedBlock");
            nestedBlock.Entities.Add(line);
            Insert nestedIns = new Insert(nestedBlock);

            Block block = new Block("MyBlock");
            block.Entities.Add(dim);
            block.Entities.Add(nestedIns);

            Insert ins = new Insert(block);
            ins.Position = new Vector3(10, 10, 0);
            dxf.AddEntity(ins);

            Circle circle = new Circle(p2, 5);
            Block block2 = new Block("MyBlock2");
            block2.Entities.Add(circle);

            Insert ins2 = new Insert(block2);
            ins2.Position = new Vector3(-10, -10, 0);
            dxf.AddEntity(ins2);

            Block block3 = new Block("MyBlock3");
            block3.Entities.Add((EntityObject)ins.Clone());
            block3.Entities.Add((EntityObject)ins2.Clone());

            Insert ins3 = new Insert(block3);
            ins3.Position = new Vector3(-10, 10, 0);
            dxf.AddEntity(ins3);

            dxf.Save("nested blocks.dxf");

            dxf = DxfDocument.Load("nested blocks.dxf");

            dxf.Save("nested blocks.dxf");
        }
Пример #4
0
    void AddCircle(netDxf.Entities.Circle c)
    {
        var          ce     = c.Center;
        CircleEntity circle = new CircleEntity(DetailEditor.instance.currentSketch.GetSketch());

        circle.c.SetPosition(new UnityEngine.Vector3((float)ce.X, (float)ce.Y, (float)ce.Z));
        circle.radius.value = c.Radius;
    }
Пример #5
0
        void ReadCircle(netDxf.Entities.Circle circle, double x, double y)
        {
            var figures = new List <IFigure>();

            figures.Add(CreateHiddenPoint(circle.Center.X + x, circle.Center.Y + y));
            figures.Add(CreateHiddenPoint(circle.Center.X + x + circle.Radius, circle.Center.Y + y));

            var figure = Factory.CreateCircleByRadius(drawing, figures);
        }
Пример #6
0
 private EntityObject ExportEllipse(GeoObject.Ellipse elli)
 {
     netDxf.Entities.EntityObject entity = null;
     if (elli.IsArc)
     {
         Plane dxfPlane;
         if (elli.CounterClockWise)
         {
             dxfPlane = Import.Plane(Vector3(elli.Center), Vector3(elli.Plane.Normal));
         }
         else
         {
             dxfPlane = Import.Plane(Vector3(elli.Center), Vector3(-elli.Plane.Normal));
         }
         if (elli.IsCircle)
         {
             GeoObject.Ellipse aligned = GeoObject.Ellipse.Construct();
             aligned.SetArcPlaneCenterStartEndPoint(dxfPlane, dxfPlane.Project(elli.Center), dxfPlane.Project(elli.StartPoint), dxfPlane.Project(elli.EndPoint), dxfPlane, true);
             entity        = new netDxf.Entities.Arc(Vector3(aligned.Center), aligned.Radius, aligned.StartParameter / Math.PI * 180, (aligned.StartParameter + aligned.SweepParameter) / Math.PI * 180);
             entity.Normal = Vector3(dxfPlane.Normal);
         }
         else
         {
             netDxf.Entities.Ellipse expelli = new netDxf.Entities.Ellipse(Vector3(elli.Center), 2 * elli.MajorRadius, 2 * elli.MinorRadius);
             entity        = expelli;
             entity.Normal = Vector3(elli.Plane.Normal);
             Plane       cdbplane = elli.Plane;
             GeoVector2D dir      = dxfPlane.Project(cdbplane.DirectionX);
             SweepAngle  rot      = new SweepAngle(GeoVector2D.XAxis, dir);
             expelli.Rotation = rot.Degree;
             SetEllipseParameters(expelli, elli.StartParameter, elli.StartParameter + elli.SweepParameter);
         }
     }
     else
     {
         if (elli.IsCircle)
         {
             entity        = new netDxf.Entities.Circle(Vector3(elli.Center), elli.Radius);
             entity.Normal = Vector3(elli.Plane.Normal);
         }
         else
         {
             netDxf.Entities.Ellipse expelli = new netDxf.Entities.Ellipse(Vector3(elli.Center), 2 * elli.MajorRadius, 2 * elli.MinorRadius);
             entity        = expelli;
             entity.Normal = Vector3(elli.Plane.Normal);
             Plane       dxfplane = Import.Plane(expelli.Center, expelli.Normal); // this plane is not correct, it has to be rotated
             Plane       cdbplane = elli.Plane;
             GeoVector2D dir      = dxfplane.Project(cdbplane.DirectionX);
             SweepAngle  rot      = new SweepAngle(GeoVector2D.XAxis, dir);
             expelli.Rotation = rot.Degree;
         }
     }
     return(entity);
 }
Пример #7
0
        public Circle(netDxf.Entities.Circle inputCircle)
        {
            MaxXMaxY    = new Vector3();
            MinXMinY    = new Vector3();
            InputCircle = inputCircle;
            MinXMinY.X  = inputCircle.Center.X - inputCircle.Radius;
            MinXMinY.Y  = inputCircle.Center.Y - inputCircle.Radius;
            MaxXMaxY.X  = inputCircle.Center.X + inputCircle.Radius;
            MaxXMaxY.Y  = inputCircle.Center.Y + inputCircle.Radius;

            Length = 2 * Math.PI * inputCircle.Radius;
        }
Пример #8
0
        public System.Windows.Shapes.Ellipse GetCanvasCircle(netDxf.Entities.Circle circle, Vector3 startVector,
                                                             double canvasHeight)
        {
            var tempEllipse = new System.Windows.Shapes.Ellipse();

            tempEllipse.Height = 2 * circle.Radius;
            tempEllipse.Width  = 2 * circle.Radius;

            tempEllipse.StrokeThickness = 1;
            tempEllipse.Stroke          = Brushes.Black;
            tempEllipse.SetValue(Canvas.LeftProperty, -InputCircle.Radius + InputCircle.Center.X - startVector.X);
            tempEllipse.SetValue(Canvas.TopProperty,
                                 -InputCircle.Radius - InputCircle.Center.Y + canvasHeight + startVector.Y);
            return(tempEllipse);
        }
Пример #9
0
 /// <summary>
 /// Convert a Nucleus arc to a netDXF one
 /// </summary>
 /// <param name="arc"></param>
 /// <returns></returns>
 public static nDE.EntityObject Convert(Arc arc)
 {
     if (arc.Closed)
     {
         var result = new nDE.Circle(Convert(arc.Circle.Origin), arc.Circle.Radius * ConversionScaling);
         result.Normal = Convert(arc.Circle.L, 1);
         SetAttributes(result, arc.Attributes);
         return(result);
     }
     else
     {
         Plane plane  = new Plane(arc.Circle.Origin, arc.Circle.L);
         var   result = new nDE.Arc(Convert(arc.Circle.Origin), arc.Circle.Radius * ConversionScaling,
                                    plane.GlobalToLocal(arc.StartPoint).Angle.Degrees, plane.GlobalToLocal(arc.EndPoint).Angle.Degrees);
         result.Normal = Convert(plane.Z, 1);
         SetAttributes(result, arc.Attributes);
         return(result);
     }
 }
Пример #10
0
        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");

        }
Пример #11
0
        private static void ToPolyline()
        {
            DxfDocument dxf = new DxfDocument();

            Vector3 center = new Vector3(1, 8, -7);
            Vector3 normal = new Vector3(1, 1, 1);

            Circle circle = new Circle(center, 7.5);
            circle.Normal = normal;

            Arc arc = new Arc(center, 5, -45, 45);
            arc.Normal = normal;

            Ellipse ellipse = new Ellipse(center, 15, 7.5);
            ellipse.Rotation = 35;
            ellipse.Normal = normal;

            Ellipse ellipseArc = new Ellipse(center, 10, 5);
            ellipseArc.StartAngle = 315;
            ellipseArc.EndAngle = 45;
            ellipseArc.Rotation = 35;
            ellipseArc.Normal = normal;

            dxf.AddEntity(circle);
            dxf.AddEntity(circle.ToPolyline(10));

            dxf.AddEntity(arc);
            dxf.AddEntity(arc.ToPolyline(10));

            dxf.AddEntity(ellipse);
            dxf.AddEntity(ellipse.ToPolyline(10));

            dxf.AddEntity(ellipseArc);
            dxf.AddEntity(ellipseArc.ToPolyline(10));

            dxf.Save("to polyline.dxf");

            dxf = DxfDocument.Load("to polyline.dxf");

            dxf.Save("to polyline2.dxf");
        }
Пример #12
0
        private static void HatchTest2()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.Vertexes[2].Bulge = 1;
            poly.IsClosed = true;

            Circle circle = new Circle(Vector3.Zero, 5);

            Ellipse ellipse = new Ellipse(Vector3.Zero,16,10);
            ellipse.Rotation = 30;
            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{circle}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{ellipse})
                                                                            };

            Hatch hatch = new Hatch(HatchPattern.Line, boundary, false);
            hatch.Pattern.Angle = 150;
            hatch.Pattern.Scale = 5;
            //hatch.Normal = new Vector3(1,1,1);
            //hatch.Elevation = 23;
            //dxf.AddEntity(poly);
            //dxf.AddEntity(circle);
            //dxf.AddEntity(ellipse);
            dxf.AddEntity(hatch);
            hatch.CreateBoundary(true);
            dxf.Save("hatchTest2.dxf");
            dxf = DxfDocument.Load("hatchTest2.dxf");
            dxf.Save("hatchTest2 copy.dxf");
        }
Пример #13
0
 private static EntityObject ProcessCircle(Circle circle, Matrix3 trans, Vector3 pos)
 {
     circle.Center = trans * circle.Center + pos;
     circle.Normal = trans * circle.Normal;
     return(circle);
 }
Пример #14
0
        private static void RadialDimension()
        {
            Font f = new Font("Arial",16);
            string text = "3.6669";

            Bitmap fakeImage = new Bitmap(1, 1);
            fakeImage.SetResolution(96, 96);
            Graphics g = Graphics.FromImage(fakeImage);

            Size size = TextRenderer.MeasureText(text, f);
           
            SizeF sizeF = g.MeasureString(text, f, new PointF(100,0), StringFormat.GenericTypographic);


            DxfDocument dxf = new DxfDocument();
            DimensionStyle myStyle = CreateDimStyle();

            Vector3 center = new Vector3(1, 2, 0);
            double radius = 3;
            Circle circle = new Circle(center, radius);
            //circle.Normal = new Vector3(1, 1, 1);
            double angle = MathHelper.HalfPI * 0.5;
            Vector3 refPoint = center + new Vector3(radius * Math.Cos(angle), radius * Math.Cos(angle), 0);


            //DiametricDimension dim = new DiametricDimension(center, refPoint, -1.0, myStyle);
            double offset = radius;
            RadialDimension dim1 = new RadialDimension(circle, 0, offset, myStyle);
            RadialDimension dim2 = new RadialDimension(circle, 45, offset, myStyle);
            RadialDimension dim3 = new RadialDimension(circle, 90, offset, myStyle);
            RadialDimension dim4 = new RadialDimension(circle, 120, offset, myStyle);
            RadialDimension dim5 = new RadialDimension(circle, 180, offset, myStyle);
            RadialDimension dim6 = new RadialDimension(circle, 220, offset, myStyle);
            RadialDimension dim7 = new RadialDimension(circle, 270, offset, myStyle);
            RadialDimension dim8 = new RadialDimension(circle, 330, offset, myStyle);
            // if the dimension normal is not equal to the circle normal strange things might happen at the moment
            //dim1.Normal = circle.Normal;
            dxf.AddEntity(circle);
            //dxf.AddEntity(dim1);
            //dxf.AddEntity(dim2);
            //dxf.AddEntity(dim3);
            //dxf.AddEntity(dim4);
            //dxf.AddEntity(dim5);
            //dxf.AddEntity(dim6);
            //dxf.AddEntity(dim7);
            //dxf.AddEntity(dim8);
            dxf.Save("dimension drawing.dxf");

            dxf = DxfDocument.Load("dimension drawing.dxf");

            DxfDocument doc = new DxfDocument();
            foreach (var c in dxf.Circles)
            {
                doc.AddEntity((EntityObject)c.Clone());
            }
            foreach (var d in dxf.Dimensions)
            {
                doc.AddEntity((EntityObject)d.Clone());
            }
            doc.Save("dimension drawing saved.dxf");
        }
Пример #15
0
 /// <summary>
 /// Initializes a new instance of the <c>RadialDimension</c> class.
 /// </summary>
 /// <param name="circle"><see cref="Circle">Circle</see> to measure.</param>
 /// <param name="rotation">Rotation in degrees of the dimension line.</param>
 /// <remarks>The center point and the definition point define the distance to be measure.</remarks>
 public RadialDimension(Circle circle, double rotation)
     : this(circle, rotation, DimensionStyle.Default)
 {
 }
Пример #16
0
        /// <summary>
        /// Initializes a new instance of the <c>RadialDimension</c> class.
        /// </summary>
        /// <param name="circle"><see cref="Circle">Circle</see> to measure.</param>
        /// <param name="rotation">Rotation in degrees of the dimension line.</param>
        /// <param name="offset">Distance between the reference point and the dimension text</param>
        /// <param name="style">The <see cref="DimensionStyle">style</see> to use with the dimension.</param>
        /// <remarks>The center point and the definition point define the distance to be measure.</remarks>
        public RadialDimension(Circle circle, double rotation, double offset, DimensionStyle style)
            : base(DimensionType.Radius)
        {
            double angle = rotation*MathHelper.DegToRad;
            Vector3 point = MathHelper.Transform(new Vector3(circle.Radius * Math.Cos(angle), circle.Radius * Math.Sin(angle), 0.0), circle.Normal, MathHelper.CoordinateSystem.Object, MathHelper.CoordinateSystem.World);
            this.center = circle.Center;
            this.refPoint = circle.Center + point;

            if (offset < 0.0)
                throw new ArgumentOutOfRangeException("offset", "The offset value cannot be negative.");
            this.offset = offset;

            if (style == null)
                throw new ArgumentNullException("style", "The Dimension style cannot be null.");
            this.style = style;
        }
Пример #17
0
        /// <summary>
        /// Creates a new Circle that is a copy of the current instance.
        /// </summary>
        /// <returns>A new Circle that is a copy of this instance.</returns>
        public override object Clone()
        {
            Circle entity = new Circle
            {
                //EntityObject properties
                Layer = (Layer)this.layer.Clone(),
                LineType = (LineType)this.lineType.Clone(),
                Color = (AciColor)this.color.Clone(),
                Lineweight = (Lineweight)this.lineweight.Clone(),
                Transparency = (Transparency)this.transparency.Clone(),
                LineTypeScale = this.lineTypeScale,
                Normal = this.normal,
                //Circle properties
                Center = this.center,
                Radius = this.radius,
                Thickness = this.thickness
            };

            foreach (XData data in this.xData.Values)
                entity.XData.Add((XData)data.Clone());

            return entity;
        }
Пример #18
0
 private static EntityObject ProcessCircle(Circle circle, Matrix3 trans, Vector3 pos)
 {
     circle.Center = trans*circle.Center + pos;
     circle.Normal = trans*circle.Normal;
     return circle;
 }
Пример #19
0
 public void DrawCircle( string strLayer, Vec2 vCentre, double dRadius )
 {
     Circle c = new Circle( new Vector3d( vCentre.x, vCentre.y, 0 ), dRadius );
     c.Layer = m_layers[ strLayer ];
     m_dxf.AddEntity( c );
 }
Пример #20
0
 /// <summary>
 /// Initializes a new instance of the <c>DiametricDimension</c> class.
 /// </summary>
 /// <param name="circle"><see cref="Circle">Circle</see> to measure.</param>
 /// <param name="rotation">Rotation in degrees of the dimension line.</param>
 /// <remarks>The center point and the definition point define the distance to be measure.</remarks>
 public DiametricDimension(Circle circle, double rotation)
     : this(circle, rotation, DimensionStyle.Default)
 {
 }
Пример #21
0
        private static void BlockWithAttributes()
        {
            DxfDocument dxf = new DxfDocument();
            Block block = new Block("BlockWithAttributes");
            block.Layer = new Layer("BlockSample");
            // It is possible to change the block position, even though it is recommended to keep it at Vector3.Zero,
            // since the block geometry is expressed in local coordinates of the block.
            // The block position defines the base point when inserting an Insert entity.
            block.Origin = new Vector3(10, 5, 0);

            // create an attribute definition, the attDef tag must be unique as it is the way to identify the attribute.
            // even thought AutoCad allows multiple attribute definition in block definitions, it is not recommended
            AttributeDefinition attdef = new AttributeDefinition("NewAttribute");
            // this is the text prompt shown to introduce the attribute value when a new Insert entity is inserted into the drawing
            attdef.Prompt = "InfoText";
            // optionally we can set a default value for new Insert entities
            attdef.Value = 0;
            // the attribute definition position is in local coordinates to the Insert entity to which it belongs
            attdef.Position = new Vector3(1, 1, 0);

            // modifying directly the text style might not get the desired results. Create one or get one from the text style table, modify it and assign it to the attribute text style.
            // one thing to note, if there is already a text style with the assigned name, the existing one in the text style table will override the new one.
            //attdef.Style.IsVertical = true;

            TextStyle txt = new TextStyle("MyStyle", "Arial.ttf");
            txt.IsVertical = true;
            attdef.Style = txt;
            attdef.WidthFactor = 2;
            // not all alignment options are available for TTF fonts 
            attdef.Alignment = TextAlignment.MiddleCenter;
            attdef.Rotation = 90;

            // remember, netDxf does not allow adding attribute definitions with the same tag, even thought AutoCad allows this behavior, it is not recommended in anyway.
            // internally attributes and their associated attribute definitions are handled through dictionaries,
            // and the tags work as ids to easily identify the information stored in the attribute value.
            // When reading a file the attributes or attribute definitions with duplicate tags will be automatically removed.
            // This is subject to change on public demand, it is possible to reimplement this behavior with simple collections to allow for duplicate tags.
            block.AttributeDefinitions.Add(attdef);

            // The entities list defines the actual geometry of the block, they are expressed in th block local coordinates
            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);

            // You can check the entity ownership with:
            Block line1Owner = line1.Owner;
            Block line2Owner = line2.Owner;
            // in this example line1Oner = line2Owner = block
            // As explained in the PaperSpace() sample, the layout associated with a common block will always be null
            Layout associatedLayout = line1.Owner.Record.Layout;
            // associatedLayout = null

            // create an Insert entity with the block definition, during the initialization the Insert attributes list will be created with the default attdef properties
            Insert insert1 = new Insert(block)
            {
                Position = new Vector3(5, 5, 5),
                Normal = new Vector3(1, 1, 1),
                Rotation = 45
            };

            // When the insert position, rotation, normal and/or scale are modified we need to transform the attributes.
            // It is not recommended to manually change the attribute position and orientation and let the Insert entity handle the transformations to maintain them in the same local position.
            // In this particular case we have changed the position, normal and rotation.
            insert1.TransformAttributes();
            
            // Once the insert has been created we can modify the attributes properties, the list cannot be modified only the items stored in it
            insert1.Attributes[0].Value = 24;

            // Modifying directly the layer might not get the desired results. Create one or get one from the layers table, modify it and assign it to the insert
            // One thing to note, if there is already a layer with the same name, the existing one in the layers table will override the new one, when the entity is added to the document.
            Layer layer = new Layer("MyInsertLayer");
            layer.Color.Index = 4;

            // optionally we can add the new layer to the document, if not the new layer will be added to the Layers collection when the insert entity is added to the document
            // in case a new layer is found in the list the add method will return the layer already stored in the list
            // this behavior is similar for all TableObject elements, all table object names must be unique (case insensitive)
            layer = dxf.Layers.Add(layer);

            // assign the new layer to the insert
            insert1.Layer = layer;

            // add the entity to the document
            dxf.AddEntity(insert1);

            // create a second insert entity
            // the constructor will automatically reposition the insert2 attributes to the insert local position
            Insert insert2 = new Insert(block, new Vector3(10, 5, 0));

            // as before now we can change the insert2 attribute value
            insert2.Attributes[0].Value = 34;

            // additionally we can insert extended data information
            XData xdata1 = new XData(new ApplicationRegistry("netDxf"));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata1.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, 0.0));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, 0.0));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, 0.0));
            xdata1.XDataRecord.Add(XDataRecord.CloseControlString);

            insert2.XData.Add(xdata1);
            dxf.AddEntity(insert2);

            // all entities support this feature
            XData xdata2 = new XData(new ApplicationRegistry("MyApplication1"));
            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);

            // multiple extended data entries might be added
            XData xdata3 = new XData(new ApplicationRegistry("MyApplication2"));
            xdata3.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata3.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata3.XDataRecord.Add(new XDataRecord(XDataCode.String, "string record"));
            xdata3.XDataRecord.Add(new XDataRecord(XDataCode.Real, 15.5));
            xdata3.XDataRecord.Add(new XDataRecord(XDataCode.Int32, 350));
            xdata3.XDataRecord.Add(XDataRecord.CloseControlString);

            Circle circle = new Circle(Vector3.Zero, 5);
            circle.Layer = new Layer("MyCircleLayer");
            // AutoCad 2000 does not support true colors, in that case an approximated color index will be used instead
            circle.Layer.Color = new AciColor(Color.MediumSlateBlue);
            circle.XData.Add(xdata2);
            circle.XData.Add(xdata3);

            dxf.AddEntity(circle);

            dxf.Save("BlockWithAttributes.dxf");
            DxfDocument dxfLoad = DxfDocument.Load("BlockWithAttributes.dxf");
        }
Пример #22
0
        private static void WipeoutEntity()
        {
            Line line1 = new Line(new Vector2(-1, -1), new Vector2(1, 1));
            Line line2 = new Line(new Vector2(-1, 1), new Vector2(1, -1));
            Circle circle = new Circle(Vector2.Zero, 0.5);

            Wipeout wipeout1 = new Wipeout(new Vector2(-1.5, -0.25), new Vector2(1.5, 0.25)); // a rectangular wipeout defined from two opposite corners
            //Wipeout wipeout1 = new Wipeout(-1.5, -0.25, 3.0, 0.5); // a rectangular wipeout defined by its bottom-left corner and its width and height
            //Wipeout wipeout1 = new Wipeout(new List<Vector2>{new Vector2(-1.5, 0.25), new Vector2(1.5, 0.25), new Vector2(1.5, -0.25), new Vector2(-1.5, -0.25)}); // a polygonal wipeout

            List<Vector2> vertexes = new List<Vector2>
            {
                new Vector2(-30, 30),
                new Vector2(-20, 60),
                new Vector2(-10, 40),
                new Vector2(10, 70),
                new Vector2(30, 20)
            };

            Wipeout wipeout2 = new Wipeout(vertexes);
            // optionally you can set the normal and elevation
            //wipeout1.Normal = new Vector3(1.0);
            //wipeout1.Elevation = 10;
            DxfDocument doc = new DxfDocument();
            doc.AddEntity(line1);
            doc.AddEntity(line2);
            doc.AddEntity(circle);
            doc.AddEntity(wipeout1);
            doc.AddEntity(wipeout2);

            doc.Save("wipeout.dxf");

            DxfDocument test = DxfDocument.Load("wipeout.dxf");
            test.Save("test.dxf");

        }
Пример #23
0
        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");

        }
Пример #24
0
        private static void RadialDimension()
        {
            DxfDocument dxf = new DxfDocument();
            DimensionStyle myStyle = CreateDimStyle();

            Vector3 center = new Vector3(1, 2, 0);
            double radius = 3;
            Circle circle = new Circle(center, radius);
            //circle.Normal = new Vector3(1, 1, 1);
            double angle = MathHelper.HalfPI * 0.5;
            Vector3 refPoint = center + new Vector3(radius * Math.Cos(angle), radius * Math.Cos(angle), 0);

            //DiametricDimension dim = new DiametricDimension(center, refPoint, -1.0, myStyle);
            double offset = 3;
            RadialDimension dim1 = new RadialDimension(circle, 0, offset, myStyle);
            RadialDimension dim2 = new RadialDimension(circle, 45, offset, myStyle);
            RadialDimension dim3 = new RadialDimension(circle, 90, offset, myStyle);
            RadialDimension dim4 = new RadialDimension(circle, 120, offset, myStyle);
            RadialDimension dim5 = new RadialDimension(circle, 180, offset, myStyle);
            RadialDimension dim6 = new RadialDimension(circle, 220, offset, myStyle);
            RadialDimension dim7 = new RadialDimension(circle, 270, offset, myStyle);
            RadialDimension dim8 = new RadialDimension(circle, 330, offset, myStyle);
            // if the dimension normal is not equal to the circle normal strange things might happen at the moment
            //dim1.Normal = circle.Normal;
            dxf.AddEntity(circle);
            dxf.AddEntity(dim1);
            dxf.AddEntity(dim2);
            dxf.AddEntity(dim3);
            dxf.AddEntity(dim4);
            dxf.AddEntity(dim5);
            dxf.AddEntity(dim6);
            dxf.AddEntity(dim7);
            dxf.AddEntity(dim8);
            dxf.Save("dimension drawing.dxf");

            dxf = DxfDocument.Load("dimension drawing.dxf");

            DxfDocument doc = new DxfDocument();
            foreach (var c in dxf.Circles)
            {
                doc.AddEntity((EntityObject)c.Clone());
            }
            foreach (var d in dxf.Dimensions)
            {
                doc.AddEntity((EntityObject)d.Clone());
            }
            doc.Save("dimension drawing saved.dxf");
        }
Пример #25
0
 /// <summary>
 /// Initializes a new instance of the <c>RadialDimension</c> class.
 /// </summary>
 /// <param name="circle"><see cref="Circle">Circle</see> to measure.</param>
 /// <param name="rotation">Rotation in degrees of the dimension line.</param>
 /// <param name="style">The <see cref="DimensionStyle">style</see> to use with the dimension.</param>
 /// <remarks>The center point and the definition point define the distance to be measure.</remarks>
 public RadialDimension(Circle circle, double rotation, DimensionStyle style)
     : this(circle.Center, circle.Radius, rotation, style)
 {
     this.normal = circle.Normal;
 }
Пример #26
0
        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");
        }
Пример #27
0
        /// <summary>
        /// Initializes a new instance of the <c>DiametricDimension</c> class.
        /// </summary>
        /// <param name="circle"><see cref="Circle">Circle</see> to measure.</param>
        /// <param name="rotation">Rotation in degrees of the dimension line.</param>
        /// <param name="offset">Distance between the reference point and the dimension text</param>
        /// <param name="style">The <see cref="DimensionStyle">style</see> to use with the dimension.</param>
        /// <remarks>The center point and the definition point define the distance to be measure.</remarks>
        public DiametricDimension(Circle circle, double rotation, double offset, DimensionStyle style)
            : base(DimensionType.Diameter)
        {
            if (circle == null)
                throw new ArgumentNullException(nameof(circle));

            Vector3 ocsCenter = MathHelper.Transform(circle.Center, circle.Normal, CoordinateSystem.World, CoordinateSystem.Object);
            this.center = new Vector2(ocsCenter.X, ocsCenter.Y);
            this.refPoint = Vector2.Polar(this.center, circle.Radius, rotation*MathHelper.DegToRad);
            if (offset < 0.0)
                throw new ArgumentOutOfRangeException(nameof(offset), "The offset value cannot be negative.");
            this.offset = offset;

            if (style == null)
                throw new ArgumentNullException(nameof(style));
            this.Style = style;
            this.Normal = circle.Normal;
            this.Elevation = ocsCenter.Z;
        }
Пример #28
0
        private Circle ReadCircle()
        {
            Vector3 center = Vector3.Zero;
            double radius = 1.0;
            double thickness = 0.0;
            Vector3 normal = Vector3.UnitZ;
            List<XData> xData = new List<XData>();

            this.chunk.Next();
            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 10:
                        center.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 20:
                        center.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 30:
                        center.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 40:
                        radius = this.chunk.ReadDouble();
                        if (radius <= 0)
                            radius = 1.0;
                        this.chunk.Next();
                        break;
                    case 39:
                        thickness = this.chunk.ReadDouble();
                        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;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(this.GetApplicationRegistry(appId));
                        xData.Add(data);
                        break;
                    default:
                        if (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
                            throw new Exception("The extended data of an entity must start with the application registry code.");
                        this.chunk.Next();
                        break;
                }
            }

            // this is just an example of the stupid autodesk dxf way of doing things, while an ellipse the center is given in world coordinates,
            // the center of a circle is given in object coordinates (different rules for the same concept).
            // It is a lot more intuitive to give the center in world coordinates and then define the orientation with the normal..
            Vector3 wcsCenter = MathHelper.Transform(center, normal, CoordinateSystem.Object, CoordinateSystem.World);

            Circle entity = new Circle
            {
                Center = wcsCenter,
                Radius = radius,
                Thickness = thickness,
                Normal = normal
            };

            entity.XData.AddRange(xData);

            return entity;
        }
Пример #29
0
 /// <summary>
 /// Convert a netDXF circle to a Nucleus one
 /// </summary>
 /// <param name="circle"></param>
 /// <returns></returns>
 public static Circle ConvertCircle(netDxf.Entities.Circle circle)
 {
     return(new Circle(circle.Radius * ConversionScaling, Convert(circle.Center), Convert(circle.Normal, 1)));
 }
Пример #30
0
        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");
        }
Пример #31
0
        private void WriteCircle(Circle circle)
        {
            this.chunk.Write(100, SubclassMarker.Circle);

            // this is just an example of the stupid autodesk dxf way of doing things, while an ellipse the center is given in world coordinates,
            // the center of a circle is given in object coordinates (different rules for the same concept).
            // It is a lot more intuitive to give the center in world coordinates and then define the orientation with the normal..
            Vector3 ocsCenter = MathHelper.Transform(circle.Center, circle.Normal, CoordinateSystem.World, CoordinateSystem.Object);

            this.chunk.Write(10, ocsCenter.X);
            this.chunk.Write(20, ocsCenter.Y);
            this.chunk.Write(30, ocsCenter.Z);

            this.chunk.Write(40, circle.Radius);

            this.chunk.Write(39, circle.Thickness);

            this.chunk.Write(210, circle.Normal.X);
            this.chunk.Write(220, circle.Normal.Y);
            this.chunk.Write(230, circle.Normal.Z);

            this.WriteXData(circle.XData);
        }
Пример #32
0
 public static Circle3D ToCircle3D(this netDxf.Entities.Circle dxf_circle, double tol_len)
 {
     return(new Circle3D(tol_len, new CoordinateSystem3D(dxf_circle.Center, dxf_circle.Normal, CoordinateSystem3DAutoEnum.AAA), dxf_circle.Radius));
 }
Пример #33
0
        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");
        }
Пример #34
0
 /// <summary>
 /// Convert a netDXF circle to a Nucleus arc
 /// </summary>
 /// <param name="circle"></param>
 /// <returns></returns>
 public static Arc Convert(netDxf.Entities.Circle circle)
 {
     return(new Arc(new Circle(circle.Radius * ConversionScaling, Convert(circle.Center), Convert(circle.Normal, 1)), ExtractAttributes(circle)));
 }
Пример #35
0
        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");


        }
Пример #36
0
        private void WriteCircle(Circle circle)
        {
            if (this.activeSection != StringCode.EntitiesSection && !this.isBlockEntities)
            {
                throw new InvalidDxfSectionException(this.activeSection, this.file);
            }

            this.WriteCodePair(0, circle.CodeName);
            this.WriteCodePair(5, circle.Handle);
            this.WriteCodePair(100, SubclassMarker.Entity);
            this.WriteEntityCommonCodes(circle);
            this.WriteCodePair(100, SubclassMarker.Circle);

            this.WriteCodePair(10, circle.Center.X);
            this.WriteCodePair(20, circle.Center.Y);
            this.WriteCodePair(30, circle.Center.Z);

            this.WriteCodePair(40, circle.Radius);

            this.WriteCodePair(39, circle.Thickness);

            this.WriteCodePair(210, circle.Normal.X);
            this.WriteCodePair(220, circle.Normal.Y);
            this.WriteCodePair(230, circle.Normal.Z);

            this.WriteXData(circle.XData);
        }
Пример #37
0
        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");

        }
Пример #38
0
        private Circle ReadCircle(ref CodeValuePair code)
        {
            var circle = new Circle();
            Vector3d center = Vector3d.Zero;
            Vector3d normal = Vector3d.UnitZ;
            Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>();

            code = this.ReadCodePair();
            while (code.Code != 0)
            {
                switch (code.Code)
                {
                    case 5:
                        circle.Handle = code.Value;
                        code = this.ReadCodePair();
                        break;
                    case 8: //layer code
                        circle.Layer = this.GetLayer(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 62: //aci color code
                        circle.Color = new AciColor(short.Parse(code.Value));
                        code = this.ReadCodePair();
                        break;
                    case 6: //type line code
                        circle.LineType = this.GetLineType(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 10:
                        center.X = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 20:
                        center.Y = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 30:
                        center.Z = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 40:
                        circle.Radius = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 39:
                        circle.Thickness = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 210:
                        normal.X = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 220:
                        normal.Y = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 230:
                        normal.Z = double.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;
                }
            }

            circle.XData = xData;
            circle.Center = center;
            circle.Normal = normal;
            return circle;
        }
Пример #39
0
        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");
        }
Пример #40
0
 /// <summary>
 /// Initializes a new instance of the <c>RadialDimension</c> class.
 /// </summary>
 /// <param name="circle"><see cref="Circle">Circle</see> to measure.</param>
 /// <param name="rotation">Rotation in degrees of the dimension line.</param>
 /// <param name="offset">Distance between the reference point and the dimension text</param>
 /// <remarks>The center point and the definition point define the distance to be measure.</remarks>
 public RadialDimension(Circle circle, double rotation, double offset)
     : this(circle, rotation, offset, DimensionStyle.Default)
 {
 }
Пример #41
0
        private static void HatchCircleBoundary()
        {
            DxfDocument dxf = new DxfDocument();

            // create a circle that will be our hatch boundary in this case it is a circle with center (5.5, -5.5, 0.0) and a radius 10.0
            Circle circle = new Circle(new Vector3(5.5, -5.5, 0), 10);

            // create the hatch boundary path with only the circle (a circle is already a closed loop it is all we need to define a valid boundary path)
            // a hatch can have many boundaries (closed loops) and every boundary path can be made of several entities (lines, polylines, arcs, circles and ellipses)
            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>
                                                        {
                                                            new HatchBoundaryPath(new List<EntityObject>{circle})
                                                        };  

            // create the hatch in this case we will use the predefined Solid hatch pattern and our circle as the boundary path
            //Hatch hatch = new Hatch(HatchPattern.Solid, boundary);
            Hatch hatch = new Hatch(HatchPattern.Line, boundary, true);
            // to give a color to the hatch, we have to options:

            // create a new layer with a color for the hatch (in this case by default the hatch will have a ByLayer color)
            //Layer hatchLayer = new Layer("HathLayer") {Color = AciColor.Green};
            //hatch.Layer = hatchLayer;

            // or give the hatch a color just for it
            // old AutoCAD versions only had 255 colors (indexed color), now in AutoCAD you can use true colors (8 bits per channel) but at the moment this is not supported.
            // if you try to give r, g, b values to define a color it will be converted to an indexed color
            // (I haven't tested this code a lot, so errors might appear and the result might not be what you expected).
            hatch.Color = AciColor.Red;

            // the hatch by itself will not show the boundary, but we can use the same entity to show the limits of the hatch, adding it to the document 
            dxf.AddEntity(circle);

            // add the hatch to the document
            dxf.AddEntity(hatch);

            dxf.Save("circle solid fill.dxf");
        }
Пример #42
0
        /// <summary>
        /// 门把手绘制
        /// </summary>
        /// <param name="dxf"></param>
        /// <param name="location"></param>
        public static void Draw(DxfDocument dxf, Location location)
        {
            float factor = 0.05f;
             float distance = 30;
            //底部小圆的圆心
             Vector3f sCircle = new Vector3f(location.X + 10 * factor, location.Y, location.Z);
            //上部同心圆圆心
             Vector3f bCircle = new Vector3f(location.X + 10 * factor, location.Y + 5 * factor + distance*factor, location.Z);

             double alpha = Math.Asin(3 / distance);
             double beta = Math.Acos(0.8);

             Vector3f v1 = new Vector3f(
                 location.X + 10 * factor - float.Parse((5 * factor * Math.Cos(alpha)).ToString()),
                 location.Y + 5 * factor - float.Parse((5 * factor * Math.Sin(alpha)).ToString()),
                 location.Z);

             Vector3f v2 = new Vector3f(
                  location.X + 10 * factor + float.Parse((5 * factor * Math.Cos(alpha)).ToString()),
                  location.Y + 5 * factor - float.Parse((5 * factor * Math.Sin(alpha)).ToString()),
                  location.Z);

             Vector3f v4 = new Vector3f(
                 location.X + 10 * factor -float.Parse((8*factor* Math.Cos(alpha)).ToString()),
                 location.Y + 5 * factor + distance * factor - float.Parse((8 * factor * Math.Sin(alpha)).ToString()),
                 location.Z
                 );

             Vector3f v5 = new Vector3f(
             location.X + 10 * factor  + float.Parse((8*factor*Math.Cos(alpha)).ToString()),
             location.Y + 5 * factor + distance * factor - float.Parse((8 * factor * Math.Sin(alpha)).ToString()),
             location.Z
             );
             Layer layer = new Layer("line");
             Line line14 = new Line(v1, v4);
             line14.Layer = layer;
             line14.Layer.Color.Index = 6;
             dxf.AddEntity(line14);

             Line line25 = new Line(v2, v5);
             line25.Layer = new Layer("line");
             line25.Layer = layer;
             dxf.AddEntity(line25);

             //arc
             Arc arc = new Arc(
                 new Vector3f(location.X + 10 * factor, location.Y + 5 * factor, location.Z),
                 5 * factor, Convert.ToInt32(180 + alpha * 180 / Math.PI), Convert.ToInt32(360 - alpha * 180 / Math.PI));
             arc.Layer = layer;
             dxf.AddEntity(arc);

             //arcup
             Arc arcup = new Arc(
                 new Vector3f(location.X + 10 * factor, location.Y + 5 * factor + distance * factor, location.Z),
                 8 * factor, Convert.ToInt32(-alpha * 180 / Math.PI), Convert.ToInt32(180 + alpha * 180 / Math.PI));
             arcup.Layer = layer;
             dxf.AddEntity(arcup);

             //arcround
             Arc arcround = new Arc(
                 new Vector3f(location.X + 10 * factor, location.Y + 5 * factor + distance * factor, location.Z),
                 10 * factor,
                 Convert.ToInt32(-(alpha +beta) * 180 / Math.PI),
                 Convert.ToInt32(180 + (alpha +beta) * 180 / Math.PI));
             arcround.Layer = layer;
             dxf.AddEntity(arcround);

             //circle
             Vector3f extrusion = new Vector3f(0, 0, 1);
             Vector3f centerWCS = new Vector3f(location.X+10*factor, location.Y+5*factor+distance*factor, location.Z);
             Vector3d centerOCS = MathHelper.Transform((Vector3d)centerWCS,
                                                       (Vector3d)extrusion,
                                                       MathHelper.CoordinateSystem.World,
                                                       MathHelper.CoordinateSystem.Object);

             Circle circle = new Circle((Vector3f)centerOCS, 7*factor);
             circle.Layer = layer;
             circle.LineType = LineType.Continuous;
             circle.Normal = extrusion;
             dxf.AddEntity(circle);

             //上部同心圆圆心
             Vector3f t1 = new Vector3f(
                 location.X + 8 * factor,
                 location.Y + 5 * factor + (distance - 7) * factor * 0.7f,
                 location.Z);
             Vector3f t2 = new Vector3f(
                 location.X + 8 * factor,
                 location.Y + 5 * factor + (distance - 7) * factor * 0.5f,
                 location.Z);
             Vector3f t3 = new Vector3f(
                 location.X + 8 * factor,
                 location.Y + 5 * factor + (distance - 7) * factor * 0.3f,
                 location.Z);
             Vector3f t4 = new Vector3f(
                 location.X + 8 * factor,
                 location.Y + 5 * factor + (distance - 7) * factor * 0.1f,
                 location.Z);

             //text
             TextStyle style = new TextStyle("True type font", "Arial.ttf");
             Text text1 = new Text("A", t1, 0.2f, style);
             text1.Layer = layer;
             text1.Alignment = TextAlignment.TopLeft;
             dxf.AddEntity(text1);

             //text
             Text text2 = new Text("A", t2, 0.2f, style);
             text2.Layer = layer;
             text2.Alignment = TextAlignment.TopLeft;
             dxf.AddEntity(text2);

             //text
             Text text3 = new Text("O", t3, 0.2f, style);
             text3.Layer = layer;
             text3.Alignment = TextAlignment.TopLeft;
             dxf.AddEntity(text3);

             //text
             Text text4 = new Text("N", t4, 0.2f, style);
             text4.Layer = layer;
             text4.Alignment = TextAlignment.TopLeft;
             dxf.AddEntity(text4);
        }
Пример #43
0
        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 Vector3d(1, 1, 1);
            attdef.Style.IsVertical = true;
            attdef.Rotation = 45;

            block.Attributes.Add(attdef.Id, attdef);
            block.Entities.Add(new Line(new Vector3d(-5, -5, 0), new Vector3d(5, 5, 0)));
            block.Entities.Add(new Line(new Vector3d(5, -5, 0), new Vector3d(-5, 5, 0)));

            Insert insert = new Insert(block, new Vector3d(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 Vector3d(-5, -5, -5));
            insert2.Attributes[0].Value = 34;

            XData xdata1 = new XData(new ApplicationRegistry("netDxf"));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            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 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.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(Vector3d.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
        }