Jump to content

VB.Net Forms in AutoCAD? Anyone?


RMS

Recommended Posts

Hey Guys!

 

I need some help here, does anyone have an example of using a form with buttons? For example I would like to make a form with a button that would then pass a string value to a command.

 

Thanks,

Rob

Link to comment
Share on other sites

I haven’t used a lot of forms while automating AutoCAD via .NET, and absolutely none with VB.NET, unfortunately. If it is any help, though, I do have the source code for one “forms” based C# routine posted here:

http://www.cadtutor.net/forum/showthread.php?25414

It was a fairly early effort so the coding may not be all that proficient.

Link to comment
Share on other sites

<[color=#2b91af]CommandMethod[/color]([color=#a31515]"ShowForm"[/color])> _
       [color=blue]Public[/color] [color=blue]Sub[/color] ShowForm()
           [color=blue]Dim[/color] frm [color=blue]As[/color] [color=blue]New[/color] [color=#2b91af]Form1[/color]
           [color=#2b91af]Application[/color].ShowModelessDialog(frm)
       [color=blue]End[/color] [color=blue]Sub[/color]

 

Code behind form if it modeless remeber to lock the document

 

[color=blue]Imports[/color] Autodesk.AutoCAD.Runtime
[color=blue]Imports[/color] Autodesk.AutoCAD.ApplicationServices
[color=blue]Imports[/color] Autodesk.AutoCAD.DatabaseServices
[color=blue]Imports[/color] Autodesk.AutoCAD.EditorInput


[color=blue]Public[/color] [color=blue]Class[/color] [color=#2b91af]Form1[/color]

   [color=blue]Private[/color] [color=blue]Sub[/color] Button1_Click([color=blue]ByVal[/color] sender [color=blue]As[/color] System.[color=#2b91af]Object[/color], [color=blue]ByVal[/color] e [color=blue]As[/color] System.[color=#2b91af]EventArgs[/color]) [color=blue]Handles[/color] Button1.Click

       [color=blue]Dim[/color] doc [color=blue]As[/color] [color=#2b91af]Document[/color] = [color=#2b91af]Application[/color].DocumentManager.MdiActiveDocument
       [color=blue]Dim[/color] ed [color=blue]As[/color] [color=#2b91af]Editor[/color] = doc.Editor

       [color=blue]Using[/color] docloc [color=blue]As[/color] [color=#2b91af]DocumentLock[/color] = doc.LockDocument
           ed.WriteMessage(vbCrLf & [color=#a31515]"You pressed a button"[/color])
       [color=blue]End[/color] [color=blue]Using[/color]

   [color=blue]End[/color] [color=blue]Sub[/color]


[color=blue]End[/color] [color=blue]Class[/color]

Link to comment
Share on other sites

I use very similar coding except I use a error handler. It just helps hunt down errors.

[font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Try
[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]  Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] cForm [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]New[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]frmTitleblock
[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]  Application[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2].ShowModalDialog(cForm)
[/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Catch[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] ex [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Exception
[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]  MsgBox(ex.Message, [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]MsgBoxStyle[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2].Critical)
[/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]End[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Try
[/color][/size][/font][/color][/size][/font][/color][/size][/font]

Link to comment
Share on other sites

Hey Guys!

 

I need some help here, does anyone have an example of using a form with buttons? For example I would like to make a form with a button that would then pass a string value to a command.

 

Thanks,

Rob

Try this quick example too

''Project FormVB

''Form module code

''Form1.vb
''============================''
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports System.Windows.Forms
Public Class Form1
   Private Sub btnAcad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcad.Click
       Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
       Dim db As Database = doc.Database
       Dim ed As Editor = doc.Editor
       Dim usered As EditorUserInteraction = ed.StartUserInteraction(Me)
       Using (usered)
           Dim tr As Transaction = db.TransactionManager.StartTransaction
           Try
               If (Me.Modal) Then
                   Dim docloc As DocumentLock = doc.LockDocument
                   Using (docloc)
                       Me.Hide()
                       Using (tr)

                           ' Do your other things here e.g. pick point
                           Dim opt As PromptPointOptions = New PromptPointOptions(vbCr & "Pick point :")
                           Dim res As PromptPointResult = ed.GetPoint(opt)
                           If (res.Status <> PromptStatus.OK) Then
                               ed.WriteMessage(vbCr & "User input error. Exit")
                           ElseIf (res.Status = PromptStatus.Cancel) Then
                               ed.WriteMessage(vbCr & "Interrupted by user input. Exit")
                               Return
                           End If
                           Dim pt As Point3d = res.Value
                           Dim txt As DBText = New DBText
                           txt.SetDatabaseDefaults()
                           txt.Position = pt
                           txt.TextString = Me.txtCommand.Text
                           txt.HorizontalMode = TextHorizontalMode.TextCenter
                           txt.VerticalMode = TextVerticalMode.TextVerticalMid
                           txt.AlignmentPoint = pt
                           Dim btr As BlockTableRecord = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                           btr.AppendEntity(txt)
                           tr.AddNewlyCreatedDBObject(txt, True)
                           ed.Regen()
                           tr.Commit()
                       End Using
                   End Using
               End If
           Catch ex As System.Exception
               Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message)
           Finally
               usered.End()
               ' Display the form again if you need:
               'If Me.Visible = False Then Me.Show()
               ''otherwise close form
               Me.Close()
           End Try
       End Using
   End Sub
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Me.txtCommand.Text = "Type the text here"
       Me.txtCommand.Focus()
   End Sub
End Class

Add this class to project

''RunApp.vb
''===================================''
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports System.Windows.Forms
<Assembly: CommandClass(GetType(FormVB.RunApp))> 
Namespace FormVB
   Public Class RunApp
       <CommandMethod("RunMe", CommandFlags.Modal)> _
       Public Shared Sub InitForm()
           Dim frm As New Form1
           Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
           ed.WriteMessage("InitForm command started")
           Dim result As DialogResult = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm)

       End Sub
   End Class
End Namespace

 

Netload the project and type RUNME in the command line

 

~'J'~

Edited by fixo
spell check
Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...