.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MText Associativity Exception

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
kpschramm
1085 Views, 10 Replies

MText Associativity Exception

I am new to developing with this API but not to VB.NET.  I am trying to associate mtext to a leader using the instructions in the AutoCAD .NET Developer's Guide (115) for AutoCAD 2012 but keep getting the eInvalidInput exception.  One suggestion is that an exception is thrown if the mtext does not exist but I checked the mtext objectid for null and valid which returns good.  I am certain that I am missing something but not sure what.  I have included my source; the exception occurs at: acLdr.EvaluateLeader() 

 

Thanks in Advance -Ken

 

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim acCurDb As Database = acDoc.Database         

Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()            

Dim acBlkTbl As BlockTable

acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)             

Dim acBlkTblRec As BlockTableRecord

acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)             

'Create the MText annotation            

Dim acMText As MText = New MText()

acMText.SetDatabaseDefaults()

acMText.Layer ="TEXT"

acMText.Contents = contents

'pt is a function that returns a Point3d(x, y, z) where z = 0

acMText.Location = pt(endPt.X + 2, endPt.Y + acMText.ActualHeight / 2)

acMText.Width = 70           

'Add the new object to Model space and the transaction             

acBlkTblRec.AppendEntity(acMText)

acTrans.AddNewlyCreatedDBObject(acMText, True)           

'Create the leader          

Dim acLdr As Leader = New Leader()

acLdr.SetDatabaseDefaults()

acLdr.Layer = "DIM"

acLdr.AppendVertex(startPt)

acLdr.AppendVertex(breakPt)

acLdr.AppendVertex(endPt)

acLdr.HasArrowHead = True             

'Add the new object to Model space and the transaction

acBlkTblRec.AppendEntity(acLdr)

acTrans.AddNewlyCreatedDBObject(acLdr,True)          

'Attach the annotation after the leader object is added

acLdr.Annotation = acMText.Object

acLdr.EvaluateLeader()

acTrans.Commit()

10 REPLIES 10
Message 2 of 11
Hallex
in reply to: kpschramm

Ken

I can't test it on a2012 but can you try as-is the next

slightly edited code

and also just a guess, check in leader settings number

of allowed verices manually

Type Leader in the command line then type S to see the things

Here is your code:

        <CommandMethod("DrawAnnoLeader", "dal", CommandFlags.Modal + CommandFlags.Redraw)> _
        Public Sub TestLeader()
            Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim acCurDb As Database = acDoc.Database

            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                Dim acBlkTbl As BlockTable

                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

                Dim acBlkTblRec As BlockTableRecord

                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                'Create the MText annotation            

                Dim acMText As MText = New MText()

                acMText.SetDatabaseDefaults()

                acMText.Layer = "TEXT"

                acMText.Contents = "contents"

                'pt is a function that returns a Point3d(x, y, z) where z = 0
                acMText.Location = New Point3d(15, 15, 0)
                'acMText.Location = pt(endPt.X + 2, endPt.Y + acMText.ActualHeight / 2)

                acMText.Width = 70

                'Add the new object to Model space and the transaction             

                acBlkTblRec.AppendEntity(acMText)

                acTrans.AddNewlyCreatedDBObject(acMText, True)

                'Create the leader          

                Dim acLdr As Leader = New Leader()

                acLdr.SetDatabaseDefaults()

                acLdr.Layer = "DIM"

                'acLdr.AppendVertex(startPt)

                'acLdr.AppendVertex(breakPt)

                'acLdr.AppendVertex(endPt)
                acLdr.AppendVertex(New Point3d(0, 0, 0))
                acLdr.AppendVertex(New Point3d(14, 14, 0))
                ' ldr.AppendVertex(New Point3d(4, 5, 0))

                acLdr.HasArrowHead = True

                'Add the new object to Model space and the transaction

                acBlkTblRec.AppendEntity(acLdr)

                acTrans.AddNewlyCreatedDBObject(acLdr, True)

                'Attach the annotation after the leader object is added

                acLdr.Annotation = acMText.ObjectId

                acLdr.EvaluateLeader()

                acTrans.Commit()

            End Using

        End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 11

Why don't you use MLeader instead? Seems to be far easier to me at least

Message 4 of 11
kpschramm
in reply to: Hallex

Thank you Hallex but I am still getting the same exception, eInputInvalid.  Not that I am ready to give up on this but what about the MLeader suggestion?  I did a quick search in the Developer's Guide and didn't see any reference for it. The good news is the leader and mtext are working, I just can't associate them so it's not the end of the world but there is always an explanation to these type of issues.  Your feedback is appreciated.

 

Ken

Message 5 of 11
Hallex
in reply to: kpschramm

Ken here is a sample to create mleader,

see if this is working on your end

    <CommandMethod("MLDF")> _
    Public Sub MleaderTest()
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
        Dim dimld As Object = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dimldrblk")
        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Try
                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("dimldrblk", ".")
                Dim arrowPoint As Point3d = ed.GetPoint(vbLf & "Pick a point for the arrowhead: ").Value
                Dim pto As New PromptPointOptions(vbLf & "Pick a point for text: ")
                pto.UseBasePoint = True
                pto.UseDashedLine = True
                pto.BasePoint = arrowPoint
                Dim symbolPoint As Point3d = ed.GetPoint(pto).Value
                Dim vec As Vector3d
                If arrowPoint.X <= symbolPoint.X Then
                    vec = New Vector3d(1, 0, 0)
                Else
                    vec = New Vector3d(-1, 0, 0)
                End If
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                Dim th As Double = db.Dimtxt
                Dim mtxt As New MText()
                mtxt.SetDatabaseDefaults()
                mtxt.Attachment = If(vec = New Vector3d(1, 0, 0), AttachmentPoint.MiddleLeft, AttachmentPoint.MiddleRight)
                mtxt.TextHeight = th
                mtxt.Width = 0.0
                mtxt.Contents = "Line 1\PLine 2\PLine 3"

                Dim mldr As New MLeader()
                mldr.LeaderLineType = LeaderType.StraightLeader
                ' --- add other properties here --- 

                mldr.AddLeaderLine(arrowPoint)
                mldr.AddFirstVertex(0, arrowPoint)
                mldr.AddFirstVertex(1, symbolPoint)
                mldr.ArrowSymbolId = db.Dimldrblk

                Dim dict As DBDictionary = DirectCast(tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)
                Dim mlstyleid As ObjectId = dict.GetAt("Standard")
                mldr.MLeaderStyle = mlstyleid
                mldr.ContentType = ContentType.MTextContent

                mldr.MText = mtxt
                mldr.TextLocation = symbolPoint
                mldr.SetDogleg(0, vec)
                mldr.LandingGap = th / 4
                mldr.DoglegLength = th

                btr.AppendEntity(mldr)
                tr.AddNewlyCreatedDBObject(mldr, True)
                tr.Commit()
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
            Finally
                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("dimldrblk", dimld)
            End Try
        End Using
    End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 6 of 11
kpschramm
in reply to: Hallex

Hallex ... Your test MLeader worked!  Thanks to TobyRsenberg8728 for the suggestion!  Is there any reason why I shouldn't mark this as a workable solution?  Also, should this be reported as a bug on AutoCAD 2012 considering the example code in the Developer's Guide does not work as written?

 

Thank you!

Message 7 of 11
Hallex
in reply to: kpschramm

Ken,

You wrote:

>>Also, should this be reported as a bug on AutoCAD 2012 considering the example code in the Developer's Guide does not work as written?

Don't hurry up, I'm still working on this issue, just a guess, maybe

you need to look at any ...ASSOC system variables

Keep watching for,

Regardfs,

Oleg

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 11
Hallex
in reply to: Hallex

In addition

I can not reconstruct the same issue here

what from my investigating is DIMASSOC variable

does not effect at all, the leader mtext stll associative

undependently

So sorry I don't know how to solve this problem

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 9 of 11
kpschramm
in reply to: Hallex

Hallex,

 

That's quite alright.  I have 2 options now, MLeader or  non-associated MText + Leader, so I will be able to proceed.  Although the MLeader is an option and not a fix to the underlying issue, do you think I should mark this thread as answered?

 

Ken

Message 10 of 11
Hallex
in reply to: kpschramm

Happy to help, Ken

Happy computing,

Cheers 🙂

 

Oleg

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 11 of 11
Hugh_Compton
in reply to: kpschramm

Hello

 

The code example above works well but I use Annotative Text.  This means the line

Dim th AsDouble = db.Dimtxt

 

returns a text ht that doesn't factor in the Annotative scale.  Is there a work around for this?

 

Thanks

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost