Author Topic: Mleader not retaining text height after copy  (Read 2526 times)

0 Members and 1 Guest are viewing this topic.

sdunn

  • Newt
  • Posts: 90
Mleader not retaining text height after copy
« on: November 19, 2013, 07:12:59 PM »
I am trying to create an mleader with mtext.  I can create the leader and all is well until you save and reopen the drawing or if you copy the mleader.  After either of these two events, the text of the mleader reverts to a height of 1.

The text inside the mleader is also not annotative and does not seem to respect the height or annotative settings.  Am I missing something obvious or is this a bug?


Code: [Select]
                          <CommandMethod("TESTLabelCoords")> _
        Public Sub LabelCoordinateWithMtext()

            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database

            Try
                ' get the point
                Dim ptOpts As New PromptPointOptions(vbLf & "Select a point to measure coordinate values: ")
                Dim ptRes As PromptPointResult = ed.GetPoint(ptOpts)

                'if ok then ask for mleader location and add coordinate values
                If PromptStatus.OK = ptRes.Status Then

                    Dim p As Point3d = ptRes.Value
                    Dim txtOpts As New PromptPointOptions(vbLf & "Pick insertion point of label: ")
                    txtOpts.UseBasePoint = True
                    txtOpts.BasePoint = ptRes.Value
                    txtOpts.UseDashedLine = True
                    Dim txtRes As PromptPointResult = ed.GetPoint(txtOpts)
                    Dim txtval As String

                    If ptRes.Value.Z = 0 Then
                        txtval = ("N:" + FormatNumber(Math.Round(ptRes.Value.Y, 4), 4, , , TriState.False).ToString _
                                  + "\P" + "E:" + FormatNumber(Math.Round(ptRes.Value.X, 4), 4, , , TriState.False).ToString)
                    Else
                        txtval = ("N:" + FormatNumber(Math.Round(ptRes.Value.Y, 4), 4, , , TriState.False).ToString _
                                  + "\P" + "E:" + FormatNumber(Math.Round(ptRes.Value.X, 4), 4, , , TriState.False)).ToString _
                          + "\P" + "ELEV:" + (FormatNumber(Math.Round(ptRes.Value.Z, 2), 2, , , TriState.False).ToString)
                    End If

                    If PromptStatus.OK = txtRes.Status Then

                        Dim mtLoc As Point3d = txtRes.Value
                        Dim tr As Transaction = db.TransactionManager.StartTransaction()

                        Using tr





                            ' Create our new MText and set its properties
                            Dim mt As New MText()
                            mt.Location = mtLoc
                            mt.Contents = txtval
                            mt.BackgroundFill = True
                            mt.BackgroundScaleFactor = 1.1
                            mt.Annotative = AnnotativeStates.True
                            mt.SetFromStyle()

                            mt.Height = 5


                            Dim ml As New MLeader()
                            Dim ldNum As Integer = ml.AddLeader()
                            Dim lnnum As Integer = ml.AddLeaderLine(ldNum)

                            ml.AddFirstVertex(lnnum, ptRes.Value)
                            ml.AddLastVertex(lnnum, txtRes.Value)
                            ml.Annotative = AnnotativeStates.True

                            Dim dict As DBDictionary = CType(tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)
                            Dim mlstyleid As ObjectId = dict.GetAt("G-080")

                            If Not mlstyleid = Nothing Then
                                ml.MLeaderStyle = mlstyleid
                                'ml.SetFromStyle()
                            End If


                            ml.MText = mt

                            '' check for preferred justification
                            If ptRes.Value.X > txtRes.Value.X Then
                                ml.TextAlignmentType = TextAlignmentType.RightAlignment
                            Else
                                ml.TextAlignmentType = TextAlignmentType.LeftAlignment
                            End If


                            ' Open the block table, the model space and
                            ' add our MText

                            Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                            Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)

                            Dim mlId = ms.AppendEntity(ml)
                            tr.AddNewlyCreatedDBObject(ml, True)

                            ' Finally we commit our transaction
                            tr.Commit()
                        End Using
                    Else
                        ed.WriteMessage(vbCrLf + "A point was not selected ")
                    End If
                Else
                    ed.WriteMessage(vbCrLf + "A point was not selected ")
                End If

            Catch e As System.Exception
                ed.WriteMessage(vbLf & "Exception {0}.", e)
            End Try

        End Sub

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Mleader not retaining text height after copy
« Reply #1 on: November 19, 2013, 08:09:03 PM »

I haven't looked at your code, ..
Does the style of the MText selected have a fixed height or 0 ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Mleader not retaining text height after copy
« Reply #2 on: November 19, 2013, 09:15:51 PM »
It seems like I ran into something similar and you have to add the mleader to the database(pass it into AppendEntity) before you set the Mtext height for it.


Have not tested to confirm but I think before the mleader  is added to the database the MleaderStlye's TextHeight property sets the Mleader TextHeight property and will take precedence.

For dealing with annotative objects I have had better results with using SetFromStyle Method
Quote from: docs
Sets the annotative property of an object from its (optionally) associated style. Returns true on successful return if the annotative property of the object changed.

As Kerry mentioned one of the issue is its fixed height, here would be a partial example of mimicking Mtext command for setting height.
Code - C#: [Select]
  1.                     if (!mtxt.SetFromStyle())
  2.                     {
  3.                         if (txtStyleTblRecord.TextSize > 0.0)
  4.                         {
  5.                             mtxt.TextHeight = txtStyleTblRecord.TextSize;
  6.                         }
  7.                         else
  8.                         {
  9.                             mtxt.TextHeight = Db.Textsize;
  10.                         }
  11.                     }
  12.  


Can't remember off the top of my head if you want a annotative Mleader with annotative textstyle.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Mleader not retaining text height after copy
« Reply #3 on: November 20, 2013, 08:19:07 AM »
You dont need to set any properties on the MText other than the Contents .  Annotative comes from the MLeader not the TextStyle
Code - C#: [Select]
  1. var mtext = new MText();
  2. mtext.SetDatabaseDefaults(db);
  3. mtext.Contents = "Contents";
  4.  

Then use the Mleader or MLeaderStyle property TextHeight to set the height.  If your MLeader is Annotative then this will be the Annotative height.  Don't forget to set your MLeader or MLeaderStyle ContentType to MTextContent.
Code - C#: [Select]
  1. var mleader = new MLeader
  2.   {
  3.        ContentType = ContentType.MTextContent,
  4.        MText = mtext,
  5.        TextHeight = 0.09375
  6.    };
Revit 2019, AMEP 2019 64bit Win 10

sdunn

  • Newt
  • Posts: 90
Re: Mleader not retaining text height after copy
« Reply #4 on: November 20, 2013, 01:40:01 PM »
That worked well.  Thank you for your help.