Author Topic: Need a little Lee Mac explaining here.  (Read 2998 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
Need a little Lee Mac explaining here.
« on: February 15, 2019, 12:08:31 PM »
Lee Mac helped me out with this bit of code that grabs all red objects on layers with a N suffix and returns them to the same layer name without the -N suffix.

I'm on muscle relaxers and pain pills right now so my brain function is limited. More so than it normally is. Which ain't saying much.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ril ( / i l s x )
  2.     (if (setq s (ssget "_X" '((8 . "*?`-N") (62 . 1))))
  3.         (repeat (setq i (sslength s))
  4.             (setq i (1- i)
  5.                   x (entget (ssname s i))
  6.                   l (assoc 8 x)
  7.             )
  8.             (entmod
  9.                 (subst
  10.                     (cons 8 (substr (cdr l) 1 (- (strlen (cdr l)) 2))) l
  11.                     (subst '(62 . 256) '(62 . 1) x)
  12.                 )
  13.             )
  14.         )
  15.     )
  16.     (princ)
  17. )

I'm trying to understand

(cons 8 (substr... below

Code - Auto/Visual Lisp: [Select]
  1.                 (subst
  2.                     (cons 8 (substr (cdr l) 1 (- (strlen (cdr l)) 2))) l
  3.                     (subst '(62 . 256) '(62 . 1) x)

If I understand, I'm returning a "substr" of the layer name starting at a given character from a list returning everything but the first element.

So, I'm guessing the first (cdr l) is the layer name with the suffix and
Code - Auto/Visual Lisp: [Select]
  1. (- (strlen (cdr l)) 2))) l

is saying to remove the last ))2))) characters from "l" element. Those two characters being the dash and the N.



Not sure at all what the
...1 in (cdr l) 1 (- (strlen...
and the
2 in (cdr l))2)))l
are doing.

Really...today the whole line is gibberish.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Need a little Lee Mac explaining here.
« Reply #1 on: February 15, 2019, 01:04:03 PM »
If you consider that (cdr L) returns a string and then look up strlen and substr in the Help, you should be able to figure it out.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: Need a little Lee Mac explaining here.
« Reply #2 on: February 15, 2019, 01:30:17 PM »
jlogan02,
Do you know how to evaluate code statements in the VLIDE ??

ADDED:
https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-CE06986C-8495-486E-AA02-CB00FD833678

You dont need to select full lines ... you CAN select any code statement ( ie included in/with a balanced set of parentheses )
« Last Edit: February 15, 2019, 01:59:25 PM by kdub »
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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Need a little Lee Mac explaining here.
« Reply #3 on: February 15, 2019, 06:17:01 PM »
Whenever attempting to understand a snippet of code, it is usually helpful to step through the evaluation from the inside-out, reviewing the values returned by each expression evaluated.

In this case, let us assume that our drawing contains a single red circle on layer "TEST-N".

Evaluating the ssget expression at the Visual LISP IDE Console yields a selection set containing such circle:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq s (ssget "_X" '((8 . "*?`-N") (62 . 1))))
  2. <Selection set: 31>

We can then verify the number of times that the repeat loop will be evaluated and the value of the index variable i by evaluating the sslength expression:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq i (sslength s))
  2. 1

The next setq expression assigns three values to three symbols:

The variable i is decremented to 0 (since selection set indexes are zero-based, i.e. they start at zero):
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq i (1- i))
  2. 0

The DXF data for the entity residing at index 0 is obtained:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq x (entget (ssname s i)))
  2. ((0 . "CIRCLE") (100 . "AcDbEntity") ... (8 . "TEST-N") (62 . 1) (100 . "AcDbCircle") (10 0.0 0.0 0.0) (40 . 1.0))

And the dotted pair for DXF group 8 (the layer) is obtained from this DXF data:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq l (assoc 8 x))
  2. (8 . "TEST-N")

Moving on to the innermost expressions within the entmod expression: first, the dotted pair corresponding to DXF group 62 (the colour) is substituted for a dotted pair with value 256 (ByLayer):
Code - Auto/Visual Lisp: [Select]
  1. _$ (subst '(62 . 256) '(62 . 1) x)
  2. ((0 . "CIRCLE") (100 . "AcDbEntity") ... (8 . "TEST-N") (62 . 256) (100 . "AcDbCircle") (10 0.0 0.0 0.0) (40 . 1.0))

This list is then supplied as an argument to the surrounding subst expression which substitutes the new layer name.

Breaking down the construction of the dotted pair for the new DXF group 8 entry, we have the following:

When supplied with a dotted pair, cdr will return the value associated with the key - in this case, the layer name:
Code - Auto/Visual Lisp: [Select]
  1. _$ (cdr l)
  2. "TEST-N"

The strlen function provides the number of characters comprising this layer name:
Code - Auto/Visual Lisp: [Select]
  1. _$ (strlen (cdr l))
  2. 6

2 characters are then subtracted from this total:
Code - Auto/Visual Lisp: [Select]
  1. _$ (- (strlen (cdr l)) 2)
  2. 4

And this figure is then supplied to the substr function to indicate the number of characters to be returned following the given starting character:
Code - Auto/Visual Lisp: [Select]
  1. _$ (substr (cdr l) 1 (- (strlen (cdr l)) 2))
  2. "TEST"

We can insert the known results of the previous evaluated expressions into this expression for clarity:
Code - Auto/Visual Lisp: [Select]
  1. _$ (substr "TEST-N" 1 4)
  2. "TEST"

In words, this expression is saying: "Return 4 characters starting from the 1st character in the string 'TEST-N'"

The cons function is then used to construct the dotted pair:
Code - Auto/Visual Lisp: [Select]
  1. _$ (cons 8 (substr (cdr l) 1 (- (strlen (cdr l)) 2)))
  2. (8 . "TEST")

And hence the second subst expression has the following arguments:
Code - Auto/Visual Lisp: [Select]
  1. (subst '(8 . "TEST") '(8 . "TEST-N") <DXF data whose colour has already been substituted>)

This will therefore return a list of DXF data in which both DXF groups 8 & 62 have been substituted with new values, corresponding to a change to the layer & colour respectively.

But the entity has not been modified at this point!

Up until now, we have only been working with a list of data and have not committed the change to the drawing database...

This is where the entmod function is introduced, to modify the entity:
Code - Auto/Visual Lisp: [Select]
  1. (entmod <DXF data whose layer & colour has been substituted>)

This final expression updates the DXF data as it appears in the drawing database, causing the objects in the drawing to be modified.
« Last Edit: February 15, 2019, 06:26:49 PM by Lee Mac »

jlogan02

  • Bull Frog
  • Posts: 327
Re: Need a little Lee Mac explaining here.
« Reply #4 on: February 16, 2019, 03:32:34 PM »
Wow!!! Save that and frame it Lee.

Kdub, literally the first thing I did before posting was "try" to use VLIDE console. I was clearly using it wrong. Thanks for that.

Lee,

Very well explained. Thanks a million. This is the kind of help I've been looking for. Real world examples that walk through the entire code to explain it line by line.

 :uglystupid2: MUST NOT FORGET VLIDE CONSOLE.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Need a little Lee Mac explaining here.
« Reply #5 on: February 16, 2019, 06:21:35 PM »
Wow!!! Save that and frame it Lee.

Very well explained. Thanks a million. This is the kind of help I've been looking for. Real world examples that walk through the entire code to explain it line by line.

You're most welcome jlogan, I'm pleased that you found my explanation helpful :-)