Jozi68 Posted July 16, 2009 Posted July 16, 2009 I would really like to select all mtext objects in my drawing, and then iterate through them, checking the contents (the textstring) of each. I also then need to change the alignment, etc of each. I believe this should be an easy exercise, but just cannot get it to work. Any code examples will be much appreciated. Jozi68 Quote
Jozi68 Posted July 23, 2009 Author Posted July 23, 2009 (edited) Hi Jozi68, I see you are really struggling all alone in Africa there. I am a beginner myself, but here is some code that might work for you: [size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] values() [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] TypedValue = { _[/size] [size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] TypedValue(DxfCode.Start, [/size][size=2][color=#a31515][size=2][color=#a31515]"TEXT"[/color][/size][/color][/size][size=2]), _[/size] [size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] TypedValue(DxfCode.Color, 1) _[/size] [size=2]}[/size] [size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] sfilter [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] SelectionFilter(values)[/size] [size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] res [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] PromptSelectionResult = ed.SelectAll(sfilter)[/size] [size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] SS [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value[/size] [size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] idarray [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] ObjectId() = SS.GetObjectIds()[/size] [size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] tm [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager[/size] [size=2][color=#008000][size=2][color=#008000]'start a transaction[/color][/size][/color][/size] [size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myT [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Transaction = tm.StartTransaction()[/size] [size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size] [size=2][color=#0000ff][size=2][color=#0000ff] Dim[/color][/size][/color][/size][size=2] id [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] ObjectId[/size] [size=2][color=#0000ff][size=2][color=#0000ff] For[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Each[/color][/size][/color][/size][size=2] id [/size][size=2][color=#0000ff][size=2][color=#0000ff]In[/color][/size][/color][/size][size=2] idarray[/size] [size=2][color=#0000ff][size=2][color=#0000ff] Dim[/color][/size][/color][/size][size=2] tt [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] DBText = [/size][size=2][color=#0000ff][size=2][color=#0000ff]CType[/color][/size][/color][/size][size=2](tm.GetObject(id, OpenMode.ForWrite), DBText)[/size] [size=2][color=#0000ff][size=2][color=#0000ff] If[/color][/size][/color][/size][size=2] Left(tt.TextString, 1) = [/size][size=2][color=#a31515][size=2][color=#a31515]"Y"[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Then[/color][/size][/color][/size] [size=2][color=#0000ff][size=2][color=#0000ff] If[/color][/size][/color][/size][size=2] tt.Rotation = 1 [/size][size=2][color=#0000ff][size=2][color=#0000ff]Then[/color][/size][/color][/size] [size=2] tt.Rotation = 2[/size] [size=2][color=#0000ff][size=2][color=#0000ff] End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]If[/color][/size][/color][/size] [size=2][color=#0000ff][size=2][color=#0000ff] End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]If[/color][/size][/color][/size] [size=2][color=#0000ff][size=2][color=#0000ff] Next[/color][/size][/color][/size][size=2] id[/size] [size=2] myT.Commit()[/size] [size=2][color=#0000ff][size=2][color=#0000ff]Finally[/color][/size][/color][/size] [size=2] myT.Dispose()[/size] [size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Try[/color][/size][/color][/size] Edited February 14, 2012 by SLW210 Quote
Jozi68 Posted July 23, 2009 Author Posted July 23, 2009 Thank you so much! This is a PERFECT solution. This problem has driven me to tears. It is such a relief to find a solution. Quote
Jozi68 Posted July 23, 2009 Author Posted July 23, 2009 No Problem, I hope you find answers for all your other questions. Quote
Maf Posted February 14, 2012 Posted February 14, 2012 (edited) Hi to all, i try to insert the code in my vb.net app, but i became an errore at this line: [font=Consolas][size=2][color=#0000ff]Dim tt As[/color][color=#2b91af][color=#2b91af][color=#2b91af]DBText[/color][/color][/color][/size][size=2][size=2] = [/size][/size][size=2][color=#0000ff][size=2][color=#0000ff][size=2][color=#0000ff]CType[/color][/size][/color][/size][/color][/size][size=2][size=2](id.GetObject([/size][/size][size=2][color=#2b91af][size=2][color=#2b91af][size=2][color=#2b91af]OpenMode[/color][/size][/color][/size][/color][/size][size=2][size=2].ForRead), [/size][/size][size=2][color=#2b91af][size=2][color=#2b91af][size=2][color=#2b91af]DBText[/color][/size][/color][/size][/color][/size][size=2][size=2])[/size][/size][/font] Error is: InvalidCastException wurde nicht Benutzercode behandelt Das Objekt des Typs "Autodesk.AutoCAD.DatabaseServices.BlockReference" kann nicht in Typ "Autodesk.AutoCAD.DatabaseServices.DBText" umgewandelt werden. Me code: .... [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] db [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Database[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] = DocumentManager.MdiActiveDocument.Database[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] Ed [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Editor[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] = DocumentManager.MdiActiveDocument.Editor[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] values()...[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] res [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]PromptSelectionResult[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] = Ed.GetSelection[/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] SS [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] Autodesk.AutoCAD.EditorInput.[/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]SelectionSet[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] = res.Value[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] idarray [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]ObjectId[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]() = SS.GetObjectIds()[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] tm [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] Autodesk.AutoCAD.DatabaseServices.[/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]TransactionManager[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] = db.TransactionManager[/size][/font][/size][/font] [font=Consolas][size=2][color=#008000][font=Consolas][size=2][color=#008000][font=Consolas][size=2][color=#008000]'start a transaction[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] myT [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]Transaction[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] = tm.StartTransaction()[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Try[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] id [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]ObjectId[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]For[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Each[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] id [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]In[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] idarray[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] tt [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]DBText[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] = [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]CType[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2](id.GetObject([/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]OpenMode[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2].ForRead), [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]DBText[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2])[/size][/font][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]If[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] Left(tt.TextString, 1) = [/size][/font][/size][/font][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515]"Test"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Then[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]If[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] tt.Rotation = 1 [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Then[/color][/size][/font][/color][/size][/font][/color][/size][/font] .... You have a Idea?? Tanks a lot Edited February 14, 2012 by SLW210 Code Tags Added Quote
SLW210 Posted February 14, 2012 Posted February 14, 2012 Please follow the CODE POSTING GUIDELINES. Quote
SLW210 Posted February 14, 2012 Posted February 14, 2012 I have moved this to the .NET, ObjectARX & VBA forum. Quote
fixo Posted February 14, 2012 Posted February 14, 2012 (edited) At the quick glance you have to cast objects as exact entity type here: [size=1]Dim id As ObjectId[/size] [size=1]For Each id In idarray[/size] [size=1]Dim ent as Entity=CType(id.GetObject(OpenMode.ForRead), Entity)[/size] [size=1][b]If Typeof ent is DBText then[/b][/size] [size=1][b]Dim tt As DBText = DirectCast(ent, DBText)[/b][/size] [size=1]If Left(tt.TextString, 1) = "Test" Then '<-- wrong statement I think, use StartWith or EndWith instead[/size] [size=1]tt.UpgradeOpen()[/size] [size=1]If tt.Rotation = 1 Then[/size] [size=1]'---- change text here ----[/size] [size=1]tt.DowngradeOpen()[/size] [size=1]End If[/size] ~'J'~ Edited February 14, 2012 by fixo spell check Quote
fixo Posted February 14, 2012 Posted February 14, 2012 In addition you may want to use selection filter like in this quick example <CommandMethod("slt")> _ Public Sub TextSelectionTest() Dim db As Database = HostApplicationServices.WorkingDatabase Dim Ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor ' simple text filter 'Dim filt As New SelectionFilter(New TypedValue() {New TypedValue(0, "TEXT")}) 'variations ' Dim filt As New SelectionFilter(New TypedValue() {New TypedValue(0, "TEXT"), New TypedValue(1, "Test*")}) '<-- start with string "Test ..." 'Dim filt As New SelectionFilter(New TypedValue() {New TypedValue(0, "TEXT"), New TypedValue(1, "*Test")}) '<-- ends with string "... Test" 'Dim filt As New SelectionFilter(New TypedValue() {New TypedValue(0, "TEXT"), New TypedValue(1, "*Test*")}) '<-- include substring "Test" 'build complex filter, 'say you want to select text which is includes substring "Test" (dxf code 1) and has rotation of 1 degree or 0.0174533 in radians (dxf code 50) Dim filt As New SelectionFilter(New TypedValue() {New TypedValue(-4, "<and"), _ New TypedValue(0, "TEXT"), _ New TypedValue(1, "*Test*"), _ New TypedValue(-4, "<and"), _ New TypedValue(-4, "<"), _ New TypedValue(50, 0.0174534), _ New TypedValue(-4, ">"), _ New TypedValue(50, 0.0174532), _ New TypedValue(-4, "and>"), _ New TypedValue(-4, "and>")}) Dim res As PromptSelectionResult = Ed.GetSelection(filt) If res.Status <> PromptStatus.OK Then Return End If Dim SS As SelectionSet = res.Value Dim idarray As ObjectId() = SS.GetObjectIds Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager 'start a transaction Dim myT As Transaction = tm.StartTransaction() Using myT Try Dim id As ObjectId For Each id In idarray Dim ent As Entity = CType(id.GetObject(OpenMode.ForRead), Entity) If TypeOf ent Is DBText Then Dim tt As DBText = DirectCast(ent, DBText) tt.UpgradeOpen() '---- change text properties here ---- tt.Rotation = 0 tt.TextString = "huh?" End If Next myT.Commit() Catch ex As Autodesk.AutoCAD.Runtime.Exception Ed.WriteMessage(vbLf & "{0}" & vbLf & "{1}", ex.Message, ex.StackTrace) End Try End Using End Sub Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.