Author Topic: Draw a rectangle using a form  (Read 7998 times)

0 Members and 1 Guest are viewing this topic.

GegH1

  • Guest
Draw a rectangle using a form
« on: July 24, 2012, 07:30:32 AM »
This is going to be really simple for someone, i'm new to VBA.Net.
I wish to enter dimensions into a form, that will then use those dimensions to create a rectangle which i place in the drawing.

Thanks to Jerry Winters i have the code to create the rectangle and place in the drawing (with hard coded dimensions.
I also have a form that i can enter dimensions into.

I'm struggling to link the two together though, can someone help.

The code to draw the box is essentially:

    'Define Command 'DrawABox2'
    <CommandMethod("DrawABox2")> _
    Public Sub DrawABox2()
        Dim myPPR As EditorInput.PromptPointResult
        Dim myRJig As New RectangleJig()
        myPPR = myRJig.StartJig(2000, 4000
        If myPPR.Status = EditorInput.PromptStatus.OK Then
            DrawRectangle(myPPR.Value, 2000, 4000, "*Model_Space", "0")
        End If
    End Sub

I want the numbers to come from a form (Form1) with 2 textboxes, Textbox1(Height) & Textbox2(width)

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Draw a rectangle using a form
« Reply #1 on: July 25, 2012, 08:00:06 AM »
Create your form and show it using Application.ShowModalDialog or Application.ShowModalWindow depending on whether you are using WPF or Windows Forms.  Use the coordinates you collect to create a Polyline or a Polyline3d depending on your needs.

The example code you show is for using a jig. Which you don't need since your getting the coordinates for the rectangle via your form.
Revit 2019, AMEP 2019 64bit Win 10

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #2 on: July 25, 2012, 08:30:39 AM »
Ok, what i don't know is how to get the form to draw the box in AutoCAD.

The full code i have is here, but obviously the jig bit isn't needed.

Public Class RectangleJig
    Inherits Autodesk.AutoCAD.EditorInput.DrawJig

    Dim BasePt As Point3d
    Dim myPR As PromptPointResult
    Dim RectWidth As Double
    Dim RectHeight As Double
    Dim myRect As Polyline2d
    Dim myPlPoints As New Point3dCollection
    Dim myOpts As JigPromptPointOptions

    Function StartJig(ByVal Width As Double, ByVal Height As Double) As PromptPointResult
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        myPlPoints.Add(New Point3d(BasePt.X, BasePt.Y, BasePt.Z))
        myPlPoints.Add(New Point3d(BasePt.X + Width, BasePt.Y, BasePt.Z))
        myPlPoints.Add(New Point3d(BasePt.X + Width, BasePt.Y - Height, BasePt.Z))
        myPlPoints.Add(New Point3d(BasePt.X, BasePt.Y - Height, BasePt.Z))
        myRect = New Polyline2d(Poly2dType.SimplePoly, myPlPoints, 0, True, 0, 0, Nothing)
        If myOpts Is Nothing Then
            myOpts = New JigPromptPointOptions()
            myOpts.Message = vbCrLf & "Place Rectangle"
            myOpts.Cursor = CursorType.Invisible
        End If
        myPR = ed.Drag(Me)

        Do
            Select Case myPR.Status
                Case PromptStatus.OK
                    Return myPR
                    Exit Do
            End Select
        Loop While myPR.Status <> PromptStatus.Cancel

        Return myPR
    End Function

    Protected Overrides Function Sampler( _
        ByVal prompts As Autodesk.AutoCAD.EditorInput.JigPrompts) _
        As Autodesk.AutoCAD.EditorInput.SamplerStatus

        myPR = prompts.AcquirePoint(myOpts)

        If myPR.Value.IsEqualTo(BasePt) Then
            Return SamplerStatus.NoChange
        Else
            Dim myTrMat As New Matrix3d
            Dim myMoveBy As New Vector3d(BasePt.GetVectorTo(myPR.Value).ToArray)
            myTrMat = Matrix3d.Displacement(myMoveBy)
            myRect.TransformBy(myTrMat)
            BasePt = myPR.Value
            Return SamplerStatus.OK
        End If

    End Function

    Protected Overrides Function WorldDraw( _
        ByVal draw As Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean
        draw.Geometry.Draw(myRect)
    End Function

    Public Function DrawRectangle(ByVal ULCorner As Geometry.Point3d, _
        ByVal Width As Double, ByVal Height As Double, _
        ByVal Space As String, ByVal LayerName As String) As DatabaseServices.Polyline2d

        Dim myDWG As Document
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord
        Dim myPL As Polyline2d
        Dim myPLPointColl As New Geometry.Point3dCollection

        Try
            myDWG = Application.DocumentManager.MdiActiveDocument
            myTransMan = myDWG.TransactionManager
            myTrans = myTransMan.StartTransaction
            myBT = myDWG.Database.BlockTableId.GetObject(OpenMode.ForRead)
            myBTR = myBT(Space).GetObject(OpenMode.ForWrite)

            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X, ULCorner.Y, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X + Width, ULCorner.Y, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X + Width, ULCorner.Y - Height, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X, ULCorner.Y - Height, ULCorner.Z))
            myPL = New Polyline2d(Poly2dType.SimplePoly, myPLPointColl, 0, True, 0, 0, Nothing)

            myBTR.AppendEntity(myPL)
            myPL.Layer = LayerName
            myTrans.AddNewlyCreatedDBObject(myPL, True)
            myTrans.Commit()
            myTrans.Dispose()
            myTransMan.Dispose()
            Return myPL

        Catch ex As SystemException
            MsgBox(ex.Message)
            Return Nothing
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    'Define Command 'DrawABox2'
    <CommandMethod("DrawABox2")> _
    Public Sub DrawABox2()
        Dim myPPR As EditorInput.PromptPointResult
        Dim myRJig As New RectangleJig()
        myPPR = myRJig.StartJig(2000, 4000)
        If myPPR.Status = EditorInput.PromptStatus.OK Then
            DrawRectangle(myPPR.Value, 2000, 4000, "*Model_Space", "0")
        End If
    End Sub

    'Show Form'
    <CommandMethod("ShowForm1")> _
    Public Sub ShowForm1()
        Dim myForm As New Form1
        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myForm)
        MsgBox("Form1 is Closed")
    End Sub

    'Show form Modeless'
    <CommandMethod("ShowForm1Modeless")> _
    Public Sub ShowForm1Modeless()
        Dim myForm As New Form1
        Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(myForm)
        MsgBox("Form1 is still here and you can interact with AutoCAD")
    End Sub


End Class


Delegate

  • Guest
Re: Draw a rectangle using a form
« Reply #3 on: July 25, 2012, 08:47:05 AM »
Read this for more info: http://www.homeandlearn.co.uk/csharp/csharp_s1p8.html

Rougly you need to add an event for your form - i.e. the ok button is clicked.
You then read the text in your text boxes and assign that to variables.
Then call your drawrectangle with the variables (convert from strings).



Code - C#: [Select]
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.          string height = textBox1.Text;
  4.          string width = textBox2.Text;
  5.  
  6.          //Check Input for errors and Need to convert from string to double here
  7.  
  8.          DrawRectangle(height, width);
  9.  
  10. }

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #4 on: July 26, 2012, 06:02:04 PM »
Csharp? do the same expressions work for VBA.Net?

Delegate

  • Guest
Re: Draw a rectangle using a form
« Reply #5 on: July 27, 2012, 03:47:21 AM »
Can convert it here: http://www.developerfusion.com/tools/convert/csharp-to-vb/

Code - Visual Basic: [Select]
  1. Private Sub button1_Click(sender As Object, e As EventArgs)
  2.         Dim height As String = textBox1.Text
  3.         Dim width As String = textBox2.Text
  4.  
  5.         'Check Input for errors and Need to convert from string to double here
  6.  
  7.         DrawRectangle(height, width)
  8.  
  9. End Sub

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #6 on: July 27, 2012, 05:10:39 AM »
I seem to get an error on this part of the code

DrawRectangle(height, width)

It says DrawRectangle is not declared

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #7 on: July 27, 2012, 07:44:39 AM »
I'm still having issues, primarily because i don't know what i'm doing. Hopefully this is a bit clearer.
The code is:

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.LayerManager
Imports Autodesk.AutoCAD.Windows

Public Class DRectang
    Public Function DrawRectangle(ByVal ULCorner As Geometry.Point3d, _
        ByVal Width As Double, ByVal Height As Double, _
        ByVal Space As String, ByVal LayerName As String) As DatabaseServices.Polyline2d

        Dim myDWG As Document
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord
        Dim myPL As Polyline2d
        Dim myPLPointColl As New Geometry.Point3dCollection

        Try
            myDWG = Application.DocumentManager.MdiActiveDocument
            myTransMan = myDWG.TransactionManager
            myTrans = myTransMan.StartTransaction
            myBT = myDWG.Database.BlockTableId.GetObject(OpenMode.ForRead)
            myBTR = myBT(Space).GetObject(OpenMode.ForWrite)

            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X, ULCorner.Y, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X + Width, ULCorner.Y, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X + Width, ULCorner.Y - Height, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X, ULCorner.Y - Height, ULCorner.Z))
            myPL = New Polyline2d(Poly2dType.SimplePoly, myPLPointColl, 0, True, 0, 0, Nothing)

            myBTR.AppendEntity(myPL)
            myPL.Layer = LayerName
            myTrans.AddNewlyCreatedDBObject(myPL, True)
            myTrans.Commit()
            myTrans.Dispose()
            myTransMan.Dispose()
            Return myPL

        Catch ex As SystemException
            MsgBox(ex.Message)
            Return Nothing
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    <CommandMethod("DrawABox")> _
    Public Sub DrawABox()
        Dim myDwg As Document
        Dim myPPR As EditorInput.PromptPointResult

        Try
            myDwg = Application.DocumentManager.MdiActiveDocument
            myPPR = myDwg.Editor.GetPoint("Select Box Starting Point")
            DrawRectangle(myPPR.Value, 2, 2, "*Model_Space", "Boxes")
        Catch ex As SystemException
            MsgBox(ex.Message)
        End Try

    End Sub

    'Show Form'
    <CommandMethod("ShowForm1")> _
    Public Sub ShowForm1()
        Dim myForm As New Form1
        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myForm)
        MsgBox("Form1 is Closed")
    End Sub

    'Show form Modeless'
    <CommandMethod("ShowForm1Modeless")> _
    Public Sub ShowForm1Modeless()
        Dim myForm As New Form1
        Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(myForm)
        MsgBox("Form1 is still here and you can interact with AutoCAD")
    End Sub


End Class

The form code for the button is:
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim height As String = TextBox1.Text
        Dim width As String = TextBox2.Text

        'Check Input for errors and Need to convert from string to double here

        DrawABox(width, height)

    End Sub

End Class

2 Issues,
Autocad crashes when i zoom extents after running the code manually (it does draw a box with the numbers hardcoded, i get a glimpse of it).
DrawAbox is apparently not declared in the form code.

Please help, i've got till monday to try to sort this out.

TheMaster

  • Guest
Re: Draw a rectangle using a form
« Reply #8 on: July 27, 2012, 06:12:27 PM »
Perhaps showing the actual code might help, because the code you show will not even compile.

DrawABox() is an instance method of a class, which means you must create an instance of the class it is a member of to call it.

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #9 on: July 27, 2012, 09:08:38 PM »
OK, You lost me. As i say i'm very new to this.

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Draw a rectangle using a form
« Reply #10 on: July 27, 2012, 11:50:25 PM »
DrawABox(width, height)
Where s the method that this calls, looks like takes 2 doubles maybe
DrawABox(ByVal width as double, ByVal length as double)
....
....

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #11 on: July 28, 2012, 12:30:33 AM »
that rings a bell, as per earlier in the main code

Public Function DrawRectangle(ByVal ULCorner As Geometry.Point3d, _
        ByVal Width As Double, ByVal Height As Double, _
        ByVal Space As String, ByVal LayerName As String) As DatabaseServices.Polyline2d

Does this mean the code for drawing the box should be part of the form and not the 'Class.vb' file?

Is this where i'm going wrong

If that is the case, what is the reason for the 'Class.vb' File? and do i guess i would need to add the rest of the Public Functions as well?

So many questions, Thankyou for your help.

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Draw a rectangle using a form
« Reply #12 on: July 28, 2012, 01:39:40 AM »
Just something quickly slapped together and not the best way
The Form
Code - Visual Basic: [Select]
  1. Public Class RectangleForm
  2.     Private Sub ButtonDraw_Click(sender As System.Object, e As System.EventArgs) Handles ButtonDraw.Click
  3.         Dim rec As New Rectangle(TextBoxLength.Text, TextBoxLength.Text)
  4.         rec.Draw()
  5.         Me.Close()
  6.     End Sub
  7. End Class
  8.  

Other code
Code - Visual Basic: [Select]
  1. Public Class Commands
  2.  
  3.     <CommandMethod("DrawRectangle")> _
  4.     Public Sub DrawRectangle()
  5.  
  6.         Dim recFrm As New RectangleForm()
  7.         Application.ShowModelessDialog(recFrm)
  8.  
  9.     End Sub
  10.  
  11. End Class
  12.  
  13.  
  14. Public Class Rectangle
  15.     Private _width As Double
  16.     Public Property Width() As Double
  17.  
  18.         Get
  19.             Return _width
  20.         End Get
  21.         Set(ByVal value As Double)
  22.             _width = value
  23.         End Set
  24.  
  25.     End Property
  26.  
  27.  
  28.     Private _length As Double
  29.     Public Property Length() As Double
  30.  
  31.         Get
  32.             Return _length
  33.         End Get
  34.         Set(ByVal value As Double)
  35.             _length = value
  36.         End Set
  37.  
  38.     End Property
  39.    
  40.  Sub New(ByVal width As Double, ByVal length As Double)
  41.  
  42.         Me.Width = width
  43.         Me.Length = length
  44.  
  45.     End Sub
  46.  
  47.     Public Sub Draw()
  48.  
  49.         Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  50.         Dim db As Database = doc.Database
  51.  
  52.         Using trx As Transaction = db.TransactionManager.StartTransaction()
  53.             Using docloc As DocumentLock = doc.LockDocument
  54.  
  55.                 Dim pl As New Polyline()
  56.  
  57.                 pl.AddVertexAt(0, Point2d.Origin, 0, 0, 0)
  58.                 pl.AddVertexAt(1, New Point2d(Width, 0), 0, 0, 0)
  59.                 pl.AddVertexAt(2, New Point2d(Width, Length), 0, 0, 0)
  60.                 pl.AddVertexAt(3, New Point2d(0, Length), 0, 0, 0)
  61.                 pl.Closed = True
  62.  
  63.                 Dim model As BlockTableRecord = SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForWrite)
  64.                 model.AppendEntity(pl)
  65.                 trx.AddNewlyCreatedDBObject(pl, True)
  66.                 trx.Commit()
  67.  
  68.             End Using
  69.         End Using
  70.  
  71.     End Sub
  72. End Class
  73.  
  74.  

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #13 on: July 28, 2012, 08:57:42 AM »
That's very cool.

So the first bit is getting the dimensions of the box, and this bit is placing the box?

Code: [Select]
    Public Sub Draw()

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database

        Using trx As Transaction = db.TransactionManager.StartTransaction()
            Using docloc As DocumentLock = doc.LockDocument

                Dim pl As New Polyline()

                pl.AddVertexAt(0, Point2d.Origin, 0, 0, 0)
                pl.AddVertexAt(1, New Point2d(Width, 0), 0, 0, 0)
                pl.AddVertexAt(2, New Point2d(Width, Length), 0, 0, 0)
                pl.AddVertexAt(3, New Point2d(0, Length), 0, 0, 0)
                pl.Closed = True

                Dim model As BlockTableRecord = SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForWrite)
                model.AppendEntity(pl)
                trx.AddNewlyCreatedDBObject(pl, True)
                trx.Commit()

            End Using
        End Using

    End Sub

So if i want the user to place the box rather than using 0,0,0 i need to change this bit to?

Code: [Select]
Public Function DrawRectangle(ByVal ULCorner As Geometry.Point3d, _
        ByVal Width As Double, ByVal Height As Double, _
        ByVal Space As String, ByVal LayerName As String) As DatabaseServices.Polyline2d

        Dim myDWG As Document
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord
        Dim myPL As Polyline2d
        Dim myPLPointColl As New Geometry.Point3dCollection

        Try
            myDWG = Application.DocumentManager.MdiActiveDocument
            myTransMan = myDWG.TransactionManager
            myTrans = myTransMan.StartTransaction
            myBT = myDWG.Database.BlockTableId.GetObject(OpenMode.ForRead)
            myBTR = myBT(Space).GetObject(OpenMode.ForWrite)

            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X, ULCorner.Y, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X + Width, ULCorner.Y, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X + Width, ULCorner.Y - Height, ULCorner.Z))
            myPLPointColl.Add(New Geometry.Point3d(ULCorner.X, ULCorner.Y - Height, ULCorner.Z))
            myPL = New Polyline2d(Poly2dType.SimplePoly, myPLPointColl, 0, True, 0, 0, Nothing)

            myBTR.AppendEntity(myPL)
            myPL.Layer = LayerName
            myTrans.AddNewlyCreatedDBObject(myPL, True)
            myTrans.Commit()
            myTrans.Dispose()
            myTransMan.Dispose()
            Return myPL

        Catch ex As SystemException
            MsgBox(ex.Message)
            Return Nothing
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

GegH1

  • Guest
Re: Draw a rectangle using a form
« Reply #14 on: July 30, 2012, 12:09:19 AM »
I'm now adapting the code and trying to place a second box next to the first (pl2), how do i 'commit' both polylines.
Code - vb.net: [Select]
  1. Public Sub Draw()
  2.  
  3.         Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  4.         Dim db As Database = doc.Database
  5.  
  6.         Using trx As Transaction = db.TransactionManager.StartTransaction()
  7.             Using docloc As DocumentLock = doc.LockDocument
  8.  
  9.                 Dim pl As New Polyline()
  10.                 Dim pl2 As New Polyline()
  11.  
  12.                 pl.AddVertexAt(0, Point2d.Origin, 0, 0, 0)
  13.                 pl.AddVertexAt(1, New Point2d(Width, 0), 0, 0, 0)
  14.                 pl.AddVertexAt(2, New Point2d(Width, Length), 0, 0, 0)
  15.                 pl.AddVertexAt(3, New Point2d(0, Length), 0, 0, 0)
  16.                 pl.AddVertexAt(4, New Point2d(0, Length * 0.5), 0, 0, 0)
  17.                 pl.AddVertexAt(5, New Point2d(Width, (Length * 0.5)), 0, 0, 0)
  18.                 pl.Closed = False
  19.                 pl2.AddVertexAt(0, Point2d.Origin, 100, 0, 0)
  20.                 pl2.AddVertexAt(1, New Point2d(Width, 0), 100, 0, 0)
  21.                 pl2.Closed = False
  22.  
  23.                 Dim model As BlockTableRecord = SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForWrite)
  24.                 model.AppendEntity(pl)
  25.                 trx.AddNewlyCreatedDBObject(pl, True)
  26.  
  27.                 Dim model2 As New BlockTableRecord = SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForWrite)
  28.                 model.AppendEntity(pl2)
  29.                 trx.AddNewlyCreatedDBObject(pl2, True)
  30.                 trx.Commit()
  31.  
  32.             End Using
  33.         End Using
  34.  
  35.     End Sub
  36. End Class

I tried on the last bit of the code with no success.
If i hadn't said it before, you help is invaluable, thankyou.
« Last Edit: July 30, 2012, 12:12:53 AM by GegH1 »