Author Topic: NE1 have Example of Winform Button with Autocad  (Read 2948 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
NE1 have Example of Winform Button with Autocad
« on: April 22, 2013, 01:52:26 AM »
I'd like to type a command, open the winform, and then select my button, then do something in cad, but return to the form after.  Is this possible?

fixo

  • Guest
Re: NE1 have Example of Winform Button with Autocad
« Reply #1 on: April 22, 2013, 02:19:37 AM »

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: NE1 have Example of Winform Button with Autocad
« Reply #2 on: April 22, 2013, 02:53:01 AM »
Probably use StartUserInteraction, take a look at this article:
http://adndevblog.typepad.com/autocad/2012/05/taking-mouse-inputs-from-a-modal-dialog-box.html
this one is would be interesting to see too
http://drive-cad-with-code.blogspot.ru/2013/02/using-applicationshowmodaldialog-be.html

Thanks! I'll definitely take a look but it turns out that when I select my button and place my method in the button event it automatically switches to cad screen and runs my command. Only issue I have is that I am running a method that retrieves the length of multiple polylines... I need to pass that length back into the forms textbox.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff

fixo

  • Guest
Re: NE1 have Example of Winform Button with Autocad
« Reply #4 on: April 22, 2013, 04:09:00 AM »
Here is quickly made an example if you need it
Code: [Select]
'__________________ 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
Code: [Select]


'__________________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 leg As Double = New Double
    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


    Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
        Me.txtLength.Text = ""
        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
                    Me.Hide()
                    Using (tr)

                        Dim peo As New PromptEntityOptions(vbLf & "Select a polyline: ")
                        peo.SetRejectMessage(vbLf & "Not a polyline...")
                        peo.AddAllowedClass(GetType(Polyline), True)
                        Dim per As PromptEntityResult = ed.GetEntity(peo)
                        If per.Status <> PromptStatus.OK Then Return

                        Dim ent As Entity = TryCast(tr.GetObject(per.ObjectId, OpenMode.ForRead), Entity)
                        If ent Is Nothing Then Return
                        Dim pline As Polyline = TryCast(ent, Polyline)
                        Me.leg = pline.Length
                        Me.txtLength.Text = String.Format("{0:f3}", Me.leg)

                    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 btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: NE1 have Example of Winform Button with Autocad
« Reply #5 on: April 22, 2013, 04:48:13 AM »
You always make it look so simple friend :)  ... wish I could compensate ya for your efforts...thanks so much!  It was the placement of my variable... like always... just in the wrong spot :)
« Last Edit: April 22, 2013, 04:52:38 AM by Alien »

fixo

  • Guest
Re: NE1 have Example of Winform Button with Autocad
« Reply #6 on: April 22, 2013, 06:05:51 AM »
Glad I've turn you in the right direction,
Happy coding
:)