Tring to get my vb.net to run with Autocad

Tring to get my vb.net to run with Autocad

Anonymous
Not applicable
1,117 Views
6 Replies
Message 1 of 7

Tring to get my vb.net to run with Autocad

Anonymous
Not applicable

Hi,

 

Its been a long time since i have built the last AutoCAD app in vb.net i have the following code pluss from memory have added the Acdbmgd.dll and ACmgd.dll as refrences but i cant seem to get the code to work... the version of autocad im running is Autocad Mechanical 2016

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim AcadApp As Object
        Dim dbxDoc As Object

        AcadApp = ThisDrawing.Application
        dbxDoc = AcadApp.Activedocument
        AcadApp.Visible = True

        Dim I As Integer
        Dim strAttributes As String
        Dim varAttributes As Object
        Dim entity As Object

        For Each entity In dbxDoc.PaperSpace

            If TypeName(entity) = "IAcadBlockReference" Then
                If entity.hasattributes = True Then

                    varAttributes = entity.GetAttributes
                    strAttributes = ""

                    For I = LBound(varAttributes) To UBound(varAttributes)

                        MsgBox("BLOCKNAME:  " & entity.name & vbCr & "TAG             :  " & varAttributes(I).TagString & vbCr & "VALUE         :  " & varAttributes(I).TextString)

                    Next
                End If
            End If
        Next entity
    End Sub
End Class
0 Likes
1,118 Views
6 Replies
Replies (6)
Message 2 of 7

jeff
Collaborator
Collaborator

You mentioned you have not built one in a while, but since I think 2013 you need to also reference accoremgd.dll.

You can also find your answers @ TheSwamp
0 Likes
Message 3 of 7

norman.yuan
Mentor
Mentor

You need to tell what your apap type is (DLL that loaded inside AutoCAD to run, or EXE that run outside AutoCAD and automates AutoCAD) by saying "run with AutoCAD".

 

Your code shows you use AutoCAD COM API. So, if it is EXE, you do not need to reference to acaddbmgd/acmgd/accoremgd.dll (and you CANNOT, anyway); if it is a DLL that is "NETLOADED" into AutoCAD, you need to refernce to accoremgd.dll, unless you use AutoCAD2012 or earlier.

 

Let's assume you are doing DLL running inside AutoCAD. Then what does it mean "not work"? 

 

Can your code compile and run?

 

Any error? Did you do debugging to single out the line of code that breaks the run? How do you get "ThisDrawing" object?

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 7

BKSpurgeon
Collaborator
Collaborator

Hi Bart

 

 

Please let us know the errors (if any) are occurring and what they are, and on which line your code is breaking. that makes things easier to trouble shoot. and it will help future readers who pass by this post.

 

rgds

 

BK

0 Likes
Message 5 of 7

Anonymous
Not applicable

The error I keep getting is as follows,


Error BC30451 'ThisDrawing' is not declared. It may be inaccessible due to its protection level.

 

I cant work out a solution to this, I thought I might of skiped somthing but even with the AcCoreMgd.dll refrenced it makes this error?

0 Likes
Message 6 of 7

_gile
Consultant
Consultant

Hi,

 

You do not define ThisDrawing which doesn't exist by default in VB as it does in VBA.

 

Try this:

 

Imports Autodesk.AutoCAD.ApplicationServices

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim AcadApp As Object
        Dim dbxDoc As Object
        Dim ThisDrawing As Object

        ThisDrawing = DocumentExtension.GetAcadDocument(Application.DocumentManager.MdiActiveDocument)
        AcadApp = ThisDrawing.Application
        dbxDoc = AcadApp.Activedocument
        AcadApp.Visible = True

        Dim I As Integer
        Dim strAttributes As String
        Dim varAttributes As Object
        Dim entity As Object

        For Each entity In dbxDoc.PaperSpace

            If TypeName(entity) = "IAcadBlockReference" Then
                If entity.hasattributes = True Then

                    varAttributes = entity.GetAttributes
                    strAttributes = ""

                    For I = LBound(varAttributes) To UBound(varAttributes)

                        MsgBox("BLOCKNAME:  " & entity.name & vbCr & "TAG             :  " & varAttributes(I).TagString & vbCr & "VALUE         :  " & varAttributes(I).TextString)

                    Next
                End If
            End If
        Next entity
    End Sub
End Class

 

Anyway, the code you provided is a kind of mix of VBA (ThisDrawing) and VB.net using the COM API with late binding.

If you want to learn .NET, I'd recommend you to avoid, as far as possible, using the COM API (AutoCAD .NET API is much more safe and powerfull) and above all using late binding which deprives you of the support provided by Visual Studio (intellisense).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 7

arcticad
Advisor
Advisor

Here is some Sample code: 

 

Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports System.Collections.Generic
Imports Autodesk.AutoCAD.Runtime
Imports System.Windows.Forms

Namespace PaperTest
    Public Class clsPaperSpaceTest
        <CommandMethod("PaperTest", CommandFlags.Modal)> _
        Public Sub SelectPaperSpace()

            ' Get Current Drawing
            Dim acDwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            ' Get Current Drawing's Database
            Dim acDb As Database = acDwg.Database

            ' Start Transaction
            Using acTrans As Transaction = acDb.TransactionManager.StartTransaction()
                ' Lock Drawing for changes
                Using acDwg.LockDocument()

                    ' get Paperspace Database
                    Dim acBlkTbl As BlockTable = TryCast(acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead), BlockTable)
                    Dim acBlkTblRec As BlockTableRecord = TryCast(acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), OpenMode.ForRead), BlockTableRecord)

                    ' Iterate through Database
                    For Each objItem As ObjectId In acBlkTblRec

                        ' Convert ObjectID to Entity
                        Dim Entity As Entity = TryCast(acTrans.GetObject(objItem, OpenMode.ForRead), Entity)

                        ' Check if Entity is Block Reference
                        If TypeOf Entity Is BlockReference Then
                            Dim acBlkRef As BlockReference = TryCast(acTrans.GetObject(objItem, OpenMode.ForRead), BlockReference)

                            ' Get Dynamic Blocks Effective Name
                            Dim btr As BlockTableRecord = DirectCast(acTrans.GetObject(acBlkRef.DynamicBlockTableRecord, OpenMode.ForRead), BlockTableRecord)
                            Dim strBlockName As String = btr.Name

                            ' Get Attribute Collection
                            Dim attCol As AttributeCollection = acBlkRef.AttributeCollection

                            For i As Integer = 0 To attCol.Count - 1
                                ' Convert Collection to Attribute Reference
                                Dim attRef As AttributeReference = TryCast(acTrans.GetObject(attCol(i), OpenMode.ForRead), AttributeReference)

                                ' Print Data to messagebox and check if block is an anonymouse block and return original name                                
                                MessageBox.Show((Convert.ToString("BlockName: ") & strBlockName) + "TAG: " + attRef.Tag.ToString())
                            Next
                        End If
                    Next
                    ' Commit even if nothing changes, This is faster than Dispose
                    acTrans.Commit()
                End Using
            End Using
        End Sub
    End Class
End Namespace
---------------------------



(defun botsbuildbots() (botsbuildbots))
0 Likes