Leader and Mtext To Multileader

Leader and Mtext To Multileader

davidNNBWC
Explorer Explorer
5,400 Views
4 Replies
Message 1 of 5

Leader and Mtext To Multileader

davidNNBWC
Explorer
Explorer

Hey I'm trying to write/find a Lisp code to convert a leader and Mtext to a multileader. i work in Civil 3d 2023 and have a ran into multiple files that i get from surveyors or architects where they have a regular leader and an Mtext right next to each other like they exploded them. Now i know i can go through the Multileader command and select Mtext however i feel like that takes too long and everyone i do i need to place the leader. does anyone have a solution?

 

I also tried to have a AI write some code the best i could have it come up with is the folowing but it always gives me errors.

(defun c:ConvertToMLeader (/ leader mtext leaderData textString textPos newMLeader leaderPoints mleaderObj acadDoc mtextObj)
  ;; Get AutoCAD Document Object
  (setq acadDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (princ "\nAutoCAD Document Object retrieved.")

  ;; Select a Leader
  (setq leader (car (entsel "\nSelect a Leader: ")))
  (if (not leader)
    (progn
      (princ "\nInvalid selection. Please select a Leader.")
      (exit)
    )
  )
  (princ "\nLeader selected.")

  ;; Select MText
  (setq mtext (car (entsel "\nSelect MText: ")))
  (if (or (not mtext) (not (eq (cdr (assoc 0 (entget mtext))) "MTEXT")))
    (progn
      (princ "\nInvalid selection. Please select a valid MText object.")
      (exit)
    )
  )
  (princ "\nMText selected.")

  ;; Extract leader vertex points
  (setq leaderData (entget leader))
  (setq leaderPoints nil)

  (foreach d leaderData
    (if (= (car d) 10) ; Extract leader points
      (setq leaderPoints (append leaderPoints (list (cdr d))))
    )
  )
  (princ "\nLeader vertex points extracted.")

  ;; Extract MText position and text content
  (setq textPos (cdr (assoc 10 (entget mtext))))
  (setq textString (cdr (assoc 1 (entget mtext))))
  (princ (strcat "\nMText position: " (vl-princ-to-string textPos)))
  (princ (strcat "\nMText content: " textString))

  ;; Ensure data is valid
  (if (and leaderPoints textPos textString)
    (progn
      ;; Create new MLeader Object
      (setq mleaderObj (vla-AddMLeader acadDoc (vlax-3d-point (car leaderPoints)) acLineWithArrow))
      (princ "\nMLeader object created.")

      ;; Add leader points
      (foreach pt (cdr leaderPoints)
        (vla-AddLeaderLine mleaderObj (vlax-3d-point pt))
      )
      (princ "\nLeader points added to MLeader.")

      ;; Set MText content
      (setq mtextObj (vla-AddMText acadDoc (vlax-3d-point textPos) textString))
      (if mtextObj
        (progn
          (vla-put-TextString mtextObj textString)
          (vla-put-TextHeight mtextObj 0.2) ; Adjust text height if necessary
          (vla-put-TextAttachmentType mleaderObj acAttachmentBottomOfTopLine)
          (vla-put-TextLocation mleaderObj (vlax-3d-point textPos))
          (princ "\nMText content set in MLeader.")
        )
        (princ "\nError: Failed to add MText to MLeader.")
      )

      ;; Delete old objects
      (entdel leader)
      (entdel mtext)
      (princ "\nOld objects deleted.")

      (princ "\nMLeader created successfully!")
    )
    (princ "\nError: Could not retrieve necessary data.")
  )
  (princ)
)

(princ "\nCommand 'ConvertToMLeader' loaded. Type ConvertToMLeader to use.")
(princ)
0 Likes
Accepted solutions (1)
5,401 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

You need to post a sample file. C3D often do things differently.

 

Also can try this @ronjonp 's routine.

It's from '09, maybe it has developed some thru the yeas.

0 Likes
Message 3 of 5

pendean
Community Legend
Community Legend
0 Likes
Message 4 of 5

apjones
Collaborator
Collaborator

Have you tried the select Mtext option in the MLEADER command?  

Pete

>Please Accept as Solution and give Kudos as appropriate to further enhance these forums. Thank you!
0 Likes
Message 5 of 5

komondormrex
Mentor
Mentor

@davidNNBWC wrote:

... where they have a regular leader and an Mtext right next to each other like they exploded them. 


you have at least to show visually what you have before and want  to have after. 

0 Likes