This is the code that calls the CopyAttribute function.
Thanks!
- Scott
Public Shared Sub InsertSymbolFromFile(ByVal doc As Document, ByVal dwgPath As String, ByVal insertPoint As Point3d, Optional ByRef attribValues As System.Collections.Specialized.StringDictionary = Nothing, Optional ByVal prompt As Boolean = False)
If Not System.IO.File.Exists(dwgPath) Then Throw New System.IO.FileNotFoundException("Could not find symbol file file to insert.", dwgPath)
If doc Is Nothing Then Throw New ArgumentNullException("The document cannot be null.", "doc")
Using t As Transaction = doc.TransactionManager.StartTransaction(), db As Database = New Database(False, False)
'read drawing
db.ReadDwgFile(dwgPath, System.IO.FileShare.Read, True, Nothing)
'insert it as a new block
Dim idBTR As ObjectId = doc.Database.Insert("Watermark", db, True)
Using bt As BlockTable = CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable), _
modelSpace As BlockTableRecord = CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord), _
bref As BlockReference = New BlockReference(insertPoint, idBTR)
Dim tbScale As Double = TitleblockScale()
bref.ScaleFactors = New Scale3d(tbScale, tbScale, tbScale)
'Add the blockreference...
modelSpace.AppendEntity(bref)
doc.TransactionManager.AddNewlyCreatedDBObject(bref, True)
'Add the attributereferences...
Using btAttRec As BlockTableRecord = CType(t.GetObject(bref.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
For Each idEnt As ObjectId In btAttRec
Dim ent As Entity = CType(t.GetObject(idEnt, OpenMode.ForRead), Entity)
If TypeOf (ent) Is AttributeDefinition Then
Dim attDef As AttributeDefinition = CType(ent, AttributeDefinition)
Dim attRef As AttributeReference = New AttributeReference()
CopyAttribute(attDef, attRef, bref)
If attribValues IsNot Nothing AndAlso attribValues.ContainsKey(attDef.Tag) Then
attRef.TextString = attribValues(attDef.Tag)
ElseIf prompt Then
Dim value As String = InputBox(attDef.Prompt, "Set Attribute Value", "")
If attribValues IsNot Nothing Then attribValues.Add(attDef.Tag, value)
attRef.TextString = value
End If
bref.AppendAttribute(attRef)
doc.TransactionManager.AddNewlyCreatedDBObject(attRef, True)
End If
Next
End Using
End Using
t.Commit()
End Using
End Sub