Select through script or AutoLisp

Select through script or AutoLisp

ezervas
Participant Participant
1,451 Views
16 Replies
Message 1 of 17

Select through script or AutoLisp

ezervas
Participant
Participant

Hi all,

 

I would like to select on screen some entities based on their handle. I already have a list of their handles in excel and I want to feed this list into a command and have the entities selected on screen. I can use VBA to export the list to an scr or lsp file, but unfortunately I use AutoCAD LT and I cannot use VBA all the way.

 

Please note that I am not looking into selecting them on screen by clicking on each individual entity.

 

I have tried the AutoLisp selection set manipulation commands and I get back the creation of selection sets, but I never manage to make any of those sets to actually "materialise" as selection on screen, which is what I want. I essentially need this as a way to select the AutoCAD LT entities from excel and continue the interaction on them in AutoCAD LT.

 

What I have tried so far is to use VBA to generate a scr file utilising the SELECT and HANDENT commands. When I copy and paste its content in the command line, it works. When I try to run it as a script it does not, although there are no errors in the command line history.

 

The closest I can get so far is to use VBA to put the commands in the Clipboard and paste it in command line, but is there any better way to do this?

 

Thanks a lot in advance!

0 Likes
Accepted solutions (1)
1,452 Views
16 Replies
Replies (16)
Message 2 of 17

ВeekeeCZ
Consultant
Consultant

Just a quick test... seems working.

0 Likes
Message 3 of 17

ezervas
Participant
Participant

Thanks, I appreciate the help!

 

I presume that the intention has been to replace the "move" command with the "select" one and that the use of the "move" command serves as demonstration that the objects are selected correctly. The problem is that "select" is kind of special and still does not work. As a matter of fact, "move" would also work with a simpler setout, without the need for a lambda function.

 

My problem is not feeding objects to just any command like "move" by handle. This is not a problem. The problem is feeding them specifically to the "select" command (or any other command with a similar effect should any exists) and actually watch the objects become selected on screen, with their grips etc. Then, having the objects selected on screen, I can do any other desired operations further as if I had selected the objects by hand. I do appreciate the idea about a lambda function nevertheless!

0 Likes
Message 4 of 17

ВeekeeCZ
Consultant
Consultant

Then use (sssetfirst nil ss) and somehow create a selection set.

 

In my example it could be 

(sssetfirst nil ((lambda ( / s) (setq s (ssadd)) (foreach h '("149D" "149C") (ssadd (handent h) s)))))

 

Also you can try to use PSELECT instead of SELECT.

0 Likes
Message 5 of 17

Sea-Haven
Mentor
Mentor

I think your biggest problem is your using say LT2024 or 25, these do not support the get "Aplication" method which is what you need to talk to Excel or Excel to talk to CAD. So you will need to do say write a file via Excel VBA or read a csv file getting the handle ID. A re-arrange of the @ВeekeeCZ code. 

 

I think Bricscad Lite supports get Application. I have Pro version. Some one may answer.

 

This is not supported 

(setq myxl (vlax-get-or-create-object "excel.Application"))

 

 

0 Likes
Message 6 of 17

DGCSCAD
Collaborator
Collaborator

You can control the visibility of selection sets using sssetfirst.

 

Edit: d'oh! Didn't see BeekeeCZ 's post. 

 

 

(defun c:hand_sel ()
(setq ss_hands (ssadd))
(setq hand_list (list "a1" "a2" "a3"));<-----Set your Handles from the CSV file here
(foreach n hand_list (ssadd (handent n) ss_hands))
(sssetfirst nil ss_hands)
(princ)
)

 

 

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 7 of 17

komondormrex
Mentor
Mentor

hey there,

generation of a lisp expression to copy from the excel to paste to the autocad command line to select objects by theirs handles. based on @ВeekeeCZ lambda.

komondormrex_0-1735501165165.pngkomondormrex_1-1735501207685.pngkomondormrex_2-1735501241836.png

 

0 Likes
Message 8 of 17

Sea-Haven
Mentor
Mentor

@komondormrex great idea just let Excel do the work, did you do a bit of VBA macro so read selection and convert to a single multi value string? Then make the string to be copied. 

 

Threw the question at Copilot and got some really useful starting code and made some changes to it. 

 

 

 

 

Sub JoinColumnValues()
    Dim lastRow As Long
    Dim i As Long
    Dim joinedValues As String
    Dim delimiter As String
    
    ' Specify the delimiter (e.g., a comma)
    delimiter = "  "
    
    ' Find the last row in column A with data
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    ' Loop through each cell in column A
    For i = 1 To lastRow
        If Cells(i, 1).Value <> "" Then
            If joinedValues = "" Then
                joinedValues = Chr(34) & Cells(i, 1).Value & Chr(34)
            Else
                joinedValues = joinedValues & delimiter & Chr(34) & Cells(i, 1).Value & Chr(34)
            End If
        End If
    Next i
    
    joinedValues = "(sssetfirst nil ((lambda ( / s) (setq s (ssadd)) (foreach h '(" & joinedValues & ") (ssadd (handent h) s)))))"
    
    ' Output the joined values into a specific cell (e.g., cell B1)
    Range("C1").Value = joinedValues
    
    ' Inform the user
    MsgBox "Values have been joined and placed in cell B1!"
End Sub

 

 

 

 

Message 9 of 17

ezervas
Participant
Participant
Hi, apologies for taking long to respond. Yes, that is the intention. It generally works, except for the "SELECT" command. It still works if you put the character sequence in the clipboard and paste it in the command line, which what I currently do. But it would be helpful to understand why this particular command doesn't work with script files.
0 Likes
Message 10 of 17

ezervas
Participant
Participant
Hi, thanks and apologies for taking that long to respond.
Creating the string and pasting it in the command line works. It even works with a simpler setup using the "HANDENT" command. But as long as you place the same text in a script file and try to run it, it does not. If rather than "SELECT" you apply the same operation to another command e.g. "MOVE", it works from the script file as well, as expected. But "SELECT" seems an edgy exception.
0 Likes
Message 11 of 17

ezervas
Participant
Participant

Hi there and apologies for taking that long to respond. Unfortunately, this yields an error and the "PSELECT" command seems not existing in the LT version, To be honest I barely understand how the code works, so it could be myself not using it right. I think I'll go with the option of creating the lisp command as text, put in the clipboard with VBA and then just paste it in the command line. For some reason, it works.

 

The reason why I insisted on script files is that I hoped to push buttons from excel, create a script file and then run it always using the same command in the AutoCAD LT command line. So, e.g. making a command "AAA" that essentially executes "whatever the commands in X.scr file say" and then just push an Excel button each time to alter the commands in the X.scr file. This seamed neat, I had the workflow in mind and then it stuck on the "SELECT" command.

 

Anyways, thanks for the help, I think I'll go for the Clipboard option.

0 Likes
Message 12 of 17

Sea-Haven
Mentor
Mentor

As I mentioned no idea why Autodesk did not do a 100% lisp implementation, then you would not have problems. You can save the macro to a file and import it into the current Excel. 

0 Likes
Message 13 of 17

ec-cad
Collaborator
Collaborator

At the end of the script, try putting in:

(command "_resume")

 

If your Lisp wants you to 'select' something, do so, and it should continue..

 

ECCAD

0 Likes
Message 14 of 17

ezervas
Participant
Participant
Hi, thanks for this, but unfortunately it shows the same behaviour. Essentially, I want to to accomplish the same result this piece of code does when typed into the AutoCAD LT command line, with having it in a script file instead.
'select (handent "2c1") (handent "2c2") ' (without the single quotes and with double space at the end).

Typing it in the command line works. Putting it in an *.scr file does not. Unfortunately, even if you add the resume command at the end. It is strange but it seems that somehow the dialogs for running the script somehow take out or prohibit any selection, even if the script commands dictate so.
0 Likes
Message 15 of 17

Kent1Cooper
Consultant
Consultant
Accepted solution

@ezervas wrote:
.....
'select (handent "2c1") (handent "2c2") ' (without the single quotes and with double space at the end).
Typing it in the command line works. Putting it in an *.scr file does not. ....

If the issue is that SELECT operates differently from inside a Script than it does at the command line, in not leaving you with the objects selected/highlighted/gripped [that's true of SELECT within an AutoLisp (command) function], then try this for your script:

select (handent "2c1") (handent "2c2")

(sssetfirst nil (ssget "_P"))

 

Kent Cooper, AIA
0 Likes
Message 16 of 17

ezervas
Participant
Participant
I could hardly believe that this did the trick and tried it several time, but it did work! Thank you so much!
0 Likes
Message 17 of 17

Kent1Cooper
Consultant
Consultant

You're welcome.  I didn't test it with handles, so it was a guess, but knowing some commands operate differently in coded approaches than they do at the Command line, I at least verified that's the case with SELECT when used in AutoLisp code.  I will tuck into background memory that it also does in a Script, in case I ever need such a usage.

Kent Cooper, AIA
0 Likes