Im sure I have a simple error, but Im spinning my wheels at the end of a long week.
Dim myMleader As MLeader = CType(mySelObj.ObjectId.GetObject(OpenMode.ForRead), Autodesk.AutoCAD.DatabaseServices.MLeader) Dim myNewValue As String = "New Value" myMleader.UpgradeOpen() myMleader.MText.Contents = myNewValue
My code above doesnt seem to update the contents of the mtext. I have tried a couple of iterations, but cant seem to get it to update.
Thanks,
matt
Solved! Go to Solution.
Solved by Ajilal.Vijayan. Go to Solution.
Try by creating a New Mtext and change the Mtext property of Mleader
Dim mt As MText = New MText
mt.SetDatabaseDefaults()
mt.Contents = "new Value"
myMleader.UpgradeOpen()
myMleader.MText = mt
That worked, Thank you very much.
I wasnt thinking of the MTEXT as being a property, and therefore trying to modify the current one. Which makes sense, since I couldnt get an objectid on the original mtext.
Thanks again,
matt
Another question, is there a trick to getting the current settings from the old mtext (text style, height, color...). Could I clone instead of creating a new?
Yes, I think you could clone the old mtext to get the properties.
Also there is entType1.GetProperties(); if you want to list the properties of any object.
You can also set to the mleader the same mtext:
MLeader mld = ent as MLeader; //Remember open for write! MText mtxt = mld.MText; //some operations on mtext //and finally update the mleader: mld.MText = mtxt;
Tested on autocad 2018 and 2014.
Here's another way to clone the properties of anything but you will need JSON.NET .. or you could use a different serializer
string serialized = JsonConvert.SerializeObject(mText); MText newMtext = JsonConvert.DeserializeObject<MText>(serialized);
from: https://stackoverflow.com/a/15788750/492
So I can't edit my answer. This forum software, eh?
So, don't do this - JSON.NET blew up ...
Newtonsoft.Json.JsonSerializationException: Error getting value from 'ColumnFlowReversed' on 'Autodesk.AutoCAD.DatabaseServices.MText'. ---> Autodesk.AutoCAD.Runtime.Exception: eNotApplicable at bool value Autodesk.AutoCAD.DatabaseServices.MText.get_ColumnFlowReversed() at object GetColumnFlowReversed(object) at object Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(object target)
so do this ...
var serializerSettings = new JsonSerializerSettings(); serializerSettings.Error = (serializer, err) => err.ErrorContext.Handled = true; string serialized = JsonConvert.SerializeObject(mLeaderMtext, serializerSettings); MText newMtext = JsonConvert.DeserializeObject<MText>(serialized, serializerSettings);
from https://stackoverflow.com/a/21901646/492
did I mention how crap this forum is?
ok, try
var serializerSettings = new JsonSerializerSettings(); serializerSettings.Error = (serializer, err) => err.ErrorContext.Handled = true; MText newMtext = new MText(); newMtext.SetDatabaseDefaults(); string serialized = JsonConvert.SerializeObject(mText, serializerSettings); JsonConvert.PopulateObject(serialized, newMtext, serializerSettings);
before I run out of time, dropping this here ...
MText newMtext = oldMtext.Clone() as MText;