Author Topic: add a data type vector to AutoLISP  (Read 2675 times)

0 Members and 1 Guest are viewing this topic.

baitang36

  • Bull Frog
  • Posts: 213
add a data type vector to AutoLISP
« on: March 07, 2022, 07:43:40 PM »
Vector is a data type not disclosed by the Autodesk. It is somewhat similar to the list, but it saves more memory and operates faster. It can also be converted with the list.
The data structure of LISP list is a linked list, which needs to be traversed from scratch, so the operation speed is slow. Vector doesn't have this limitation and can start from anywhere, so it's faster.
A vector of print will be displayed like this: #4 (1, 2, 3, 4), indicating that the vector has four elements, and the list of elements is in parentheses.
After research, I have basically understood its mechanism. Let's test it here.
I wrote a small program syz-vector.fas, you can operate the vector after loading.
Several related functions:
Vector                        create a vector
Vector-length              gets the length of the vector
list->vector
vector->List
vector-ELT                 gets an element of the vector
Vector-ELT<-             replaces an element of a vector
Vector-swap               swaps two elements of a vector

test example:
Command: (vector 1 2 3 4 5)
#5(1 2 3 4 5)

Command: (setq AA (vector 1 2 3 4 5))
#5(1 2 3 4 5)

Command: (vector-length AA)
5

Command: (vector->List AA)
(1 2 3 4 5)

Command: (list->vector '(2 3 4 5))
#4(2 3 4 5)

Command: (vector->ELT AA 3)
4

Command: (vector ELT<- AA 8 3)
8
Command: (print AA)
#5(1 2 3 8 5)

Command: (vector-swap AA 0 3)
#5(8 2 3 1 5)

Command: (vector-swap AA 1 4)
#5(8 5 3 1 2)
« Last Edit: March 07, 2022, 07:47:14 PM by baitang36 »

JohnK

  • Administrator
  • Seagull
  • Posts: 10664
Re: add a data type vector to AutoLISP
« Reply #1 on: March 07, 2022, 08:23:30 PM »
Vectors are very cool (I use them in C++)!!

baitang36, you are cool too!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

baitang36

  • Bull Frog
  • Posts: 213
Re: add a data type vector to AutoLISP
« Reply #2 on: March 09, 2022, 02:38:47 AM »
Vectors are very cool (I use them in C++)!!

baitang36, you are cool too!
thank you. you are cool too!

d2010

  • Bull Frog
  • Posts: 326
Re: add a data type vector to AutoLISP
« Reply #3 on: April 15, 2022, 05:04:21 AM »
How to delete first element of Vector, without CDR or convertted the Vector to List?
Code: [Select]
(setq v100 (vector "12" "Here is hrun"))
(vector??Delete v100 0)
(princ  v100)
Must display on screen$, the
#1("Here is hrun")
??

dexus

  • Bull Frog
  • Posts: 213
Re: add a data type vector to AutoLISP
« Reply #4 on: April 15, 2022, 05:19:52 AM »
This looks really cool!

How did you make a function with variable amount of arguments?

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: add a data type vector to AutoLISP
« Reply #5 on: April 15, 2022, 06:42:33 AM »
I used VECTOR function ...

(setq v1 (VECTOR 1 2 3 4 5 6 7) )
#7(1 2 3 4 5 6 7)

_$ (setq v2 (VECTOR 1 2 3 4) )
#4(1 2 3 4)

_$ (type v1)
_LPPTYPE
_$

... so this one is a function with a variable number of arguments . . . !
...
is there any guru here who can explain to us
how to create a defun that accepts a variable number of arguments?
...
what's the trick ?
« Last Edit: April 15, 2022, 02:57:52 PM by domenicomaria »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2152
  • class keyThumper<T>:ILazy<T>
Re: add a data type vector to AutoLISP
« Reply #6 on: April 15, 2022, 07:58:37 PM »
\
how to create a defun that accepts a variable number of arguments?
...
what's the trick ?

The simple way is to pass either
a list of values
or an associative list of keys and values.

I find an associative list helps with maintaining the data integrity.

a A list of values can be added to conventional parameters
Code - Auto/Visual Lisp: [Select]
  1. (defun doStuff( point radius datalist)
  2.    
  3.     ;; you'll need to unpack the datalist and assign the values as local variables
  4.     ;; OR address the values by index in the list)
  5.  
  6.     ;; Then process the data . . . .
  7. )
  8.  
  9. (setq datalist '((name "thingie") (width 3) (size 4.7263) (depth 5) (mirror 1)  (hidden 1) ))
  10.     ;=>((NAME "thingie") (WIDTH 3) (SIZE 4.7263) (DEPTH 5) (MIRROR 1) (HIDDEN 1))
  11.  
  12. (doStuff point radius datalist);
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8821
  • AKA Daniel
Re: add a data type vector to AutoLISP
« Reply #7 on: April 16, 2022, 12:22:01 AM »
does (vector '(220 344.12 313.81 1)) work?
for some reason acad barfs when passing this to arx

edit, I wonder if this is something internal for creating a vlax-safearray
« Last Edit: April 16, 2022, 12:26:01 AM by It's Alive! »

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: add a data type vector to AutoLISP
« Reply #8 on: April 16, 2022, 02:46:20 AM »
@kdub

The problem of passing a variable number of arguments to a function
is easily solved by passing a list of arguments ...
... because the LIST function accepts a variable number of arguments ...
...
what I am trying to figure out is how do you create a function (using DEFUN)
that accepts a variable number of arguments ...

... because DEFUN accepts a predefined number of arguments ...
... if BAITANG succeeded, this means that there are aspects of Visual Lisp
that are unknown to almost all users ...

... and I wonder if anyone who knows them wants to share this information ...

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8821
  • AKA Daniel
Re: add a data type vector to AutoLISP
« Reply #9 on: April 16, 2022, 07:19:22 AM »
... because DEFUN accepts a predefined number of arguments ...
... if BAITANG succeeded, this means that there are aspects of Visual Lisp
that are unknown to almost all users ...

This may be true from a purely autolisp point of view, however lisp functions created from ARX or .NET do not have this limitation.
consider ARX function acedDefun and how it uses acedGetArgs. The resbuf can be difficult to unpack if it contains multiple types such as
a combination of list and non list items, probably why it was never implemented (i.e. common lisp (&rest args))

What BAITANG has found is probably some internal method in the autolisp evaluator.


domenicomaria

  • Swamp Rat
  • Posts: 725
Re: add a data type vector to AutoLISP
« Reply #10 on: April 16, 2022, 07:53:21 AM »
FAS file can contain ONLY VLISP code ...

Not  .NET or C++ ...


"What BAITANG has found is probably
some internal method in the autolisp evaluator."


What ?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8821
  • AKA Daniel
Re: add a data type vector to AutoLISP
« Reply #11 on: April 16, 2022, 08:49:59 AM »
FAS file can contain ONLY VLISP code ...

Not  .NET or C++ ...


"What BAITANG has found is probably
some internal method in the autolisp evaluator."


What ?

The autolisp evaluator is written in C, the FAS reader is probably read by C, BAITANG uses the address of an internal lisp function in the FAS right?



kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2152
  • class keyThumper<T>:ILazy<T>
Re: add a data type vector to AutoLISP
« Reply #12 on: April 16, 2022, 04:38:48 PM »

... and I wonder if anyone who knows them wants to share this information ...

You could try to encourage @baitang36 to post the source code instead of compiled code !!



Code - Auto/Visual Lisp: [Select]
  1. _$ (type v1)
  2. _LPPTYPE
I haven't followed up on this , but I thought LPPTYPE was a type from Common Lisp (Lisp++  Library )


Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

baitang36

  • Bull Frog
  • Posts: 213
Re: add a data type vector to AutoLISP
« Reply #13 on: April 17, 2022, 08:52:15 AM »


You could try to encourage @baitang36 to post the source code instead of compiled code !!


It's not that I don't want to provide the source code, but that I can't make LSP run directly

The reserved function of autodisk cannot be used in LSP, but only in FAS.

baitang36

  • Bull Frog
  • Posts: 213
Re: add a data type vector to AutoLISP
« Reply #14 on: April 17, 2022, 08:56:02 AM »
The autolisp evaluator is written in C, the FAS reader is probably read by C, BAITANG uses the address of an internal lisp function in the FAS right?
Not C, this is LSP, which is a function reserved by Autodesk for its own use and not provided to users.