Author Topic: Mleader Mtext Attachment/Alignment?  (Read 789 times)

0 Members and 1 Guest are viewing this topic.

Atook

  • Swamp Rat
  • Posts: 942
Mleader Mtext Attachment/Alignment?
« 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);
  13.  
  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);
  22.  
  23. label.MText = mText;
  24.  
  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);
  30.  

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?

Master_Shake

  • Swamp Rat
  • Posts: 1447
Re: Mleader Mtext Attachment/Alignment?
« Reply #1 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?

Atook

  • Swamp Rat
  • Posts: 942
Re: Mleader Mtext Attachment/Alignment?
« Reply #2 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.

Master_Shake

  • Swamp Rat
  • Posts: 1447
Re: Mleader Mtext Attachment/Alignment?
« Reply #3 on: August 06, 2018, 02:54:57 PM »
Does changing AttachmentPoint exhibit the same "flipping" action?

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

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

Atook

  • Swamp Rat
  • Posts: 942
Re: Mleader Mtext Attachment/Alignment?
« Reply #4 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);
  8.  
  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);
  17.  
  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
  23.  
  24.     label.SetLastVertex(idx, textPoint);
  25.     label.MText = mText;
  26.  
  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);
  32.  
« Last Edit: August 06, 2018, 05:33:23 PM by Atook »

sonny3g

  • Newt
  • Posts: 27
Re: Mleader Mtext Attachment/Alignment?
« Reply #5 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;"