Message 1 of 20
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello, this is the code that I've put together by reading posts on here and elsewhere.
I have drawings in a library folder like "SMD_0201.dwg". The drawing consists of a rectangle and an invisible attribute.
I want to bring the appropriate block (dwg) into my drawing and place the references at the correct location and rotation with the correct attribute text.
When I use this, all seems to run fine (the rectangles show up placed perfectly and rotated), but there are no attributes. Then when I double pick on the block, it does not have a block name, it doesn't even have a definition.
BlockName = the path and name of the block
basename = block name
Public Sub InsertSMDComp(ByVal InsPt As Point3d, ByVal BlockName As String, ByVal basename As String, ByVal RefDes As String, _
ByVal rotation As Double, ByVal scale As Double)
Dim smdTransMan As DatabaseServices.TransactionManager
Dim smdTrans As DatabaseServices.Transaction
Dim myDwg As Document
Dim myBT As BlockTable
myDwg = Application.DocumentManager.MdiActiveDocument
smdTransMan = myDwg.TransactionManager
smdTrans = smdTransMan.StartTransaction
myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
Dim myDB As Database
myDB = myDwg.Database
Try
Using db As Database = New Database(False, True)
db.ReadDwgFile(BlockName, IO.FileShare.Read, True, "")
Dim BlkId As ObjectId
BlkId = myDB.Insert(BlockName, db, True)
Dim bt As BlockTable = smdTrans.GetObject(myDB.BlockTableId, OpenMode.ForRead, True)
Dim btr As BlockTableRecord = smdTrans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
Dim bref As BlockReference
bref = New BlockReference(InsPt, BlkId)
bref.Rotation = rotation
bref.ScaleFactors = New Scale3d(scale, scale, scale)
btr.AppendEntity(bref)
smdTrans.AddNewlyCreatedDBObject(bref, True)
'Set the Attribute Value
Dim myAttColl As DatabaseServices.AttributeCollection
Dim myEnt As DatabaseServices.Entity
Dim myBTREnum As BlockTableRecordEnumerator
myAttColl = bref.AttributeCollection
myBTREnum = btr.GetEnumerator
While myBTREnum.MoveNext
myEnt = myBTREnum.Current.GetObject(OpenMode.ForWrite)
If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
Dim myAttRef As New DatabaseServices.AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef, bref.BlockTransform)
myAttRef.TextString = RefDes
myAttColl.AppendAttribute(myAttRef)
smdTrans.AddNewlyCreatedDBObject(myAttRef, True)
End If
End While
smdTrans.Commit()
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.ToString)
End Try
smdTrans.Dispose()
smdTransMan.Dispose()
End Sub
Help!
Thanks,
Mark
Solved! Go to Solution.