.NET

.NET

Reply
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 1 of 6 (1,555 Views)
Accepted Solution

Create Autocad vb.net applciation to work in all Autocad versions

1555 Views, 5 Replies
01-28-2014 02:32 AM

I have written an applciation in vb.net which interacts with AUTOCAD for 2011 version.

 

created object as,

 

Dim AcadApp As AcadApplication

Try
                AcadApp = GetObject(, "AutoCAD.Application.18.1")
            Catch ex As Exception
                AcadApp = CreateObject("AutoCAD.Application.18.1")
            End Try

 

now the applciation needs to be run in 2010 & lower versions.

Can anyone help me out on how to create a object which works for all versions.

 

Thank you.

I have got it now. I wrote as,

 

Try
                AcadApp = GetObject(, "AutoCAD.Application")
                Threading.Thread.Sleep(500)
            Catch ex As Exception
                AcadApp = CreateObject("AutoCAD.Application")
            End Try

 

I used threading.thread.sleep(500), then it is working in all versions.

 

Thanks alot for all for oyur immediate responses.

New Member
Alecia0001
Posts: 1
Registered: ‎01-28-2014
Message 2 of 6 (1,545 Views)

Re: Create Autocad vb.net applciation to work in all Autocad versions

01-28-2014 03:32 AM in reply to: muthineni

On eof my friend is a CAd engineer, I think he will help you out for this, here is the profile I have shared this thread to him also, he will get back to in sometime. But if you have some urgency you can contact him directly.

*Expert Elite*
norman.yuan
Posts: 1,063
Registered: ‎04-27-2009
Message 3 of 6 (1,529 Views)

Re: Create Autocad vb.net applciation to work in all Autocad versions

01-28-2014 06:35 AM in reply to: muthineni

In general, if you want the code to work with multiple version of AutoCAD, in the method CreateObject()/GetObject() you pass non-version speicific program ID:

 

           Try
                AcadApp = GetObject(, "AutoCAD.Application")
            Catch ex As Exception
                AcadApp = CreateObject("AutoCAD.Application")
            End Try

 

So, that the the code runs, it would try to run whatever version of AutoCAD installed in the running computer.

 

However, since your project is referenced to AutoCAD 2011 COM type library (because you delaration: Dim AcadApp As AcadApplication, e.g. early binding), therefore the application you are doing is depending on AutoCAD 2011. Whether the app works with other version of AutoCAD totally depends on Autodesk's decision of how AutoCAD is version compatible. In general, it is backward-compatible. That means, the app you are doing would likely work with a few later version of AutoCAD (AutoCAD 2012, 2013...), but very likely not with any earlier version (AutoCAD 2010, 2009...).

 

The usual approach is to use the earlest version of AutoCAD from your targeted AutoCAD versions your app want to work with for the development. Somtimes using late binding may help overcome potential issues, such as running the app with computers where multiple AutoCAD versions are installed.

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 6 (1,505 Views)

Re: Create Autocad vb.net applciation to work in all Autocad versions

01-28-2014 12:53 PM in reply to: muthineni

Not sure about if this helps, just found my old project created for A2009

You can change references to your current release:

Imports System.Windows.Forms
Imports System.IO
'Reference to AutoCAD/ObjectDBX Common 17.0 Type Library:
Imports Autodesk.AutoCAD.Interop.Common
'Reference to AutoCAD 2009 Type Library:
Imports Autodesk.AutoCAD.Interop

' On form drop from Toolbox: FolderBrowserDialog1, 2 buttons , 3 labels and 4 text boxes
' (see picture)

Public Class Form1
    Dim folder As String = ""
    Dim files As List(Of String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        files = New List(Of String)
    End Sub
    'Button1 - Text "Batch"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            ChangeBlocksDBX(files, TextBox1.Text, TextBox2.Text, TextBox3.Text)
            MessageBox.Show("Done")
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCr & ex.StackTrace)
        End Try

    End Sub
    'Button2 - text "Select Folder"
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dia As FolderBrowserDialog = Me.FolderBrowserDialog1
        With dia
            .SelectedPath = "C:\Test\BATM\" '<-- change default folder here
            .Description = "Select a folder: "
        End With

        Dim res As Windows.Forms.DialogResult = dia.ShowDialog()
        If res = Windows.Forms.DialogResult.OK Then
            folder = dia.SelectedPath
        End If
        TextBox4.Text = folder
        Try
            Dim dwgFiles() As String = Directory.GetFiles(folder, "*.dwg", SearchOption.TopDirectoryOnly) ''.Select(Function(ff) Path.GetFileName(ff))
            For Each dwgname As String In dwgFiles
                files.Add(dwgname)
            Next dwgname
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        If files.Count = 0 Then
            MessageBox.Show("no files")

        End If


    End Sub
    Private Sub ChangeBlocksDBX(ByVal dwgFiles As List(Of String), ByVal blkname As String, ByVal atag As String, ByVal aValue As String)

        Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication = Nothing
        Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument = Nothing

        Dim oEnt As AcadEntity
        Dim oblkRef As AcadBlockReference
        Dim oAtt As AcadAttributeReference
        Dim oBlock As AcadBlock
        Dim oLayouts As AcadLayouts
        Dim oLayout As AcadLayout

        AcadApp = CType(GetObject(, "Autocad.Application"), Autodesk.AutoCAD.Interop.AcadApplication)

        acadDoc = CType(AcadApp.ActiveDocument, Autodesk.AutoCAD.Interop.AcadDocument)

        Dim dbxvers As String = "ObjectDBX.AxDbDocument." & AcadApp.ActiveDocument.GetVariable("acadver").ToString().Substring(0, 2)

        Dim dbxDoc As AxDbDocument

        dbxDoc = acadDoc.Application.GetInterfaceObject(dbxvers)

        Dim dwgName As String

        On Error Resume Next

        For Each dwgName In dwgFiles

            dbxDoc.Open(dwgName)

            oLayouts = CType(dbxDoc.Layouts, AcadLayouts)

            For Each oLayout In oLayouts

                For Each oEnt In oLayout.Block

                    If TypeOf oEnt Is AcadBlockReference Then

                        oblkRef = CType(oEnt, AcadBlockReference)

                        If oblkRef.EffectiveName = blkname Then
                            Dim attVar As Object
                            attVar = oblkRef.GetAttributes
                            Dim i As Integer
                            For i = 0 To UBound(attVar)
                                oAtt = CType(attVar(i), AcadAttributeReference)
                                If oAtt.TagString = atag Then
                                    oAtt.TextString = aValue
                                    Exit For
                                End If
                            Next i
                        End If
                    End If

                Next
            Next
            dbxDoc.SaveAs(dwgName, Nothing)

        Next

        On Error Resume Next
        dbxDoc = Nothing


    End Sub

End Class

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
CADbloke
Posts: 43
Registered: ‎08-12-2008
Message 5 of 6 (1,497 Views)

Re: Create Autocad vb.net applciation to work in all Autocad versions

01-28-2014 01:17 PM in reply to: muthineni
See my answer at http://forums.autodesk.com/t5/NET/Visual-Studio-2010-Project-Save-As/m-p/4780201#M38907 - basically, use extension methods to cater for API changes and build configs to cater for project references.
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 6 of 6 (1,479 Views)

Re: Create Autocad vb.net applciation to work in all Autocad versions

01-28-2014 09:06 PM in reply to: CADbloke

I have got it now. I wrote as,

 

Try
                AcadApp = GetObject(, "AutoCAD.Application")
                Threading.Thread.Sleep(500)
            Catch ex As Exception
                AcadApp = CreateObject("AutoCAD.Application")
            End Try

 

I used threading.thread.sleep(500), then it is working in all versions.

 

Thanks alot for all for oyur immediate responses.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.