VB.NET开发Autocad,拿来就用的常用语句

Imports AcApps = Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.Runtime

Dim doc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument Using dc As AcApps.DocumentLock = doc.LockDocument Using tr As Transaction = doc.Database.TransactionManager.StartTransaction End Using End Using
3.1、打开模型空间

Dim doc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument Using dc As AcApps.DocumentLock = doc.LockDocument Using tr As Transaction = doc.TransactionManager.StartTransaction() Using bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) Using btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite) ‘‘‘‘‘‘‘‘’’’’’’’’ End Using End Using tr.Commit() End Using End Using
3.2、打开标注样式表

Using dc As AcApps.DocumentLock = doc.LockDocument Using tr As Transaction = doc.Database.TransactionManager.StartTransaction Using DimStyleTbl As DimStyleTable = DirectCast(tr.GetObject(doc.Database.DimStyleTableId, OpenMode.ForWrite), DimStyleTable) ‘‘‘‘‘’’’’’ End Using End Using End Using
3.3、打开块表

Dim doc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument Using dc As AcApps.DocumentLock = doc.LockDocument Using tr As Transaction = doc.TransactionManager.StartTransaction() Using bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite) If bt.Has(BlkName) Then Else End If End Using tr.Commit() End Using End Using
4.1 获取距离

Dim doc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument Dim pdo As New PromptDistanceOptions(vbLf & "第一点") With pdo .AllowNone = True .AllowZero = False .AllowNegative = False .UseDashedLine = True End With Dim pdr As PromptDoubleResult = doc.Editor.GetDistance(pdo) If Not pdr.Status = PromptStatus.OK Then Exit Sub End If
4.2 获取Double

Dim doc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument Dim PDO As New PromptDoubleOptions(vbLf & " ") With PDO .AllowNegative = False .AllowZero = False .AllowNone = True End With Dim pdr As PromptDoubleResult = doc.Editor.GetDouble(PDO)
4.3 获取对象 (Entity)

Dim doc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument Dim peo As New PromptEntityOptions("指定对象") With peo .AllowNone = True End With Dim per As PromptEntityResult = doc.Editor.GetEntity(peo) If Not per.Status = PromptStatus.OK Then Exit Sub End If
4.4 获取点

Dim doc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument Dim PPO As New PromptPointOptions("拾取插入点") With PPO .AllowNone = True End With Dim ppr As PromptResult = doc.Editor.GetPoint(PPO) If Not ppr.Status = PromptStatus.OK Then Exit Sub End If
4.5 获取整数

Dim pio As New PromptIntegerOptions("数量") With pio .LowerLimit = 1 End With Dim pir As PromptIntegerResult = doc.Editor.GetInteger(pio) If Not pir.Status = PromptStatus.OK Then Exit Sub End If
4.6 获取选择集

Dim tvs As TypedValue() = New TypedValue() {New TypedValue(CInt(DxfCode.Start), "LINE")} Dim SF As New SelectionFilter(tvs) Dim psr As PromptSelectionResult = doc.Editor.GetSelection(SF) If Not psr.Status = PromptStatus.OK Then Exit Sub End If
5.1 定义图块

Dim blockname As String = String.Format("DD.MQBK.JGT.M2Mark", FormatJiaGongTuDaiHao) Dim doc As acapps.Document = acapps.Application.DocumentManager.MdiActiveDocument Using dc As acapps.DocumentLock = doc.LockDocument Using tr As Transaction = doc.TransactionManager.StartTransaction() Using bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite) If Not bt.Has(blockname) Then Dim entList As New List(Of Entity) Dim L1 As New Line(New Point3d(0, 0, 0), New Point3d(100, 100, 0)) Dim C1 As New Circle() With {.Center = New Point3d(50, 50, 0), .Radius = 50} entList.Add(L1) entList.Add(C1) Using btr1 As New Autodesk.AutoCAD.DatabaseServices.BlockTableRecord() With btr1 For Each ent As Entity In entList .AppendEntity(ent) Next .Origin = New Point3d(0, 0, 0) .Name = blockname End With '向数据库添加图块 bt.Add(btr1) tr.AddNewlyCreatedDBObject(btr1, True) End Using tr.Commit() End If End Using End Using End Using
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!