Code Red => .NET => Topic started by: Atook on August 06, 2018, 01:18:13 PM

Title: Mleader Mtext Attachment/Alignment?
Post by: Atook on August 06, 2018, 01:18:13 PM
Running into troubles with programatically created MLeaders.

If the angle is right to left, the leader will cross over the text, which I'm trying to avoid.

If the user grip edits the leader, it corrects it's self automatically. I can't use MgdDbg snoop to look for changes between the two, because it crashes  on MLeaders for some reason  :cry:

Leader creation code:
Code - C#: [Select]
  1. // create the MLeader
  2. MLeader label = new MLeader();
  3. label.SetDatabaseDefaults();
  4. label.ContentType = ContentType.MTextContent;
  5. label.Layer = layerName;
  6. int ldNum = label.AddLeader();
  7. int idx = label.AddLeaderLine(ldNum);
  8. label.AddFirstVertex(idx,leaderPoint);
  9. #if ACAD_APP
  10. label.AddLastVertex(idx, textPoint);//<-needed for AutoCAD, crashes BricsCAD
  11. #endif
  12. label.SetLastVertex(idx, textPoint);
  14. // new Mtext for the text
  15. MText mText = new MText();
  16. mText.SetDatabaseDefaults();
  17. mText.Contents = labelString;
  18. mText.Rotation = rotation;
  19. mText.TextHeight = textHeight;
  20. mText.TextStyleId = Styles.GetTextStyleID(AID_Strings.PipeSizeTextStyle);
  21. mText.SetAttachmentMovingLocation(AttachmentPoint.MiddleCenter);
  23. label.MText = mText;
  25. // add the mleader to currspace
  26. BlockTableRecord curSpace =
  27.     (BlockTableRecord)tr.GetObject(Active.Database.CurrentSpaceId, OpenMode.ForWrite);
  28. curSpace.AppendEntity(label);
  29. tr.AddNewlyCreatedDBObject(label, true);

I've looked in the MLeader events for a refresh method or something to call, but can't find anything. 

In the attached sample drawing, both leaders were created with code, the lower leader was just grip edited.

Any ideas?
Title: Re: Mleader Mtext Attachment/Alignment?
Post by: Master_Shake on August 06, 2018, 02:23:13 PM
Does the mleader correct it self only if you select the grip or also if a regen is performed?
Title: Re: Mleader Mtext Attachment/Alignment?
Post by: Atook on August 06, 2018, 02:24:44 PM
Only for the grip edit. Regen doesn't help, though I tried it. :)

It's almost like it needs to recalculate or something.
Title: Re: Mleader Mtext Attachment/Alignment?
Post by: Master_Shake on August 06, 2018, 02:54:57 PM
Does changing AttachmentPoint exhibit the same "flipping" action?

Code - C#: [Select]
  1. mText.SetAttachmentMovingLocation(AttachmentPoint.MiddleCenter);

Code - C#: [Select]
  1. mText.SetAttachmentMovingLocation(AttachmentPoint.MiddleRight);
Title: Re: Mleader Mtext Attachment/Alignment?
Post by: Atook on August 06, 2018, 03:54:21 PM
mText.SetAttachmentMovingLocation(AttachmentPoint.MiddleCenter); does exhibit the same flipping behavior, it just flips the text to the other side of the grip.

I actually commented that line out and have the same results. I think it's a remnant of my previous attempts to sort this issue. I'll probably remove it.

Funny enough, I just double checked how these look when created via BricsCAD and they look fine.

I don't know if it's something funky with AutoCAD's MLeader implementation or if I'm messing up somehow.

EDIT: Solved. (sorta)

Looking at this post from Kean (, I set the dogleg vector based on the X values of the points involved. Funny enough, BricsCAD did not like that, so I stuck it in the #IF statement. The code now looks like this:
Code - C#: [Select]
  1.     // new Mtext for the text
  2.     MText mText = new MText();
  3.     mText.SetDatabaseDefaults();
  4.     mText.Contents = labelString;
  5.     mText.Rotation = rotation;
  6.     mText.TextHeight = textHeight;
  7.     mText.TextStyleId = Styles.GetTextStyleID(AID_Strings.PipeSizeTextStyle);
  9.     // create the MLeader
  10.     MLeader label = new MLeader();
  11.     label.SetDatabaseDefaults();
  12.     label.ContentType = ContentType.MTextContent;
  13.     label.Layer = layerName;
  14.     int ldNum = label.AddLeader();
  15.     int idx = label.AddLeaderLine(ldNum);
  16.     label.AddFirstVertex(idx,leaderPoint);
  18. #if ACAD_APP // weird kludge fixes for Autodesk
  19.     label.AddLastVertex(idx, textPoint);
  20.     Vector3d dl = new Vector3d((textPoint.X >= leaderPoint.X ? 1 : -1), 0, 0);
  21.     label.SetDogleg(idx, dl);
  22. #endif
  24.     label.SetLastVertex(idx, textPoint);
  25.     label.MText = mText;
  27.     // add the mleader to currspace
  28.     BlockTableRecord curSpace =
  29.         (BlockTableRecord)tr.GetObject(Active.Database.CurrentSpaceId, OpenMode.ForWrite);
  30.     curSpace.AppendEntity(label);
  31.     tr.AddNewlyCreatedDBObject(label, true);
Title: Re: Mleader Mtext Attachment/Alignment?
Post by: sonny3g on July 01, 2019, 09:43:38 AM
Having a similar problem and so far, it appears that if I add the line below after the "tr.AddNewlyCreatedDBObject(label, true);" line then my leader always comes horizontal.  Is it possible that might also work for your problem by moving the AttchmentPoint.MiddleCenter after adding the new object?

"label.TextAttachmentDirection = TextAttachmentDirection.AttachmentHorizontal;"