I wan't to append a new attribute to a new block.
But I get an error that there is no database
What do I wrong??
PS the loaded file has no attributes
Dim mDialog As New frmPaalDefinitie()
For Each item In lvPalen.Items
mDialog.tsPaal.Items(item.imageindex).enabled = False
Next
mDialog.ShowDialog()
If mDialog.DialogResult <> Windows.Forms.DialogResult.OK Then Exit Sub
Dim blockname, filename As String
blockname = mDialog.lblType.Text
filename = iAutocadDir + "\tekening\" + blockname + ".dwg"
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim acDocEd As Editor = acDoc.Editor
Dim afmeting As Integer
afmeting = Val(mDialog.txtAfmeting.Text)
Dim acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Try
Dim db As Database = New Database(False, False)
db.ReadDwgFile(filename, IO.FileShare.Read, True, "")
Dim BlkId As ObjectId
BlkId = acCurDb.Insert(blockname, db, False)
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
Dim blk As BlockTableRecord = BlkId.GetObject(OpenMode.ForWrite)
For Each objId As ObjectId In blk
Dim ent As Entity = objId.GetObject(OpenMode.ForWrite)
ent.TransformBy(Matrix3d.Scaling(afmeting, New Point3d(0, 0, 0)))
Next
Dim btr As BlockTableRecord = acBlkTbl(blockname).GetObject(OpenMode.ForRead)
Dim pntInsert As Point3d
Dim bref As New BlockReference(pntInsert, btr.ObjectId)
Dim attRef As New AttributeReference
Dim attDef As New AttributeDefinition
attDef.Prompt = "Paalnr"
attDef.Position = New Point3d(0, 0, 0)
attDef.Layer = "Paal-tekst"
attDef.ColorIndex = 2
attDef.Tag = "Paalnr"
attDef.TextString = "nr"
attRef.SetPropertiesFrom(attDef)
bref.AttributeCollection.AppendAttribute(attRef)
acTrans.AddNewlyCreatedDBObject(attRef, True)
acTrans.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.ToString)
End Try
Solved! Go to Solution.
You have to add the BlockReference to the database and the transaction before adding AttributeReferences or Annotation Scales to the BlockReference. Your code is currently not adding the Bref at all.
You should also be adding the Attribute Definitions to the BlockTableRecord, because if you don't then if the user manually inserts that block with this code getting executed, the manually inserted block will not contain the Attributes.
I only wan't to make the block.
I don't want to insert the block in my drawing (I use another command for that)
If I understand it right i must first complete the transaction and then find the block and append attributes.
But i don't no how to append the attributes to the block.
In that case, you don't even want to create a BlockReference or AttributeReference in this code. What you want to do is add AttributeDefinition objects to the BlockTableRecord. Then in your "other command" where the BlockReference is created and added to the database, you will need to create and add an AttributeReference object for each AttributeDefinition Object in the BlockTableRecord.
There are loads of examples, either on this discussion group, or on Kean Walmsley's blog, or on The Swamp, of which I am not member, but I know there are some experienced people over there. (hi Kerri, and Jefferey)
If I was not at home (Dial-up Internet) and getting ready for bed at the moment, I could provide a link or two, but you should be able to find what you need if you use the right search parameters on one of the aforementioned sites. I'll check back tomorrow while I am at work (High Speed Internet), and see if someone else hasn't already provided a link to some examples, or if you have already found them on your own.
Yes, i have got the answer
Thanks for your help chiefbraincloud !!
Try
Dim db As Database = New Database(False, False)
db.ReadDwgFile(filename, IO.FileShare.Read, True, "")
Dim BlkId As ObjectId
BlkId = acCurDb.Insert(blockname, db, False)
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
Dim blk As BlockTableRecord = BlkId.GetObject(OpenMode.ForWrite)
For Each objId As ObjectId In blk
Dim ent As Entity = objId.GetObject(OpenMode.ForWrite)
ent.TransformBy(Matrix3d.Scaling(afmeting, New Point3d(0, 0, 0)))
Next
Dim attDef1, attDef2 As New AttributeDefinition
Dim attRef As New AttributeReference
attDef1.Prompt = "Paalnr"
attDef1.Layer = "Paal-tekst"
attDef1.Position = New Point3d(iPalenDx * iHoofdschaal, iPalenDy * iHoofdschaal, 0)
attDef1.ColorIndex = 2
attDef1.Tag = "Paalnr"
attDef1.TextString = "nr"
attDef1.Justify = AttachmentPoint.BaseLeft
attDef1.Preset = False
attDef1.Height = iPalenTH * iHoofdschaal
blk.AppendEntity(attDef1)
acTrans.AddNewlyCreatedDBObject(attDef1, True)
attDef2.Prompt = "Afmeting"
attDef2.Position = New Point3d(0, 0, 0)
attDef2.Layer = "Paal-tekst"
attDef2.ColorIndex = 2
attDef2.Tag = "Afmeting"
attDef2.TextString = afmeting
attDef2.Invisible = True
attDef2.Preset = True
attDef2.Verifiable = False
blk.AppendEntity(attDef2)
acTrans.AddNewlyCreatedDBObject(attDef2, True)
acTrans.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.ToString)
End Try
Can't find what you're looking for? Ask the community or share your knowledge.