Multi-line Leader Text Only Left Justified

Multi-line Leader Text Only Left Justified

mgorecki
Collaborator Collaborator
843 Views
2 Replies
Message 1 of 3

Multi-line Leader Text Only Left Justified

mgorecki
Collaborator
Collaborator

Hello,

The code I'm using to insert my mleaders will add the mleader either pointing to the left or right.  The problem is, when its pointing to the right, the text is still left justified within the bounding box.

Is there a way to set the mtext justification?

    Public Sub AddMLeader2(ByVal ldrPnt1 As Point3d, ByVal ldrPnt2 As Point3d, ByVal ldrTextVal As String, _
                          ByVal ldrTextWidth As Double, ByVal dwgTextHeight As Double)
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database

        Using mlTran As Transaction = db.TransactionManager.StartTransaction()
            Dim blkTbl As BlockTable = TryCast(mlTran.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
            Dim blkTblRec As BlockTableRecord = TryCast(mlTran.GetObject(blkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
            Dim mldr As MLeader = New MLeader()
            Dim mText As MText = New MText()
            mldr.SetDatabaseDefaults()

            ' Creates a new leader cluster
            Dim leaderNumber As Integer = mldr.AddLeader()

            ' Add a leader line to the cluster
            Dim leaderLineNum As Integer = mldr.AddLeaderLine(leaderNumber)
            mldr.AddFirstVertex(leaderLineNum, ldrPnt1)
            mldr.AddLastVertex(leaderLineNum, ldrPnt2)

            Dim direction As Double = 1.0
            If (ldrPnt2 - ldrPnt1).DotProduct(Vector3d.XAxis) < 0.0 Then direction = -1.0
            mldr.SetDogleg(leaderNumber, Vector3d.XAxis.MultiplyBy(direction))
            mText.Width = ldrTextWidth
            mText.Height = dwgTextHeight
            mText.Contents = ldrTextVal
            mldr.MText = mText

            blkTblRec.UpgradeOpen()
            blkTblRec.AppendEntity(mldr)

            mlTran.AddNewlyCreatedDBObject(mldr, True)
            mlTran.Commit()
        End Using
    End Sub

Thanks,

Mark

0 Likes
Accepted solutions (1)
844 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can use the MLeader.MoveMLeader() method with MoveType.MoveContentAndDoglegPoints and a vector which length equals: (MLeader.GoglegLength + MLeader.LandingGap) * 2 + MText.ActualWidth.

 

It seems to me you do not need to use the DotProduct, you can simply compare the start point and end end point X coordinates.

 

        static void AddMleader(Point3d startPoint, Point3d endPoint, string textContents, double textWidth, double textHeight)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var mleader = new MLeader();
                mleader.SetDatabaseDefaults();
                mleader.ContentType = ContentType.MTextContent;
                int index = mleader.AddLeader();
                mleader.AddLeaderLine(index);
                mleader.AddFirstVertex(index, startPoint);
                mleader.AddLastVertex(index, endPoint);

                MText mtext = new MText();
                mtext.SetDatabaseDefaults();
                mtext.Width = textWidth;
                mtext.Height = textHeight;
                mtext.Contents = textContents;

                mleader.MText = mtext;
                mtext.Location = mleader.TextLocation;
                if (endPoint.X < startPoint.X)
                {
                    var offset = (mleader.DoglegLength + mleader.LandingGap) * 2 + mtext.ActualWidth;
                    mleader.MoveMLeader(Vector3d.XAxis * -offset, MoveType.MoveContentAndDoglegPoints);
                }

                var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                cSpace.AppendEntity(mleader);
                tr.AddNewlyCreatedDBObject(mleader, true);
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

mgorecki
Collaborator
Collaborator

Hello Gile,

Thank you very much for that, it did the trick.  I didn't know about the mleader.MoveMLeader command.

 

Thanks again,

Mark

0 Likes