Set layout with initget

Set layout with initget

EnM4st3r
Advocate Advocate
1,269件の閲覧回数
19件の返信
メッセージ1/20

Set layout with initget

EnM4st3r
Advocate
Advocate

Hi, I want to select a Layout using initget.

I wrote the following function to do that for me. Its working when there are no spaces within the Layoutname but if there are spaces in it, for example "A0 - view A", then it does not work.

Any suggestions?

 

(defun setlayout (/ list2String) ; checks if its in Model area, if so, asks the user to change
   (if (= (getvar "ctab") "Model")
    (progn
      (defun list2String (lst space / str) 
        (setq str (car lst))
        (foreach itm (cdr lst)
          (setq str (strcat str space itm))
        )
        str
      )
      (initget 1 (list2String (layoutlist) " "))
      (setvar "ctab" (getkword (strcat "Select Layout [" (list2String (layoutlist) "/") "]: ")))
    )
  )
);defun

 

0 件のいいね
解決済み
1,270件の閲覧回数
19件の返信
返信 (19)
メッセージ2/20

ВeekeeCZ
Consultant
Consultant
解決済み

I would use numbers... like this.

 

(progn (initget "1 2 3") (getkword "Select [1:Layout-a/2:Lay/3:Layout1]: "))

 

See the attached routine to see this example in code.

0 件のいいね
メッセージ3/20

Kent1Cooper
Consultant
Consultant

Keywords cannot have spaces in them, because a space is the delimiter between keywords in a single string in an (initget) function.

Kent Cooper, AIA
0 件のいいね
メッセージ4/20

EnM4st3r
Advocate
Advocate

yes, thats the problem..

 

went with @ВeekeeCZ suggestion and thats my result now:

(defun setlayout (/ lst) ; checks if its in Model area, if so, asks the user to change
  (setq lst (layoutlist))
   (if (= (getvar "ctab") "Model")
    (progn
      (initget 1 (substr (apply 'strcat (mapcar '(lambda (x) (strcat " " (itoa (vl-position x lst)))) lst)) 2))
      (setvar "ctab" (nth (atoi (getkword (strcat "Select Layout [" (substr (apply 'strcat (mapcar '(lambda (x) (strcat "/" (itoa (vl-position x lst)) ":" x)) lst)) 2) "]: "))) lst))
    )
  )
);defun
0 件のいいね
メッセージ5/20

ВeekeeCZ
Consultant
Consultant

FYI

(layoutlist) returns a list of layouts in order of creation. If you wanna have them sorted in current order, use something like this

 

;; layouts in order
  (defun :layouts-in-order (/ l) ; pbejse https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/layout-order/td-p/3749964
    (vlax-for o (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
      (setq l (cons (list (vla-get-name o) (vla-get-taborder o)) l)))
    (mapcar 'car (cdr (vl-sort l '(lambda (a b) (< (cadr a) (cadr b)))))))

 

メッセージ6/20

komondormrex
Mentor
Mentor

not in order of creation, but in strings sorting order to be precise.

0 件のいいね
メッセージ7/20

Sea-Haven
Mentor
Mentor

This is a GOTO but uses numbers like GOTO layout 12. Needs multi getvals.lsp as pops a dcl for layout number, save in a support path or change (if (not AH:getvalsm)(load "Multi Getvals.lsp")) to include full path of file use "\\". Hint 99 will go to last 0 goes to model.

SeaHaven_0-1696983586645.png

 

 

 

0 件のいいね
メッセージ8/20

komondormrex
Mentor
Mentor

@Kent1Cooper wrote:

Keywords cannot have spaces in them


actually they could, see the gif.

 

 

0 件のいいね
メッセージ9/20

ВeekeeCZ
Consultant
Consultant

@komondormrex wrote:

@Kent1Cooper wrote:

Keywords cannot have spaces in them


actually they could, see the gif.

 


 

Ohh, come on! They can't. Period. 

As you know, in this case of yours are keywords actually just those numbers. 

0 件のいいね
メッセージ10/20

komondormrex
Mentor
Mentor

Period. 


it is comma)



As you know, in this case of yours are keywords actually just those numbers. 


not at all.

 

0 件のいいね
メッセージ11/20

ВeekeeCZ
Consultant
Consultant

Ok, nice. Looks cool!

 

Hey, I have 4 layouts:

AA 1

AA 2

BB 1

BB 2

I would like to pick them in the same fashion as you did. Just typing: aa 2

And please, drop those initial numbers of yours, just wasted space.

So the prompt would look like this:

Select layout [AA 1/AA 2/BB 1/ BB 2]: 

Thx!

0 件のいいね
メッセージ12/20

komondormrex
Mentor
Mentor

@ВeekeeCZ 

i said it is a comma not ellipsis and show that it is possible to get layout set even if it has spaces in its name using kw. that's it.

0 件のいいね
メッセージ13/20

ВeekeeCZ
Consultant
Consultant

Well, I can imagine that your trick might be useful for some partial cases... but this seems too much to me.

 

Denying the claim that a keyword can contain a space is fundamentally wrong. Because if we accept it, we will create 2 keywords. Period.

Have fun!

0 件のいいね
メッセージ14/20

komondormrex
Mentor
Mentor

you know, that one is the case. btw you can not set active layout with spaced name with the solution of yours or i am missing something?

0 件のいいね
メッセージ15/20

ВeekeeCZ
Consultant
Consultant

Not sure what you mean. 

 

(progn (initget "1 2 3") (getkword "Select [1:Layout bbb/2:Lay/3:Layout aaa]: "))

 

You can pick it by a mouse, you can type the highlighted number. Cannot type the name itself. That's the limitation... but spaces are not limited.

 

 

Your example... I don't like it because it presents something that it's not ready for. Say I don't realize that I have AA twice. So I start typing AA... then I would need to pick from 2 options... it should narrow it down.... but it's not... it's a dead end.

0 件のいいね
メッセージ16/20

komondormrex
Mentor
Mentor

was caddy glitch i think, returned nil a couple of times. now works with exception of setting a layout from drop down list. 

upd.

not a glitch as i thought. it works not if i stay not in a model tab.

0 件のいいね
メッセージ17/20

ВeekeeCZ
Consultant
Consultant

OK, I see.

Never thought of that testing. We don't use it. Good to know.

0 件のいいね
メッセージ18/20

Sea-Haven
Mentor
Mentor
解決済み

So for a dynamic initget using (layoutlist)

 

 

 

(defun c:laygo ( / lays)
(setq lays (layoutlist))
(setq lays (cons "Please choose " lays))
(if (not AH:Butts)(load "Multi radio buttons.lsp"))
(setq ans (ah:butts 1 "V" lays))
(setvar 'ctab ans)
(princ)
)

 

 Or if you have lots of layouts the above supports about 20 layouts max.

 

 

(defun c:layl ( / lays but)
(setq lays (layoutlist))
(if (not AHlstbox)(load "Listbox-AH.lsp"))
(setq but (ahlstbox "Pick a layout" lays 20 10))
(setvar 'ctab (nth but lays))
(princ)
)

 

SeaHaven_0-1697070417536.png

 

メッセージ19/20

ВeekeeCZ
Consultant
Consultant

Ok, tied some generic solution with minimum necessary translation. Seems good enough to me. Some issues obvious to you? Alternatively, post a solution that you consider better.

(layoutlist) is just for example.

 

(defun c:LayoutListPickable ( / lst lst-key)
  
  (setq lst (layoutlist))
  (setq lst-key (mapcar '(lambda (x / n) (cons (strcat (if (< (setq n (vl-position x lst)) 10) "0" "") (itoa n) ":" (vl-string-translate " ()[]<>/" "-{}{}{}|" x)) x)) lst))
  
  (initget 1 (substr (apply 'strcat (mapcar '(lambda (x) (strcat " " (car x))) lst-key)) 2))
  (setvar 'ctab (cdr (assoc (getkword (strcat "Select Layout [" (substr (apply 'strcat (mapcar '(lambda (x) (strcat "/" (car x))) lst-key)) 2) "]: ")) lst-key)))
  
  (princ)
  )

 

0 件のいいね
メッセージ20/20

EnM4st3r
Advocate
Advocate
thx, this one is nice 👍
0 件のいいね