.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Tony Tanzillo - commandline class to run a command asynchronously, in 2015

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
JanetDavidson
747 Views, 2 Replies

Tony Tanzillo - commandline class to run a command asynchronously, in 2015

 Hello All,

I used to use Tony Tanzillo's , piece of art , CommandLine Class, in my project  ( Autocad 2012 ,   VS 2010 ) in order to run a command

asynchronously.

 

Company is  upgrading to  (2015 , VS2012).

The class does not work and thorw an error in 2015 . I changed the  Pinvoke,   'acad.exe' changed to 'accore.dll  .

After altering that, I don't get error anymore  but still no result .

 

An example to use the class is :

commandline.command("line")

 

Can someone help me how to fix this please.

I am aware of new [editor.command] in new 2015 to run a command asynchronously . But unfortuantely that is not an option in my scenario.

Thanks.

 

 

 

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Public Class CommandLine
    ' Methods
    Shared Sub New()
        ResTypes.Item(GetType(String)) = &H138D
        ResTypes.Item(GetType(Double)) = &H1389
        ResTypes.Item(GetType(Point3d)) = &H1391
        ResTypes.Item(GetType(ObjectId)) = &H138E
        ResTypes.Item(GetType(Integer)) = &H1392
        ResTypes.Item(GetType(Short)) = &H138B
        ResTypes.Item(GetType(Point2d)) = &H138A
        ResTypes.Item(GetType(Byte)) = &H138B
    End Sub
    <DllImport("accore.dll", CallingConvention:=CallingConvention.Cdecl)> _
    Private Shared Function acedCmd(ByVal resbuf As IntPtr) As Integer
    End Function
    Public Shared Function Cmd(ByVal args As IList) As Integer
        If Application.DocumentManager.IsApplicationContext Then
            Return 0
        End If
        Dim num As Integer = 0
        Dim num2 As Integer = 0
        Using buffer As ResultBuffer = New ResultBuffer
            Dim obj2 As Object
            For Each obj2 In args
                num2 += 1
                buffer.Add(CommandLine.TypedValueFromObject(obj2))
            Next
            If (num2 > 0) Then
                Dim strA As String = CStr(Application.GetSystemVariable("USERS1"))
                Dim flag As Boolean = (String.Compare(strA, "DEBUG", True) = 0)
                Dim num3 As Integer = IIf(flag, 1, 0)
                Dim systemVariable As Object = Application.GetSystemVariable("CMDECHO")
                Dim num4 As Short = CShort(systemVariable)
                If ((num4 <> 0) OrElse flag) Then
                    Application.SetSystemVariable("CMDECHO", num3)
                End If
                num = CommandLine.acedCmd(buffer.UnmanagedObject)
                If ((num4 <> 0) OrElse flag) Then
                    Application.SetSystemVariable("CMDECHO", systemVariable)
                End If
            End If
        End Using
        Return num
    End Function
    Public Shared Function Command(ByVal ParamArray args As Object()) As Integer
        If Application.DocumentManager.IsApplicationContext Then
            Return 0
        End If
        Dim stat As Integer = 0
        If args Is Nothing Or args.Length = 0 Then
            Using rb As ResultBuffer = New ResultBuffer()
                rb.Add(New TypedValue(5000))
                Return acedCmd(rb.UnmanagedObject)
            End Using
        End If

        Dim cnt As Integer = 0
        Using buffer As ResultBuffer = New ResultBuffer
            Dim o As Object
            For Each o In args
                cnt += 1
                buffer.Add(CommandLine.TypedValueFromObject(o))
            Next

            If (cnt > 0) Then
                Dim s As String = CStr(Application.GetSystemVariable("USERS1"))
                Dim debug As Boolean = (String.Compare(s, "DEBUG", True) = 0)
                Dim val As Integer = IIf(debug, 1, 0)
                Dim cmdecho As Object = Application.GetSystemVariable("CMDECHO")
                Dim c As Short = CShort(cmdecho)
                If ((c <> 0) OrElse debug) Then
                    Application.SetSystemVariable("CMDECHO", val)
                End If
                stat = CommandLine.acedCmd(buffer.UnmanagedObject)
                If ((c <> 0) OrElse debug) Then
                    Application.SetSystemVariable("CMDECHO", cmdecho)
                End If
            End If
        End Using
        Return stat
    End Function
    Private Shared Function TypedValueFromObject(ByVal val As Object) As TypedValue
        Dim restype As Integer
        If Not ResTypes.TryGetValue(val.GetType, restype) Then
            Throw New InvalidOperationException("Unsupported type in Command() method")
        End If
        Return New TypedValue(restype, val)
    End Function
    ' Fields
    Private Const ACAD_EXE As String = "accore.dll"
    Private Shared ResTypes As Dictionary(Of Type, Integer) = New Dictionary(Of Type, Integer)
    Private Const RT3DPOINT As Integer = &H1391
    Private Const RTENAME As Integer = &H138E
    Private Const RTLONG As Integer = &H1392
    Private Const RTNONE As Integer = &H1388
    Private Const RTNORM As Integer = &H13EC
    Private Const RTPOINT As Integer = &H138A
    Private Const RTREAL As Integer = &H1389
    Private Const RTSHORT As Integer = &H138B
    Private Const RTSTR As Integer = &H138D
End Class

 

2 REPLIES 2
Message 2 of 3
_gile
in reply to: JanetDavidson

Hi,

 

AutoCAD 2015 brings some new Editor instance methods: Command() and CommandAsync() so that you do not need anymore P/Invoking acedCmd().

You can see this thread:

http://through-the-interface.typepad.com/through_the_interface/2014/03/autocad-2015-calling-commands...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3
JanetDavidson
in reply to: _gile

Thanks for getting back to me on this.

Janet,

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost