MEASURE command using SendCommand in VB.NET

By Fenton Webb

Issue

Is there an example showing how to use the MEASURE command with the ActiveX SendCommand method from AutoCAD VB.NET?

Solution

Below is a VB.NET example. Add the following references to the VB.NET Class Library Project:

  • acmgd.dll
  • Autodesk.AutoCAD.Interop.Common.dll
  • Autodesk.AutoCAD.Interop.dll 

NOTE: acdbmgd.dll is not required for this example, as it does not use any .NET database entities.

Another NOTE: be aware that whenever your reference COM interop assemblies, you are instantly tying yourself to the processor type, therefore you will need to consider x86 and x64 build types – Any CPU is not advisable.

Imports Autodesk.AutoCAD.Runtime    Imports Autodesk.AutoCAD.Interop       Imports Autodesk.AutoCAD.ApplicationServices    Imports Autodesk.AutoCAD.Interop.Common           _    Public Sub MYMEASURE()      Dim app As AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication      Dim obj1, obj2 As Object                Try        'Use ActiveX GetEntity function, as we only want to select a single entity        app.ActiveDocument.Utility.GetEntity(obj1, obj2, "select a polyline which you want to measure")                  'Cast Object to AcadObject        Dim tmpObj As AcadObject = CType(obj1, AcadObject)                  Dim strObjName As String        strObjName = tmpObj.ObjectName                  '   Check its a polyline        If strObjName = "AcDbPolyline" Then          Dim str1 As String                    'Handle of polyline is used to identify it to MEADSURE command          str1 = "(handent """ + tmpObj.Handle + """" + ")"          Dim str As String                    'Insert instances of 'myblock' at 500 unit spacing along polyline          '(replace myblock with name of your block)          ' the polyline needs to be longer than 500          str = "_.measure "          str = str & str1 & vbCr & "_block" & vbCr & "myblock" & vbCr & "_Yes" & vbCr & "500" & vbCr          app.ActiveDocument.SendCommand(str)        End If                Catch ex As Exception        ' Print the exception on the command line        Dim ed As Editor        ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor        ed.WriteMessage(ex.ToString())      End Try    End Sub

Comments

7 responses to “MEASURE command using SendCommand in VB.NET”

  1. Hi Fenton!
    Like to remind what not only commands, but their options also should be “internationalized”. That is not “Yes” but “_Yes” and not “block” but “_block”.

  2. Fenton Webb Avatar
    Fenton Webb
  3. Perhaps the reason is that you rarely have to deal with localized versions of AutoCAD? :-)

  4. What does this have to do with reference to the subject? This looks like SPAM.

  5. Francisco Silva Avatar
    Francisco Silva

    Hi Alexander, i have a problem with
    Dim ed As Editor… this is a Imports Autodesk.AutoCAD.EditorInput?
    When i change this then i have:
    Warning 1 Variable ‘obj1’ is passed by reference before it has been assigned a value. A null reference exception could result at runtime.
    Warning 2 Variable ‘obj2’ is passed by reference before it has been assigned a value. A null reference exception could result at runtime.
    you can help me?

  6. Jorge Renatto Avatar
    Jorge Renatto

    Regards,
    Maybe someone can help me solve my problem.
    I wonder why this code does not work when working with AutoCAD 2013:
    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.Interop
    Imports Autodesk.AutoCAD.Interop.Common
    Public Class Class1
    Public ReadOnly Property ThisDrawing() As AcadDocument
    Get
    Return Autodesk.AutoCAD.ApplicationServices.Application. _
    DocumentManager.MdiActiveDocument.acaddocument
    End Get
    End Property
    <CommandMethod(“COMANDO”)> Public Sub COMANDO()
    Thisdrawing.Utility.Prompt(“Hola mundo”)
    End Sub
    End Class
    I wonder if the way we work with INTEROP, and INTEROP.COMMON, has changed in AutoCAD 2013, and if so, what would be the new way of working with these references?.

  7. Jorge Renatto Avatar
    Jorge Renatto

    Regards,
    Maybe someone can help me solve my problem.
    I wonder why this code does not work when working with AutoCAD 2013:
    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.Interop
    Imports Autodesk.AutoCAD.Interop.Common
    Public Class Class1
    Public ReadOnly Property ThisDrawing() As AcadDocument
    Get
    Return Autodesk.AutoCAD.ApplicationServices.Application. _
    DocumentManager.MdiActiveDocument.acaddocument
    End Get
    End Property
    <CommandMethod(“COMANDO”)> Public Sub COMANDO()
    Thisdrawing.Utility.Prompt(“Hola mundo”)
    End Sub
    End Class
    I wonder if the way we work with INTEROP, and INTEROP.COMMON, has changed in AutoCAD 2013, and if so, what would be the new way of working with these references?.

Leave a Reply to Alexander RivilisCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading