Author Topic: Copying annotative textstyle  (Read 3395 times)

0 Members and 1 Guest are viewing this topic.

clint78z

  • Mosquito
  • Posts: 5
Copying annotative textstyle
« on: August 20, 2012, 12:36:19 PM »
I am trying to copy an annotive textstyle from a template drawing and insert it into my current drawing. The textstyle it is annotative when I copy it, then when it comes to adding it to the current text style table it set it as non annotative. I tried setting the current drawing to annotative, any ideas??

Code: [Select]
        <CommandMethod("AddTextTmpl")> _
        Public Sub AddtextTmpl()
            Dim acDocMgr As DocumentCollection = Application.DocumentManager
            Dim acDoc As Document = acDocMgr.MdiActiveDocument
            Dim acDb As Database = acDoc.Database
            Dim TmpltDb As Database = New Database(False, False)
            Dim fname As String = "Q:\Eng_proj\Eng_Data\DRAWINGS\CAD Standards\ISSUED STANDARDS\TEMPLATES\ARC_LAYOUT.dwt"
            TmpltDb.ReadDwgFile(fname, System.IO.FileShare.Read, True, "")

            HostApplicationServices.WorkingDatabase = acDb
            '' Lock the new document
            Try
                Using acLckDoc As DocumentLock = acDoc.LockDocument()
                    Using actrans As Transaction = acDb.TransactionManager.StartTransaction()
                        'set up the drawing variables properly
                        acDb.AnnotativeDwg = True
                        acDb.Ltscale = 1
                        acDb.Insunits = Autodesk.AutoCAD.DatabaseServices.UnitsValue.Undefined
                        acDb.Measurement = 1
                        acDb.Psltscale = 1
                        acDb.MsLtScale = 1

                        Dim acTextStyleTable As TextStyleTable = actrans.GetObject(acDb.TextStyleTableId, OpenMode.ForWrite)
                        Dim acTextStyleTblRec As TextStyleTableRecord = New TextStyleTableRecord

                        Using actempltrans As Transaction = TmpltDb.TransactionManager.StartTransaction
                            'get the correct textstyles from the template
                            Dim templateTextStyleTbl As TextStyleTable = actempltrans.GetObject(TmpltDb.TextStyleTableId, OpenMode.ForRead)
                            Dim templateTextStyleTableRecord = New TextStyleTableRecord
                            For Each acObjId As ObjectId In templateTextStyleTbl
                                templateTextStyleTableRecord = actempltrans.GetObject(acObjId, OpenMode.ForRead)
                                acDoc.Editor.WriteMessage(vbLf & templateTextStyleTableRecord.Name)
                                If templateTextStyleTableRecord.Name.Contains("ARC") Then
                                    Try
                                        acDb.Textstyle = acTextStyleTblRec.ObjectId
                                        acTextStyleTable.Add(acTextStyleTblRec)
                                        actrans.AddNewlyCreatedDBObject(acTextStyleTblRec, True)
                                        acTextStyleTblRec.CopyFrom(templateTextStyleTableRecord)
                                     Catch ex As Exception
                                        ' This will catch textstyle names that already exist
                                    End Try
                                End If
                            Next
                            actempltrans.Commit()
                        End Using
                        actrans.Commit()
                    End Using
                End Using
            Catch ex As System.Exception
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message)
            Finally
            End Try
        End Sub

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Copying annotative textstyle
« Reply #1 on: August 20, 2012, 01:25:45 PM »
Hi,

Use WblockCloneObjects().

Code - C#: [Select]
  1.         public ObjectId ImportTextStyle(Database targetDb, string sourceFile, string textStyle)
  2.         {
  3.             using (Database sourceDb = new Database())
  4.             {
  5.                 sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, false, "");
  6.                 using (Transaction trx = sourceDb.TransactionManager.StartTransaction())
  7.                 {
  8.                     TextStyleTable sourceTextStyleTable =
  9.                         (TextStyleTable)trx.GetObject(sourceDb.TextStyleTableId, OpenMode.ForRead);
  10.                     if (!sourceTextStyleTable.Has(textStyle))
  11.                         return ObjectId.Null;
  12.                     ObjectIdCollection idCol = new ObjectIdCollection();
  13.                     ObjectId sourceTextStyleId = sourceTextStyleTable[textStyle];
  14.                     idCol.Add(sourceTextStyleId);
  15.                     IdMapping idMap = new IdMapping();
  16.                     sourceDb.WblockCloneObjects(idCol, targetDb.TextStyleTableId, idMap, DuplicateRecordCloning.Ignore, false);
  17.                     trx.Commit();
  18.                     return idMap[sourceTextStyleId].Value;
  19.                 }
  20.             }
  21.         }
« Last Edit: August 20, 2012, 01:29:18 PM by gile »
Speaking English as a French Frog

clint78z

  • Mosquito
  • Posts: 5
Re: Copying annotative textstyle
« Reply #2 on: August 20, 2012, 02:33:26 PM »
Thanks I gave it a shot with clone before coming here. The documentation on this is not really clear when clone should be used.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Copying annotative textstyle
« Reply #3 on: August 20, 2012, 06:21:45 PM »
Going a little further with a generic extension method to import any SymbolTableRecord from a file to the Database SymbolTable

C#
Code - C#: [Select]
  1.     public static class Extension
  2.     {
  3.         public static ObjectId ImportSymbolTableRecord<T>(this Database targetDb, string sourceFile, string recordName)
  4.             where T : SymbolTable
  5.         {
  6.             using (Database sourceDb = new Database())
  7.             {
  8.                 sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, false, "");
  9.                 ObjectId sourceTableId, targetTableId;
  10.                 switch (typeof(T).Name)
  11.                 {
  12.                     case "BlockTable":
  13.                         sourceTableId = sourceDb.BlockTableId;
  14.                         targetTableId = targetDb.BlockTableId;
  15.                         break;
  16.                     case "DimStyleTable":
  17.                         sourceTableId = sourceDb.DimStyleTableId;
  18.                         targetTableId = targetDb.DimStyleTableId;
  19.                         break;
  20.                     case "LayerTable":
  21.                         sourceTableId = sourceDb.LayerTableId;
  22.                         targetTableId = targetDb.LayerTableId;
  23.                         break;
  24.                     case "LinetypeTable":
  25.                         sourceTableId = sourceDb.LinetypeTableId;
  26.                         targetTableId = targetDb.LinetypeTableId;
  27.                         break;
  28.                     case "RegAppTable":
  29.                         sourceTableId = sourceDb.RegAppTableId;
  30.                         targetTableId = targetDb.RegAppTableId;
  31.                         break;
  32.                     case "TextStyleTable":
  33.                         sourceTableId = sourceDb.TextStyleTableId;
  34.                         targetTableId = targetDb.TextStyleTableId;
  35.                         break;
  36.                     case "UcsTable":
  37.                         sourceTableId = sourceDb.UcsTableId;
  38.                         targetTableId = targetDb.UcsTableId;
  39.                         break;
  40.                     case "ViewTable":
  41.                         sourceTableId = sourceDb.ViewportTableId;
  42.                         targetTableId = targetDb.ViewportTableId;
  43.                         break;
  44.                     case "ViewportTable":
  45.                         sourceTableId = sourceDb.ViewportTableId;
  46.                         targetTableId = targetDb.ViewportTableId;
  47.                         break;
  48.                     default:
  49.                         throw new ArgumentException("Requires a concrete type derived from SymbolTable");
  50.                 }
  51.  
  52.                 using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
  53.                 {
  54.                     T sourceTable = (T)tr.GetObject(sourceTableId, OpenMode.ForRead);
  55.                     if (!sourceTable.Has(recordName))
  56.                         return ObjectId.Null;
  57.                     ObjectIdCollection idCol = new ObjectIdCollection();
  58.                     ObjectId sourceTableRecordId = sourceTable[recordName];
  59.                     idCol.Add(sourceTableRecordId);
  60.                     IdMapping idMap = new IdMapping();
  61.                     sourceDb.WblockCloneObjects(idCol, targetTableId, idMap, DuplicateRecordCloning.Ignore, false);
  62.                     tr.Commit();
  63.                     return idMap[sourceTableRecordId].Value;
  64.                 }
  65.             }
  66.         }
  67.     }
using example:
Code - C#: [Select]
  1. ObjectId id = db.ImportSymbolTableRecord<TextStyleTable>(sourceFileName, textStyleName);

F#
Code - F#: [Select]
  1. type Database with
  2.     member db.ImportSymbolTableRecord<'T when 'T :> SymbolTable>(sourceFile, (recordName:string)) =
  3.         use sourceDb = new Database()
  4.         sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, false, "")
  5.         let  sourceTableId, targetTableId =
  6.             match typeof<'T>.Name with
  7.            | "BlockTable" -> sourceDb.BlockTableId, db.BlockTableId
  8.            | "DimStyleTable" -> sourceDb.DimStyleTableId, db.DimStyleTableId
  9.            | "LayerTable" -> sourceDb.LayerTableId, db.LayerTableId
  10.            | "LinetypeTable" -> sourceDb.LinetypeTableId, db.LinetypeTableId
  11.            | "RegAppTable" -> sourceDb.RegAppTableId, db.RegAppTableId
  12.            | "TextStyleTable" -> sourceDb.TextStyleTableId, db.TextStyleTableId
  13.            | "UcsTable" -> sourceDb.UcsTableId, db.UcsTableId
  14.            | "ViewTable" -> sourceDb.ViewTableId, db.ViewTableId
  15.            | "ViewportTable" -> sourceDb.ViewportTableId, db.ViewportTableId
  16.            | _ -> invalidArg "T" "Requires a concrete type derived from SymbolTable"
  17.  
  18.        use tr = sourceDb.TransactionManager.StartTransaction()
  19.        let sourceTable = tr.GetObject(sourceTableId, OpenMode.ForRead) :?> 'T
  20.         if sourceTable.Has(recordName) then
  21.             let idCol = new ObjectIdCollection()
  22.             let sourceTableRecordId = sourceTable.[recordName];
  23.             idCol.Add(sourceTableRecordId) |> ignore
  24.             let idMap = new IdMapping()
  25.             sourceDb.WblockCloneObjects(idCol, targetTableId, idMap, DuplicateRecordCloning.Ignore, false)
  26.             tr.Commit()
  27.             idMap.[sourceTableRecordId].Value
  28.         else
  29.             ObjectId.Null
using example:
Code - F#: [Select]
  1. let id = db.ImportSymbolTableRecord<TextStyleTable>(sourceFileName, textStyleName)

VB
Code - vb.net: [Select]
  1.     Module Extension
  2.         <System.Runtime.CompilerServices.Extension()> _
  3.         Public Function ImportSymbolTableRecord(Of T As SymbolTable)(targetDb As Database, sourceFile As String, recordNameAs String) As ObjectId
  4.             Using sourceDb As New Database()
  5.                 sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, False, "")
  6.                 Dim sourceTableId As ObjectId, targetTableId As ObjectId
  7.                 Select Case GetType(T).Name
  8.                     Case "BlockTable"
  9.                         sourceTableId = sourceDb.BlockTableId
  10.                         targetTableId = targetDb.BlockTableId
  11.                         Exit Select
  12.                     Case "DimStyleTable"
  13.                         sourceTableId = sourceDb.DimStyleTableId
  14.                         targetTableId = targetDb.DimStyleTableId
  15.                         Exit Select
  16.                     Case "LayerTable"
  17.                         sourceTableId = sourceDb.LayerTableId
  18.                         targetTableId = targetDb.LayerTableId
  19.                         Exit Select
  20.                     Case "LinetypeTable"
  21.                         sourceTableId = sourceDb.LinetypeTableId
  22.                         targetTableId = targetDb.LinetypeTableId
  23.                         Exit Select
  24.                     Case "RegAppTable"
  25.                         sourceTableId = sourceDb.RegAppTableId
  26.                         targetTableId = targetDb.RegAppTableId
  27.                         Exit Select
  28.                     Case "TextStyleTable"
  29.                         sourceTableId = sourceDb.TextStyleTableId
  30.                         targetTableId = targetDb.TextStyleTableId
  31.                         Exit Select
  32.                     Case "UcsTable"
  33.                         sourceTableId = sourceDb.UcsTableId
  34.                         targetTableId = targetDb.UcsTableId
  35.                         Exit Select
  36.                     Case "ViewTable"
  37.                         sourceTableId = sourceDb.ViewportTableId
  38.                         targetTableId = targetDb.ViewportTableId
  39.                         Exit Select
  40.                     Case "ViewportTable"
  41.                         sourceTableId = sourceDb.ViewportTableId
  42.                         targetTableId = targetDb.ViewportTableId
  43.                         Exit Select
  44.                     Case Else
  45.                         Throw New ArgumentException("Requires a concrete type derived from SymbolTable")
  46.                 End Select
  47.  
  48.                 Using tr As Transaction = sourceDb.TransactionManager.StartTransaction()
  49.                     Dim sourceTable As T = DirectCast(tr.GetObject(sourceTableId, OpenMode.ForRead), T)
  50.                     If Not sourceTable.Has(recordName) Then
  51.                         Return ObjectId.Null
  52.                     End If
  53.                     Dim idCol As New ObjectIdCollection()
  54.                     Dim sourceTableRecordId As ObjectId = sourceTable(recordName)
  55.                     idCol.Add(sourceTableRecordId)
  56.                     Dim idMap As New IdMapping()
  57.                     sourceDb.WblockCloneObjects(idCol, targetTableId, idMap, DuplicateRecordCloning.Ignore, False)
  58.                     tr.Commit()
  59.                     Return idMap(sourceTableRecordId).Value
  60.                 End Using
  61.             End Using
  62.         End Function
  63.     End Module
using example:
Code - vb.net: [Select]
  1. Dim id As ObjectId = db.ImportSymbolTableRecord(Of TextStyleTable)(sourceFileName, textStyleName)
« Last Edit: August 21, 2012, 03:53:48 AM by gile »
Speaking English as a French Frog

clint78z

  • Mosquito
  • Posts: 5
Re: Copying annotative textstyle
« Reply #4 on: August 23, 2012, 11:31:05 AM »
Nice stuff the clone is much easier for this. I discovered why my first set of code didn't work, I have to commit the inner transaction before addding the object to the outer transaction. I wasnt aware of this before. At least I figured out something new. Also have to be careful with the host database set back to the current drawing before ending the proceeedure.

Anyone know if you use the cloning.replace feature on a textstyle or layer if it will update the new drawing from the source. Or do I have to copy properties if there is a duplicate??