#Region " Namespaces " Imports AcDb = Autodesk.AutoCAD.DatabaseServices Imports AcGe = Autodesk.AutoCAD.Geometry Imports AcRx = Autodesk.AutoCAD.Runtime Imports System.Math #End Region Public Module Module1 _ Public Sub DrawLineTest() Module1.DrawLine(New AcGe.Point3d(1000, 1000, 0), Module1.DTR(30), 1000) End Sub Friend Function DrawLine(ByVal startPoint As AcGe.Point3d, _ ByVal angle As Double, ByVal length As Double) As AcDb.Line Dim id As AcDb.ObjectId Dim line As AcDb.Line = Nothing Using db As AcDb.Database = AcDb.HostApplicationServices.WorkingDatabase() Dim endpoint As AcGe.Point3d = Module1.PolarPoint(startPoint, angle, length) startPoint = Module1.TransformByUCS(startPoint, db) endpoint = Module1.TransformByUCS(endpoint, db) Using tr As AcDb.Transaction = db.TransactionManager.StartTransaction Try Dim btr As AcDb.BlockTableRecord = tr.GetObject(db.CurrentSpaceId, AcDb.OpenMode.ForWrite, False) line = New AcDb.Line(startPoint, endpoint) id = btr.AppendEntity(line) db.TransactionManager.AddNewlyCreatedDBObject(line, True) tr.Commit() Catch ex As Exception tr.Abort() End Try End Using End Using Return line End Function Friend Function TransformByUCS(ByVal point As AcGe.Point3d, ByVal db As AcDb.Database) As AcGe.Point3d Return point.TransformBy(GetUcsMatrix(db)) End Function Friend Function GetUcsMatrix(ByVal db As AcDb.Database) As AcGe.Matrix3d Dim origin As AcGe.Point3d = db.Ucsorg Dim xAxis As AcGe.Vector3d = db.Ucsxdir Dim yAxis As AcGe.Vector3d = db.Ucsydir Dim zAxis As AcGe.Vector3d = xAxis.CrossProduct(yAxis) Return AcGe.Matrix3d.AlignCoordinateSystem(AcGe.Point3d.Origin, AcGe.Vector3d.XAxis, AcGe.Vector3d.YAxis, _ AcGe.Vector3d.ZAxis, origin, xAxis, yAxis, zAxis) End Function Friend Function PolarPoint(ByVal BasePoint As AcGe.Point3d, ByVal angle As Double, ByVal distance As Double) As AcGe.Point3d Dim x As Double = distance * Cos(angle) Dim y As Double = distance * Sin(angle) Return New AcGe.Point3d(BasePoint.X + x, BasePoint.Y + y, BasePoint.Z) End Function Friend Function DTR(ByVal ang As Double) As Double Return ang / 180 * PI End Function End Module