AutoCAD Mechanical and Visual Studio (VB) connection

AutoCAD Mechanical and Visual Studio (VB) connection

danijel.radenkovic
Collaborator Collaborator
2,873 Views
11 Replies
Message 1 of 12

AutoCAD Mechanical and Visual Studio (VB) connection

danijel.radenkovic
Collaborator
Collaborator

Hello to all,

 

I have done it successful with Inventor 2017 and Visual Basic 2010 Express. So, when I start Visual Basic I have Inventor 2017 Addin template.

 

I would like to make the same connection between Visual Basic Express and AutoCAD Mechanical 2017 to have also template:

img12.PNG

 

Is it possible or not?

 

Cause, something that I have read on the http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=18162797#steps is that I need Visual Studio 2015.  Also, do I need classic AutoCAD or I can continue with AutoCAD Mechanical 2017?

 

Any help is very appreciated.

Danijel

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Accepted solutions (1)
2,874 Views
11 Replies
Replies (11)
Message 2 of 12

_gile
Consultant
Consultant
Accepted solution

Hi,

 

IMHO, you should use Visual Studio 2015 (Community Edition is free), and learn how to start an AutoCAD plug-in project from scratch.

Project templates are usefull, but do not teach anything about the Visual Studio project itself.

When you'll understand the project structure, you can quite easily build your own templates.

You can see this thread.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 12

danijel.radenkovic
Collaborator
Collaborator

@_gile wrote:

Hi,

 

IMHO, you should use Visual Studio 2015 (Community Edition is free), and learn how to start an AutoCAD plug-in project from scratch.

Project templates are usefull, but do not teach anything about the Visual Studio project itself.

When you'll understand the project structure, you can quite easily build your own templates.

You can see this thread.

 


Hello sir,

You are right. I have always believed that Autodesk VB plug-in Templates are better for using cause they includes all references. So, I agree with your suggestion to start with creating projects from scratch.

I have also few more things where I need advice:

1. Do I need classic AutoCAD to iterate with Visual Studio 2015 or it is more than enough to have just AutoCAD Mechanical?

2. Does AutoCAD have possibility to iterate with Visual Studio via Apprentice as it is possible with Inventor?

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 4 of 12

_gile
Consultant
Consultant

Hi,

 

I do not use Mechanical but you should be able to program the AutoCAD part referencing the standard libraries and probably reference some specific ones for the Mechanical part.

 

About Apprentice, it seems to be specific to Inventor (I never use it).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 12

danijel.radenkovic
Collaborator
Collaborator

@_gile wrote:

Hi,

 

I do not use Mechanical but you should be able to program the AutoCAD part referencing the standard libraries and probably reference some specific ones for the Mechanical part.

 

About Apprentice, it seems to be specific to Inventor (I never use it).


Thank you very much for the quick answer.

Apprentice access to properties is very useful cause don't need Inventor to be opened to manage with some data and properties. I was thinking it is also possible with AutoCAD, so I just can list all attributes via VisualBasic and manage with them.

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 6 of 12

danijel.radenkovic
Collaborator
Collaborator

I have found an code to list all of the attributes of the opened drawing, but there is something that I did wrong.

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

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim AcadApp As Object
        Dim dbxDoc As Object

        AcadApp = StartAutoCADSession()
        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

startacadsession.PNG

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 7 of 12

_gile
Consultant
Consultant

Hi,

 

If you absolutely need to use COM API (stand-alone exe), you should try to get already runing AutoCAD with GetObject and if it fails create a new one with GetObject

 

Not sure about VB syntax

Dim acadApp A AcadApplication
' Get a running instance of AutoCAD
Try
    acadApp = GetObject(, "AutoCAD.Application")
    ' An error occurs if no instance is running
Catch
    Try
        ' Create a new instance of AutoCAD
        acadApp = CreateObject("AutoCAD.Application")
    Catch
        ' If an instance of AutoCAD is not created then message and exit
        MsgBox("Instance of 'AutoCAD.Application' could not be created.")
        Exit Sub
    End Try
End Try


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 12

danijel.radenkovic
Collaborator
Collaborator

@_gile wrote:

Hi,

 

If you absolutely need to use COM API (stand-alone exe), you should try to get already runing AutoCAD with GetObject and if it fails create a new one with GetObject

 

Not sure about VB syntax

Dim acadApp A AcadApplication
' Get a running instance of AutoCAD
Try
    acadApp = GetObject(, "AutoCAD.Application")
    ' An error occurs if no instance is running
Catch
    Try
        ' Create a new instance of AutoCAD
        acadApp = CreateObject("AutoCAD.Application")
    Catch
        ' If an instance of AutoCAD is not created then message and exit
        MsgBox("Instance of 'AutoCAD.Application' could not be created.")
        Exit Sub
    End Try
End Try

I agree, I would like to iterate with already opened acad document, but there is a error with definition:

not defined.PNG

I have tried also with:

Dim acadApp as AutoCAD.AcadApplication
Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 9 of 12

danijel.radenkovic
Collaborator
Collaborator

I made mistake cause I have not included reference for AutoCAD 2017 Type Library.

 

defined.PNG

 

It is ok now. I have adapted the code but still I am not getting the information about attributed from the drawing.

 

attributes.PNG

 

I have tried with:

Dim acadApp As AutoCAD.AcadApplication
        ' Get a running instance of AutoCAD
        Try
            acadApp = GetObject(, "AutoCAD.Application")
            ' An error occurs if no instance is running
        Catch
            Try
                ' Create a new instance of AutoCAD
                acadApp = CreateObject("AutoCAD.Application")
            Catch
                ' If an instance of AutoCAD is not created then message and exit
                MsgBox("Instance of 'AutoCAD.Application' could not be created.")
                Exit Sub
            End Try
        End Try

        'Dim AcadApp As Object
        Dim dbxDoc As Object

        'AcadApp = StartAutoCADSession()
        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
                    MsgBox("sss")
                    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
Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 10 of 12

danijel.radenkovic
Collaborator
Collaborator
Dim acadApp As AutoCAD.AcadApplication
        ' Get a running instance of AutoCAD
        Try
            acadApp = GetObject(, "AutoCAD.Application")
            ' An error occurs if no instance is running
        Catch
            Try
                ' Create a new instance of AutoCAD
                acadApp = CreateObject("AutoCAD.Application")
            Catch
                ' If an instance of AutoCAD is not created then message and exit
                MsgBox("Instance of 'AutoCAD.Application' could not be created.")
                Exit Sub
            End Try
        End Try

        'Dim AcadApp As Object
        Dim dbxDoc As Object

        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

The only thing that I am trying to do is to list all blocks and their attributes in msgbox.

Code above doesn't work. Does anyone know why?

 

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 11 of 12

norman.yuan
Mentor
Mentor

A bit advice: do not just list a big chunk of code and simply say it does not work and let potential respondents play a guess game (OK, your code is not too long. But you get the idea). You need to explain in detail what "does not work" mean? is the code execution breaks with error (error message, at which line)? or the code when through without expected result (if so, did you step through the code to examine the data in variables to find out why easily)?

 

I can see a couple of potential issues:

 

1. If the code goes via the first Try...Catch...block's "Catch..."clause (having to create a new AutoCAD instance, the code follows may not work, depending on AutoCAD's "Startup" mode: there may not be a "ActiveDocument" to work with.

 

2. If TypeName(entity) = "IAcadBlockReference" Then ... may not be good choice, because an AcadBlockReference may be derived from difference Interface, depending on AutoCAD's version. (if I have to guess, this is the line of code that might bypass the real work you need). The reliable code should be like:

 

If TypeOf entity Is AcadBlockReference Then

   ...you try to retrieve attribute information here

End if

 

It is very easy to step through the code and find where the code does not work as expected:

 

  • place a break point at a line of code. If the execution does not hit it (execution does not stop at the break point, code before the break point could be wrong
  • Once the break point is hit, press F10/F11 to execute the code line by line. IN the meantime you can examine the variables in the code to see what the information in it.

If you do these, you might have answered your own question in just a few minutes.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 12 of 12

danijel.radenkovic
Collaborator
Collaborator

Hello,

Apologize for my problem is not clearly explained. I will try to explain it better. I have installed AutoCAD Mechanical 2017 and I am trying to iterate with some opened AutoCAD drawing through Visual Basic 2015, to get some block attributes from it.

By your suggestion, I have modified the code little bit, but I am still not getting the return of the block attributes (block name, attribute's tag & attribute's value) .

This is the first time that I am trying to iterate with AutoCAD drawing using Visual Basic.

 

 

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes