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 activeDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument

Using docLock As AcApps.DocumentLock = activeDoc.LockDocument
    Using trans As Transaction = activeDoc.Database.TransactionManager.StartTransaction
        ' 작업 수행
    End Using
End Using

다양한 테이블 열기

3.1 모델 공간 열기


Dim currentDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument
Using docLock As AcApps.DocumentLock = currentDoc.LockDocument
    Using trans As Transaction = currentDoc.Database.TransactionManager.StartTransaction()
        Using blockTbl As BlockTable = trans.GetObject(currentDoc.Database.BlockTableId, OpenMode.ForRead)
            Using blockTblRec As BlockTableRecord = trans.GetObject(blockTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                ' 작업 수행
            End Using
        End Using
        trans.Commit()
    End Using
End Using

3.2 차원 스타일 테이블 열기


Using docLock As AcApps.DocumentLock = currentDoc.LockDocument
    Using trans As Transaction = currentDoc.Database.TransactionManager.StartTransaction
        Using dimStyleTbl As DimStyleTable = DirectCast(trans.GetObject(currentDoc.Database.DimStyleTableId, OpenMode.ForWrite), DimStyleTable)
            ' 작업 수행
        End Using
    End Using
End Using

3.3 블록 테이블 열기


Dim activeDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument

Using docLock As AcApps.DocumentLock = activeDoc.LockDocument
    Using trans As Transaction = activeDoc.Database.TransactionManager.StartTransaction()
        Using blockTbl As BlockTable = trans.GetObject(activeDoc.Database.BlockTableId, OpenMode.ForWrite)
            If blockTbl.Has(blockName) Then
                ' 블록이 존재하는 경우
            Else
                ' 블록이 존재하지 않는 경우
            End If
        End Using
        trans.Commit()
    End Using
End Using

다양한 Get 메서드

4.1 거양 가져오기


Dim currentDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument
Dim distOpt As New PromptDistanceEnvironmentOptions(vbLf & "첫 번째 지점 선택")
With distOpt
    .AllowNone = True
    .AllowZero = False
    .AllowNegative = False
    .UseDashedLine = True
End With
Dim distResult As PromptDoubleResult = currentDoc.Editor.GetDistance(distOpt)
 
If Not distResult.Status = PromptStatus.OK Then
    Exit Sub
End If

4.2 Double 값 가져오기


Dim currentDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument
Dim dblOpt As New PromptDoubleEnvironmentOptions(vbLf & "값 입력")
With dblOpt
    .AllowNegative = False
    .AllowZero = False
    .AllowNone = True
End With

Dim dblResult As PromptDoubleResult = currentDoc.Editor.GetDouble(dblOpt)

4.3 객체(Entity) 가져오기


Dim currentDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument
            Dim entOpt As New PromptEntityOptions("객체 지정")
            With entOpt
                .AllowNone = True
            End With
            Dim entResult As PromptEntityResult = currentDoc.Editor.GetEntity(entOpt)
            If Not entResult.Status = PromptStatus.OK Then
                Exit Sub
            End If

4.4 점(Point) 가져오기


  Dim currentDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument

        Dim ptOpt As New PromptPointOptions("삽입점 선택")
        With ptOpt
            .AllowNone = True
        End Using

        Dim ptResult As PromptResult = currentDoc.Editor.GetPoint(ptOpt)

        If Not ptResult.Status = PromptStatus.OK Then
            Exit Sub
        End If

4.5 정수(Integer) 가져오기


  Dim intOpt As New PromptIntegerOptions("수량")
        With intOpt
            .LowerLimit = 1
        End With

        Dim intResult As PromptIntegerResult = currentDoc.Editor.GetInteger(intOpt)
        If Not intResult.Status = PromptStatus.OK Then
            Exit Sub
        End If

4.6 선택 집합(Selection Set) 가져오기


  Dim typeVals As TypedValue() = New TypedValue() {New TypedValue(CInt(DxfCode.Start), "LINE")}
            Dim selFilter As New SelectionFilter(typeVals)
            Dim selResult As PromptSelectionResult = currentDoc.Editor.GetSelection(selFilter)

        If Not selResult.Status = PromptStatus.OK Then
            Exit Sub
        End If

블록 정의 및 삽입

5.1 블록 정의


        Dim blockName As String = String.Format("DD.MQBK.JGT.M2Mark", FormatDrawingNumber)
        Dim currentDoc As AcApps.Document = AcApps.Application.DocumentManager.MdiActiveDocument

        Using docLock As AcApps.DocumentLock = currentDoc.LockDocument
            Using trans As Transaction = currentDoc.Database.TransactionManager.StartTransaction()
                Using blockTbl As BlockTable = trans.GetObject(currentDoc.Database.BlockTableId, OpenMode.ForWrite)
                    If Not blockTbl.Has(blockName) Then

                        Dim entityList As New List(Of Entity)

                        Dim line1 As New Line(New Point3d(0, 0, 0), New Point3d(100, 100, 0))
                        Dim circle1 As New Circle() With {.Center = New Point3d(50, 50, 0), .Radius = 50}

                        entityList.Add(line1)
                        entityList.Add(circle1)

                        Using blockTblRec As New Autodesk.AutoCAD.DatabaseServices.BlockTableRecord()
                            With blockTblRec
                                For Each entity As Entity In entityList
                                    .AppendEntity(entity)
                                Next

                                .Origin = New Point3d(0, 0, 0)
                                .Name = blockName
                            End With

                            ' 데이터베이스에 블록 추가
                             blockTbl.Add(blockTblRec)
                            trans.AddNewlyCreatedDBObject(blockTblRec, True)
                        End Using

                        trans.Commit()
                   
                    End If
                End Using

            End Using
        End Using    

태그: VB.NET AutoCAD .NET API CAD 개발 트랜잭션 처리

6월 15일 22:25에 게시됨