03. Create the line


Class Inheritance Tree

Firstly, we need to understand how the drawing objects inheritance relationship.
Objects Relationship

Line Drawing

01.“Line” Class

Line is the most basic drawing object, and it is the most simple one to draw.

Firstly, let’s have a look at the class “Line”

  • method
  • property

Line Class

02.Create Line

I have two ways to create the line:

WAY1

  1. declare a line first (now the line start point and endpoint are 0,0,0)
  2. define the start and end points
  3. Assign the point to the line

Way2

  • Define the start and end point when declaring the line

Add the two lines into the BlocktableRecord and commit the transaction.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace _03.Create_Line
{
    public class LineExam
    {
        [CommandMethod("LineDemo")]
        public void LineDemo()
        { 
            //WAY1
            //declare a line object
            Line line1 = new Line();


            //declare the two points for the line
            Point3d startPnt = new Point3d(10, 10, 0);
            Point3d endPnt = new Point3d(100, 100, 0);

            //set the line properties
            line1.StartPoint = startPnt;
            line1.EndPoint = endPnt;


        
        
            //WAY1
            Line line2 = new Line(new Point3d(0, 0, 0), new Point3d(0, 100, 0));



            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                //Open the block table in read mode
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                //open the block table record in write mode
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //add the two lines into the Blocktable Record
                btr.AppendEntity(line1);
                //update the data
                tr.AddNewlyCreatedDBObject(line1, true);

                btr.AppendEntity(line2);
                tr.AddNewlyCreatedDBObject(line2, true);


                tr.Commit();


            }
        }
    }
}

03. Result

Result


Reference

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD .NET Developer’s Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-3e30.htm,topicNumber=d0e26245


Author: Ford Yang
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Ford Yang !
  TOC