Author Topic: LM:SentenceCase Use in routine  (Read 4896 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
LM:SentenceCase Use in routine
« on: November 18, 2022, 01:22:02 PM »
I want to set the layer filter name to Sentence Case using Lee Mac's LM:SentenceCase

Code - Auto/Visual Lisp: [Select]
  1. ;; Sentence Case - Lee Mac
  2. ;; Returns the supplied string converted to Sentence Case
  3.  
  4. (defun LM:SentenceCase ( s / f )
  5.     (vl-list->string
  6.         (mapcar
  7.             (function
  8.                 (lambda ( a b c )
  9.                     (if (or f (= 46 a)) (progn (setq f (= 32 b)) b) c)
  10.                 )
  11.             )
  12.             (cons 46 (vl-string->list s))
  13.             (vl-string->list (strcase s))
  14.             (vl-string->list (strcase s t))
  15.         )
  16.     )
  17. )

I'm just not sure where to put the defun (LM:SentenceCase) in the code below.

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;| --------------------------------
  3. Function created by unknown
  4. -------------------------------- |;
  5.  
  6. (defun get-layer-filter-names (/ collection)
  7.   (setq names (list ""))
  8.   (if
  9.     (not
  10.         (setq collection (vl-catch-all-apply
  11.                            (function
  12.                              (lambda ()
  13.                                (vla-item
  14.                                  (vla-getextensiondictionary
  15.                                    (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
  16.                                  )
  17.                                  "ACAD_LAYERFILTERS"
  18.                                )
  19.                              )
  20.                            )
  21.                          )
  22.         )
  23.       )
  24.     )
  25.     (vlax-for item collection
  26.       (setq names (cons (strcase (vla-get-name item)) names))
  27.     )
  28.   )
  29.   names
  30. ); function
  31.  
  32. (get-layer-filter-names)
  33.  
  34. ;; get layer filter name..
  35.  
  36. (setq layn (strcase "Stations Standard Layers"))
  37. (if (member layn names)
  38.   (princ "\nStations Standard Layers already exists.")
  39.   (command "._-layer" "filter" "Rename" "Stations Standard Layer" layn "")
  40. ); if
J. Logan
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #1 on: November 18, 2022, 02:57:03 PM »
My question still stands. However, I was wanting Title Case not Sentence Case.
Code - Auto/Visual Lisp: [Select]
  1. ;; Title Case - Lee Mac
  2. ;; Returns the supplied string converted to Title Case
  3.  
  4. (defun LM:TitleCase ( s )
  5.     (vl-list->string
  6.         (mapcar
  7.             (function
  8.                 (lambda ( a b c ) (if (= 32 a) b c))
  9.             )
  10.             (cons 32 (vl-string->list s))
  11.             (vl-string->list (strcase s))
  12.             (vl-string->list (strcase s t))
  13.         )
  14.     )
  15. )

Usage (LM:TitleCase "Stations Standard Layers")
J. Logan
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 327
Solved: LM:SentenceCase Use in routine
« Reply #2 on: November 18, 2022, 03:50:39 PM »
Solved. Instead of using the variable
Code - Auto/Visual Lisp: [Select]
  1. layn
, just spell it out like you want it.

Code - Auto/Visual Lisp: [Select]
  1. (setq layn (strcase "Stations Standard Layers"))
  2. (if (member layn names)
  3.   (princ "\nStations Standard Layers already exists.")
  4.   (command "._-layer" "filter" "Rename" "Stations Standard Layer" "Stations Standard Layers" "")
J. Logan
ACAD 2018

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

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2122
  • class keyThumper<T>:ILazy<T>
Re: LM:SentenceCase Use in routine
« Reply #3 on: November 18, 2022, 05:01:11 PM »
Ahh, now I understand . . . You just want to add an 's' to a specific filter name.

. . . but you don't seem to be checking if the layerFilter you are renaming actually exists.

I assume you want to batch process drawings, or run this as part of a config routine, but you do realise that Right-click on the Filter name offers a rename option.

Regards,

added:

a piccy if 'Renamed' filter does not exist

« Last Edit: November 18, 2022, 05:06:33 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.

jlogan02

  • Bull Frog
  • Posts: 327
Dammit almost solved: LM:SentenceCase Use in routine
« Reply #4 on: November 18, 2022, 05:36:57 PM »
The "Stations Standard Layers already exists" message doesn't work. If the filter name exists I get...

Invalid layer filter name.
Enter new layer filter name:

This is generated by the command line syntax for the layer filter rename.

Code - Auto/Visual Lisp: [Select]
  1.  (setq layn (strcase "Stations Standard Layers"))
  2. (if (member layn names)
  3.   (princ "Stations Standard Layers already exists.")
  4.   (command "._-layer" "filter" "Rename" "Stations Standard Layer" "Stations Standard Layers" "")
  5. ); if

Clearly I'm wrong somewhere.

kdub, this would be in startup. Check if it exists if not add the "s"
« Last Edit: November 18, 2022, 05:47:21 PM by jlogan02 »
J. Logan
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #5 on: November 18, 2022, 06:08:23 PM »
Ahh, now I understand . . . You just want to add an 's' to a specific filter name.

. . . but you don't seem to be checking if the layerFilter you are renaming actually exists.

I assume you want to batch process drawings, or run this as part of a config routine, but you do realise that Right-click on the Filter name offers a rename option.

Regards,

added:

a piccy if 'Renamed' filter does not exist

I gotcha. My "if" statement should include a search for the filter name not the variable "layn".

I think I need to be looking at the extension directory for the layer filter name.
J. Logan
ACAD 2018

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

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2122
  • class keyThumper<T>:ILazy<T>
Re: LM:SentenceCase Use in routine
« Reply #6 on: November 18, 2022, 07:13:53 PM »
Perhaps something like :
(may not be bomb proof :)

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:DoTest (/ oldName newName extensionDict layerFilter)
  3.   (or *acdoc*
  4.   )
  5.   (setq oldName "frutier"
  6.         newName "Fruitiers"
  7.   )
  8.  
  9.   (if (and
  10.         (setq extensionDict
  11.                (vla-getExtensionDictionary (vla-get-Layers *acdoc*))
  12.         )
  13.         (setq layerFilters
  14.                (kdub:safeitem extensionDict "ACAD_LAYERFILTERS")
  15.         )
  16.       )
  17.     (if (setq layerFilter (kdub:safeitem layerFilters newName))
  18.       (princ
  19.         (strcat "\n" (vla-get-Name layerFilter) " already exists.")
  20.       )
  21.       ;;else
  22.       (if (setq layerFilter (kdub:safeitem layerFilters oldName))
  23.         (progn
  24.           ;;(vla-put-Name layerFilter newName)
  25.           (command "._-layer" "filter" "Rename" oldName newName "")
  26.           (princ "\nFilter name was changed.")
  27.         )
  28.         ;;else
  29.         (princ (strcat "\n" oldName " filter does not exists."))
  30.       )
  31.     )
  32.     ;;else
  33.     ;;
  34.   )
  35.  
  36.   (princ)
  37. )
  38.  
  39.  
  40. ;;;--------------------------------------------------------------------
  41.  
  42. (defun kdub:safeitem (collection item / returnvalue)
  43.              (setq returnvalue
  44.                     (vl-catch-all-apply
  45.                       'vla-item
  46.                       (list collection item)
  47.                     )
  48.              )
  49.            )
  50.       )
  51.     returnvalue
  52.   )
  53. )
  54.  
  55. ;;;--------------------------------------------------------------------
  56.  
  57.  

« Last Edit: November 18, 2022, 07:32:11 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.

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #7 on: November 21, 2022, 11:12:15 AM »
Thanks kdub, I'll take a look at this. Is your safeitem routine a shortened method for getting at the filters collection by using vla-item?
J. Logan
ACAD 2018

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

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2122
  • class keyThumper<T>:ILazy<T>
Re: LM:SentenceCase Use in routine
« Reply #8 on: November 21, 2022, 02:47:31 PM »
Thanks kdub, I'll take a look at this. Is your safeitem routine a shortened method for getting at the filters collection by using vla-item?

Hi,
It just a GuardClause function which either returns  the collection item (VLA-OBJECT) or nil

An optimised version is
Code - Auto/Visual Lisp: [Select]
  1. (defun GetSafeItem (collection item / vlaObj)
  2.   (vl-catch-all-apply
  3.     (function (lambda () (setq vlaObj (vla-item collection item ))))
  4.   )
  5.   vlaObj
  6. )
  7.  

Test this
Code - Auto/Visual Lisp: [Select]
  1.   (or *acdoc*
  2.   )
  3. (GetSafeItem (vla-get-layers *acdoc*) "0")
  4.  
  5. (GetSafeItem (vla-get-layers *acdoc*) "NoSuchLayer")
  6.  
  7. (vla-item (vla-get-layers *acdoc*) "NoSuchLayer")
  8.  

result:
Code: [Select]
#<VLA-OBJECT IAcadLayer 0000021e6f957cd8>

nil

; error: Automation Error. Key not found


We just need to test the returned value, rather than have the function crash.
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.

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #9 on: November 21, 2022, 05:46:38 PM »
Perhaps something like :
(may not be bomb proof :)

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:DoTest (/ oldName newName extensionDict layerFilter)
  3.   (or *acdoc*
  4.   )
  5.   (setq oldName "frutier"
  6.         newName "Fruitiers"
  7.   )
  8.  
  9.   (if (and
  10.         (setq extensionDict
  11.                (vla-getExtensionDictionary (vla-get-Layers *acdoc*))
  12.         )
  13.         (setq layerFilters
  14.                (kdub:safeitem extensionDict "ACAD_LAYERFILTERS")
  15.         )
  16.       )
  17.     (if (setq layerFilter (kdub:safeitem layerFilters newName))
  18.       (princ
  19.         (strcat "\n" (vla-get-Name layerFilter) " already exists.")
  20.       )
  21.       ;;else
  22.       (if (setq layerFilter (kdub:safeitem layerFilters oldName))
  23.         (progn
  24.           ;;(vla-put-Name layerFilter newName)
  25.           (command "._-layer" "filter" "Rename" oldName newName "")
  26.           (princ "\nFilter name was changed.")
  27.         )
  28.         ;;else
  29.         (princ (strcat "\n" oldName " filter does not exists."))
  30.       )
  31.     )
  32.     ;;else
  33.     ;;
  34.   )
  35.  
  36.   (princ)
  37. )
  38.  
  39.  
  40. ;;;--------------------------------------------------------------------
  41.  
  42. (defun kdub:safeitem (collection item / returnvalue)
  43.              (setq returnvalue
  44.                     (vl-catch-all-apply
  45.                       'vla-item
  46.                       (list collection item)
  47.                     )
  48.              )
  49.            )
  50.       )
  51.     returnvalue
  52.   )
  53. )
  54.  
  55. ;;;--------------------------------------------------------------------
  56.  
  57.  

Just tested this with no luck. Well, that's not entirely true. Regardless if the old filter exists or not the routine tells me it doesn't exist. That's literally all it does.

For a dumb guy like me, your code makes perfect sense. I'll be darned if I see why it's not working. As I see it, it's an either or situation. If the "s" exists "NewName" do nothing. If the "s" doesn't exist OldName...rename the filter, add the "s".

Thanks for the explanation on the GetSafeItem.
J. Logan
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #10 on: November 21, 2022, 06:15:21 PM »
Just went through the whole process manually.

Used the command line to create the filter "Stations Standard Layers"
Set another filter current.
Used the command line to delete the filter. It did not delete it.

I couldn't rename it either.

Right click in the layers dialog works for both deleting and renaming.

Letter limit? Or spaces? I just manually named a filter Station and manually renamed it to Stations, just fine.
J. Logan
ACAD 2018

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

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2122
  • class keyThumper<T>:ILazy<T>
Re: LM:SentenceCase Use in routine
« Reply #11 on: November 21, 2022, 06:36:32 PM »
That's weird ;
The default name for a new Properties Filter (in the dialog) has spaces.

Running the version you posted :

Code: [Select]
Command: DOTEST
._-layer
Current layer:  "Apple"
Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile/Xref]: filter
Current layer filter: "All"
Enter a layer filter option [New/Set/Rename/Edit/Delete/eXit]: Rename
Enter layer filter to rename: Fruit Drops
Enter new layer filter name: Fruit Dropper
Current layer filter: "All"
Enter a layer filter option [New/Set/Rename/Edit/Delete/eXit]:
Command:
Filter name was changed.


If the Layer Properties Manager is OPEN, the changes ( made by the program) are not displayed untill it is closed and re-opened

« Last Edit: November 21, 2022, 06:43:09 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.

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #12 on: November 22, 2022, 12:33:29 PM »
Ok, now do me a favor and run the routine with the new name Fruit Dropper as the filter. Does that give you the "already exists" message?
J. Logan
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #13 on: November 22, 2022, 01:59:11 PM »
Is it the difference between a group filter and a properties filter? All works fine if I make it a properties filter.
J. Logan
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 327
Re: LM:SentenceCase Use in routine
« Reply #14 on: November 22, 2022, 02:21:53 PM »
Group filter test...again. Says the filter I'm looking for is Current, but then says isn't found.

J. Logan
ACAD 2018

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