VB.NET开发Autocad,拿来就用的常用语句
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Imports AcApps = Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.Runtime
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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、打开模型空间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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、打开标注样式表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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、打开块表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 获取距离
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 获取点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 获取整数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 获取选择集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 定义图块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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