TheSwamp
Code Red => .NET => Topic started by: NirantarVidyarthee 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.
-
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
(http://i1221.photobucket.com/albums/dd473/Jeffrey_H/AutoCadWinForm.png)
For 2012 maybe earlier versions will need to change Embed Interop Types to false for Autodesk.AutoCAD.Interop & Autodesk.AutoCAD.Interop.Common
(http://i1221.photobucket.com/albums/dd473/Jeffrey_H/EmbedInterop.png)
Here is all the code that is behind the form and just for example
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
Const progIDstr As String = "AutoCAD.Application.18.2"
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.
-
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.
-
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.
-
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.
-
Did you look at the "Application type" popup ?
-
Thanks.
I think that should help.
-
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.
-
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
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 (http://msdn.microsoft.com/en-us/library/w3xx6ewx(v=VS.100).aspx) for doing whatever needed.
Class library's do not need a Main method.
-
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.