eInvalid Input when using SetSystemVariable()

eInvalid Input when using SetSystemVariable()

Anonymous
Not applicable
1,026 Views
1 Reply
Message 1 of 2

eInvalid Input when using SetSystemVariable()

Anonymous
Not applicable

I am left a little perplexed with this issue. This error occurs only in some drawings, which leaves me to believe it's a problem with the drawing itself, right?

 

Well; here's the thing... I am trying to set a leader style using the following line of code:

 AcApp.SetSystemVariable("CMLEADERSTYLE", "STD-48") 'Set Leader

 

As we can see from the following screenshot, the style STD-48 exists:

mleaderstyle.png

 

So the drawing contains the style yet returns an eInvalidInput whenever I try to set it using the "SetSystemVariable".

 

For more background on how I use it, see the CreateLeader() method that is built (keep in mind, this was not programmed by me):

Public Function CreateLeader(ByVal acDoc As Document, ByRef CurrentConvIdColl As ObjectIdCollection, ByVal StartPoint As Point3d, ByVal PromptMessage As String, ByVal textValue As String, ByVal Infeed As Boolean, ByRef ctp As tpConveyorToolPalette) As Boolean
        Dim oldOSMODE As Object = AcApp.GetSystemVariable("osmode") 'Get Snap Mode
        Dim oldLayer As Object = AcApp.GetSystemVariable("clayer") 'get current layer
        Dim oldLeaderStyle As Object = AcApp.GetSystemVariable("cmleaderstyle") 'get current leader style
        Dim oldTextStyle As Object = AcApp.GetSystemVariable("textstyle") 'get current text style
        Dim oldOtrho As Object = AcApp.GetSystemVariable("ORTHOMODE")

        Try
            Dim acBlkTbl As BlockTable
            Dim acBlkTblRec As BlockTableRecord
            Dim db As Database = acDoc.Database ' Current database
            Dim ed As Editor = acDoc.Editor
            Dim leaderId As ObjectId
            Dim mt As MText
            Dim pPtOpts As New PromptPointOptions("")
            Dim pPtRes As PromptPointResult
            Dim EndPoint As Point3d
            Dim lbl As Windows.Forms.Label
            Dim idx As Integer
            Dim ml As MLeader

            'Determine which label
            If Infeed Then lbl = ctp.lblSetInfeedLeader Else lbl = ctp.lblSetDischargeLeader
            If Not IsMLeaderExists("STD-48") Then InsertLayoutTemplate() : ed.WriteMessage("STD-48 was not found. Drawing has been updated with latest template")

            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()

            'Set "None" Snap Mode
            AcApp.SetSystemVariable("OSMODE", 0)
            AcApp.SetSystemVariable("ORTHOMODE", 0)
            SetLayerCurrent(acDoc, "QUOTE") 'Set Layer QUOTE

            AcApp.SetSystemVariable("OSMODE", oldOSMODE) 'Set Old Snap Mode
            AcApp.SetSystemVariable("CMLEADERSTYLE", "STD-48") 'Set Leader, ERROR OCCURS HERE!!

            'Determine EndPoint
            SetLabelImage({lbl}, "Set")
            pPtOpts.Message = PromptMessage 'Prompt for user insertion point
            pPtOpts.UseBasePoint = True
            pPtOpts.BasePoint = StartPoint
            pPtRes = ed.GetPoint(pPtOpts) 'Get user to select the insertion point

            If pPtRes.Status = PromptStatus.OK Then
                SetTextStyleCurrent(acDoc, "SIMPLEX-96") 'Set text style

                EndPoint = New Point3d(pPtRes.Value.X, pPtRes.Value.Y, pPtRes.Value.Z) 'Fix up the point to be a point3d object
                Using lock As DocumentLock = acDoc.LockDocument
                    db = acDoc.Database 'Get the current database

                    'Start a transaction
                    Using acTrans As Transaction = db.TransactionManager.StartTransaction()
                        'Open Model space for write
                        acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead)
                        acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                        'Create Leader
                        ml = New MLeader
                        ml.SetDatabaseDefaults()
                        ml.ContentType = ContentType.MTextContent
                        ml.ArrowSymbolId = Nothing
                        ml.TextAttachmentType = TextAttachmentType.AttachmentMiddle

                        'Create Text
                        mt = New MText
                        mt.SetDatabaseDefaults()
                        mt.SetContentsRtf(textValue)
                        If EndPoint.X < StartPoint.X Then
                            'Change location to fit user's pick point anf Right to Left text
                            mt.Location = New Point3d(EndPoint.X - textValue.Length * ml.TextHeight * ml.Scale, EndPoint.Y, EndPoint.Z) 'Normal Left to Right text
                        Else
                            mt.Location = EndPoint
                        End If

                        ml.MText = mt
                        ml.TextHeight = 0.1
                        idx = ml.AddLeaderLine(StartPoint)
                        leaderId = acBlkTblRec.AppendEntity(ml)
                        acTrans.AddNewlyCreatedDBObject(ml, True)
                        CurrentConvIdColl.Add(leaderId)

                        'Commit the changes and dispose of the transaction
                        acTrans.Commit()
                    End Using 'Dispose transaction
                End Using 'Dispose Lock
            Else
                'Set Old Layer
                SetLayerCurrent(acDoc, oldLayer)
                AcApp.SetSystemVariable("CMLEADERSTYLE", oldLeaderStyle) 'Set old Leader
                SetTextStyleCurrent(acDoc, oldTextStyle) 'Set old text style

                Return False
            End If

            SetLayerCurrent(acDoc, oldLayer) 'Set Old Layer
            AcApp.SetSystemVariable("CMLEADERSTYLE", oldLeaderStyle) 'Set old Leader
            SetTextStyleCurrent(acDoc, oldTextStyle) 'Set old text style
            AcApp.SetSystemVariable("ORTHOMODE", oldOtrho)
            SetLabelImage({lbl}, "Completed")

            Return True
        Catch ex As Exception
            'Set Old Layer
            SetLayerCurrent(acDoc, oldLayer)
            AcApp.SetSystemVariable("CMLEADERSTYLE", oldLeaderStyle) 'Set old Leader
            SetTextStyleCurrent(acDoc, oldTextStyle) 'Set old text style
            CustomErrHandler(ex, System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Return False
        End Try
    End Function

 The program goes through the CreateLeader() succesfully two times. On the third time, it stops on the CMLEADERSTYLE setting.

 

 

I have AutoCAD 2014, should I be setting the leader style in a different and more robust way?

 

Thank you,

Alex

0 Likes
Accepted solutions (1)
1,027 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Accepted solution

Fixed it by using a different method of setting the leader.

 

Public Sub SetLeader(ByVal mlstylename As String)
        Dim doc As Document = Ac.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim mldict As DBDictionary = Nothing
        Dim mldstyle As MLeaderStyle = Nothing

        Try
            Using lock As DocumentLock = doc.LockDocument
                Using tr As Transaction = doc.TransactionManager.StartTransaction
                    mldict = CType(tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)

                    'Verify if style exists, if not, insert a layout template
                    If Not IsDbDictionaryExists(mldict, mlstylename) Then
                        InsertLayoutTemplate() 'Inserts a layout containing all styles
                    End If

                    'Verify if it exists again, if not ... Provide an error message
                    If Not IsDbDictionaryExists(mldict, mlstylename) Then
                        ed.WriteMessage("Mleader style ""{0}"" does not exists", mlstylename)
Exit Sub End If mldstyle = CType(tr.GetObject(mldict.GetAt(mlstylename), OpenMode.ForRead), MLeaderStyle) db.MLeaderstyle = mldict.GetAt(mlstylename) tr.Commit() End Using End Using Catch ex As System.Exception ed.WriteMessage(ex.Message & vbCr & ex.StackTrace) Finally mldstyle.Dispose() mldict.Dispose() End Try End Sub Public Function IsDbDictionaryExists(ByVal parent As DBDictionary, ByVal dictname As String) As Boolean Dim exists As Boolean = False For Each edict As DBDictionaryEntry In parent Try If edict.Key = dictname Then exists = True Exit For End If Catch End Try Next Return exists End Function

 

0 Likes