.NET

Reply
Contributor
DLyaskov
Posts: 14
Registered: ‎06-24-2010
Message 1 of 2 (229 Views)

Text used for context of MLeader does not annotate

229 Views, 1 Replies
08-23-2012 07:18 PM

Hello,

 

I run into a problem creating an annotative MLeader with MText context . I am using a dialog box to construct the string, set the MLeader Style and layer on which to put my Mleader.  The problem arises when the MLeader Style uses annotative Text Style, the MText doesn’t annotativly scales to the correct size, compared to creating the MLeader with the toolbar button. It seems to work fine if the Text Style is not annotative.  I tried to make the MLeader Style and MText Style current so the newly created objects inherit the correct defaults, but this did not help.

 

Is it better to just change the MLeader Style of the Mleader object and MTex style of the mtext object, instead of making these style current and creating the objects.

 

In addition, I set the annotative property of the MText object to true and still without success.

                if (mldTextStyle.Annotative == AnnotativeStates.True)

                {

                    mtxt.Annotative = AnnotativeStates.True;

                }

                mtxt.TextHeight = mldTextStyle.TextSize;

 

Any ideas will be greatly appreciated.

 

This is the code which sets the current MLeader style to the one chosen by the user, current MText Style to the one used in the MLeader Style, and creating MLeader and MText. 

 

                DBDictionary mldict = trans.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead) as DBDictionary;

                db.MLeaderstyle = mldict.GetAt(settings.MultileaderStyle);

                MLeaderStyle mldStyle = trans.GetObject(db.MLeaderstyle, OpenMode.ForRead) as MLeaderStyle;

                MLeader mld = new MLeader();

                mld.SetDatabaseDefaults();

                mld.Layer = settings.Layer;

                mld.LeaderLineType = LeaderType.StraightLeader;

                mld.AddLeader();

                mld.AddLeaderLine(0);

                mld.AddFirstVertex(0, ptFirst);

                mld.AddLastVertex(0, ptSecond);

                mld.EnableDogleg = true;

                mld.EnableLanding = true;

                mld.DoglegLength = mldStyle.DoglegLength;

                mld.LandingGap = mldStyle.LandingGap;

                if (ptFirst[0] > ptSecond[0])

                {

                    mld.SetDogleg(0, new Vector3d(-1, 0, 0));

                }

 

                db.Textstyle = mld.TextStyleId;

                MText mtxt = new MText();

                mtxt.SetDatabaseDefaults();

                mtxt.Contents = cunstructText();

                TextStyleTableRecord mldTextStyle = trans.GetObject(mld.TextStyleId, OpenMode.ForRead) as TextStyleTableRecord;

                if (mldTextStyle.Annotative == AnnotativeStates.True)

                {

                    mtxt.Annotative = AnnotativeStates.True;

                }

                mtxt.TextHeight = mldTextStyle.TextSize;

 

                mld.ContentType = ContentType.MTextContent;

                mld.MText = mtxt;

 

 

I would also be greatfull for any suggestions how to optimize the above code. 

 

Thank you.

Mentor
khoa.ho
Posts: 219
Registered: ‎09-15-2011
Message 2 of 2 (202 Views)

Re: Text used for context of MLeader does not annotate

08-24-2012 05:31 PM in reply to: DLyaskov

Hi DLyaskov,

It would be better if you can give a running code to debug. Anyway, I found the problem is that you check the annotative of MLeader TextStyle, not MLeader Style. They are different. In AutoCAD, one is from STYLE command (which is Text style), another from MLEADERSTYLE (which is MLeader style). So I fixed your code and rewrote it to make it more structured and understandable.

I added a new struct MLeaderInfo to store all needed settings, including a new boolean IsAnnotative. Then it's easier to control annotative before creating a Multileader.

The following code works for me (in AutoCAD 2009). You may revise a little bit with newer AutoCAD versions:

public struct MLeaderInfo
{
	public string MLeaderStyleName { get; set; }
	public string LayerName { get; set; }
	public string Contents { get; set; }
	public Point3d FirstPoint { get; set; }		// Start point of the leader
	public Point3d SecondPoint { get; set; }	// Next point of the leader
	public bool IsAnnotative { get; set; }
}

[CommandMethod("AddMLeader")]
public void AddMLeader()
{
	var info = new MLeaderInfo
	{
		MLeaderStyleName = "Standard",
		LayerName = "0",
		Contents = "Sample MLeader",
		FirstPoint = new Point3d(1, 1, 0),
		SecondPoint = new Point3d(2, 2, 0),
		IsAnnotative = true
	};

	Document doc = Application.DocumentManager.MdiActiveDocument;
	Database db = doc.Database;
	this.AddMLeader(db, info);
}

public void AddMLeader(Database db, MLeaderInfo info)
{
	using (Transaction trans = db.TransactionManager.StartTransaction())
	{
		try
		{
			var mlDict = (DBDictionary)trans.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead);
			// Set the drawing current leader style (DimStyle) from the input info
			db.MLeaderstyle = mlDict.GetAt(info.MLeaderStyleName);
			var mLeaderStyle = (MLeaderStyle)trans.GetObject(db.MLeaderstyle, OpenMode.ForRead);
			if (info.IsAnnotative)
			{
				mLeaderStyle.UpgradeOpen();
				mLeaderStyle.Annotative = AnnotativeStates.True;
				mLeaderStyle.DowngradeOpen();
			}
			var mLeader = new MLeader
				{
					Layer = info.LayerName,
					LeaderLineType = LeaderType.StraightLeader,
					EnableDogleg = true,
					EnableLanding = true,
					DoglegLength = mLeaderStyle.DoglegLength,
					LandingGap = mLeaderStyle.LandingGap,
					ContentType = ContentType.MTextContent,
				};
			mLeader.SetDatabaseDefaults();
			mLeader.AddLeader();
			mLeader.AddLeaderLine(0);
			mLeader.AddFirstVertex(0, info.FirstPoint);
			mLeader.AddLastVertex(0, info.SecondPoint);
			if (info.FirstPoint[0] > info.SecondPoint[0])
			{
				mLeader.SetDogleg(0, new Vector3d(-1, 0, 0));
			}

			var mText = new MText();
			mText.SetDatabaseDefaults();
			mText.Contents = info.Contents;
			// If the MLeader is annotative, set the attached MText is also annotative
			if (mLeaderStyle.Annotative == AnnotativeStates.True)
			{
				mText.Annotative = AnnotativeStates.True;
			}
			mText.TextHeight = mLeader.TextHeight; // mLeaderTextStyle.TextSize;

			// Attach the MText to MLeader
			mLeader.MText = mText;

			var bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
			var model = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
			model.AppendEntity(mLeader);
			model.DowngradeOpen();

			trans.AddNewlyCreatedDBObject(mLeader, true);
			trans.Commit();
		}
		catch (System.Exception ex)
		{

		}
	}
}

 
-Khoa

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.