【2025 ODA teigha .NET系列开发教程 第三章】带你读写DWG、DXF文件内的所有2D Entity,包括源码

在这里插入图片描述
本节课程带你使用ODA

曲线类

using System;
using System.Collections.Generic;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject
{
  public class DBPointEx
  {
    public DBPointEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        // Changes point marker to "cross" instead of default "dot".
        db.Pdmode = 2;

        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            const int interval = 1;

            for (int i = 0; i < 4; i++)
              for (int j = 0; j < 4; j++)
                // Creates DBPoint entity and adds it into the Block Table Record.
                using (DBPoint dbPoint = new DBPoint())
                {
                  btr.AppendEntity(dbPoint);
                  // Sets position for every DBPoint entity.
                  dbPoint.Position = new Point3d(interval * j, interval * i, 0);
                  // Sets color for every DBPoint entity.
                  dbPoint.Color = Color.FromRgb((byte)(i * 60), (byte)(j * 60), 200);
                  // Sets thickness for every DBPoint entity.
                  dbPoint.Thickness = j;
                  // Sets thickness for every DBPoint entity.
                  dbPoint.EcsRotation = i * 0.314;
                }
          }
          ta.Commit();
        }
        db.SaveAs(path + "DBPointEx.dwg", DwgVersion.Current);
      }
    }
  }
}

直线

using System;
using System.Collections.Generic;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject
{
  public class LineEx
  {
    public LineEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            // Number of lines.
            const int lineNum = 10;
            // Line color from color index.
            short color = 1;
            for (int i = 0; i < lineNum; i++)
              // Creates Line entity and adds it into the Block Table Record.
              using (Line line = new Line())
              {
                btr.AppendEntity(line);
                if (color == 7)
                  color = 1;
                // Sets the same start point for all Line entity.
                line.StartPoint = new Point3d(0, 0, 0);
                // Sets end point for Line entity depending on its number.
                line.EndPoint = new Point3d((Math.Cos(2 * Math.PI / lineNum * i) * 10), (Math.Sin(2 * Math.PI / lineNum * i) * 10), 0);
                // Sets color from color index for Line entity depending on its number.
                line.Color = Color.FromColorIndex(ColorMethod.ByAci, color++);
                // Sets Thickness for Line entity depending on its number.
                line.Thickness = (double)i / 10;
                // Prints values of Angle, Delta and Thickness properties.
                Console.WriteLine("Angle is: " + line.Angle);
                Console.WriteLine("Delta is: " + line.Delta);
                Console.WriteLine("Thickness is: " + line.Thickness + "\n");
              }
          }
          ta.Commit();
        }
        db.SaveAs(path + "LineEx.dwg", DwgVersion.Current);
      }
    }
  }
}

圆弧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject
{
  public class ArcEx
  {
    public ArcEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            Arc arc1 = new Arc();
            btr.AppendEntity(arc1);
            arc1.Radius = 1;
            arc1.StartAngle = 0.0;
            arc1.EndAngle = Math.PI;
            arc1.Thickness = 1;

            Arc arc2 = new Arc(Point3d.Origin, 1.5, Math.PI / 4, 5 * Math.PI / 4);
            btr.AppendEntity(arc2);

            Arc arc3 = new Arc(Point3d.Origin, new Vector3d(0, 0, -1), 2.0, 0.0, Math.PI / 2);
            btr.AppendEntity(arc3);
            arc3.Thickness = 0.5;

          }
          ta.Commit();
        }
        db.SaveAs(path + "ArcEx.dwg", DwgVersion.Current);
      }
    }
  }
}

圆形

using System;
using System.Collections.Generic;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject
{
  public class CircleEx
  {
    public CircleEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            // Number of circles.
            const int cirNum = 10;
            // Circle color from color index.
            short color = 1;
            // Circle thickness.
            short thickness = 0;
            // Angle for calculating circle center.
            double alpha = 0.0;
            // Circle radius.
            double radius = 1;

            for (int i = 0; i < cirNum; i++)
              // Creates Circle entity and adds it into the Block Table Record.
              using (Circle cir = new Circle())
              {
                btr.AppendEntity(cir);
                // Sets Circle properties
                cir.Center = new Point3d(Math.Cos(alpha), Math.Sin(alpha), 0);
                cir.Radius = radius;
                cir.Thickness = thickness;
                // Sets color from color index for Circle entity.
                cir.Color = Color.FromColorIndex(ColorMethod.ByAci, color);

                thickness++;
                radius += 0.5;
                alpha += 2 * Math.PI / cirNum;
                color++;
                if (color == 7)
                  color = 1;

                // Prints Circle properties.
                Console.WriteLine("Center is: " + cir.Center);
                Console.WriteLine("Radius is: " + cir.Radius);
                Console.WriteLine("Circumference is: " + cir.Circumference);
                Console.WriteLine("Thickness is: " + cir.Thickness);
                Console.WriteLine("Normal is: " + cir.Normal + "\n");
              }
          }
          ta.Commit();
        }
        db.SaveAs(path + "CircleEx.dwg", DwgVersion.Current);
        db.Dispose();
      }
    }
  }
}

椭圆

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject
{
  public class EllipseEx
  {
    public EllipseEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            Ellipse el1 = new Ellipse(Point3d.Origin, new Vector3d(0, 0, 1), new Vector3d(2, 0, 0), 0.5, 0.0, 2 * Math.PI);
            btr.AppendEntity(el1);
            el1.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
                        
            Ellipse el2 = new Ellipse(Point3d.Origin, new Vector3d(1, 0, 0), new Vector3d(0, 0, 2), 0.5, 0.0, 2 * Math.PI);
            btr.AppendEntity(el2);
            el2.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);

            Ellipse el3 = new Ellipse();
            btr.AppendEntity(el3);
            el3.Set(Point3d.Origin, new Vector3d(1, 0, 1), new Vector3d(-2 * Math.Cos(Math.PI / 4), 0, 2 * Math.Sin(Math.PI / 4)), 0.5, 0.0, 2 * Math.PI);
            el3.Color = Color.FromColorIndex(ColorMethod.ByAci, 3);

            Ellipse el4 = new Ellipse();
            btr.AppendEntity(el4);
            el4.Set(Point3d.Origin, new Vector3d(1, 0, -1), new Vector3d(2 * Math.Cos(Math.PI / 4), 0, 2 * Math.Sin(Math.PI / 4)), 0.5, 0.0, 2 * Math.PI);
            el4.Color = Color.FromColorIndex(ColorMethod.ByAci, 4);

            Ellipse elArc1 = new Ellipse();
            btr.AppendEntity(elArc1);
            elArc1.Set(new Point3d(5, 0, 0), new Vector3d(0, 0, 1), new Vector3d(1, 0, 0), 0.7, Math.PI / 4, 3 * Math.PI / 4);
            elArc1.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);

            Ellipse elArc2 = new Ellipse();
            btr.AppendEntity(elArc2);
            elArc2.Set(new Point3d(5, 0, 0), new Vector3d(0, 0, 1), new Vector3d(1, 0, 0), 0.7, -5*Math.PI / 4, Math.PI / 4);
            elArc2.Color = Color.FromRgb(20, 20, 255);
           }
          ta.Commit();
        }
        db.SaveAs(path + "EllipseEx.dwg", DwgVersion.Current);
      }
    }
  }
}

3D多段线 Polyline3d

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject.Polyline3dEx
{
  public class Polyline3dEx
  {
    public Polyline3dEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            // Creates Polyline3d entity with specified parameters
            Polyline3d pLine1 = new Polyline3d(Poly3dType.SimplePoly, createCollectionForPolyline(1), false);
            btr.AppendEntity(pLine1);
            printPlolylineParams(pLine1);

            // Creates empty Polyline3d entity then append vertices and set properties
            Polyline3d pLine2 = new Polyline3d();
            btr.AppendEntity(pLine2);
            pLine2.Closed = false;
            foreach (Point3d pt in createCollectionForPolyline(2))
              pLine2.AppendVertex(new PolylineVertex3d(pt));
            // Converts created SimplePoly polyline to QuadSplinePoly type
            pLine2.ConvertToPolyType(Poly3dType.QuadSplinePoly);
            printPlolylineParams(pLine2);

            // Creates Polyline3d entity with specified parameters
            Polyline3d pLine3 = new Polyline3d(Poly3dType.SimplePoly, createCollectionForPolyline(3), false);
            btr.AppendEntity(pLine3);
            pLine3.Closed = true;
            // Converts created SimplePoly polyline to QuadSplinePoly type
            pLine3.ConvertToPolyType(Poly3dType.QuadSplinePoly);
            // Straightens QuadSplinePoly polyline
            pLine3.Straighten();
            // Converts created SimplePoly polyline to CubicSplinePoly type
            pLine3.ConvertToPolyType(Poly3dType.CubicSplinePoly);
            printPlolylineParams(pLine3);

            // Creates Polyline3d entity with specified parameters
            Polyline3d pLine4 = new Polyline3d(Poly3dType.SimplePoly, createCollectionForPolyline(4), false);
            btr.AppendEntity(pLine4);
            
            // Vertices to be inserted to SimplePoly polyline
            PolylineVertex3d vrtx1 = new PolylineVertex3d(new Point3d(45, -1, -3));
            PolylineVertex3d vrtx2 = new PolylineVertex3d(new Point3d(45, 12, -3));
            
            ObjectId[] verticesID = new ObjectId[12];
            int j = 0;
            foreach (ObjectId obj in pLine4)
            {
              using (DBObject dbObj = (DBObject)tm.GetObject(obj, OpenMode.ForRead))
              {
                if (dbObj is PolylineVertex3d)
                {
                  // Gets all vertices IDs
                  verticesID[j] = obj;
                  j++;
                }
              }
            }
            // Insrets vertices
            pLine4.InsertVertexAt(ObjectId.Null, vrtx1);
            pLine4.InsertVertexAt(verticesID[11], vrtx2);

            // Creates spline fitted polyline of CubicSplinePoly type and segments number of 2
            pLine4.SplineFit(Poly3dType.CubicSplinePoly, 2);
            printPlolylineParams(pLine4);
          }
          ta.Commit();
        }
        db.SaveAs(path + "Polyline3dEx.dwg", DwgVersion.Current);
      }
    }

    // Creates collection of Point3ds as a polyline vertices
    Point3dCollection createCollectionForPolyline(int i)
    {
      Point3d[] p3d = new Point3d[12];
      
      p3d[0] = new Point3d(6 + 10 * i, 0, 0);
      p3d[1] = new Point3d(1 + 10 * i, 1, 0);
      p3d[2] = new Point3d(0 + 10 * i, 2, -3);
      p3d[3] = new Point3d(5 + 10 * i, 3, -3);

      p3d[4] = new Point3d(6 + 10 * i, 4, 0);
      p3d[5] = new Point3d(1 + 10 * i, 5, 0);
      p3d[6] = new Point3d(0 + 10 * i, 6, -3);
      p3d[7] = new Point3d(5 + 10 * i, 7, -3);

      p3d[8] = new Point3d(6 + 10 * i, 8, 0);
      p3d[9] = new Point3d(1 + 10 * i, 9, 0);
      p3d[10] = new Point3d(0 + 10 * i, 10, -3);
      p3d[11] = new Point3d(5 + 10 * i, 11, -3);

      return new Point3dCollection(p3d);
    }

    // Prints polyline parameters to the console
    void printPlolylineParams(Polyline3d pLine)
    {
      Console.WriteLine("Polytype is " + pLine.PolyType);
      Console.WriteLine("Closed is " + pLine.Closed);
      int i = 0;
      
      // Gets vertices of a polyline and prints parameters of control and simple vertices 
      foreach (ObjectId objId in pLine)
      {
        using (DBObject obj = (DBObject)objId.GetObject(OpenMode.ForRead))
        {
          if (obj is PolylineVertex3d)
          {
            PolylineVertex3d pt = (PolylineVertex3d)obj;
            if ((pt.VertexType == Vertex3dType.ControlVertex) || (pt.VertexType == Vertex3dType.SimpleVertex))
            {
              Console.WriteLine("Vertex #" + i + ": " + pt.Position);
              i++;
            }
          }
        }
      }
      Console.WriteLine("Length is " + pLine.Length + System.Environment.NewLine);
    }  
  }
}

射线[Ray]

using System;
using System.Collections.Generic;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject
{
  public class RayEx
  {
    public RayEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            // Number of rays.
            const int rayNum = 10;
            // Ray color from color index.
            short color = 1;
            for (int i = 0; i < rayNum; i++)
              // Creates Ray entity and adds it into the Block Table Record.
              using (Ray ray = new Ray())
              {
                btr.AppendEntity(ray);
                if (color == 7)
                  color = 1;
                // Sets the same base point for all Ray entity.
                ray.BasePoint = new Point3d(0, 0, 0);
                // Sets second point for Ray entity depending on its number.
                ray.SecondPoint = new Point3d((Math.Cos(2 * Math.PI / rayNum * i) * 10), (Math.Sin(2 * Math.PI / rayNum * i) * 10), 0);
                // Sets color from color index for Ray entity depending on its number.
                ray.Color = Color.FromColorIndex(ColorMethod.ByAci, color++);
                // Prints value of UnitDir property.
                Console.WriteLine("UnitDir is: " + ray.UnitDir + "\n");
             }
          }
          ta.Commit();
        }
        db.SaveAs(path + "RayEx.dwg", DwgVersion.Current);
      }
    }
  }
}

多线[Mline]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject.MlineEx
{
  public class MlineEx
  {
    public MlineEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            // Multiline style
            MlineStyle mlStyle = new MlineStyle();
            
            Ray ray = new Ray();
            btr.AppendEntity(ray);
            ray.Color = Color.FromRgb(100, 100, 100);
            ray.UnitDir = new Vector3d(1, 0, 0);

            Mline mline1 = new Mline();
            btr.AppendEntity(mline1);
            mline1.AppendSegment(Point3d.Origin);
            mline1.Justification = MlineJustification.Bottom;
            mline1.AppendSegment(new Point3d(2, 2, 0));
            mline1.AppendSegment(new Point3d(4, 0, 0));
            mline1.AppendSegment(new Point3d(6, 6, 0));
            mline1.AppendSegment(new Point3d(16, 16, 0));
          
            mline1.MoveVertexAt(3, new Point3d(6, 2, 0));
            Point3d removedVertex = new Point3d();
            mline1.RemoveLastSegment(out removedVertex);
            Console.WriteLine("mline1:");
            printMline(mline1);
            
            Mline mline2 = new Mline();
            btr.AppendEntity(mline2);
            mline2.Justification = MlineJustification.Top;
            mline2.AppendSegment(new Point3d(8, 0, 0));
            mline2.AppendSegment(new Point3d(10, 2, 0));
            mline2.AppendSegment(new Point3d(12, 0, 0));
            mline2.AppendSegment(new Point3d(14, 2, 0));
            Console.WriteLine("mline2:");
            printMline(mline2);

            // Sets multiline style
            using (DBDictionary dic = (DBDictionary)tm.GetObject(db.MLStyleDictionaryId, OpenMode.ForWrite))
            {
              dic["ODA"] = mlStyle;
              mlStyle.Name = "ODAstyle";
              mlStyle.Description = "123";
              mlStyle.ShowMiters = true;
              mlStyle.StartInnerArcs = true;
              // Defines caps shapes
              mlStyle.StartRoundCap = true;
              mlStyle.EndSquareCap = true;

              // Set multiline as filled by color
              mlStyle.Filled = true;
              mlStyle.FillColor = Color.FromRgb(255, 200, 255);
              Teigha.Colors.Color color = Teigha.Colors.Color.FromRgb(255, 0, 0);

              // Adds multiline elements of specific color and offset
              mlStyle.Elements.Add(new MlineStyleElement(0, color, db.ByLayerLinetype), true);
              mlStyle.Elements.Add(new MlineStyleElement(0.5, color, db.ByLayerLinetype), true);
              mlStyle.Elements.Add(new MlineStyleElement(0.7, color, db.ByLayerLinetype), true);
              mlStyle.Elements.Add(new MlineStyleElement(2, color, db.ByLayerLinetype), true);
            }
                        
            Mline mline3 = new Mline();
            btr.AppendEntity(mline3);
            // Applies created style to the Mline entity
            mline3.Style = mlStyle.Id;
            // Sets caps to be drawn
            mline3.SupressStartCaps = false;
            mline3.SupressEndCaps = false;
            // Appends multiline segments
            mline3.AppendSegment(new Point3d(16, 0, 0));
            mline3.AppendSegment(new Point3d(18, 2, 0));
            mline3.AppendSegment(new Point3d(20, 0, 0));
            mline3.AppendSegment(new Point3d(22, 2, 0));

            Console.WriteLine("mline3:");
            printMline(mline3);
              
            Mline mline4 = new Mline();
            btr.AppendEntity(mline4);
            mline4.AppendSegment(new Point3d(24, 0, 0));
            mline4.AppendSegment(new Point3d(26, 2, 0));
            mline4.AppendSegment(new Point3d(28, 0, 0));
            mline4.AppendSegment(new Point3d(30, 2, 0));
            mline4.Scale = 0.1;
            mline4.IsClosed = true;

            Console.WriteLine("mline4:");
            printMline(mline4);
          }
          ta.Commit();
        }
        db.SaveAs(path + "MlineEx.dwg", DwgVersion.Current);
      }
    }

    // Prints Mline entity info 
    void printMline(Mline mline)
    {
      for (int i = 0; i < mline.NumberOfVertices; i++)
      {
        Console.WriteLine("\tvertex" + i + ": " + mline.VertexAt(i));
      }
      Console.WriteLine("\tJustification: " + mline.Justification);
      Console.WriteLine("\tIsClosed: " + mline.IsClosed);
      Console.WriteLine("\tStyle: " + ((MlineStyle)mline.Style.GetObject(OpenMode.ForRead)).Name + System.Environment.NewLine);
    }
  }
}

文字

单行文字[DBText]

using System;
using System.Collections.Generic;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject
{
  public class DBTextEx
  {
    public DBTextEx(String path)
    {
      using(Database db = new Database(true, true))
      {
        // Changes point marker to "cross" instead of default "dot".
        db.Pdmode = 2;
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            // Creates a DBText entity with default settings.
            using (DBText dbText1 = new DBText())
            {
              btr.AppendEntity(dbText1);
              dbText1.TextString = "This is a text with default settings";
            }

            // Creates a DBText entity with height of 1.
            using (DBText dbText2 = new DBText())
            {
              btr.AppendEntity(dbText2);
              dbText2.Position = new Point3d(0, -2, 0);
              dbText2.Height = 1;
              dbText2.TextString = "This is a text with Height of " + dbText2.Height;
            }

            // Creates a DBText entity with WidthFactor of 2.
            using (DBText dbText3 = new DBText())
            {
              btr.AppendEntity(dbText3);
              dbText3.Position = new Point3d(0, -4, 0);
              dbText3.Height = 0.5;
              dbText3.WidthFactor = 2;
              dbText3.TextString = "This is a text with Height of " + dbText3.Height + " and WidthFactor of " + dbText3.WidthFactor;
            }

            // Creates a DBText entity with AlignmentPoint at (10, -6, 0) and TopLeft justify mode.
            using (DBText dbText4 = new DBText())
            {
              btr.AppendEntity(dbText4);
              dbText4.Position = new Point3d(0, -6, 0);
              dbText4.Height = 0.5;
              dbText4.AlignmentPoint = new Point3d(10, -6, 0);
              using (DBPoint alignmentPoint4 = new DBPoint(dbText4.AlignmentPoint))
              {
                btr.AppendEntity(alignmentPoint4);
                alignmentPoint4.Color = Color.FromRgb(255, 0, 0);
              }
              dbText4.Justify = AttachmentPoint.TopLeft;
              dbText4.TextString = "This is a text with Position at " + dbText4.Position + ", AlignmentPoint at " + dbText4.AlignmentPoint + " and " + dbText4.Justify + "  justify mode";
            }

            // Creates a DBText entity with AlignmentPoint at (10, -8, 0) and TopCenter justify mode.
            using (DBText dbText5 = new DBText())
            {
              btr.AppendEntity(dbText5);
              dbText5.Position = new Point3d(0, -8, 0);
              dbText5.Height = 0.5;
              dbText5.AlignmentPoint = new Point3d(10, -8, 0);
              using (DBPoint alignmentPoint5 = new DBPoint(dbText5.AlignmentPoint))
              {
                btr.AppendEntity(alignmentPoint5);
                alignmentPoint5.Color = Color.FromRgb(255, 0, 0);
              }
              dbText5.Justify = AttachmentPoint.TopCenter;
              dbText5.TextString = "This is a text with Position at " + dbText5.Position + ", AlignmentPoint at " + dbText5.AlignmentPoint + " and " + dbText5.Justify + " mode";
            }

            // Creates a DBText entity with AlignmentPoint at (10, -10, 0) and TopRight justify mode.
            using (DBText dbText6 = new DBText())
            {
              btr.AppendEntity(dbText6);
              dbText6.Position = new Point3d(0, -10, 0);
              dbText6.Height = 0.5;
              dbText6.AlignmentPoint = new Point3d(10, -10, 0);
              using (DBPoint alignmentPoint6 = new DBPoint(dbText6.AlignmentPoint))
              {
                btr.AppendEntity(alignmentPoint6);
                alignmentPoint6.Color = Color.FromRgb(255, 0, 0);
              }
              dbText6.Justify = AttachmentPoint.TopRight;
              dbText6.TextString = "This is a text with Position at " + dbText6.Position + ", AlignmentPoint at " + dbText6.AlignmentPoint + " and " + dbText6.Justify + " mode";
            }

            // Creates a DBText entity with AlignmentPoint at (10, -12, 0) and BaseCenter justify mode.
            using (DBText dbText7 = new DBText())
            {
              btr.AppendEntity(dbText7);
              dbText7.Position = new Point3d(0, -12, 0);
              dbText7.Height = 0.5;
              dbText7.AlignmentPoint = new Point3d(10, -12, 0);
              using (DBPoint alignmentPoint7 = new DBPoint(dbText7.AlignmentPoint))
              {
                btr.AppendEntity(alignmentPoint7);
                alignmentPoint7.Color = Color.FromRgb(255, 0, 0);
              }
              dbText7.Justify = AttachmentPoint.BaseCenter;
              dbText7.TextString = "This is a text with Position at " + dbText7.Position + ", AlignmentPoint at " + dbText7.AlignmentPoint + " and " + dbText7.Justify + " mode";
            }

            // Creates a DBText entity with AlignmentPoint at (10, -14, 0) and BaseAlign justify mode.
            using (DBText dbText8 = new DBText())
            {
              btr.AppendEntity(dbText8);
              dbText8.Position = new Point3d(0, -14, 0);
              dbText8.Height = 0.5;
              dbText8.AlignmentPoint = new Point3d(10, -14, 0);
              using (DBPoint alignmentPoint8 = new DBPoint(dbText8.AlignmentPoint))
              {
                btr.AppendEntity(alignmentPoint8);
                alignmentPoint8.Color = Color.FromRgb(255, 0, 0);
              }
              dbText8.Justify = AttachmentPoint.BaseAlign;
              dbText8.TextString = "This is a text with Position at " + dbText8.Position + ", AlignmentPoint at " + dbText8.AlignmentPoint + " and " + dbText8.Justify + " mode";
            }

            // Creates a DBText entity with AlignmentPoint at (10, -16, 0) and BaseFit justify mode.
            using (DBText dbText9 = new DBText())
            {
              btr.AppendEntity(dbText9);
              dbText9.Position = new Point3d(0, -16, 0);
              dbText9.Height = 0.5;
              dbText9.AlignmentPoint = new Point3d(10, -16, 0);
              using (DBPoint alignmentPoint9 = new DBPoint(dbText9.AlignmentPoint))
              {
                btr.AppendEntity(alignmentPoint9);
                alignmentPoint9.Color = Color.FromRgb(255, 0, 0);
              }
              dbText9.Justify = AttachmentPoint.BaseFit;
              dbText9.TextString = "This is a text with Position at " + dbText9.Position + ", AlignmentPoint at " + dbText9.AlignmentPoint + " and " + dbText9.Justify + " mode";
            }

            // Creates a DBText entity with Oblique of -0.7.
            using (DBText dbText10 = new DBText())
            {
              btr.AppendEntity(dbText10);
              dbText10.Position = new Point3d(0, -18, 0);
              dbText10.Height = 0.5;
              dbText10.Oblique = -0.7;
              dbText10.TextString = "This is a text with oblique angle of " + dbText10.Oblique;
            }

            // Creates a DBText entity with Oblique of 0.
            using (DBText dbText11 = new DBText())
            {
              btr.AppendEntity(dbText11);
              dbText11.Position = new Point3d(0, -20, 0);
              dbText11.Height = 0.5;
              dbText11.TextString = "This is a text with oblique angle of " + dbText11.Oblique;
            }

            // Creates a DBText entity with Oblique of 0.7.
            using (DBText dbText12 = new DBText())
            {
              btr.AppendEntity(dbText12);
              dbText12.Position = new Point3d(0, -22, 0);
              dbText12.Height = 0.5;
              dbText12.Oblique = 0.7;
              dbText12.TextString = "This is a text with oblique angle of " + dbText12.Oblique;
            }

            // Creates a DBText entity with Rotation of -0.7.
            using (DBText dbText13 = new DBText())
            {
              btr.AppendEntity(dbText13);
              dbText13.Position = new Point3d(0, -24, 0);
              dbText13.Height = 0.5;
              dbText13.Rotation = -0.7;
              dbText13.TextString = "This is a text with rotation angle of " + dbText13.Rotation;
            }

            // Creates a DBText entity with Rotation of 0.0.
            using (DBText dbText14 = new DBText())
            {
              btr.AppendEntity(dbText14);
              dbText14.Position = new Point3d(0, -26, 0);
              dbText14.Height = 0.5;
              dbText14.TextString = "This is a text with rotation angle of " + dbText14.Rotation;
            }

            // Creates a DBText entity with Rotation of 0.7.
            using (DBText dbText15 = new DBText())
            {
              btr.AppendEntity(dbText15);
              dbText15.Position = new Point3d(0, -28, 0);
              dbText15.Height = 0.5;
              dbText15.Rotation = 0.7;
              dbText15.TextString = "This is a text with rotation angle of " + dbText15.Rotation;
            }

            // Creates a DBText entity mirrored in X and Y directions.
            using (DBText dbText16 = new DBText())
            {
              btr.AppendEntity(dbText16);
              dbText16.Position = new Point3d(0, -30, 0);
              dbText16.Height = 0.5;
              dbText16.IsMirroredInX = true;
              dbText16.IsMirroredInY = true;
              dbText16.TextString = "This is a text mirrored in X and Y";
            }

            // Creates a DBText entity with thickness of 5.
            using (DBText dbText17 = new DBText())
            {
              btr.AppendEntity(dbText17);
              dbText17.Position = new Point3d(0, -32, 0);
              dbText17.Height = 0.5;
              dbText17.Thickness = 5;
              dbText17.TextString = "This is a text with thickness of " + dbText17.Thickness;
            }
          }
          ta.Commit();
        }
        db.SaveAs(path+"DBTextEx.dwg", DwgVersion.Current);
      }
    }
  }
}

多行文字[MTex]

using System;
using System.Collections.Generic;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;
using Teigha.GraphicsInterface;

namespace CDevGuideExamplesProject
{
  public class MTextEx
  {
    // Special fragment elaboration callback function. Adds extracted text fragment to list and prints some info. 
    MTextFragmentCallbackStatus MTextDelegateFunct(MTextFragment frag, Object data)
    {
      ((List<MTextFragment>)data).Add(frag);
      Console.WriteLine("Text content is: " + frag.Text);
      Console.WriteLine("Text color is: " + frag.Color.ColorIndex);
      Console.WriteLine("Text height is: " + frag.CapsHeight);
      Console.WriteLine("Oblique angle is: " + frag.ObliqueAngle);
      Console.WriteLine("Font name is: " + frag.TrueTypeFont);
      Console.WriteLine();
      return MTextFragmentCallbackStatus.Continue;
    }

    public MTextEx(String path)
    {
      Database db = new Database(true, true);
      // Changes point marker to "cross" instead of default "dot".
      db.Pdmode = 2;

      TransactionManager tm = db.TransactionManager;
      using (Transaction ta = tm.StartTransaction())
      {
        using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
        {
          // Creates an MText entity contains text with different formatting options applied to single words or characters 
          // within a paragraph or a text section.                
          using (MText mtext1 = new MText())
          {
            btr.AppendEntity(mtext1);
            mtext1.TextHeight = 0.2;
            mtext1.Contents = "This is a multiline text with" + MText.ParagraphBreak + "paragraph breaks, " + MText.ParagraphBreak +
            MText.BlockBegin + MText.ObliqueChange + "12;an oblique text, " + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + MText.OverlineOn + "an overlined text, " + MText.OverlineOff + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + MText.UnderlineOn + "an underlined text, " + MText.UnderlineOff + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + MText.WidthChange + "2;a text with the changed width" + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + MText.HeightChange + "0.5;a text with the changed height" + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + "a text " + MText.ColorChange + "10;in " + MText.ColorChange + "100;different " + MText.ColorChange + "200;colors, " + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + "a text " + MText.FontChange + "Times New Roman;in different " + MText.FontChange + "Tahoma;fonts" + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + "a stacked text with different allignments: " + MText.AlignChange + "0;1" + MText.StackStart + "1/2;  " + MText.AlignChange + "1;1" + MText.StackStart + "1/2;  " + MText.AlignChange + "2;1" + MText.StackStart + "1/2;" + MText.BlockEnd + MText.ParagraphBreak +
            MText.BlockBegin + "a text " + MText.TrackChange + "2;with the changed space" + MText.TrackChange + "0.4;between characters" + MText.BlockEnd + MText.ParagraphBreak;
            mtext1.Location = new Point3d(0, 0, 0);

            // Explodes mtext1 entity into a sequence of text fragments, passing each fragment to the MTextDelegateFunct function 
            // and stores each fragment in a list.
            List<MTextFragment> aFrgm = new List<MTextFragment>();
            mtext1.ExplodeFragments(new MTextFragmentCallback(MTextDelegateFunct), aFrgm);
          }

          // Creates an MText entity with text flowing from top to bottom.
          using (MText mtext2 = new MText())
          {
            btr.AppendEntity(mtext2);
            mtext2.TextHeight = 0.2;
            mtext2.Location = new Point3d(-2.0, 0.0, 0.0);
            mtext2.FlowDirection = FlowDirection.TopToBottom;
            mtext2.Contents = "This is a multiline text with FlowDirection of " + mtext2.FlowDirection;
          }

          // Creates an MText entity with LineSpacingFactor of 2.
          using (MText mtext3 = new MText())
          {
            btr.AppendEntity(mtext3);
            mtext3.TextHeight = 0.2;
            mtext3.Width = 2;
            mtext3.Location = new Point3d(16.0, 0.0, 0.0);
            mtext3.LineSpacingFactor = 2;
            mtext3.Contents = "This is a multiline text with LineSpacingFactor of " + mtext3.LineSpacingFactor;
          }

          // Creates an MText entity with LineSpacingFactor of 0.8.
          using (MText mtext4 = new MText())
          {
            btr.AppendEntity(mtext4);
            mtext4.TextHeight = 0.2;
            mtext4.Width = 2;
            mtext4.Location = new Point3d(22.0, 0.0, 0.0);
            mtext4.LineSpacingFactor = 0.8;
            mtext4.Contents = "This is a multiline text with LineSpacingFactor of " + mtext4.LineSpacingFactor;
          }

          // Creates an MText entity with AtLeast value of LineSpacingStyle.
          using (MText mtext5 = new MText())
          {
            btr.AppendEntity(mtext5);
            mtext5.Location = new Point3d(28, 0.0, 0.0);
            mtext5.TextHeight = 0.2;
            mtext5.Width = 2;
            mtext5.LineSpacingStyle = LineSpacingStyle.AtLeast;
            mtext5.Contents = "This is a multiline text with LineSpacingStyle of " + MText.HeightChange + "0.6;" + mtext5.LineSpacingStyle;
          }

          // Creates an MText entity with Exactly value of LineSpacingStyle.
          using (MText mtext6 = new MText())
          {
            btr.AppendEntity(mtext6);
            mtext6.TextHeight = 0.2;
            mtext6.Width = 2;
            mtext6.Location = new Point3d(34.0, 0.0, 0.0);
            mtext6.LineSpacingStyle = LineSpacingStyle.Exactly;
            mtext6.Contents = "This is a multiline text with LineSpacingStyle of " + MText.HeightChange + "0.6;" + mtext6.LineSpacingStyle;
          }

          // Creates an MText entity with default BackgroundScaleFactor.
          using (MText mtext7 = new MText())
          {
            btr.AppendEntity(mtext7);
            mtext7.TextHeight = 0.2;
            mtext7.Width = 4;
            mtext7.Location = new Point3d(40, 0.0, 0.0);
            mtext7.BackgroundFill = true;
            mtext7.BackgroundFillColor = Color.FromRgb(250, 200, 250);
            mtext7.Contents = "This is a multiline text with BackgroundScaleFactor of " + mtext7.BackgroundScaleFactor;
          }

          // Creates an MText entity with BackgroundScaleFactor of 5.
          using (MText mtext8 = new MText())
          {
            btr.AppendEntity(mtext8);
            mtext8.TextHeight = 0.2;
            mtext8.Width = 4;
            mtext8.Location = new Point3d(46, 0.0, 0.0);
            mtext8.BackgroundFill = true;
            mtext8.BackgroundFillColor = Color.FromRgb(250, 200, 250);
            mtext8.BackgroundScaleFactor = 5;
            mtext8.Contents = "This is a multiline text with BackgroundScaleFactor of " + mtext8.BackgroundScaleFactor;
          }

          // Creates an MText entity with TopLeft attachment point.
          using (MText mtext9 = new MText())
          {
            btr.AppendEntity(mtext9);
            mtext9.TextHeight = 0.2;
            mtext9.Width = 3;
            mtext9.Location = new Point3d(0, -10, 0);
            mtext9.Attachment = AttachmentPoint.TopLeft;
            mtext9.Contents = "This is a multiline text with " + mtext9.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint9 = new DBPoint(mtext9.Location))
            {
              btr.AppendEntity(attPoint9);
            }
          }

          // Creates an MText entity with TopCenter attachment point.
          using (MText mtext10 = new MText())
          {
            btr.AppendEntity(mtext10);
            mtext10.TextHeight = 0.2;
            mtext10.Width = 3;
            mtext10.Location = new Point3d(5, -10, 0);
            mtext10.Attachment = AttachmentPoint.TopCenter;
            mtext10.Contents = "This is a multiline text with " + mtext10.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint10 = new DBPoint(mtext10.Location))
            {
              btr.AppendEntity(attPoint10);
            }
          }

          // Creates an MText entity with TopRight attachment point.
          using (MText mtext11 = new MText())
          {
            btr.AppendEntity(mtext11);
            mtext11.TextHeight = 0.2;
            mtext11.Width = 3;
            mtext11.Location = new Point3d(10, -10, 0);
            mtext11.Attachment = AttachmentPoint.TopRight;
            mtext11.Contents = "This is a multiline text with " + mtext11.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint11 = new DBPoint(mtext11.Location))
            {
              btr.AppendEntity(attPoint11);
            }
          }

          // Creates an MText entity with MiddleLeft attachment point.
          using (MText mtext12 = new MText())
          {
            btr.AppendEntity(mtext12);
            mtext12.TextHeight = 0.2;
            mtext12.Width = 3;
            mtext12.Location = new Point3d(0, -15, 0);
            mtext12.Attachment = AttachmentPoint.MiddleLeft;
            mtext12.Contents = "This is a multiline text with " + mtext12.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint12 = new DBPoint(mtext12.Location))
            {
              btr.AppendEntity(attPoint12);
            }
          }

          // Creates an MText entity with MiddleCenter attachment point.
          using (MText mtext13 = new MText())
          {
            btr.AppendEntity(mtext13);
            mtext13.TextHeight = 0.2;
            mtext13.Width = 3;
            mtext13.Location = new Point3d(5, -15, 0);
            mtext13.Attachment = AttachmentPoint.MiddleCenter;
            mtext13.Contents = "This is a multiline text with " + mtext13.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint13 = new DBPoint(mtext13.Location))
            {
              btr.AppendEntity(attPoint13);
            }
          }

          // Creates an MText entity with MiddleRight attachment point.
          using (MText mtext14 = new MText())
          {
            btr.AppendEntity(mtext14);
            mtext14.TextHeight = 0.2;
            mtext14.Width = 3;
            mtext14.Location = new Point3d(10, -15, 0);
            mtext14.Attachment = AttachmentPoint.MiddleRight;
            mtext14.Contents = "This is a multiline text with " + mtext14.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint14 = new DBPoint(mtext14.Location))
            {
              btr.AppendEntity(attPoint14);
            }
          }

          // Creates an MText entity with BottomLeft attachment point.
          using (MText mtext15 = new MText())
          {
            btr.AppendEntity(mtext15);
            mtext15.TextHeight = 0.2;
            mtext15.Width = 3;
            mtext15.Location = new Point3d(0, -20, 0);
            mtext15.Attachment = AttachmentPoint.BottomLeft;
            mtext15.Contents = "This is a multiline text with " + mtext15.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint15 = new DBPoint(mtext15.Location))
            {
              btr.AppendEntity(attPoint15);
            }
          }

          // Creates an MText entity with BottomCenter attachment point.
          using (MText mtext16 = new MText())
          {
            btr.AppendEntity(mtext16);
            mtext16.TextHeight = 0.2;
            mtext16.Width = 3;
            mtext16.Location = new Point3d(5, -20, 0);
            mtext16.Attachment = AttachmentPoint.BottomCenter;
            mtext16.Contents = "This is a multiline text with " + mtext16.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint16 = new DBPoint(mtext16.Location))
            {
              btr.AppendEntity(attPoint16);
            }
          }

          // Creates an MText entity with BottomRight attachment point.
          using (MText mtext17 = new MText())
          {
            btr.AppendEntity(mtext17);
            mtext17.TextHeight = 0.2;
            mtext17.Width = 3;
            mtext17.Location = new Point3d(10, -20, 0);
            mtext17.Attachment = AttachmentPoint.BottomRight;
            mtext17.Contents = "This is a multiline text with " + mtext17.Attachment + " attachment point";
            // Creates an DBPoint entity indicating the attachment point.
            using (DBPoint attPoint17 = new DBPoint(mtext17.Location))
            {
              btr.AppendEntity(attPoint17);
            }
          }

          // Creates an MText entity and sets dynamic columns.
          using (MText mtext18 = new MText())
          {
            btr.AppendEntity(mtext18);
            mtext18.Location = new Point3d(15, -10, 0);
            mtext18.TextHeight = 0.2;
            mtext18.Height = 3;
            mtext18.SetDynamicColumns(5, 0.4, true);
            mtext18.Contents = "This is a multiline text with " + mtext18.ColumnType + " type of columns. " + MText.ParagraphBreak +
              "ColumnWidth is: " + mtext18.ColumnWidth + MText.ParagraphBreak +
              "ColumnColumnGutterWidth is: " + mtext18.ColumnGutterWidth + MText.ParagraphBreak +
              "ColumnAutoHeight is: " + mtext18.ColumnAutoHeight + MText.ParagraphBreak +
              "ColumnFlowReversed is: " + mtext18.ColumnFlowReversed + MText.ParagraphBreak + MText.ParagraphBreak +
              "Teigha.NET for .dwg files is a managed .NET component containing tools for working with .dwg file data. Built on top of the Teigha for .dwg files C++ development environment, Teigha.NET for .dwg files exposes access to .dwg application development using the .NET framework using any .NET language (C#, Visual Basic).";
          }

          // Creates an MText entity and sets dynamic columns in flow reversed mode.
          using (MText mtext19 = new MText())
          {
            btr.AppendEntity(mtext19);
            mtext19.Location = new Point3d(15, -15, 0);
            mtext19.TextHeight = 0.2;
            mtext19.Height = 3;
            mtext19.SetDynamicColumns(5, 0.4, true);
            mtext19.ColumnFlowReversed = true;
            mtext19.Contents = "This is a multiline text with " + mtext19.ColumnType + " type of columns. " + MText.ParagraphBreak +
              "ColumnWidth is: " + mtext19.ColumnWidth + MText.ParagraphBreak +
              "ColumnColumnGutterWidth is: " + mtext19.ColumnGutterWidth + MText.ParagraphBreak +
              "ColumnAutoHeight is: " + mtext19.ColumnAutoHeight + MText.ParagraphBreak +
              "ColumnFlowReversed is: " + mtext19.ColumnFlowReversed + MText.ParagraphBreak + MText.ParagraphBreak +
              "Teigha.NET for .dwg files is a managed .NET component containing tools for working with .dwg file data. Built on top of the Teigha for .dwg files C++ development environment, Teigha.NET for .dwg files exposes access to .dwg application development using the .NET framework using any .NET language (C#, Visual Basic).";
          }

          // Creates an MText entity and sets static columns.
          using (MText mtext20 = new MText())
          {
            btr.AppendEntity(mtext20);
            mtext20.Location = new Point3d(35, -10, 0);
            mtext20.TextHeight = 0.2;
            mtext20.Height = 2;
            mtext20.SetStaticColumns(5, 0.5, 2);
            mtext20.Contents = "This is a multiline text with " + mtext20.ColumnType + " type of columns. " + MText.ParagraphBreak +
              "ColumnWidth is: " + mtext20.ColumnWidth + MText.ParagraphBreak +
              "ColumnColumnGutterWidth is: " + mtext20.ColumnGutterWidth + MText.ParagraphBreak +
              "ColumnCount is: " + mtext20.ColumnCount + MText.ParagraphBreak +
              "ColumnFlowReversed is: " + mtext20.ColumnFlowReversed + MText.ParagraphBreak + MText.ParagraphBreak +
              "Teigha.NET for .dwg files is a managed .NET component containing tools for working with .dwg file data. Built on top of the Teigha for .dwg files C++ development environment, Teigha.NET for .dwg files exposes access to .dwg application development using the .NET framework using any .NET language (C#, Visual Basic).";
          }

          // Creates an MText entity and sets the rotation angle.
          using (MText mtext21 = new MText())
          {
            btr.AppendEntity(mtext21);
            mtext21.Location = new Point3d(0, -30, 0);
            mtext21.TextHeight = 0.4;
            mtext21.Rotation = 0.7;
            mtext21.Contents = "Rotation angle is: " + mtext21.Rotation;
          }
        }
        ta.Commit();
      }
      db.SaveAs(path + "MTextEx.dwg", DwgVersion.Current);
      db.Dispose();
    }
  }
}

Helix

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;

namespace CDevGuideExamplesProject.HelixEx
{
  public class HelixEx
  {
    public HelixEx(String path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite))
          {
            // Creates an expanding helix (BaseRadius < TopRadius)
            Helix helix1 = new Helix();
            btr.AppendEntity(helix1);
            helix1.AxisVector = Vector3d.YAxis;
            helix1.StartPoint = new Point3d(0.5, 0.0, 0);
            helix1.SetAxisPoint(new Point3d(0.0, 0.0, 0), false);
            helix1.Constrain = ConstrainType.Height;
            helix1.TopRadius = 2;
            helix1.Height = 7.0;
            helix1.Turns = 4.0;
            helix1.CreateHelix();

            // Creates a helix converging to a point (TopRadius == 0)
            Helix helix2 = new Helix();
            btr.AppendEntity(helix2);
            helix2.AxisVector = new Vector3d(0, 1, 0);
            helix2.StartPoint = new Point3d(-8.0, 0.0, 0);
            helix2.SetAxisPoint(new Point3d(-10.0, 0.0, 0), false);
            helix2.TopRadius = 0;
            helix2.Constrain = ConstrainType.Height;
            helix2.Height = 7.0;
            helix2.Turns = 4.0;
            helix2.CreateHelix();

            // Creates a narrowing helix (BaseRadius > TopRadius)
            Helix helix3 = new Helix();
            btr.AppendEntity(helix3);
            helix3.CopyFrom(helix2);
            helix3.StartPoint = new Point3d(-18.0, 0.0, 0);
            helix3.SetAxisPoint(new Point3d(-20.0, 0.0, 0), false);
            helix3.TopRadius = 0.5;
            helix3.CreateHelix();

            // Creates a cylindrical helix (BaseRadius == TopRadius)
            Helix helix4 = new Helix();
            btr.AppendEntity(helix4);
            helix4.CopyFrom(helix1);
            helix4.StartPoint = new Point3d(12.0, 0.0, 0);
            helix4.SetAxisPoint(new Point3d(10.0, 0.0, 0), false);
            helix4.TopRadius = 2;
            helix4.CreateHelix();

            // Creates a helix diverging from a point (BaseRadius == 0)
            Helix helix5 = new Helix();
            btr.AppendEntity(helix5);
            helix5.CopyFrom(helix1);
            helix5.SetAxisPoint(new Point3d(20.0, 0.0, 0), false);
            helix5.BaseRadius = 0.0;
            helix5.TopRadius = 2;
            helix5.CreateHelix();
            
            // Creates a helix with the Height of 4
            Helix helix6 = new Helix();
            btr.AppendEntity(helix6);
            helix6.AxisVector = Vector3d.YAxis;
            helix6.TopRadius = 2;
            helix6.StartPoint = new Point3d(-18.0, 10.0, 0);
            helix6.SetAxisPoint(new Point3d(-20.0, 10.0, 0), false);
            helix6.Turns = 3;
            helix6.Constrain = ConstrainType.Height;
            helix6.Height = 4;
            helix6.CreateHelix();

            // Creates a helix with the Height changed to 7 with ConstrainType.TurnHeight
            Helix helix7 = new Helix();
            btr.AppendEntity(helix7);
            helix7.CopyFrom(helix6);
            helix7.StartPoint = new Point3d(-12.0, 10.0, 0);
            helix7.SetAxisPoint(new Point3d(-14.0, 10.0, 0), false);
            helix7.Turns = 3;
            helix7.Constrain = ConstrainType.TurnHeight;
            helix7.Height = 7;
            helix7.CreateHelix();

            // Creates a helix with the Height changed to 7 with ConstrainType.Turns
            Helix helix8 = new Helix();
            btr.AppendEntity(helix8);
            helix8.CopyFrom(helix7);
            helix8.StartPoint = new Point3d(-6.0, 10.0, 0);
            helix8.SetAxisPoint(new Point3d(-8.0, 10.0, 0), false);
            helix8.Turns = 3;
            helix8.Constrain = ConstrainType.Turns;
            helix8.Height = 7;
            helix8.CreateHelix();

            // Creates a helix with the TurnHeight changed to 2 with ConstrainType.Turns
            Helix helix9 = new Helix();
            btr.AppendEntity(helix9);
            helix9.CopyFrom(helix6);
            helix9.StartPoint = new Point3d(-0.0, 10.0, 0);
            helix9.SetAxisPoint(new Point3d(-2.0, 10.0, 0), false);
            helix9.Constrain = ConstrainType.Turns;
            helix9.TurnHeight = 2;
            helix9.CreateHelix();

            // Creates a helix with the TurnHeight changed to 2 with ConstrainType.TurnHeight
            Helix helix10 = new Helix();
            btr.AppendEntity(helix10);
            helix10.CopyFrom(helix6);
            helix10.StartPoint = new Point3d(6.0, 10.0, 0);
            helix10.SetAxisPoint(new Point3d(4.0, 10.0, 0), false);
            helix10.Constrain = ConstrainType.TurnHeight;
            helix10.TurnHeight = 2;
            helix10.CreateHelix();

            // Creates a helix with clockwise turns direction 
            Helix helix11 = new Helix();
            btr.AppendEntity(helix11);
            helix11.CopyFrom(helix6);
            helix11.StartPoint = new Point3d(-18.0, 20.0, 0);
            helix11.SetAxisPoint(new Point3d(-20.0, 20.0, 0), false);
            helix11.Twist = false;
            helix11.CreateHelix();

            // Creates a helix with the Turns changed to 5 with ConstrainType.TurnHeight
            Helix helix12 = new Helix();
            btr.AppendEntity(helix12);
            helix12.CopyFrom(helix11);
            helix12.StartPoint = new Point3d(-12.0, 20.0, 0);
            helix12.SetAxisPoint(new Point3d(-14.0, 20.0, 0), false);
            helix12.Constrain = ConstrainType.TurnHeight;
            helix12.Turns = 5;
            helix12.CreateHelix();

            // Creates a helix with the Turns changed to 5 with ConstrainType.Height
            Helix helix13 = new Helix();
            btr.AppendEntity(helix13);
            helix13.CopyFrom(helix11);
            helix13.StartPoint = new Point3d(-6.0, 20.0, 0);
            helix13.SetAxisPoint(new Point3d(-8.0, 20.0, 0), false);
            helix13.Constrain = ConstrainType.Height;
            helix13.Turns = 5;
            helix13.CreateHelix();
          }
          ta.Commit();
        }
        db.SaveAs(path + "HelixEx.dwg", DwgVersion.Current);
      }
    }
  }
}

其他

块定义

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;


namespace CDevGuideExamplesProject
{
  public class BlockEx
  {
    const String path1 = "BlockEx1.dwg";

    public BlockEx(string path)
    {
      using (Database db = new Database(true, true))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          using (BlockTable bt = (BlockTable)ta.GetObject(db.BlockTableId, OpenMode.ForWrite))
          {
            // Get the modelspace block
            using (BlockTableRecord modelSpace = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite))
            {
              // Create a new database to be attached as external reference  
              CreateDatabase(path);
              // Attach the created database as an exernal reference to the current database
              ObjectId xRefId = db.AttachXref(path1, "ExternalBlock");

              // Create a BlockReference object that refer to the external block and add it to the modelspace block
              using (BlockTableRecord xBlock = (BlockTableRecord)xRefId.GetObject(OpenMode.ForWrite))
              {
                // Reload xref
                ObjectIdCollection xrefCollection = new ObjectIdCollection();
                xrefCollection.Add(xBlock.ObjectId);
                db.ReloadXrefs(xrefCollection);
                // Create a block reference and append it to modelspace
                BlockReference xBlockRef = new BlockReference(Point3d.Origin, xRefId);
                modelSpace.AppendEntity(xBlockRef);
              }
            }
          }
          ta.Commit();
        }
        db.SaveAs(path + "BlockEx2.dwg", DwgVersion.Current);
      }
    }

    // Create a new database, add new block definition and block reference objects. This database will be attached as an external reference.  
    static void CreateDatabase(String path)
    {
      Database db = new Database(true, true);
      TransactionManager tm = db.TransactionManager;
      using (Transaction ta = tm.StartTransaction())
      {
        using (BlockTableRecord modelSpace = (BlockTableRecord)ta.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))
        {
          using (BlockTable bt = (BlockTable)ta.GetObject(db.BlockTableId, OpenMode.ForWrite))
          {
            using (BlockTableRecord blk1 = new BlockTableRecord())
            {
              blk1.Name = "Block1";
              bt.Add(blk1);
              blk1.Origin = new Point3d(0, 5, 0);

              // Add some entities to the block definition blk1
              Circle cir1 = new Circle();
              blk1.AppendEntity(cir1);
              cir1.Radius = 3;

              Circle cir2 = new Circle();
              cir2.Thickness = 1;
              blk1.AppendEntity(cir2);
              cir2.Radius = 5;

              Line line = new Line(new Point3d(0, -6, 0), new Point3d(0, 6, 0));
              blk1.AppendEntity(line);

              // Create a block reference for the block definition blk1, set additional properties and explode this block reference
              using (BlockReference blockRef = new BlockReference(Point3d.Origin, blk1.ObjectId))
              {
                modelSpace.AppendEntity(blockRef);
                blockRef.ScaleFactors = new Scale3d(2, 2, 2);
                blockRef.Rotation = 0.7;
                blockRef.Position = new Point3d(5, 0, 0);
                try
                {
                  blockRef.ExplodeToOwnerSpace();
                }
                catch (Teigha.Runtime.Exception ex)
                {
                  System.Console.WriteLine("The block cannot be exploded");
                  System.Console.WriteLine("{0}", ex.Message);
                }
              }
            }
          }
        }
        ta.Commit();
      }
      db.SaveAs(path + path1, DwgVersion.Current);
      db.Dispose();
    }
  }
}

绑定

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.Runtime;
using Teigha.DatabaseServices;
using Teigha.Geometry;


namespace CDevGuideExamplesProject
{
  public class BindXrefsEx
  {
    //const String path1a = "c:\\AL-01-20-010.dwg";
    //const String path1aa = "X-01-PITCH.dwg";
    //const String path1aa = "AL-01-20-010.dwg";
    //const String path2a = "c:\\ArcEx2.dwg";
    const String path1a = "master.dwg";
    //const String path2a = "D:\\Sample\\xref\\masterBind.dwg";

    public BindXrefsEx(string path)
    {
      using (Database db = new Database(false, false))
      {
        TransactionManager tm = db.TransactionManager;
        using (Transaction ta = tm.StartTransaction())
        {
          db.ReadDwgFile(path + path1a, FileOpenMode.OpenForReadAndAllShare, false, "", false);
          //db.ReadDwgFile(path + path1aa, FileOpenMode.OpenForReadAndAllShare, false, "", false);

          ObjectIdCollection xrefCollection = new ObjectIdCollection();

          using (BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForWrite)) //.Open(OpenMode.ForRead))
          {
            foreach (ObjectId id in bt)
            {
              using (BlockTableRecord pBlock = (BlockTableRecord)id.GetObject(OpenMode.ForWrite))
              {
                XrefStatus status = pBlock.XrefStatus;
                if (pBlock.XrefStatus != XrefStatus.NotAnXref)
                {
                  switch (status)
                  {
                    case Teigha.DatabaseServices.XrefStatus.Unresolved:
                      {
                        ObjectIdCollection xrefCollectTmp = new ObjectIdCollection();
                        xrefCollectTmp.Add(id);
                        try
                        {
                          db.ReloadXrefs(xrefCollectTmp);
                          xrefCollection.Add(id);
                        }
                        catch (Teigha.Runtime.Exception ex)
                        {
                          if (ex.Message == "eFileNotFound")
                          {
                            continue;
                          }
                        }
                      }
                      break;
                    case Teigha.DatabaseServices.XrefStatus.Resolved:
                      xrefCollection.Add(id);
                      break;
                    case Teigha.DatabaseServices.XrefStatus.FileNotFound:
                    case Teigha.DatabaseServices.XrefStatus.Unreferenced:
                    case Teigha.DatabaseServices.XrefStatus.Unloaded:
                      try
                      {
                        db.DetachXref(pBlock.Id);
                      }
                      catch (Teigha.Runtime.Exception ex)
                      {
                        if (ex.Message == "eXRefDependent")
                        {
                          continue;
                        }
                      }
                      break;
                    default:
                      break;
                  }
                  //                  db.ReloadXrefs(xrefCollection);
                }
              }
            }
            try
            {
              db.ReloadXrefs(xrefCollection);
              db.BindXrefs(xrefCollection, true);
            }
            catch (Teigha.Runtime.Exception ex)
            {
              System.Console.WriteLine("The block cannot be bound");
              System.Console.WriteLine("{0}", ex.Message);
            }
          }

          ta.Commit();
          //ta.Abort();
        }
        db.SaveAs(path + "masterBind.dwg", DwgVersion.Current);
      }
    }
  }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三好学生~张旺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值