.NET

Reply
Member
kpschramm
Posts: 4
Registered: ‎03-14-2012
Message 1 of 11 (356 Views)
Accepted Solution

MText Associativity Exception

356 Views, 10 Replies
03-14-2012 11:35 AM

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()

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 11 (348 Views)

Re: MText Associativity Exception

03-14-2012 01:09 PM 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
Active Member
TobyRosenberg8728
Posts: 7
Registered: ‎03-14-2012
Message 3 of 11 (332 Views)

Re: MText Associativity Exception

03-14-2012 11:01 PM in reply to: kpschramm

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

Member
kpschramm
Posts: 4
Registered: ‎03-14-2012
Message 4 of 11 (323 Views)

Re: MText Associativity Exception

03-15-2012 05:43 AM 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

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 11 (315 Views)

Re: MText Associativity Exception

03-15-2012 07:29 AM 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
Member
kpschramm
Posts: 4
Registered: ‎03-14-2012
Message 6 of 11 (313 Views)

Re: MText Associativity Exception

03-15-2012 07:55 AM 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!

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 11 (309 Views)

Re: MText Associativity Exception

03-15-2012 08:59 AM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 8 of 11 (304 Views)

Re: MText Associativity Exception

03-15-2012 09:40 AM 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
Member
kpschramm
Posts: 4
Registered: ‎03-14-2012
Message 9 of 11 (303 Views)

Re: MText Associativity Exception

03-15-2012 09:48 AM 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

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 10 of 11 (298 Views)

Re: MText Associativity Exception

03-15-2012 11:19 AM in reply to: kpschramm

Happy to help, Ken

Happy computing,

Cheers :smileyhappy:

 

Oleg

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community