See the top rated post in this thread. Click here

Results 1 to 7 of 7

Thread: Learning AutoLisp

  1. #1
    Login to Give a bone
    0

    Default Learning AutoLisp

    I'm just starting to venture into the world of AutoLISP. With that in mind, I've "inherited" numerous LISP routines. Obviously I need to determine what each line of code/script does. Currently I have the following line and could use some explanation of it does?
    (if (setq selVariable1 (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," strVariable1)) '(410 . "~Model))))

    1. I believe the "setq" keyword assigns something to the "selection Variable 1"
    2. The "strcat" probably takes what ever texts is assigned to "String Variable 1" and concatenates it with a `*U*, prefix ? So if "String Variable 1" = "Hello World", then the result of the "strcat" keyword would be "`*U*,Hello World" What I don't know is why you would ever want this? What does the `*U*, designate/mean?
    3. I have no idea what a "cons 2" keyword does?
    4. I have no idea what a '(410 . "~Model) does?
    5. I have no idea what a '(0 . "INSERT") does?
    6. Does the "list" keyword make some kind of filter of what to include/dis-include in the "ssget" keyword command?
    7. Does the "ssget" keyword select objects from the database?
    8. Why the "_X"? I'm assuming this is just a variable called "X" but in English?

    Christmas

  2. #2
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    561
    Login to Give a bone
    0

    Default Re: Learning AutoLisp

    To understand what your asking you need to go back to the source of Autocad objects every object has a set of unique id's the dxf code.

    So 0 . Insert is that the object type is a block, 0 . "line" would mean its a line.
    the 2. *U* is the name of a block in this case its a dynamic block, a typical would be 2 . "North" where the north point block's name is "North" the * is used as wildcard search.
    410 . is the space so can be Model or a layout etc.

    Try this copy to command line pick an object, you will see some of the dxf code ID's try various objects.
    (entget (car (entsel "Pick object")))

    Re ssget filters just google just that. W CP F E plus more.

    The "X" is get all.


    A wildcard (0 . "*LINE") finds lines, lwpolylines etc (0 . "*text") finds text and mtext
    Google dxf codes.

  3. #3
    Active Member
    Join Date
    2015-12
    Location
    Western Europe
    Posts
    57
    Login to Give a bone
    1

    Default Re: Learning AutoLisp

    (setq selVariable1 (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," strVariable1)) '(410 . "~Model))))

    I have removed the (if as the rest of the statement is missing. This line constructs a selection set of entities

    (setq selVariable1 => assigns something to the named variable

    (ssget "_X" => search everywhere and return the objects that meet the criteria in the following filter list

    (list => what follows is a list and should be evaluated

    '(0 . "INSERT") => this is the first dotted pair. It is preceded by a " ' " (quote) which means it is taken literally and not evaluated. the 0 is a dxf code and relates to an entity type. The "INSERT" is the type of entity (a block)

    (cons 2 (strcat "`*U*," strVariable1)) => (cons is a list constructor so this should be evaluated. It is constructing a dotted pair. 2 is a dxf code and for blocks this is the blocks name

    (strcat constructs a string from various string element
    "*U*," This is how anonymous blocks are stored in the database. The "*U*" means all anonymous blocks. Notice the comma at the end. This is there so something else can be added
    strVariable1 = > the value contained in the variable

    if strVariable1 contains the text "Manhole" this would construct the string "*U*,Manhole" and then construct the dotted pair (2 . "*U*,Manhole") => anonymous block or block called "Manhole"

    '(410 . "~Model) this should be '(410 . "~Model") as it was missing the final ". Again this is preceded by a quote so is not evaluated but taken literally 410 is a dxf code for a space (either model or a paperspace layout) in this case the space is
    preceded by a "~" wildcard which means not. So this means everything not in modelspace

    The whole line constructs a selectionset of block entities that are anonymous or the same name as the value of strVariable1 that are not in modelspace

    References

    DXF Codes

  4. #4
    Login to Give a bone
    0

    Default Re: Learning AutoLisp

    So it might be like this?

    [All Blocks] * [dynamic blocks that are named (anonymous + strVariable1)] * [Not in model space]

    Wouldn't I get the same result with this code?
    (setq selVariable1 (ssget "_X" (list (cons 2 (strcat "`*U*," strVariable1)) '(410 . "~Model))))

    [dynamic blocks that are named (anonymous + strVariable1)] * [Not in model space]

    One "follow-up" question . . . What is the apostrophe prefix thing between the first quote and the first asterisk mean/designate in the "`*U*," ?
    Last edited by Christmas050873782348; 2020-06-10 at 10:24 PM.

  5. #5
    Active Member
    Join Date
    2015-12
    Location
    Western Europe
    Posts
    57
    Login to Give a bone
    1

    Default Re: Learning AutoLisp

    Quote Originally Posted by Christmas050873782348 View Post
    So it might be like this?

    [All Blocks] * [dynamic blocks that are named (anonymous + strVariable1)] * [Not in model space]

    Wouldn't I get the same result with this code?
    (setq selVariable1 (ssget "_X" (list (cons 2 (strcat "`*U*," strVariable1)) '(410 . "~Model))))

    [dynamic blocks that are named (anonymous + strVariable1)] * [Not in model space]

    One "follow-up" question . . . What is the apostrophe prefix thing between the first quote and the first asterisk mean/designate in the "`*U*," ?
    Not quite, the blocks don't have to be dynamic.

    It searches for Blocks not in modelspace that are anonymous or named the same as strVariable1. This kind of filter is often used for dynamic blocks or blocks with attributes, although for attributes it would be normal to include a (66 . 1) dotted pair which only attributed blocks (normally) have. A quirk with dxf dotted pairs is that if it is the default setting the pair is often missing.

    The apostrophe before the * ie '* is because the "*" character is also a wildcard. If for instance you wanted to include 3D & LWPolylines and any arc or spline fitted variants, instead of producing a long comma separated string; you can just say "*Polyline". As I previous stated the ' or quote mark means that the next character or list etc is to be interpreted literally as in the * is not a wildcard but the "*" character which is how all anonymous blocks are preceeded. As a note, this also happens with other objects (i.e dimensions "*D###") which are also stored in the block table. Similar dynamic blocks, e.g. simple blocks with only visibility states or only a flip state, will have one anonymous block per visibility/flip state as opposed one block for every inserted instance.

    Hope this helps.

    Once the selection set is gathered, it is iterated and each block is queried for its 'effectivename or 'name property which is then compared against strVariable1. If the two match the block is further processed, if it doesn't match it's skipped.

  6. #6
    Login to Give a bone
    0

    Default Re: Learning AutoLisp

    Okay, next question . . .

    I have a LISP file setup like:

    (defun c:Stamp1 ( / strName)
    . . .
    )

    (defun CM:Stampname ( obj )
    . . .
    )

    (defun c:Stamp2 ( / StrName)
    . . . .
    )

    (defun CM:Stampname ( obj )
    . . .
    )

    My question is if the multiple "CM:Stampname" functions are the same, do I really need multiple copies? Also when Stamp1 and Stamp2 call these functions, which block of code actually executes?

    Christmas May

  7. #7
    Active Member
    Join Date
    2015-12
    Location
    Western Europe
    Posts
    57
    Login to Give a bone
    1

    Default Re: Learning AutoLisp

    Without seeing the complete codes, if the (defun cm: () ..) sub functions are EXACTLY the same (allowing for the fact that AutoCAD will treat (SETQ ...) the same as (setq ..) as it is case insensitive), then you only need one of them, and as they are loaded from top to bottom then the last (lower) one will be the one executed as it will replace any previous sub function with the same name. There is unfortunately a however, as sub functions can be independent from the main function as shown in your example or imbedded within a main function. The first is totally independent (if all variables are localised) whereas the second can access variables from the main function if all variables are not localised; and both can access global variables, and that's without straying into (lambda) functions.

    Both have their uses. A prime example of a localised subfunction would be a local *error* function that replaces AutoCADs own error function whilst the main code is being run. The local error function is declared as a local variable and once the code finishes it is set to nil and control reverts to to AutoCADs own error function. Ideal for reverting AutoCAD to the state before the lisp was run eg resetting system variables or closing open files etc when CAD operators use the "ESC" key.

Similar Threads

  1. Replies: 0
    Last Post: 2013-05-05, 02:43 AM
  2. Learning worksets
    By christo4robin in forum Revit - Worksharing/Worksets/Revit Server
    Replies: 2
    Last Post: 2003-05-22, 01:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •