.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.Net COM Library Windows dialog box

12 REPLIES 12
Reply
Message 1 of 13
wel666
2072 Views, 12 Replies

VB.Net COM Library Windows dialog box

Is it possible to use modal dialog boxes with vb.net using COM libraries?

I have two files one specifies the endpoint of a line in code the other ask for user input for the endpoints of the line.

The program that specifies the endpoints work fine. I can make it modal, modeless, and run it in process as a dll or out of process as an exe file. Although it wants to flash if any program is open hidden behind AutoCAD.

The program I ask for user input will run fine out of process as an exe file but I get an error message “AutoCAD main window is invisible”

 

Imports Autodesk.AutoCAD.Interop.Common

Imports Autodesk.AutoCAD.Interop

Imports Autodesk.AutoCAD.Runtime

 

Public Class myLINE

    Dim Acad = GetObject(, "AutoCAD.Application")

    Dim ThisDrawing As AcadDocument = Acad.ActiveDocument

    <CommandMethod("runlouver")> _

    Sub runLouver()

        Dim wel As DialogResult

        Dim myform As Form = New myLINE

 

        wel = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myform)

 

    End Sub

 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

 

        Dim objLIne As AcadLine

        Dim pt1 As Object

        Dim pt2 As Object

        Me.Visible = False

 

        pt1 = ThisDrawing.Utility.GetPoint(, "Pick point one")

        pt2 = ThisDrawing.Utility.GetPoint(, "Pick point two")

        objLIne = ThisDrawing.ModelSpace.AddLine(pt1, pt2)

        ThisDrawing.Regen(AcRegenType.acActiveViewport)

        Me.Visible = True

 

    End Sub

End Class

 

 

Imports Autodesk.AutoCAD.Interop.Common

Imports Autodesk.AutoCAD.Interop

Imports Autodesk.AutoCAD.Runtime

 

Public Class myLINE

    Dim Acad = GetObject(, "AutoCAD.Application")

    Dim ThisDrawing As AcadDocument = Acad.ActiveDocument

    <CommandMethod("runlouver")> _

    Sub runLouver()

        Dim wel As DialogResult

        Dim myform As Form = New myLINE

 

        wel = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myform)

 

    End Sub

 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

 

        Dim objLIne As AcadLine

        Dim pt1(2) As Double

        Dim pt2(2) As Double

        Me.Visible = False

 

        pt2(0) = 32: pt2(1) = 32

        objLIne = ThisDrawing.ModelSpace.AddLine(pt1, pt2)

        ThisDrawing.Regen(AcRegenType.acActiveViewport)

        Me.Visible = True

 

    End Sub

End Class

 

12 REPLIES 12
Message 2 of 13
Hallex
in reply to: wel666

This code is working in A2010 on my machine, see if this helps

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            Me.Hide()
            Dim appver As Object = TryCast(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Autodesk\AutoCAD", "CurVer", Nothing), Object)

            If appver IsNot Nothing Then
                MsgBox("you have " + "Autocad.Application." + appver.ToString() + " installed")
            Else
                MsgBox("Out of luck, sorry, use your another code instead")
            End If

            Dim vernum As Integer = CInt(Math.Floor(Convert.ToDouble(appver.ToString().Substring(1), Globalization.CultureInfo.InvariantCulture)))

            Dim progID As String = "Autocad.Application." & vernum.ToString()

            Dim acApp As AcadApplication = Nothing

            Try
                acApp = Marshal.GetActiveObject(progID)
            Catch
                Try
                    acApp = Activator.CreateInstance(Type.GetTypeFromProgID(progID), True)
                Catch
                    MessageBox.Show("Impossible to create an instance of " + progID)
                    Return
                End Try
            End Try
            MsgBox("Wait!...")
            acApp.Visible = True

            Dim acDoc As AcadDocument = acApp.ActiveDocument

            Dim objLIne As AcadLine
            Dim pt1(2) As Double
            Dim pt2(2) As Double
            Me.Visible = False
            pt2(0) = 32 : pt2(1) = 32
            objLIne = acDoc.ModelSpace.AddLine(pt1, pt2)
            acDoc.Regen(AcRegenType.acActiveViewport)
            Dim cent() As Double = New Double(2) {16, 16, 0}
            acApp.ZoomCenter(CType(cent, Object), 16)
            Me.Visible = True    
            Me.Show()
        Catch ex As System.Exception
            MessageBox.Show(ex.Message & vbLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Finally
            ' in case you want to finish your work with thisdrawing:
            'If acApp IsNot Nothing Then
            '    acApp.Quit()
            '    System.Runtime.InteropServices.Marshal.ReleaseComObject(acApp)
            'End If
        End Try


    End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 13
norman.yuan
in reply to: wel666

From your post, the description of your project is not that clear.

 

It seems that the code shown in your post is in an EXE application. or in an DLL that is referenced in an EXE application. If so, then the code will not proceed, because the code uses AutoCAD managed API ("Import Autodesk.AutoCAD.Runtime" "<CommandMethod(...)>..", and Application.ShowModalDialog(...)...), which cannot be used in out-process application.

 

So, your issue is not COM Library vs. DialogBox, rather, it is using AutoCAD managed API in correct environment.

Message 4 of 13
wel666
in reply to: wel666

 
Message 5 of 13
wel666
in reply to: norman.yuan

I complied both sets of code as an exe and dll. Both sets of code works as an exe running out of process. It is the code that request user input that will not run in process as dll. The error says "AutoCAD main window is invisible
Message 6 of 13
wel666
in reply to: Hallex

Hallex - Thanks for takng your time to send me your code.

 

When I changed your code to ask for user input "pt1 = ThisDrawing.Utility.GetPoint(, "Pick point one")"

I am still geting the error message "AutoCAD main window is invisable at Autodesk.AutoCAD,Interop.IAcadUtilty.GetPoint(Object Ponit Object, Object Prompt)

 

Is there a way to tell the autocad screen to get focus?

Message 7 of 13
Hallex
in reply to: wel666

Try another code snip, keep in mind if you'll set form invisible like in

your code:

Me.Visible=False

then AutoCAD is know nothing this form, better yet use

Me.Hide()...Me.Show():

      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

            Dim appver As Object = TryCast(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Autodesk\AutoCAD", "CurVer", Nothing), Object)

            If appver IsNot Nothing Then
                MsgBox("you have " + "Autocad.Application." + appver.ToString() + " installed")
            Else
                MsgBox("Out of luck, sorry, use your another code instead")
            End If

            Dim vernum As Integer = CInt(Math.Floor(Convert.ToDouble(appver.ToString().Substring(1), Globalization.CultureInfo.InvariantCulture)))

            Dim progID As String = "Autocad.Application." & vernum.ToString()

            Dim acApp As AcadApplication = Nothing

            Try
                acApp = Marshal.GetActiveObject(progID)
            Catch
                Try
                    acApp = Activator.CreateInstance(Type.GetTypeFromProgID(progID), True)
                Catch
                    MessageBox.Show("Impossible to create an instance of " + progID)
                    Return
                End Try
            End Try
            MsgBox("Wait!...")
            acApp.Visible = True

            Dim acDoc As AcadDocument = acApp.ActiveDocument
            Dim objLIne As AcadLine
            Dim pt1 As Object
            Dim pt2 As Object

            Me.Hide()

            pt1 = acDoc.Utility.GetPoint(, vbLf + "Pick point one: ")
            pt2 = acDoc.Utility.GetPoint(pt1, vbLf + "Pick point two: ")
            objLIne = acDoc.ModelSpace.AddLine(pt1, pt2)
            acDoc.Regen(AcRegenType.acActiveViewport)
           
            Me.Show()

        Catch ex As System.Exception
            MessageBox.Show(ex.Message & vbLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Finally
            ' in case you want to finish your work with thisdrawing:
            'If acApp IsNot Nothing Then
            '    acApp.Quit()
            '    System.Runtime.InteropServices.Marshal.ReleaseComObject(acApp)
            'End If
        End Try


    End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 13
wel666
in reply to: Hallex

Hallex - Thanks again. I am getting the same error message. I am using AutoCAD 2014, Visual Studio 2013, and .NET Framework 4.5

It is when I ask for user input I have the problem. If I ask for user input with objectARX it works (it still flashes if any windows are open

behind the autoCAD window). I have done something wrong some place, where is the question.

Message 9 of 13
Hallex
in reply to: wel666

' A2014 Net.Framework 4.0
' Do not run this application directly from Visual Studio, open instead any drawing and
'  type this in the command line  (use double backslashes):
' (startapp "C:\\.....\\bin\\Debug\\AcadComForm2014.exe")
' change full path of this winform application
' Also do not use managed DLL's, just reference AutoCAD 2014 Library 
' make sure you set 'Copy Local' to False and
' Embed Interop Types' to True

Imports AutoCAD
Imports System.Runtime.InteropServices

Public Class Form1

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim acApp As AcadApplication = Nothing
        Try

            Dim appver As Object = TryCast(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Autodesk\AutoCAD", "CurVer", Nothing), Object)

            If appver IsNot Nothing Then
                MsgBox("you have " + "Autocad.Application." + appver.ToString() + " installed")
            Else
                MsgBox("Out of luck, sorry, use your another code instead")
            End If

            Dim vernum As Integer = CInt(Math.Floor(Convert.ToDouble(appver.ToString().Substring(1), Globalization.CultureInfo.InvariantCulture)))

            Dim progID As String = "Autocad.Application." & vernum.ToString()

            ' Dim acApp As AcadApplication = Nothing

            Try
                acApp = Marshal.GetActiveObject(progID)
            Catch
                Try
                    acApp = Activator.CreateInstance(Type.GetTypeFromProgID(progID), True)
                Catch
                    MessageBox.Show("Impossible to create an instance of " + progID)
                    Return
                End Try
            End Try
            MsgBox("Wait!...")
            System.Windows.Forms.Application.DoEvents()


            acApp.WindowState = AcWindowState.acMax
            acApp.Visible = True
            Dim acDoc As AcadDocument = acApp.ActiveDocument
            Dim acUtil As AcadUtility = acDoc.Utility
         
            Me.Hide()

            Dim objLIne As AcadLine
            Dim pt1 As Object
            Dim pt2 As Object
            acDoc.WindowState = AcWindowState.acMax
            acUtil.Prompt(vbLf + "Pick two points >> " + vbCr)
            pt1 = acUtil.GetPoint(, vbLf + "Pick point one: ")
            pt2 = acUtil.GetPoint(pt1, vbLf + "Pick point two: ")
            objLIne = acDoc.ModelSpace.AddLine(pt1, pt2)
            acDoc.Regen(AcRegenType.acActiveViewport)

            Me.Show()

        Catch ex As System.Exception
            acApp.Quit()
            MessageBox.Show(ex.Message & vbLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Finally
            ' in case you want to finish your work with thisdrawing:
            If acApp IsNot Nothing Then
                acApp.Quit()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(acApp)
            End If
        End Try


    End Sub


End Class

 

 Try this code instead

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 10 of 13
wel666
in reply to: Hallex

Hallex - I do appreciate your effort in helping me with this problem. My problem was not with showing the form but with the user interaction with autoCAD. Without some of the code you sent me to try I might not have figured that out.

 

This is what I finally did

 

Imports Autodesk.AutoCAD.Interop

Imports Autodesk.AutoCAD.Interop.Common

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Runtime

 

 

PublicClassForm1

 

 

<CommandMethod("ShowVbForm")> _

PublicSub ShowForm()

Dim result AsDialogResult = DialogResult.OK

Dim form1Instance AsForm1 = NewForm1

DoWhile result = DialogResult.OK

result = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form1Instance)

Loop

EndSub

 

 

 

PrivateSub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Try

Dim Acad AsAcadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication

Dim activeEditor AsEditor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor

Using userInteraction AsEditorUserInteraction = activeEditor.StartUserInteraction(Me.Handle)

Dim acDoc AsAcadDocument = Acad.ActiveDocument

Dim pt1 AsObject = acDoc.Utility.GetPoint(, "Pick point one")

Dim pt2 AsObject = acDoc.Utility.GetPoint(pt1, "Pick point two")

Dim objLIne AsAcadLine = acDoc.ModelSpace.AddLine(pt1, pt2)

acDoc.Regen(AcRegenType.acActiveViewport)

EndUsing

Catch ex As System.Exception

MessageBox.Show(ex.Message & vbLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)

EndTry

EndSub

EndClass

Message 11 of 13
norman.yuan
in reply to: wel666

As your previous post mentioned both DLL and EXE, it is not clear where you run the code you JUST showed iin this reply to Hallex.

 

Your code showed here, if compiled as DLL and "NETLOAD" into AutoCAD, should run OK (but I'd question why you use COM in the mix with .NET API).

 

However, I got the impression that you are using the DLL with an EXE application, which automate AutoCAD. If so, stop, it does not work: AutoCAD managed API cannot be used in EXE application.

 

If you look at the code Hallex showed, there is no AutoCAD .NET API involved.

 

So, if you could describe where/how the code you showed here is used more clearly.

Message 12 of 13
wel666
in reply to: norman.yuan

I will compile the code as a DLL and load in into AutoCAD with the netlaod command. I am using the COM libraries because I have a couple of thousand hours of code written for autocad 2000 – 2004 (VB6) and the company wants it to run on AutoCAD 2014 and I think the fastest way is to reuse as much of the COM code as I can. They want to eventually change it over to objectARC when they have the time. If you know a faster better cheaper way let me know. Nothing is set in stone.
Message 13 of 13
norman.yuan
in reply to: wel666

Well, now that it is clear that your code posted yesterday is "NETLOAD"ed into AutoCAD to run, I tried the code EXACTLY copied from your post with my AutoCAD 2014. As I guessed in my previous reply, it works as expected. So, what is the problem you ran into with the same code?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost