Author Topic: bad argument type: consp <Selection set: 75d3> When run this lisp  (Read 14439 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
I created this lisp to add a string to a set of attributes
When run gives this error
Code: [Select]
bad argument type: consp <Selection set: 75d3>
The lisp
Code: [Select]
(defun c:StAdd2 (/ doc spc _StartUndo _EndUndo DocActv spc
st s ss c
ent obj att atta attsa)
  (vl-load-com)

  (defun *error* ( msg ) (and doc (_EndUndo doc)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ))
  (defun _StartUndo ( doc ) (_EndUndo doc) (vla-StartUndoMark doc))
  (defun _EndUndo ( doc ) (if (= 8 (logand 8 (getvar 'UNDOCTL))) (vla-EndUndoMark doc)))
  (defun DocActv (/ doc spc ) (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))) (setq spc (if (or (eq AcModelSpace (vla-get-ActiveSpace doc)) (eq :vlax-true   (vla-get-MSpace doc))) (vla-get-ModelSpace doc) (vla-get-PaperSpace doc))))

  
  (WHILE
    (setq c -1)
    (setq St (getstring "\nString to be Added: "))
    (setq ss (ssget '((0 . "INSERT") (66 . 1))))
    
    (foreach x ss
      (setq ent (ssname ss (setq c (1+ c))))
      (setq obj (vlax-ename->vla-object ent))
      (setq att (vla-get-textstring obj))
      (setq atta (strcat st att))
      (setq AttSA (vla-put-TextString att atta))
      )
    )
  )

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #1 on: February 21, 2011, 07:24:17 AM »
Hi,

Without reading your code, this error message means that you're trying to pass a selection set to a function which requieres a list.
Speaking English as a French Frog

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #2 on: February 21, 2011, 07:38:36 AM »
It's already made by Lee in a few hours a go.

http://www.cadtutor.net/forum/showthread.php?56993-Change-text-inside-blocks

EDIT: If server is not working at the moment , you can try later .
« Last Edit: February 21, 2011, 07:41:59 AM by Tharwat »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #3 on: February 21, 2011, 08:28:52 AM »
Code: [Select]
(foreach x ss
Just as Gile said. You are trying to step through your selection set with foreach (works on lists). Use while or repeat.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

SOFITO_SOFT

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #4 on: February 21, 2011, 04:24:43 PM »
Hello
This function is very useful to pass a SELECTION SET to a LIST.

Code: [Select]
( DEFUN SS-TO-LIST ( SS / CONT LIS)
( IF ( AND
       SS
       ( EQUAL ( TYPE SS ) 'PICKSET )
     )
  ( PROGN
    ( SETQ LIS NIL CONT 0 )
    ( WHILE ( < CONT ( SSLENGTH SS ) )
      ( SETQ LIS ( APPEND LIS ( LIST ( SSNAME SS CONT ) ) ) )
      ( SETQ CONT ( 1+ CONT ))
    )
  )
  ( SETQ LIS '() )
)
LIS
)
Greetings  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #5 on: February 21, 2011, 05:01:45 PM »
Sofito,

append is pretty slow - better to use cons as shown in these examples.

Also, no need for:

Code: [Select]
( SETQ LIS NIL or
Code: [Select]
( SETQ LIS '() )
Since 'LIS' is localised in the code.

SOFITO_SOFT

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #6 on: February 21, 2011, 05:56:07 PM »
Hello people of the swamp.
Lee: I also tend to use cons better than append , but sometimes (as in this case) I use append to the list has the same order of elements as the SS. Sometimes this is important.
And yes ... LIS is localised "(DEFUN SS-TO-LIST (SS / CONT LIS <<<!!!) " but so the code is more clear to Hassan. If he has tried a foreach with a SELECTION SET, he needs a few things clear.
My DEFUN can be improved,sure, but it sure Hassan is clearly understood (I think !!!  :-) )
Thank you for your good and expert  advices.
Greetings from Madrid. :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #7 on: February 21, 2011, 05:58:56 PM »
but sometimes (as in this case) I use append to the list has the same order of elements as the SS. Sometimes this is important.

Very rarely :? And if the order is important, a reverse/cons combination is still quicker than append.

And yes ... LIS is localised "(DEFUN SS-TO-LIST (SS / CONT LIS <<<!!!) " but so the code is more clear to Hassan.

I don't see how redundant code is 'clear' for a beginner to learn from.

SOFITO_SOFT

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #8 on: February 22, 2011, 03:05:00 AM »
Hello:
best redundant code that requires 2 or 3 deductions code ... skills are developed gradually.
Except for those who are very smart, who are experts of birth. :lol:
Greetings. :-)

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #9 on: February 22, 2011, 07:49:47 AM »
Dumped
I think that, I'll go back to old lisp and choose one by one and bick one by one

Code: [Select]
(defun c:SA (/ doc spc)
  (vl-load-com)

  (defun *error* ( msg ) (and doc (_EndUndo doc)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ))
  (defun _StartUndo ( doc ) (_EndUndo doc) (vla-StartUndoMark doc))
  (defun _EndUndo ( doc ) (if (= 8 (logand 8 (getvar 'UNDOCTL))) (vla-EndUndoMark doc)))
  (defun DocActv (/ doc spc ) (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))) (setq spc (if (or (eq AcModelSpace (vla-get-ActiveSpace doc)) (eq :vlax-true   (vla-get-MSpace doc))) (vla-get-ModelSpace doc) (vla-get-PaperSpace doc))))

  (while
    (setq St (getstring "\nString to be Added: "))

    (WHILE
      (setq ATT (car (nentsel "\nSelect attribute: ")))
      (SETQ ATTVL (vlax-ename->vla-object ATT))
      (SETQ ATTSTR (VLA-GET-TEXTSTRING ATTVL))
      (setq StAdd (strcat ATTSTR st))
      (setq AttStAdd (vla-put-TextString ATTVL StAdd))
      )
    )
  )

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #10 on: February 22, 2011, 01:23:34 PM »
but sometimes (as in this case) I use append to the list has the same order of elements as the SS. Sometimes this is important.

Very rarely :? And if the order is important, a reverse/cons combination is still quicker than append.

And yes ... LIS is localised "(DEFUN SS-TO-LIST (SS / CONT LIS <<<!!!) " but so the code is more clear to Hassan.

I don't see how redundant code is 'clear' for a beginner to learn from.

Even (reverse...) isn't needed if you start at the back end of the list then step the index backwards.  Then you can (cons...) to your hearts content and still keep the original list order.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

cadman6735

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #11 on: April 21, 2011, 11:56:07 AM »
Hi dgorsman,

May I ask how you start at the back end of a list and index backwards?

Thanks

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #12 on: April 21, 2011, 11:57:47 AM »
Code: [Select]
(repeat (setq i (sslength ss))
  (setq l (cons (ssname ss (setq i (1- i))) l))
)

 :-)

cadman6735

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #13 on: April 21, 2011, 12:09:09 PM »
Ok

I would have never of thought to count backwards...    :ugly:

thanks for showing me a new technique.
« Last Edit: April 21, 2011, 12:19:20 PM by cadman6735 »

cadman6735

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #14 on: April 21, 2011, 12:39:47 PM »
So what is a selection set?

I always thought of a selection set to be a list of entities or a table of entities.

While and repeat evaluate expressions, repeating a course of action over and over again till stopped somehow. 
Foreach evaluates expressions on a list.  A concept I had not caught onto till now.

I have seen vlax-for functions used on tables so this tells me that a selection set is not a table and it is not a list, so what is a selection set?  Is it its own thing?

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #15 on: April 21, 2011, 12:46:24 PM »
Hi dgorsman,

May I ask how you start at the back end of a list and index backwards?

Thanks

Code: [Select]
(setq i (- (sslength ss_set) 1))
(while (> i -1)
   ...
   (setq i (- i 1))
)

No point in recalculating the length of a selection set/list/whatever each loop if its not going to change during the loop.  As noted, with lists it has the added advantage that using (cons...) to build a sub-list will result in the same relative order as the original list.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #16 on: April 21, 2011, 12:54:26 PM »
No point in recalculating the length of a selection set/list/whatever each loop if its not going to change during the loop.

It doesn't, consider the following:

Code: [Select]
(setq l '(1 2 3 4 5)) 

(repeat (setq i (length l))
  (setq l (cdr l))
  (print i)
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #17 on: April 21, 2011, 01:07:29 PM »
So what is a selection set?

A SelectionSet is a SelectionSet - SelectionSets have their own data type in LISP: PICKSET

Code: [Select]
Command: (setq ss (ssget))
Select objects: 2 found
Select objects:
<Selection set: 5>

Command: (type ss)
PICKSET

In a sense, a SelectionSet is an array of entities, with each entity residing at an index in the array. Formally, it cannot be called a 'LIST' since it is not of LIST type and cannot be manipulated as such.

Note also that when manipulating SelectionSets, the variable does not 'store' the SelectionSet, but rather 'points' to it (the same applies to Entity Names), hence, we can modify the SelectionSet using such functions as ssdel/ssadd without needing to change the value of the variable pointing to the SelectionSet:

Code: [Select]
Command: (sslength ss)
2
Command: (ssdel (ssname ss 0) ss)
<Selection set: 5>

Command: (sslength ss)
1

Notice we didn't use:

Code: [Select]
(setq ss (ssdel (ssname ss 0) ss))
Also, the index of entities in a SelectionSet is dynamic - if you delete an entity at index 0 say, the indexes of all the other entities will be shifted down 1:

Code: [Select]
Command: (sslength ss)
2
Command: (ssdel (ssname ss 0) ss)
<Selection set: 5>
Command: (sslength ss)
1

Command: (ssname ss 0)
<Entity name: 7ef03a78>
Command: (ssname ss 1)
nil

While and repeat evaluate expressions, repeating a course of action over and over again till stopped somehow.

While: Loop expression evaluation until a test expression returns nil.

Repeat: Loop expression evaluation a set number of times.

Foreach: evaluate one or more expressions on every item of a list in turn.

Note that when we use such functions with SelectionSets, we are only constructing a loop in which to use ssname.

I have seen vlax-for functions used on tables so this tells me that a selection set is not a table and it is not a list, so what is a selection set?  Is it its own thing?

vlax-for is for use on VLA Collection Objects, i.e. the Layer Collection, Block Collection, but also Dictionaries.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #18 on: April 21, 2011, 01:08:10 PM »
With repeat, no.  But to each their own.

I settled on using (while...) constructs for most cases of iteration a while back.  Makes coding a little faster (benefits of consistency :-) ), especially since there is usually some exit condition that needs to be checked for aside from index underflow.  It also has the benefit of visually separating out the length calculation from the continue/exit logic.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #19 on: April 21, 2011, 01:09:24 PM »
I'd say they both have their uses.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #20 on: April 21, 2011, 01:09:34 PM »
With repeat, no.  But to each their own.

Apologies, I thought your remark was directed at my code.

cadman6735

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #21 on: April 21, 2011, 01:41:25 PM »
Quote
vlax-for is for use on VLA Collection Objects, i.e. the Layer Collection, Block Collection, but also Dictionaries.

This is what I thought a selection set was, A Collection of Objects or (entities).  But I have sinced learned that entities are on the screen and objects are not.  

Line = entity, Layer = Object.

So Foreach functions, vanila and visual work on Objects (layers, dimstyles, textstyles etc...)

but the foreach function does not work on Entities, such as line, circles, text, polyline etc..  
A selection set is nothing more than a collection of entities and to manipulate this collection I have the ssFunctions with While or Repeat functions.

Just trying to verbalize my thoughts to grasp a new concept, (new concept to me that is)  Am I on the right path?

Edit:  I just reread the quote I quoted from lee and I completly contradict myself,  I need to think more on this.
« Last Edit: April 21, 2011, 01:46:27 PM by cadman6735 »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #22 on: April 21, 2011, 01:49:14 PM »
This is what I thought a selection set was, A Collection of Objects or (entities).

Accessed through the SelectionSets Collection using Visual LISP, a SelectionSet can be manipulated as a VL Collection Object, as one can use vlax-for to iterate through the VLA-Objects contained therein.

However, note that in the examples in my previous post I am not using Visual LISP.

 
But I have sinced learned that entities are on the screen and objects are not.

This is not true.

Note that Entities can be converted to VLA-Objects using vlax-ename->vla-object and vice versa using vlax-vla-object->ename

 
So Foreach functions, vanila and visual work on Objects (layers, dimstyles, textstyles etc...)

but the foreach function does not work on Entities, such as line, circles, text, polyline etc.. 

Clarify: 'work on Entities'

Foreach is a function to evaluate expressions on items in a list, nothing more.

I suggest you re-read my previous post :-)

cadman6735

  • Guest
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #23 on: April 21, 2011, 02:23:17 PM »
 :-)  Thanks Lee

I almost understand,  I am on the verge of understanding a concept and just trying to discuss my thoughts.

Quote
Note that Entities can be converted to VLA-Objects using vlax-ename->vla-object and vice versa using vlax-vla-object->ename

This is a good point, my comment comes from a blog I stumbled across and it seemed a simple enough concept.  This is why I verbalize my thoughts, so I can get different points of view for a straighter line of sight.

Thanks to all

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #24 on: April 21, 2011, 02:28:30 PM »
You're welcome CADMan. :-)

I suppose the concept to keep in mind is that where the Drawing Database manipulation (modifying graphical/non-graphical entities) is concered, Vanilla AutoLISP and Visual LISP run in parallel. Some things are easier accomplished with Vanilla, some with Visual.

However, Visual LISP gives you access to the ActiveX object model, exposing methods/properties that Vanilla can't emulate.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: bad argument type: consp <Selection set: 75d3> When run this lisp
« Reply #25 on: April 21, 2011, 02:47:01 PM »
... like things outside of AutoCAD: MSXML DOM, ADO, Excel object model, Scripting FileSystemObjects, to name a few.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}