Author Topic: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?  (Read 7336 times)

0 Members and 1 Guest are viewing this topic.

NirantarVidyarthee

  • Guest
Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« on: September 27, 2011, 02:52:25 AM »
I am using VB.Net Express 2008 and AutoCAD 2011.

I want to create a stand alone application similar one I used to create in VB6 where the program starts with main Sub() and displays forms when I want them to display and not as start-up by default.

This is how the App should work:

1. Double-click on the App shortcut to start the app.
2. Display multiple forms for user to enter data.
3. When user enters data in the last form and clicks OK, the program opens AutoCAD in the background.
4. Program creates a new drawing and saves it at predefined location.
5. Based on the input data it creates some geometry in the drawing and then closes the drawing and AutoCAD.
6. The application ends.

My question is what type of application should I create - Console Application or Windows.Forms application and which AutoCAD namespaces should I import?

Thanks for any help.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #1 on: September 27, 2011, 02:48:29 PM »
The data they enter seems that the UI already makes it easy to enter or would be better to build a Class Library and 'NetLoad' in AutoCAD and from there use
Application.ShowModalDialog()
 
I guess you could do something like this where you build a WinForm app and do some simple things, but maybe show you what you will have to deal with and its limitations
 
For example a little WinForm app for adding a Circle
 

 
For 2012 maybe earlier versions will need to change Embed Interop Types to false for Autodesk.AutoCAD.Interop & Autodesk.AutoCAD.Interop.Common
 

 
Here is all the code that is behind the form and just for example
 
Code: [Select]
Imports System.IO
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.Interop
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
    Const progIDstr As String = "AutoCAD.Application.18.2"

    Private Sub ButtonCreateCircle_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCreateCircle.Click
        LabelExecution.Text = "Starting"
        Dim app As AcadApplication = Nothing
        Try
            Try
                app = Marshal.GetActiveObject(progIDstr)
                app.Visible = True
            Catch
                app = New AcadApplicationClass()
                app.Visible = True
            End Try
        Catch ex As Exception
            LabelExecution.Text = "Canot start AutoCAD: " & ex.Message
            Return
        End Try
 
        Dim doc As AcadDocument = Nothing
        If app.Documents.Count > 1 Then
            doc = app.ActiveDocument
        Else
            doc = app.Documents.Add("acad.dwt")
        End If
        If doc Is Nothing Then
            LabelExecution.Text = "No drawing is open"
            Return
        End If
        Try
            doc.SendCommand(GetCircleCommand)
        Catch ex As Exception
            LabelExecution.Text = "Command failed: " & ex.Message
            Return
        End Try
    End Sub
 
    Private Function GetCircleCommand() As String
        Dim sb As New StringBuilder()
        sb.Append("C ")
        'Double.TryParse(Me.TextBoxX.Text, )
        sb.Append(Me.TextBoxX.Text & ",")
        sb.Append(Me.TextBoxY.Text & ",")
        sb.Append(Me.TextBoxZ.Text & " ")
        sb.Append(Me.TextBoxRadius.Text & " ")
        Return sb.ToString()
    End Function
End Class

 
 
 
For 2011 would need need to change
Code: [Select]
Const progIDstr As String = "AutoCAD.Application.18.2"
Code: [Select]
Const progIDstr As String = "AutoCAD.Application.18.1"

I could upload project if needed and if AutoCAD is not already started you will have to wait until it starts up.
 
« Last Edit: September 27, 2011, 02:52:44 PM by Jeff H »

NirantarVidyarthee

  • Guest
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #2 on: September 28, 2011, 12:15:46 AM »
Thanks Jeff for your reply.

I would prefer not use .dll because that would need loading it into an open drawing. I want to run the application and then open a drawing after getting the data since the data decides which template I need to use.

The problem with Windows.Forms application is that by default it starts up with a form that I don't want to use. I would rather have a functionality similar to VB6 where I can control (using sub maim()) which form to display and when or even not to have a form at all.

The problem with console application is that it displays the console window which I don't to display.

After seeing the complexity of this, it seems that VB6 'Standard EXE' may still be the best option for this.



I have searched everywhere but have been unable to find a definitive solution for this.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #3 on: September 28, 2011, 01:15:52 AM »
Hi,

If I understend what you want, you can build your application from the Console application template so that it starts with the Main() method, and then change the output type from Console to Windows application in the first tab of the project properties windows so that the console won't display when you'll run your application.
« Last Edit: September 28, 2011, 01:26:20 AM by gile »
Speaking English as a French Frog

NirantarVidyarthee

  • Guest
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #4 on: September 28, 2011, 03:30:00 AM »
gile,

Thank you for very useful information. I am not sure if this is relevant to Express editions or only to full version.

I have seen the project properties window but can't find the option to 'change the output type from Console to Windows application' in any of the tabs. May be this is available only to the full version and not in the Expression editions. Or may be there is some other way in Express edition. Or I am missing something.

I am attaching the image of the properties dialog that I see in the express edition.

Thanks again for help.


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #5 on: September 28, 2011, 05:03:28 AM »
Did you look at the "Application type" popup ?
Speaking English as a French Frog

NirantarVidyarthee

  • Guest
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #6 on: September 30, 2011, 09:04:21 AM »
Thanks.

I think that should help.

Draftek

  • Guest
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #7 on: September 30, 2011, 09:45:20 AM »
You do know that all applications begin with a static Main method, right?

There is no need to start with a console template if your going to display a windows form at some point.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #8 on: September 30, 2011, 10:37:44 AM »
To add to what Draftek posted
When you create a Winforms app with C# a file named Program.cs is added with Main method.
 
In VB if you do not add a Main method the compiler creates one for you.
 
So I could add another form Form2 and add this to the code posted earlier so Form2 will be the startup form
Code: [Select]
Public Class Form1
 
    <STAThread()> _
    Shared Sub Main()
 
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form2)
 
    End Sub
Or in the pic you posted you can change the Startup form in the comboBox below the Application type, which will just change what form is passed in Application.Run()
 
Also VB has Application Model for doing whatever needed.
Class library's do not need a Main method.

 
 
 

NirantarVidyarthee

  • Guest
Re: Stand Alone app That Open AutoCAD - Console Or Windows.Forms?
« Reply #9 on: October 01, 2011, 12:54:41 AM »
My limitations are that I am an amateur programmer not professionally trained. So, I am not aware of many intricacies of VB.Net (VB6 was realtively far more easy to pick up). And I'm using Express Edition.

So, I'll have to find out what 'static Sub Main' means and also have a look at 'Application Model'.

I think I understand what 'There is no need to start with a console template if your going to display a windows form at some point.' means.

As I said my main concern was how to avoid form as startup.

The changing 'Application Type' does help.

Also, I found another method on the net. It says - In a 'Windows.Form' app, disable 'Enable Application Framework'. This immediately makes the sub main as StartUp.

But I don't know the price I have to pay for this, because I don't know what the Application Framework setting does. If this setting does not affect my code, then it seems the best way to achive what I want. Back to the net unless somebody already knows and explains to me.

It has been a great help from all of you.