I'm working on a c# program designed to insert a block already on the autocad drawing to any place the user clicks on and automatically add a string value to the first custom attribute. I've done part of this by creating a new block reference with the new clicked point location and the existing block name.
Only for some reason the two custom attributes on the existing block are not present on the new block, it just shows the lines of the block with no text. See small portion of the program below. Does anyone know why the existing block attributes are not being added to the new block, and if so how would I go about doing so? I've gone through a number of forums on the matter and they all show how to create brand new attributes, rather than getting predefined attributes from the existing block and adding them to the new.
Scale3d blkScale = new Scale3d(drgScalemm, drgScalemm, drgScalemm);
ObjectId bdId = bt[blkName];
Point3d pt = ptInsert.Value;
BlockReference insblkref = new BlockReference(pt, bdId);
insblkref.ScaleFactors = blkScale;
insblkref.Rotation = 0;
btr.AppendEntity(insblkref);
tr.AddNewlyCreatedDBObject(insblkref, true);
You are making a wrong assumption. Attributes are not automatically inserted from the Block-definition when inserting a block from "code". So You need to add the attributes "manually".
A Code snipped in VB:
Sub AttributenToevoegen(ByVal BlokRefId As ObjectId)
Dim doc = Application.DocumentManager.MdiActiveDocument
Dim dwg = doc.Database
Using doc.LockDocument
Using transactie = doc.TransactionManager.StartTransaction()
Try
Dim Ref As BlockReference
Ref = transactie.GetObject(BlokRefId, OpenMode.ForWrite)
Dim a = Ref.Name
Dim BlokDefinities As BlockTable
BlokDefinities = transactie.GetObject(dwg.BlockTableId, OpenMode.ForRead)
Dim Blokdefid = BlokDefinities(Ref.Name)
Dim BlokDefinitie As BlockTableRecord
BlokDefinitie = transactie.GetObject(Blokdefid, OpenMode.ForRead)
Dim AttRefIdColl = Ref.AttributeCollection
For Each elementId In BlokDefinitie
Dim Element As Entity
Element = transactie.GetObject(elementId, OpenMode.ForRead)
If TypeOf Element Is AttributeDefinition Then
Dim attribuutdefinitie = CType(Element, AttributeDefinition)
Dim attribuutreferentie As New AttributeReference
attribuutreferentie.SetAttributeFromBlock(attribuutdefinitie, Ref.BlockTransform)
AttRefIdColl.AppendAttribute(attribuutreferentie)
transactie.AddNewlyCreatedDBObject(attribuutreferentie, True)
End If
Next
transactie.Commit()
Catch ex As Exception
MsgBox("Er ging iets fout: " & vbCrLf & ex.Message)
End Try
End Using
End Using