How to pass selection set to lisp command.

How to pass selection set to lisp command.

WonkaPet
Advocate Advocate
2,848 Views
7 Replies
Message 1 of 8

How to pass selection set to lisp command.

WonkaPet
Advocate
Advocate

Hi there,

 

I'm trying to pass a selection set using the Document.Editor.Command or Document.SendStringToExecute command is there anyway to do this? I want to pass an object collection to a lisp function I have built. I have tried to use this to run the command but I get "eInvalidInput" as an error.

 

Document.Editor.Command("LISP_PLOTSET", SelectionSet, InsertionPoint)

0 Likes
Accepted solutions (1)
2,849 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant
Accepted solution

Hi,

 

To call a LISP function, you should use Application.Invoke() method. To make the function accessible, you have to use vl-acad-defun on the LISP side: (vl-acad-defun 'LISP_PLOTSET).

 

var args = new ResultBuffer(
    new TypedValue((int)LispDataType.Text, "LISP_PLOTSET"),
    new TypedValue((int)LispDataType.SelectionSet, SelectionSet),
    new TypedValue((int)LispDataType.Point3d, InsertionPoint));
Application.Invoke(args);

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8

WonkaPet
Advocate
Advocate

Thank you @_gile that worked like a charm.

0 Likes
Message 4 of 8

David_Prontnicki
Collaborator
Collaborator

@_gile 

Hi gile, quick question on the Application.Invoke. I am getting an eInvalid input error with mine and I was hoping you could help.

 

Here is the Lisp where I am trying to pass a value:

 

(defun c:cedblc ( colorMappingFilePath / )
(command "ConvertPStyles" colorMappingFilePath)
)

(vl-acad-defun 'cedblc)

 

Here is my call to:

 

Private Sub ConvertPlotStyles(ByVal lispFilePath As String, ByVal colorMappingFilePath As String)

        Try

            Dim args = New ResultBuffer(
                New TypedValue(CInt(LispDataType.Text), lispFilePath),
                New TypedValue(CInt(LispDataType.Text), colorMappingFilePath))
            Application.Invoke(args)

        Catch ex As Exception

            MessageServices.DisplayError("Error Converting Plotstyle!", "Ensure drawing name and path are correct and try again.", ex)

        End Try

    End Sub

 

Where lispFilePath = "C:\Program Files\Layer Converter\Support\cedblc.lsp"
And colorMappingFilePath = "M:\Plotting\Plot Styles\Color Mapping.stb"

 

I am trying to pass the location and file of the .stb to the LiSP as this is something that can change.

 

I noticed if I pass this "(command "ConvertPStyles" "M:\\Plotting\\Plot Styles\\Color Mapping.stb")" directly into the command line I get the error below. So I don't know if that is the issue, having the full path of the .stb, or my value is not getting passed to the LiSP. 

 

Note: I am using CommandFlags.Session

 

Annotation 2022-03-29 205740.png

0 Likes
Message 5 of 8

_gile
Consultant
Consultant

Hi,

  1. Test your LISP function directly in AutoCAD until it work as expected.
  2. You do not need the (vl-acad-defun ...) expression if the LISP function name is prefixed with 'c:'
  3. The first typed value in the result buffer have to be the LISP function name: ((int)LispDataType.Text, "c:cedblc").
  4. You do not need the CommandFlags.Session while calling LISP which only works in Document context.

If you're not comfortable with LISP, use Editor.Command instead.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

David_Prontnicki
Collaborator
Collaborator

@_gile 

 

I have tested my LiSP and fond that I need to add the path of the .stb file to the support paths. I got the lisp working on its own.

 

(defun c:cedblcctl ( colorMappingFilePath colorMappingFile )

(vl-load-com)

(setq originalPlotStylePath (getenv "PrinterStyleSheetDir"))

(vla-put-PrinterStyleSheetPath (vla-get-Files (vla-get-Preferences (vlax-get-acad-object))) colorMappingFilePath)

(command "ConvertPStyles" colorMappingFile)

(vla-put-PrinterStyleSheetPath (vla-get-Files (vla-get-Preferences (vlax-get-acad-object))) originalPlotStylePath)

)

 

The lisp works if I load it and use this in the command line: 

(c:cedblcctl "M:\\Plotting\\Plot Styles" "Colliers Color Mapping.stb")
 
But I get an eInvalid input when called from my program.

 

    Private Sub ConvertPlotStyles(ByVal lispFilePath As String, ByVal colorMappingFilePath As String)

        Try

            Dim acCurrentEditor = Active.Editor

            Dim lispLoadString As String = "(LOAD " & lispFilePath & ")"

            openedDrawingFile.SendStringToExecute(lispLoadString, True, False, False)

            Dim colorMappingFilePathDoubleSlashes As String = colorMappingFilePath.Replace("\", "\\")

            Dim stbPath As String = colorMappingFilePathDoubleSlashes.Substring(0, colorMappingFilePathDoubleSlashes.LastIndexOf("\\")) ' = "M:\\Plotting\\Plot Styles"
            Dim stbFile As String = colorMappingFilePathDoubleSlashes.Substring(colorMappingFilePathDoubleSlashes.LastIndexOf("\\") + 2) ' = "Color Mapping.stb"

            Dim args = New ResultBuffer(
                New TypedValue(CInt(LispDataType.Text), "c:cedblcctl"),
                New TypedValue(CInt(LispDataType.Text), stbPath),
                New TypedValue(CInt(LispDataType.Text), stbFile))
            Application.Invoke(args)

        Catch ex As Exception

            MessageServices.DisplayError("Error Converting Plotstyle!", "Ensure drawing name and path are correct and try again.", ex)

        End Try

    End Sub

 

 

I cant use Editor.Command with ConvertPStyles. This is the one command that is almost impossible to work with. Anyway, I think everything is right but it wont work.

0 Likes
Message 7 of 8

_gile
Consultant
Consultant

Application.Invoke runs synchronously but Document.SendStringToExecute does not.

That means the LISP file is loaded at the end of your method, after it was called by Application.Invoke.

You could either use an autoloading mechanism that ensure the LISP file is already loaded befor you run the LISP, or build a LISP (lambda) expression that does all the stuff as string to be passed to SendStringToExecute.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 8

_gile
Consultant
Consultant

You can try something like this:

var doc = Application.DocumentManager.MdiActiveDocument;
string cmd =
    "((lambda (colorMappingFilePath colorMappingFile / prefs)" +
    "(vl-load-com)" +
    "(setq prefs (vla-get-Files (vla-get-Preferences (vlax-get-acad-object))))" +
    "(setq originalPlotStylePath (getenv \"PrinterStyleSheetDir\"))" +
    "(vla-put-PrinterStyleSheetPath prefs colorMappingFilePath)" +
    "(command \"ConvertPStyles\" colorMappingFile)" +
    "(vla-put-PrinterStyleSheetPath prefs originalPlotStylePath))" +
    "\"M:\\\\Plotting\\\\Plot Styles\"" +
    "\"Colliers Color Mapping.stb\")";
doc.SendStringToExecute(cmd + "\n", false, false, false);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes