start lisp-Command immediately after sendstringtoexecute

start lisp-Command immediately after sendstringtoexecute

jan_tappenbeck
Collaborator Collaborator
1,070 Views
1 Reply
Message 1 of 2

start lisp-Command immediately after sendstringtoexecute

jan_tappenbeck
Collaborator
Collaborator

hi !

 

i have a vb.net-programm to create a drawing and save automatically.

 

before the dwg will be saved a lisp should be loaded and run. (kind of postprocessing).

 

the following function will work without any mistake:

 

 Public Function LoadAndStartLisp(ByVal LispFilePath As String, Optional LispCommand As String = "")
        Dim Status As Integer = 0
        AcReInit()

        ' keine LISP-Datei angegeben
        If LispFilePath.Length = 0 Then Return -2

        If System.IO.File.Exists(LispFilePath) = False Then
            Return -1
        End If

        Dim acDoc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

        Using acTrans As Transaction = _Database.TransactionManager.StartTransaction()
            Using acDoc.LockDocument
                ' erfolgreich geladen -> 1
                Status = 1

                Try
                    Dim LispCommand2Load As String = "(load " & Chr(34) & LispFilePath & Chr(34) & "\r)"
                    _AcDocument.SendStringToExecute(LispCommand2Load, True, False, False)
                Catch ex As Exception
                    _Editor.WriteMessage("Kann Lisp nicht laden!" & vbCrLf & ex.ToString)
                    Return -2 ' Lisp nicht geladen
                End Try

                ' ausführen eines bestimmten Commandos
                If LispCommand.Length > 0 Then
                    Try
                        _AcDocument.SendStringToExecute(LispCommand, True, False, False)
                        _Editor.WriteMessage("rufe Lisp-Befehl auf: " & LispCommand)
                    Catch ex As Exception
                        _Editor.WriteMessage("Fehler beim Ausführen des LISP Befehls " & LispCommand & "" & vbCrLf & ex.ToString)
                        Return -3 ' Fehler beim Ausführen des LISP-Befehls
                    End Try
                End If

            End Using 'acDoc
        End Using 'acTrans
        Return Status
    End Function

 

my little test-lisp Shows first a alert-message and then inserts a block into dwg.

 

after the vb.net Programm finished the block the message will not Show and no block will be insert.

 

but!!!

 

if i double-click the middle mouse-wheel the lisp-Programm will be run.

 

in my first thoughts i missed a RETURN in the LispCommand2Load-Variable - i add \r - but this was not the solution!

 

could someone help me ?

 

regards Jan

0 Likes
1,071 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant

Hi,

 

You can simply a space (" ") as validation with SendStringToExecute (as you'd do at command line).

In the path string, you have to rplace each anti-slash with a slash or a double anti slache before using it in the (load ...) lISP expression.

 

C#

 

private void LoadAndStartLisp(string lispPath, string lispCmd = "")
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var loadStr = "(load \"" + lispPath.Replace(@"\", @"\\") + "\") ";
    doc.SendStringToExecute(loadStr, true, false, false);
    if (lispCmd != "")
        doc.SendStringToExecute(lispCmd.Trim() + " ", true, false, false);
}

 

VB (telerik conversion)

 

Private Sub LoadAndStartLisp(lispPath As String, Optional lispCmd As String = "")
    Dim doc = Application.DocumentManager.MdiActiveDocument
    Dim loadStr = "(load """ + lispPath.Replace("\", "\\") + """) "
    doc.SendStringToExecute(loadStr, True, False, False)
    If lispCmd <> "" Then
	doc.SendStringToExecute(lispCmd.Trim() + " ", True, False, False)
    End If
End Sub

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes