Author Topic: eOutOfRange - Copying Text Style from an external Drawing  (Read 1645 times)

0 Members and 1 Guest are viewing this topic.

GumbyCAD

  • Newt
  • Posts: 84
eOutOfRange - Copying Text Style from an external Drawing
« on: April 15, 2014, 10:15:54 PM »
Copying Text Style from an external Drawing

After trying "gile" awesome Post
http://www.theswamp.org/index.php?topic=42539.msg477455#msg477455

I am trying to copy an external TextSytle to my current drawing.

This seams to work beautifully.  But from one drawing I get an eOutOfRange  error.

My Code Just InCase

In My Form
Code: [Select]
            If optFromExternalFile.Checked Then
                ed.WriteMessage(ImportStylesFromFile(txtExternalFile.Text, SelStyles))
            End If

'Sub Function
Code - Visual Basic: [Select]
  1.     Private Function ImportStylesFromFile(ByVal SourceFileName As String, ByVal StyleNames As List(Of String)) As String
  2.  
  3.         Dim CurrentDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  4.         Dim ed As Editor = CurrentDoc.Editor()
  5.  
  6.         'Get orignal drawing
  7.        Dim CurrentDB As Database = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
  8.         Dim Result As String = ""
  9.  
  10.         'Create List Of class to store data about layers
  11.        Dim LDets As List(Of StyleDets) = New List(Of StyleDets)
  12.  
  13.         Try
  14.  
  15.             If optTextStyles.Checked Then
  16.  
  17.                 For Each StyleName As String In StyleNames
  18.  
  19.                     Using DokLock As DocumentLock = CurrentDoc.LockDocument
  20.                         CurrentDB.ImportSymbolTableRecord(Of TextStyleTable)(SourceFileName, StyleName, chkOverWriteLayer.Checked)
  21.                     End Using
  22.  
  23.                 Next
  24.  
  25.             End If
  26.  
  27.         Catch ex As Exception
  28.  
  29.             Result = ex.ToString
  30.  
  31.         End Try
  32.  
  33.         Return Result
  34.  
  35.     End Function
  36.  

Code - Visual Basic: [Select]
  1.     <System.Runtime.CompilerServices.Extension()> _
  2.     Public Function ImportSymbolTableRecord(Of T As SymbolTable)(targetDb As Database, sourceFile As String, recordName As String, ByVal OverWrite As Boolean) As ObjectId
  3.  
  4.         'Dim CurrentDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  5.  
  6.         Using sourceDb As New Database()
  7.  
  8.             sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, False, "")
  9.  
  10.             Dim sourceTableId As ObjectId, targetTableId As ObjectId
  11.  
  12.             Select Case GetType(T).Name
  13.                 Case "DimStyleTable"
  14.                     sourceTableId = sourceDb.DimStyleTableId
  15.                     targetTableId = targetDb.DimStyleTableId
  16.                     Exit Select
  17.                 Case "LinetypeTable"
  18.                     sourceTableId = sourceDb.LinetypeTableId
  19.                     targetTableId = targetDb.LinetypeTableId
  20.                     Exit Select
  21.                 Case "TextStyleTable"
  22.                     sourceTableId = sourceDb.TextStyleTableId
  23.                     targetTableId = targetDb.TextStyleTableId
  24.                     Exit Select
  25.                 Case Else
  26.                     Throw New ArgumentException("Requires a concrete type derived from SymbolTable")
  27.             End Select
  28.  
  29.             Dim DupValues As DuplicateRecordCloning = If(OverWrite <> False, DuplicateRecordCloning.Replace, DuplicateRecordCloning.Ignore)
  30.  
  31.             Using tr As Transaction = sourceDb.TransactionManager.StartTransaction()
  32.                 Dim sourceTable As T = DirectCast(tr.GetObject(sourceTableId, OpenMode.ForRead), T)
  33.                 If Not sourceTable.Has(recordName) Then
  34.                     Return ObjectId.Null
  35.                 End If
  36.                 Dim idCol As New ObjectIdCollection()
  37.                 Dim sourceTableRecordId As ObjectId = sourceTable(recordName)
  38.                 idCol.Add(sourceTableRecordId)
  39.                 Dim idMap As New IdMapping()
  40.                 sourceDb.WblockCloneObjects(idCol, targetTableId, idMap, DupValues, False)
  41.                 tr.Commit()
  42.                 Return idMap(sourceTableRecordId).Value
  43.  
  44.             End Using
  45.  
  46.         End Using
  47.  
  48.     End Function
  49.  

edit:kdub :-> formatting code=vb.net
« Last Edit: April 15, 2014, 10:37:12 PM by Kerry »

fixo

  • Guest
Re: eOutOfRange - Copying Text Style from an external Drawing
« Reply #1 on: April 16, 2014, 12:40:05 AM »
Check this working code (A2014-2010)

Code - Visual Basic: [Select]
  1.   '' borrowed from Kean Walmsley
  2.   Public Shared Function GetFiled(ed As Editor) As String
  3.         Dim filename As String = String.Empty
  4.  
  5.         Try
  6.  
  7.             Dim opts As New PromptOpenFileOptions("Select a drawing")
  8.  
  9.             opts.Filter = "Drawing (*.dwg)|*.dwg|Design Web Format (*.dwf)|*.dwf|" & "All files (*.*)|*.*"
  10.  
  11.             Dim pr As PromptFileNameResult = ed.GetFileNameForOpen(opts)
  12.  
  13.             If pr.Status = PromptStatus.OK Then
  14.                 filename = pr.StringResult
  15.                 ed.WriteMessage(vbLf & "File selected: {0}.", filename)
  16.             End If
  17.             Return filename
  18.         Catch ex As System.Exception
  19.             ed.WriteMessage(ex.Message)
  20.             Return String.Empty
  21.         End Try
  22.  
  23.     End Function
  24.     <CommandMethod("COPYTXTS")> _
  25.     Public Shared Sub ExtCopyTextStyles()
  26.  
  27.         Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  28.  
  29.         Dim db As Database = doc.Database
  30.  
  31.         Dim ed As Editor = doc.Editor
  32.  
  33.         Dim txtstylesToClone As New ObjectIdCollection()
  34.  
  35.         Dim extdb As Database = Nothing
  36.  
  37.         Try
  38.  
  39.             extdb = New Database(False, True)
  40.  
  41.             Dim tr As Transaction = doc.TransactionManager.StartTransaction()
  42.  
  43.             Using tr
  44.  
  45.                 Dim txtstyNames As New List(Of String)()
  46.  
  47.                 Dim filename As String = GetFiled(ed)
  48.  
  49.                 If filename.Trim() = String.Empty Then
  50.                     Return
  51.                 End If
  52.  
  53.                 extdb.ReadDwgFile(filename, System.IO.FileShare.Read, True, Nothing)
  54.  
  55.                 If extdb Is Nothing Then
  56.                     Return
  57.                 End If
  58.  
  59.                 Dim tr2 As Transaction = extdb.TransactionManager.StartTransaction()
  60.  
  61.                 Using tr2
  62.  
  63.                     ' Open the Textsyle table
  64.  
  65.                     Dim tt As TextStyleTable = DirectCast(tr2.GetObject(extdb.TextStyleTableId, OpenMode.ForRead), TextStyleTable)
  66.  
  67.                     For Each entId As ObjectId In tt
  68.  
  69.                         Dim ttr As TextStyleTableRecord = DirectCast(tr2.GetObject(entId, OpenMode.ForRead), TextStyleTableRecord)
  70.  
  71.                         Dim txtstyleName As String = ttr.Name
  72.  
  73.                         If (Not entId.IsErased) Then
  74.  
  75.                             txtstyNames.Add(txtstyleName)
  76.  
  77.                             txtstylesToClone.Add(entId)
  78.  
  79.                         End If
  80.                     Next
  81.  
  82.  
  83.                     txtstyNames.Sort()
  84.  
  85.                     ''PrintList(ed, txtstyNames)''for test only
  86.  
  87.                     tr2.Commit()
  88.                 End Using
  89.  
  90.  
  91.                 If txtstylesToClone.Count > 0 Then
  92.  
  93.                     ' We use wblockCloneObjects to clone between DWGs
  94.  
  95.                     Dim idMap As New IdMapping()
  96.  
  97.                     db.WblockCloneObjects(txtstylesToClone, db.TextStyleTableId, idMap, DuplicateRecordCloning.Ignore, False)
  98.  
  99.                     extdb.Dispose()
  100.                 End If
  101.  
  102.                 ed.WriteMessage(vbLf & vbTab & "Imported: {0} textstyles", txtstylesToClone.Count)
  103.  
  104.                 tr.Commit()
  105.             End Using
  106.         Catch ex As System.Exception
  107.             ed.WriteMessage(ex.Message)
  108.  
  109.         Finally
  110.         End Try
  111.     End Sub

edit:kdub :-> formatting code=vb.net
« Last Edit: April 16, 2014, 04:56:02 AM by Kerry »

GumbyCAD

  • Newt
  • Posts: 84
Re: eOutOfRange - Copying Text Style from an external Drawing
« Reply #2 on: April 16, 2014, 03:33:08 AM »
So restructured Extension Method is as follows:
(using fixo supplied sample that works nicely)

Code - Visual Basic: [Select]
  1.     <System.Runtime.CompilerServices.Extension()> _
  2.     Public Function ImportSymbolTableRecord(Of T As SymbolTable)(targetDb As Database, sourceFile As String, recordName As String, ByVal OverWrite As Boolean) As ObjectId
  3.  
  4.         Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  5.         Dim ed As Editor = doc.Editor
  6.  
  7.         Dim StylesToClone As New ObjectIdCollection()
  8.         Dim extdb As Database = Nothing
  9.  
  10.         Try
  11.  
  12.             Using docLock As DocumentLock = doc.LockDocument
  13.  
  14.                 extdb = New Database(False, True)
  15.                 Dim tr As Transaction = targetDb.TransactionManager.StartTransaction()
  16.  
  17.                 Using tr
  18.  
  19.                     extdb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, True, Nothing)
  20.  
  21.                     If extdb Is Nothing Then
  22.                         Return ObjectId.Null
  23.                     End If
  24.  
  25.                     Dim sourceTableId As ObjectId, targetTableId As ObjectId
  26.  
  27.                     Dim tr2 As Transaction = extdb.TransactionManager.StartTransaction()
  28.                     Using tr2
  29.  
  30.                         Select Case GetType(T).Name
  31.                             Case "DimStyleTable"
  32.                                 sourceTableId = extdb.DimStyleTableId
  33.                                 targetTableId = targetDb.DimStyleTableId
  34.                                 Exit Select
  35.                             Case "LinetypeTable"
  36.                                 sourceTableId = extdb.LinetypeTableId
  37.                                 targetTableId = targetDb.LinetypeTableId
  38.                                 Exit Select
  39.                             Case "TextStyleTable"
  40.                                 sourceTableId = extdb.TextStyleTableId
  41.                                 targetTableId = targetDb.TextStyleTableId
  42.                                 Exit Select
  43.                             Case Else
  44.                                 Throw New ArgumentException("Requires a concrete type derived from SymbolTable")
  45.                         End Select
  46.  
  47.                         ' Open the Textsyle table
  48.                        Dim tt As T = DirectCast(tr2.GetObject(sourceTableId, OpenMode.ForRead), T)
  49.  
  50.                         If Not tt.Has(recordName) Then
  51.                             Return ObjectId.Null
  52.                         End If
  53.  
  54.                         If tt(recordName).IsErased = True Then
  55.                             Return ObjectId.Null
  56.                         End If
  57.  
  58.                         StylesToClone.Add(tt(recordName))
  59.  
  60.                         tr2.Commit()
  61.                     End Using
  62.  
  63.                     Dim DupValues As DuplicateRecordCloning = If(OverWrite <> False, DuplicateRecordCloning.Replace, DuplicateRecordCloning.Ignore)
  64.  
  65.                     If StylesToClone.Count > 0 Then
  66.  
  67.                         Dim idMap As New IdMapping()
  68.                         targetDb.WblockCloneObjects(StylesToClone, targetTableId, idMap, DupValues, False)
  69.                         extdb.Dispose()
  70.  
  71.                     End If
  72.  
  73.                     ed.WriteMessage(vbLf & vbTab & "Imported: {0} " & GetType(T).Name.Replace("Table", ""), recordName)
  74.  
  75.                     tr.Commit()
  76.  
  77.                 End Using
  78.  
  79.             End Using
  80.         Catch ex As System.Exception
  81.             ed.WriteMessage(ex.Message)
  82.  
  83.         Finally
  84.         End Try
  85.  
  86.     End Function
  87.  

A Big Thanks.
 :mrgreen:  :-D :-D :-D :-D :-D :-D :-D :-D :-D :-D :-D :-D :-D :-D :-D



edit:kdub :-> formatting code=vb.net
« Last Edit: April 16, 2014, 04:56:34 AM by Kerry »

fixo

  • Guest
Re: eOutOfRange - Copying Text Style from an external Drawing
« Reply #3 on: April 16, 2014, 06:00:10 AM »
Thank you too,
saved in my code storage :)