Here is the code I used to insert an entity.
- Scott
[code]
Public Shared Sub InsertSymbol(ByVal doc As Document, ByVal dwgPath As String, ByVal insertPoint As Point3d, Optional ByRef attribValues As StringDictionary = Nothing, Optional ByVal prompt As Boolean = False)
If Not File.Exists(dwgPath) Then Throw New 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, FileShare.Read, True, Nothing)
'insert it as a new block
Dim idBTR As ObjectId = doc.Database.Insert("Watermark", db, True)
'create a ref to the block
Using bt As BlockTable = t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), _
btr As BlockTableRecord = t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), _
bref As BlockReference = New BlockReference(insertPoint, idBTR)
btr.AppendEntity(bref)
doc.TransactionManager.AddNewlyCreatedDBObject(bref, True)
Using btAttRec As BlockTableRecord = t.GetObject(bref.BlockTableRecord, OpenMode.ForRead)
For Each idEnt As ObjectId In btAttRec
Dim ent As Entity = t.GetObject(idEnt, OpenMode.ForRead)
If TypeOf (ent) Is AttributeDefinition Then
'MsgBox("test2")
Dim attDef As AttributeDefinition = ent
Dim attRef As AttributeReference = New AttributeReference()
With attRef
'.SetAttributeFromBlock(attDef, bref.BlockTransform)
.HorizontalMode = attDef.HorizontalMode
If attDef.VerticalMode <> TextVerticalMode.TextBase Or attDef.HorizontalMode <> TextHorizontalMode.TextLeft Then
.AlignmentPoint = New Point3d(attDef.AlignmentPoint.X + bref.Position.X, _
attDef.AlignmentPoint.Y + bref.Position.Y, _
attDef.AlignmentPoint.Z + bref.Position.Z)
End If
.VerticalMode = attDef.VerticalMode
.WidthFactor = attDef.WidthFactor
.FieldLength = attDef.FieldLength
.Height = attDef.Height
.Rotation = attDef.Rotation
.TextStyle = attDef.TextStyle
.Position = New Point3d(attDef.Position.X + bref.Position.X, _
attDef.Position.Y + bref.Position.Y, _
attDef.Position.Z + bref.Position.Z)
.Tag = attDef.Tag
If attribValues IsNot Nothing AndAlso attribValues.ContainsKey(attDef.Tag) Then
.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)
.TextString = value
End If
End With
bref.AppendAttribute(attRef)
doc.TransactionManager.AddNewlyCreatedDBObject(attRef, True)
End If
Next
End Using
End Using
t.Commit()
End Using
End Sub
[/code]
Message was edited by: smcclure