TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MSTG007 on September 08, 2015, 04:18:36 PM

Title: Change Lisp from Suffix to Prefix
Post by: MSTG007 on September 08, 2015, 04:18:36 PM
How hard is it to change this to a Prefix setup oppose from the Suffix?

Thanks for the help!

Code: [Select]
(defun C:MF (/ ent elay ldata); = Move To -New Layer [and make that Layer]
  (prompt "\nTo make ...-New Layers and move objects to them,")
  (setq ss (ssget ":L")); not things on locked Layers
  (repeat (sslength ss)
    (setq
      ent (ssname ss 0); first [remaining] object in selection
      elay (cdr (assoc 8 (entget ent)))
      ldata ; Layer data without non-transferable elements
        (vl-remove-if-not
          '(lambda (x) (member (car x) '(0 100 2 70 62 6)))
          (entget (tblobjname "layer" elay))
        ); vl-remove-if-not & ldata
    ); setq
    (if
      (or
        (< (strlen elay) 5)
          ; not a long-enough Layer name to already end in -New with anything preceding
        (/= (strcase (substr elay (- (strlen elay) 3))) "-New"); not already on a Layer like that
      ); and
      (progn
        (entmake (subst (cons 2 (strcat elay "-New")) (assoc 2 ldata) ldata)); create Layer
        (command "_.chprop" ent "" "_layer" (strcat elay "-New") ""); move object to it
      ); progn
    ); if
    (ssdel ent ss); take that one out of selection
  ); repeat
); defun
Title: Re: Change Lisp from Suffix to Prefix
Post by: Lee Mac on September 08, 2015, 06:31:55 PM
Try the following:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:mf ( / a e i n s )
  2.     (if (setq s (ssget "_:L" '((8 . "~New-*"))))
  3.         (repeat (setq i (sslength s))
  4.             (setq e (entget (ssname s (setq i (1- i))))
  5.                   a (assoc 8 e)
  6.                   n (strcat "New-" (cdr a))
  7.             )
  8.             (or (tblsearch "layer" n)
  9.                 (entmake (subst (cons 2 n) (cons 2 (cdr a)) (entget (tblobjname "layer" (cdr a)))))
  10.             )
  11.             (entmod (subst (cons 8 n) a e))
  12.         )
  13.     )
  14.     (princ)
  15. )
Title: Re: Change Lisp from Suffix to Prefix
Post by: MSTG007 on September 09, 2015, 07:08:31 AM
Thanks Lee for the update. Could I ask what is the difference between the way the original code versus the revised one? I did not know if there was a few strings in there that just needed to be "swapped" to become a prefix from the original suffix.

Also, for a side note (I guess) with users who are on Civil3D Cogo / parts, the original lisp can take those parts and move them to the correct layer. I tried with the revised and am not getting the same result. Thank you  Lee for your help!
Title: Re: Change Lisp from Suffix to Prefix
Post by: Lee Mac on September 09, 2015, 12:56:55 PM
Thanks Lee for the update. Could I ask what is the difference between the way the original code versus the revised one? I did not know if there was a few strings in there that just needed to be "swapped" to become a prefix from the original suffix.

The original code included an if statement to test whether the layer of the selected object was already suffixed, however, this is unnecessary as such objects can easily be omitted from the initial selection using an appropriate ssget filter list.

To revise the original code you would essentially need to change (cons 2 (strcat elay "-New")) to (cons 2 (strcat "New-" elay)) however, the test expressions for the if statement would also need to be either removed or amended to test for the existence of this prefix.

Also, for a side note (I guess) with users who are on Civil3D Cogo / parts, the original lisp can take those parts and move them to the correct layer. I tried with the revised and am not getting the same result. Thank you  Lee for your help!

I don't use the Vertical applications (such as Civil/Map etc.), but I'm guessing that entmod may not be sufficient to change the layer for objects native to these applications; the following code will change the ActiveX layer property instead.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:mf ( / a e i n s )
  2.     (if (setq s (ssget "_:L" '((8 . "~New-*"))))
  3.         (repeat (setq i (sslength s))
  4.             (setq e (ssname s (setq i (1- i)))
  5.                   a (cdr (assoc 8 (entget e)))
  6.                   n (strcat "New-" a)
  7.             )
  8.             (if (or (tblsearch "layer" n)
  9.                     (entmake (subst (cons 2 n) (cons 2 a) (entget (tblobjname "layer" a))))
  10.                 )
  11.                 (vla-put-layer (vlax-ename->vla-object e) n)
  12.             )
  13.         )
  14.     )
  15.     (princ)
  16. )

(EDIT: Changed (assoc 8 (entget e)) to (cdr (assoc 8 (entget e))) to save a few keystrokes later on)
Title: Re: Change Lisp from Suffix to Prefix
Post by: MSTG007 on September 09, 2015, 01:02:43 PM
That's amazing. Basically the same outline of the code but swapping out the ent to the vla. Wow. It works. Active X... wow.
Title: Re: Change Lisp from Suffix to Prefix
Post by: Lee Mac on September 09, 2015, 01:21:39 PM
That's amazing. Basically the same outline of the code but swapping out the ent to the vla. Wow. It works. Active X... wow.

Thanks - I've tweaked the code a little more as noted above, as retention of the dotted pair was no longer required.
Title: Re: Change Lisp from Suffix to Prefix
Post by: JohnK on September 09, 2015, 03:16:25 PM
Try the following:
...
"*meh*" post.

Thanks Lee for the update. Could I ask what is the difference between the way the original code versus the revised one? I did not know if there was a few strings in there that just needed to be "swapped" to become a prefix from the original suffix.

The original code included an if statement to test whether the layer of the selected object was already suffixed, however, this is unnecessary as such objects can easily be omitted from the initial selection using an appropriate ssget filter list.

To revise the original code you would essentially need to change (cons 2 (strcat elay "-New")) to (cons 2 (strcat "New-" elay)) however, the test expressions for the if statement would also need to be either removed or amended to test for the existence of this prefix.
...
Good post.

Your second post is nice, Lee. :)
Title: Re: Change Lisp from Suffix to Prefix
Post by: Lee Mac on September 09, 2015, 05:33:45 PM
Thank you John, I take your point.
Title: Re: Change Lisp from Suffix to Prefix
Post by: Kerry on September 09, 2015, 06:22:42 PM
Thank you John, I take your point.

That in itself is a skill.

Sometimes John is more obscure than even I am.
Title: Re: Change Lisp from Suffix to Prefix
Post by: roy_043 on September 10, 2015, 03:44:43 AM
Code - Auto/Visual Lisp: [Select]
  1. (defun c:mf ( / a e i n s )
  2.     (if (setq s (ssget "_:L" '((8 . "~New-*"))))
  3.         (repeat (setq i (sslength s))
  4.             (setq e (ssname s (setq i (1- i)))
  5.                   a (cdr (assoc 8 (entget e)))
  6.                   n (strcat "New-" a)
  7.             )
  8.             (if (or (tblsearch "layer" n)
  9.                     (entmake (subst (cons 2 n) (cons 2 a) (entget (tblobjname "layer" a))))
  10.                 )
  11.                 (vla-put-layer (vlax-ename->vla-object e) n)
  12.             )
  13.         )
  14.     )
  15.     (princ)
  16. )

A note of warning. Using, almost, raw entget data for entmake can be tricky. I would advise against it. In this case there can be an issue with a possible extension dictionary. At least in BricsCAD V14 there is.
Code: [Select]
: (entmake (subst '(2 . "Target") '(2 . "Source") (entget (tblobjname "layer" "Source"))))
((-1 . <Entity name: cbf8cb0>) (0 . "LAYER") (5 . "AD") (102 . "{ACAD_XDICTIONARY") (360 . <Entity name: 1afb2f50>) (102 . "}") (330 . <Entity name: cae2638>) (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "Target") (70 . 0) (62 . 7) (6 . "Continuous") (290 . 1) (370 . -3) (390 . <Entity name: cae9f18>) (347 . <Entity name: caeb0a0>))
: AUDIT
Fix any errors detected? Yes/<No>: y

Name: AcDbDictionary(B3)
Value: Owner Id (B5)
Validation: Invalid
Replaced by: Set to AcDbLayerTableRecord(AD)

Name: AcDbLayerTableRecord(B5)
Value: Duplicate ownership of reference (B3)
Validation: Invalid
Replaced by: Removed

117 objects audited
Total errors found during audit 2, fixed 2
Title: Re: Change Lisp from Suffix to Prefix
Post by: MSTG007 on December 10, 2015, 10:00:26 AM
I hate to ask this. Is there away to remove the suffix piece?
Title: Re: Change Lisp from Suffix to Prefix
Post by: ronjonp on December 10, 2015, 10:19:56 AM
Quick example:
Code - Auto/Visual Lisp: [Select]
  1. (setq test "string-suffix")
  2. (substr test 1 (- (strlen test) (strlen "-suffix")))
Title: Re: Change Lisp from Suffix to Prefix
Post by: MSTG007 on December 11, 2015, 07:33:06 AM
Ron,
Sorry I am having a hard time following on this...

I was toying around with this... nadda luck though.

Code: [Select]
(defun c:removesuffix ( / a e i n s )
    (if (setq s (ssget "_:L" '((8 . "*_DEMO"))))
        (repeat (setq i (sslength s))
            (setq e (ssname s (setq i (1- i)))
                  a (cdr (assoc 8 (entget e)))
                  n (strcat "_DEMO" a)
            )
           (if (or (tblsearch "layer" n)
                    (entmake (subst (cons 2 n) (cons 2 a) (entget (tblobjname "layer" a))))
                )
                (vla-put-layer (vlax-ename->vla-object e) n)
           )
        )
    )
    (princ)
)
(vl-load-com) (princ)

Title: Re: Change Lisp from Suffix to Prefix
Post by: ronjonp on December 11, 2015, 09:10:35 AM
Maybe more descriptive variables & comments will help.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:removesuffix (/ ename i layername layernamewithoutsuffix selectionset suffix)
  2.   ;; Identify what the suffix is
  3.   (setq suffix "_DEMO")
  4.   ;; Get a selection set of items that are on layers (strcat "*" suffix)
  5.   (if (setq selectionset (ssget "_:L" (list (cons 8 (strcat "*" suffix)))))
  6.     ;; Repeat the number of items in the selection set
  7.     (repeat (setq i (sslength selectionset))
  8.       (setq ;; Get the entity name while removing 1 from index 'i'
  9.             ename                  (ssname selectionset (setq i (1- i)))
  10.             ;; Grab its layer
  11.             layername              (cdr (assoc 8 (entget ename)))
  12.             ;; Remove the suffix from the layername
  13.             layernamewithoutsuffix (substr layername 1 (- (strlen layername) (strlen suffix)))
  14.       )
  15.       ;; If we find an existing layer without the suffix or we successfully create the new layer
  16.       (if (or (tblsearch "layer" layernamewithoutsuffix)
  17.               (entmake (subst (cons 2 layernamewithoutsuffix)
  18.                               (cons 2 layername)
  19.                               (entget (tblobjname "layer" layername))
  20.                        )
  21.               )
  22.           )
  23.         ;; Put the item selected on the layer without the suffix
  24.         (entmod (subst (cons 8 layernamewithoutsuffix) (assoc 8 (entget ename)) (entget ename)))
  25.       )
  26.     )
  27.   )
  28.   ;; Clean exit
  29.   (princ)
  30. )
Title: Re: Change Lisp from Suffix to Prefix
Post by: MSTG007 on December 11, 2015, 09:13:34 AM
Gosh, I need to do this on all the other ones, so I know what the heck is going on. I forget so easily. Thank you for that. Seriously THANK YOU!
Title: Re: Change Lisp from Suffix to Prefix
Post by: ronjonp on December 11, 2015, 09:19:57 AM
Gosh, I need to do this on all the other ones, so I know what the heck is going on. I forget so easily. Thank you for that. Seriously THANK YOU!
:)  Glad to help out.