Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Set MLeader to Existing MLeader Style via LISP

15 REPLIES 15
Reply
Message 1 of 16
parker.depriest
2830 Views, 15 Replies

Set MLeader to Existing MLeader Style via LISP

Hello,

 

I am trying to create a lisp routine that sets all existing MLEADERs to a certain pre-set MLEADERSTYLE, the equivalent of doing a Quick Select for Mleaders, and setting the style under the properties window.

 

I was able to write a similar routine that selects all dimensions and sets them to a certain DIMSTYLE, using entmod and DXF code 3 for dimstyle.  So far, I have not been able to find a group code for MLEADERSTYLE 

 

 Any help would be greatly appreciated!

 

 

 

(defun C:dimstylechange (/ ENTITIES NO_OF_ENTITIES SSPOSITION ENTITY_NAME
OLD_ENTLIST NEW_STYLE NEW_ENTLIST)

(setvar "CMDECHO" 0)

(setq ENTITIES (ssget "X" '((0 . "DIMENSION"))))

(setq NO_OF_ENTITIES (sslength ENTITIES))

(setq SSPOSITION 0)

(repeat NO_OF_ENTITIES

;***CHANGE STYLE***
(setq ENTITY_NAME (ssname ENTITIES SSPOSITION))
(setq OLD_ENTLIST (entget ENTITY_NAME))
(setq OLD_STYLE (assoc 3 OLD_ENTLIST))
(setq NEW_STYLE (cons 3 "BCR 11x17"))
(setq NEW_ENTLIST (subst NEW_STYLE OLD_STYLE OLD_ENTLIST))

(entmod NEW_ENTLIST)


;***CHANGE LAYER***
(setq OLD_ENTLIST (entget ENTITY_NAME))
(setq OLD_STYLE (assoc 8 OLD_ENTLIST))
(setq NEW_STYLE (cons 8 "DIM"))
(setq NEW_ENTLIST (subst NEW_STYLE OLD_STYLE OLD_ENTLIST))

(entmod NEW_ENTLIST)

(setq SSPOSITION (1+ SSPOSITION))
)

 

(command ".CHPROP" ENTITIES "" "C" "BYLAYER" "LT" "BYLAYER" "")

(princ (strcat "\n..." (rtos NO_OF_ENTITIES 2 0) " Dimension(s) changed..."))

(setvar "CMDECHO" 1)

(princ)

)

15 REPLIES 15
Message 2 of 16


@parker.depriest wrote:

....  So far, I have not been able to find a group code for MLEADERSTYLE 

....


I don't have MLeaders [too old a version of AutoCAD], but I wonder whether that might be available with a little less work as a VLA Property.  Try drawing an MLeader [so it's the last thing in the drawing], then doing:

 

(vlax-dump-object (vlax-ename->vla-object (entlast)))

 

and see whether any of the listed Properties contains the current style.  [For Text and Mtext and Dimensions, the Property is called "StyleName", and it could very well be the same for MLeaders.]  If it does, you can use a (vla-put...) operation on a vla-object conversion of each MLeader found, instead of the whole (entget)/(subst)/(entmod) procedure.

Kent Cooper, AIA
Message 3 of 16
phanaem
in reply to: parker.depriest

How often do you need to do that? Maybe a Quick Select and change style in properties dialog is enough.

But if you have some reasons to use lisp, try this:

(defun C:TEST ( / *error* acDoc l new e ss)
  (setq acDoc (vla-get-activeDocument (vlax-get-acad-object)))
  (vla-StartUndoMark acDoc)
  
  (defun *error* (msg)
    (princ msg)
    (vla-EndUndoMark acDoc)
    )
  
  (if
    (ssget "_X" '((0 . "MULTILEADER")))
     (progn
       (princ "\n\n\n****Mleader Styles****\n\n")
       (setq l (mapcar
                 (function
                   (lambda (s)
                     (princ (strcat "   " (cdr s) "\n"))
                     (cdr s)
                   )
                 )
                 (vl-remove-if
                   '(lambda (x) (/= (car x) 3))
                   (dictsearch (namedobjdict) "ACAD_MLEADERSTYLE")
                 )
               )
       )
       (textscr)
       (if
         (member (setq new (getstring T "\nEnter new style name: ")) l)
          (progn
            (vlax-for e (setq ss (vla-get-ActiveSelectionSet acDoc))
              (vla-put-StyleName e new)
            )
            (vla-delete ss)
          )
          (princ "\nStayle name not found.")
       )
       (graphscr)
     )
  )
  (princ)
)
Message 4 of 16
francinez
in reply to: phanaem

Hello, about your lisp is it possible to apply a Mleader Styles in accordance with the dimstyle?

Exemple:

-dimstyle 01_Dim1-1 have the mleader Styles 01_Tender1-1

-dimstyle 01_Dim1-2 have the mleader Styles 01_Tender1-2

Message 5 of 16
phanaem
in reply to: francinez

I'm not sure I understood your request. Can you clarify your goal a little bit more?
Message 6 of 16
francinez
in reply to: phanaem

When a purge my drawing I lose the mleader style of my annotation and only when I restore them I can add or remove leaders!

For exemple find attached a drawing with 3 different Mleaderstyle.

Thanks for your help

Message 7 of 16
phanaem
in reply to: francinez

It is the first time I see a mleader without a style associated. How do you get this kind of mleader? Maybe imported from other software? Anyway, based only on presence of a dimstyle, it is hard to create a association between a mleader and a style name. I guess you want to create a mleader style for each of the orphan (that is, without style) mleader object, but I don't see any clear way to associate a particular object to a particular style name.
Message 8 of 16
francinez
in reply to: phanaem

Hi, I lose the mleader style by purge, to have again the mleader style I reinsert the template on which I have all mleader style and all dimstyle

Message 9 of 16

no error trapping but try this - not tested

 

(defun c:CMS()
(vla-com-load)

(setq
ss (ssget "_X" '((0 . "MULTILEADER")))
i 0
)
(repeat (sslength ss)
(setq obj (vlax-ename->vla-object (ssname ss i)))
(vla-put-StyleName obj "Your Style")
(setq i (1+ i))
)

(princ)
)

you can also change "Your Style"  to the following code 

(vl-string-subst "Tender" "Dim" (vla-get-stylename obj))

This will change the dimention style from *Dim* to *tender*

 

Ahmed Abd-Elmotey
Landscape Architect & beginner lisp programmer
Message 10 of 16

Good morning,

I tested your lisp but a get an error message: error: no function definition: VLA-COM-LOAD

I modifed (vla-com-load) to (vl-load-com) but i get an oder error message: ; error: Automation Error. Null object ID

Even if I will not have a positive result I thank you for your help

Message 11 of 16

sorry for the first strange function (vla-com-load) i usually don't use it.

I didn't use error trapping .. this error means that you don't have a style that matches the pattern you said about
eg.
for mlstyle called 01_Dim1-1 >> 01_Tender1-1 is not available in the drawing

Ahmed Abd-Elmotey
Landscape Architect & beginner lisp programmer
Message 12 of 16

All needed style are on the drawing

What I need exactly is to assign to the mleader with layer Dim1-1 the mleader style 01_Tender1-1

 

Mleader with layer Dim1-1 = mleaderstyle 01_Tender1-1

Mleader with layer Dim1-2 = mleaderstyle 02_Tender1-2

Mleader with layer Dim1-3 = mleaderstyle 03_Tender1-3

Message 13 of 16
hmsilva
in reply to: francinez

HI francinez,

 

Untested...

 

(vl-load-com)
(defun c:demo (/ i lay mlstyles obj ss)
    (if (setq ss (ssget "_X" '((0 . "MULTILEADER"))))
        (progn
            (or adoc (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
            (vlax-for ml (vla-item (vla-get-dictionaries adoc) "ACAD_MLEADERSTYLE")
                (setq mlstyles (cons (vla-get-name ml) mlstyles))
            )
            (repeat (setq i (sslength ss))
                (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                      lay (vla-get-layer obj)
                )
                (cond ((and (vl-position "01__Tender 1-1" mlstyles)
                            (= lay "Dim 1-1")
                            (vlax-write-enabled-p obj)
                       )
                       (vla-put-stylename obj "01__Tender 1-1")
                      )
                      ((and (vl-position "01__Tender 1-2" mlstyles)
                            (= lay "Dim 1-2")
                            (vlax-write-enabled-p obj)
                       )
                       (vla-put-stylename obj "01__Tender 1-2")
                      )
                      ((and (vl-position "01__Tender 1-3" mlstyles)
                            (= lay "Dim 1-3")
                            (vlax-write-enabled-p obj)
                       )
                       (vla-put-stylename obj "01__Tender 1-3")
                      )
                )
            )
        )
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 14 of 16
francinez
in reply to: hmsilva

Perfect I have testet it and it works, only little change with the exact name of mleader style!

Spoiler
 

Smiley Happy 

Message 15 of 16
hmsilva
in reply to: francinez

Glad I could help! 🙂

Henrique

EESignature

Message 16 of 16

am sorry i was searching for a mleader style called Dim 1-3 ! glad that hmsilva solution worked for you.
Ahmed Abd-Elmotey
Landscape Architect & beginner lisp programmer

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost