Changing TextStyle on Specific Mleaderstyles via (V)LISP via a List

Changing TextStyle on Specific Mleaderstyles via (V)LISP via a List

Anonymous
Not applicable
957 Views
6 Replies
Message 1 of 7

Changing TextStyle on Specific Mleaderstyles via (V)LISP via a List

Anonymous
Not applicable

Hi all,

I have pieced together a function (with several sub-functions I've found here - props to all other authors I borrowed from) to:

  1. Evaluate if a specific mleaderstyle exists in the drawing
  2. If it's present, change the Textstyle to Standard

Code:

 

(DEFUN RESETMLEADERS ()

  ::****************************************
  ;;function to test if mleaderstyle exists
  ::****************************************
  (defun mLeaderstyleExist (styl / dict result)
    (if	(setq dict (dictsearch (namedobjdict) "ACAD_MLEADERSTYLE"))
      (foreach x dict
	(if (and (= (car x) 3)
		 (eq (strcase (cdr x)) (strcase styl))
	    )
	  (setq result t)
	)
      )
    )
    result
  )
  ::****************************************
  ;;reset mleaderstyle "HEX-ARROW"
  ::****************************************
  (defun RESEThexa ()
    (setq dict	(dictsearch (namedobjdict) "ACAD_MLEADERSTYLE")
	  dict1	(member (cons 3 "HEX-ARROW") dict)
	  el1	(entget (cdr (assoc 350 dict1)))
	  er	(tblobjname "style" "Standard")
    )
    (entmod (subst (cons 342 er) (assoc 342 el1) el1))
  )
  ::****************************************
  ;;reset mleaderstyle "HEX-DOT"
  ::****************************************
  (defun RESEThexd ()
    (setq dict	(dictsearch (namedobjdict) "ACAD_MLEADERSTYLE")
	  dict1	(member (cons 3 "HEX-DOT") dict)
	  el1	(entget (cdr (assoc 350 dict1)))
	  er	(tblobjname "style" "Standard")
    )
    (entmod (subst (cons 342 er) (assoc 342 el1) el1))
  )
  ::****************************************
  ;;reset mleaderstyle "HEX-LOOP"
  ::****************************************
  (defun RESEThexl ()
    (setq dict	(dictsearch (namedobjdict) "ACAD_MLEADERSTYLE")
	  dict1	(member (cons 3 "HEX-LOOP") dict)
	  el1	(entget (cdr (assoc 350 dict1)))
	  er	(tblobjname "style" "Standard")
    )
    (entmod (subst (cons 342 er) (assoc 342 el1) el1))
  )
  ::****************************************
  ;;reset mleaderstyle "HEX-TILDE"
  ::****************************************
  (defun RESEThext ()
    (setq dict	(dictsearch (namedobjdict) "ACAD_MLEADERSTYLE")
	  dict1	(member (cons 3 "HEX-TILDE") dict)
	  el1	(entget (cdr (assoc 350 dict1)))
	  er	(tblobjname "style" "Standard")
    )
    (entmod (subst (cons 342 er) (assoc 342 el1) el1))
  )
  ::****************************************
  ;;check if mleaderstyle exists - if so, run its RESET function
  ::****************************************
  (IF
    (mleaderstyleexist "HEX-ARROW")
     (RESEThexa)
     (PRINC)
  )					;END IF
;;;__________________________________
  (IF
    (mleaderstyleexist "HEX-DOT")
     (RESEThexd)
     (PRINC)
  )					;END IF
;;;__________________________________
  (IF
    (mleaderstyleexist "HEX-LOOP")
     (RESEThexl)
     (PRINC)
  )					;END IF
;;;__________________________________
  (IF
    (mleaderstyleexist "HEX-TILDE")
     (RESEThext)
     (PRINC)
  )					;END IF
;;;__________________________________
)					;end defun RESETMLEADERS


While this RESETMLEADERS function works, I think it could be done more efficiently by:

 

  1. passing a list of mleaderstyles to look for (instead of defining a ton of individual subfunctions)
  2. and for each that exists in the drawing
  3. set the textstyle to Standard.

The thing is, I'm not really strong with the syntax to pass/evaluate lists and doing something foreach.

If someone could point me in the direction of just the general structure of how to do this with mleaderstyles, I'd really appreciate it.

 

I understand that VLISP can be more efficient when working with mleaderstyles - but I'm even less proficient with VLISP.

 

Thanks so much!

0 Likes
Accepted solutions (2)
958 Views
6 Replies
Replies (6)
Message 2 of 7

SeeMSixty7
Advisor
Advisor
Accepted solution

You are correct. Just glancing through you could do something like this.

(DEFUN RESETMLEADERS ()
  ::****************************************
  ;;function to test if mleaderstyle exists
  ::****************************************
  (defun mLeaderstyleExist (styl / dict result)
    (if	(setq dict (dictsearch (namedobjdict) "ACAD_MLEADERSTYLE"))
      (foreach x dict
	(if (and (= (car x) 3)
		 (eq (strcase (cdr x)) (strcase styl))
	    )
	  (setq result t)
	)
      )
    )
    result
  )
;;;;;;;;;;;;Added the list of mleaders styles (setq mleaderlist (list "HEX-ARROW" "HEX-DOT" "HEX-LOOP" "HEX-TILDE"))
;;;;;;;;;;;; created your local function to update a mleaderstyle (defun ResetTheMLeader (mleaderstylename) (setq dict (dictsearch (namedobjdict) "ACAD_MLEADERSTYLE") dict1 (member (cons 3 mleaderstylename) dict) el1 (entget (cdr (assoc 350 dict1))) er (tblobjname "style" "Standard") ) (entmod (subst (cons 342 er) (assoc 342 el1) el1)) ) ;;;step through the list and update (foreach mleaderitm meladerlist (resetthemleader mleaderitm) ) ) ;end defun RESETMLEADERS

Good luck

Message 3 of 7

Anonymous
Not applicable

Thanks for tidying up my code, SeeMSixty7 - it works!

 

Just so I can apply this moving forward - am I correct in seeing that you:

  1. set a variable named MLEADERLIST comprising the list of mleaderstyles to evaluate for in the drawing
  2. then, with the "foreach", it assigns each list element of the MLEADERLIST variable to a floating variable called MLEADERITM
  3. for each found list element at MLEADERITM, execute the RESETTHEMLEADER function

Is that a basic structure of how identifying a LIST and <do something> FOREACH works? If so, I think I finally get it! 🙂

 

For posterity, here's my final cleaned-up version:

 

(DEFUN RESETMLEADERS ()

;;;;;;;;;;;;Added the list of mleaders styles
  (setq mleaderlist (list "HEX-ARROW" "HEX-DOT" "HEX-LOOP" "HEX-TILDE"))
;;;;;;;;;;;; created your local function to update a mleaderstyle
  (defun ResetTheMLeader (mleaderstylename)
    (setq dict	(dictsearch (namedobjdict) "ACAD_MLEADERSTYLE")
	      dict1	(member (cons 3 mleaderstylename) dict)
	      el1	(entget (cdr (assoc 350 dict1)))
	      er	(tblobjname "style" "Standard")
    )
    (entmod (subst (cons 342 er) (assoc 342 el1) el1))
  )
  ;;;step through the list and update
  (foreach mleaderitm mleaderlist
  	 (resetthemleader mleaderitm)
  )
  
)					;end defun RESETMLEADERS

 

Thanks again!

 

Message 4 of 7

SeeMSixty7
Advisor
Advisor

That is correct. Code looks good. Sounds like you have it down.

 

Good luck!

0 Likes
Message 5 of 7

ronjonp
Advisor
Advisor
Accepted solution

FWIW, you could add a couple of checks to make sure that the mleader dictionary and standard style exist as well as feed it a list of names directly. Also be sure to localize your variables ( bold below ). 8-)

(defun resetthemleaders	(mleaderstylenames / dict el1 er)
  (if (and (setq dict (dictsearch (namedobjdict) "ACAD_MLEADERSTYLE"))
	   (setq er (tblobjname "style" "Standard"))
      )
    (foreach mln mleaderstylenames
      (if (setq el1 (dictsearch (cdr (assoc -1 dict)) mln))
	(entmod (subst (cons 342 er) (assoc 342 el1) el1))
      )
    )
  )
)
;; (resetthemleaders '("HEX-ARROW" "HEX-DOT" "HEX-LOOP" "HEX-TILDE"))

 

Message 6 of 7

Anonymous
Not applicable

Thanks, RONJONP!

 

This has worked, too... I think I understand how this works, now that I've gotten my head around FOREACH.

 

Thanks!

0 Likes
Message 7 of 7

ronjonp
Advisor
Advisor

Glad you're learning 🙂 One advantage of passing a list to the function is this:

(and (setq dict (dictsearch (namedobjdict) "ACAD_MLEADERSTYLE"))
	   (setq er (tblobjname "style" "Standard"))
      )

Can be set outside of the loop which should help with performance ( not that we'd notice ) 8-).