How to import libraries into "Visual Basic for Applications"

How to import libraries into "Visual Basic for Applications"

Anonymous
Not applicable
2,212 Views
2 Replies
Message 1 of 3

How to import libraries into "Visual Basic for Applications"

Anonymous
Not applicable

Hi All, 

I'm new into AutoCAD programming (I'm used to developing Revit Apps through the API) but now my company needs to update some old VB routines (made to work in AutoCAD 2012) in AutoCAD 2018. 

 

I found this thread (quite old), and I'm trying to replicate the explained procedure in the "Visual Basic for Applications" Editor that comes with AutoCAD 2018 (Tools>Macro>Visual Basic Editor).

 

https://forums.autodesk.com/t5/net/enodatabase-with-inserting-a-block-c/td-p/4653083

 

Example Code:

 

<CommandMethod("IBLK")> Public Sub InsertBLockfromOutside()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor
        Dim blockname As String = "TestBLK"
        Dim drawingPath As String = "C:\Users\alamzaki\Desktop\Test_data\PG-VSON-4-1_afattintel_test.dwg"
        Dim blkPos As Point3d = New Point3d(0, 0, 0)
        Try
            Using tx As Transaction = db.TransactionManager.StartTransaction()
                Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
                Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                If Not bt.Has(blockname) Then
                    InsertBlock(blockname, drawingPath, blkPos, db, ms, tx)
                End If
                tx.Commit()
            End Using
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try

End Sub

Public Sub InsertBlock(blkName As String, dwgPath As String, positon As Point3d, acDb As Database, btr As BlockTableRecord, trans As Transaction)
        Dim dbdwg As Database = New Database(False, True)
        dbdwg.ReadDwgFile(dwgPath, FileOpenMode.OpenForReadAndAllShare, True, "")
        Dim id As ObjectId = acDb.Insert(blkName, dbdwg, True)
        dbdwg.Dispose()
        Dim blkRef As New BlockReference(positon, id)
        btr.AppendEntity(blkRef)
        trans.AddNewlyCreatedDBObject(blkRef, True)
    End Sub

 

The methods created calls some objects that I cannot find (i.e. Database, Point3d, Transaction, etc.) and don't really know where to add a library (my intuition is that if I'm working directly into AutoCAD I don't really need to call for a library - API, right?)

 

So, what am I missing here? Any tip of advice will be well received.

 

Thanks in advance!

 

 

0 Likes
2,213 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

I already came to choose which libraries I want to work with (Tools>References).

 

Also, I realized that using the VB7.1 I need to declare and then assign a variable (I'm used to work with C#, VB is now for me). But when I try to declare a variable "doc" as "Document", the "Document" doesn't shows in the IntelliSense display.

 

What am I missing here?

0 Likes
Message 3 of 3

norman.yuan
Mentor
Mentor

By "update some old VB routines", I assume you are talk old VBA code. However the example code you quoted is AutoCAD .NET API code, which is quite different thing and CANNOT be used in AutoCAD VBA (VBA uses AutoCAD COM API, BTW).

 

While AutoCAD VBA is still alive, in most cases, if not all, it would be better to invest AutoCAD customizing programming effort into AutoCAD .NET API than quite out-dated VBA (especially when you have .NET programming background, and just start learning AutoCAD programming anyway), unless the old VBA code only needs very trivial update (you may still need to be quite good and knowledgable at AutoCAD programming, though). If the old VBA code is 32-bit based, and has quite some complicated UI, the chances are, you simply cannot upgrade the code for 64-bit AutoCAD VBA beccause of lack of COM component support in 64-bit VBA. If I were you, I'd not waste time on AutoCAD VBA, and start (to learn) with AutoCAD .NET API.

 

Direct to your question: no you cannot use .NET libraries (such as your example code) in AutoCAD VBA (unless you expose the .NET code as COM component, which would be another painful process to handle).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes