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

一,导入命名空间

二、使用事务处理

三、打开各种表

四、各种Get

五、定义图块、插入图块

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 一、导入命名空间 

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

 

 二、使用事务处理

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
View Code

  三、打开各种表

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
View Code

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
View Code

  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
View Code

 

 四、各种Get

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
View Code

 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)
View Code

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
View Code

 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
View Code

 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
View Code

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
View Code

 

 五、定义图块、插入图块  

 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    
View Code

 

 

 

posted @ 2023-06-22 16:52  rf8862  阅读(178)  评论(0编辑  收藏  举报