Creating a Block With an Attribute, Why Does One Work, But Not The Other
I want to create a block that includes a polyline (in a rounded triangle shape) and an attribute.
In this first example (which I found online, I can successfully create the block if I use a circle.
` Public Sub CreateDeltaNoteBlock2(ByVal dwg_scale As Double)
"Get the current database and start a transaction
Dim podDWG As Document = Application.DocumentManager.MdiActiveDocument
Dim podDB As Database = podDWG.Database
Using acTrans As Transaction = podDB.TransactionManager.StartTransaction() "Open the Block table for read Dim acBlkTbl As BlockTable acBlkTbl = acTrans.GetObject(podDB.BlockTableId, OpenMode.ForRead) If Not acBlkTbl.Has("Delta_Note") Then Using acBlkTblRec As New BlockTableRecord acBlkTblRec.Name = "Delta_Note" "Set the insertion point for the block acBlkTblRec.Origin = New Point3d(0, 0, 0) "Add a circle to the block Using acCirc As New Circle acCirc.Center = New Point3d(0, 0, 0) acCirc.Radius = 6 * dwg_scale acBlkTblRec.AppendEntity(acCirc) "Add an attribute definition to the block Using acAttDef As New AttributeDefinition acAttDef.Position = New Point3d(0, 0, 0) acAttDef.Verifiable = True acAttDef.Prompt = "Enter Delta Number: " acAttDef.Tag = "DeltaNote" acAttDef.TextString = "" acAttDef.Height = 1 acAttDef.Justify = AttachmentPoint.MiddleCenter acBlkTblRec.AppendEntity(acAttDef) acBlkTbl.UpgradeOpen() acBlkTbl.Add(acBlkTblRec) acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True) End Using End Using End Using End If "Save the new object to the database acTrans.Commit() End Using End Sub'
But when I use the same code and I replace the circle with a polyline. It skips the "Using acAttDef As New AttributeDefinition" section.
` Public Sub CreateDeltaNoteBlock2(ByVal dwg_scale As Double)
"Get the current database and start a transaction
Dim podDWG As Document = Application.DocumentManager.MdiActiveDocument
Dim podDB As Database = podDWG.Database
Using acTrans As Transaction = podDB.TransactionManager.StartTransaction() "Open the Block table for read Dim acBlkTbl As BlockTable acBlkTbl = acTrans.GetObject(podDB.BlockTableId, OpenMode.ForRead) If Not acBlkTbl.Has("Delta_Note") Then Using acBlkTblRec As New BlockTableRecord acBlkTblRec.Name = "Delta_Note" "Set the insertion point for the block acBlkTblRec.Origin = New Point3d(0, 0, 0) Dim centerPoint As Point2d = New Point2d(0, 0) Dim distFromCenter As Double = 4.944 Dim delta_pt1 As Point2d = PolarPoints2D(centerPoint, DegreesToRadians(201.0), (distFromCenter * dwg_scale)) Dim delta_pt2 As Point2d = PolarPoints2D(centerPoint, DegreesToRadians(219.0), (distFromCenter * dwg_scale)) Dim delta_pt3 As Point2d = PolarPoints2D(centerPoint, DegreesToRadians(321.0), (distFromCenter * dwg_scale)) Dim delta_pt4 As Point2d = PolarPoints2D(centerPoint, DegreesToRadians(339.0), (distFromCenter * dwg_scale)) Dim delta_pt5 As Point2d = PolarPoints2D(centerPoint, DegreesToRadians(81.0), (distFromCenter * dwg_scale)) Dim delta_pt6 As Point2d = PolarPoints2D(centerPoint, DegreesToRadians(99.0), (distFromCenter * dwg_scale)) " Add a circle to the block "Using acCirc As New Circle "acCirc.Center = New Point3d(0, 0, 0) "acCirc.Radius = 6 * dwg_scale "Create the polyline Using deltaPoly As New Polyline deltaPoly.SetDatabaseDefaults() deltaPoly.AddVertexAt(0, delta_pt1, 0, 0, 0) deltaPoly.AddVertexAt(1, delta_pt2, 0, 0, 0) deltaPoly.AddVertexAt(2, delta_pt3, 0, 0, 0) deltaPoly.AddVertexAt(3, delta_pt4, 0, 0, 0) deltaPoly.AddVertexAt(4, delta_pt5, 0, 0, 0) deltaPoly.AddVertexAt(5, delta_pt6, 0, 0, 0) deltaPoly.Closed = True "Sets the bulge at index 0, 2, and 4 deltaPoly.SetBulgeAt(0, (Math.Tan((120 / 4) * (Math.PI / 180)))) deltaPoly.SetBulgeAt(2, (Math.Tan((120 / 4) * (Math.PI / 180)))) deltaPoly.SetBulgeAt(4, (Math.Tan((120 / 4) * (Math.PI / 180)))) acBlkTblRec.AppendEntity(deltaPoly) "Add an attribute definition to the block Using acAttDef As New AttributeDefinition acAttDef.Position = New Point3d(0, 0, 0) acAttDef.Verifiable = True acAttDef.Prompt = "Enter Delta Number: " acAttDef.Tag = "DeltaNote" acAttDef.TextString = "" acAttDef.Height = 1 acAttDef.Justify = AttachmentPoint.MiddleCenter acBlkTblRec.AppendEntity(acAttDef) acBlkTbl.UpgradeOpen() acBlkTbl.Add(acBlkTblRec) acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True) End Using End Using End Using End If " Save the new object to the database acTrans.Commit() End Using End Sub`
A pic of the error is attached, though I've created polylines before without issue.
Comments
-
ok, set a break point near top and see where code skips out.
It you add a try/catch, you can catch the exception and read its error:
catch (exception ex){ debug.print(ex.message); }....
The fact that you did not mention how the debug went tells me you have never done that?0 -
I actually did use a break point. As I stepped through each line, it just skipped the attribute section. It jumped from acBlkTblRec.AppendEntity(deltaPoly) to the End Using for that section.
It wasn't until it tried to insert the block and update the attribute text that it crashed. Probably because it didn't have the attribute due to being skipped.
The error is attached as a jpeg in the first post.0 -
oh, good. I have not heard of code jumping like that.
Normally it jumps to a catch, or crashes if none.
In this case, please add the try catch and look at the error.
Also, remove the bulge section to try to narrow down the complexity.
From my code library, this is the relevent code I use to make a lwpline (C#)://Add the pline object AcDb.Polyline pline; try { pline = new AcDb.Polyline(verts.Count); pline.SetDatabaseDefaults(); pline.Closed = closed; if (strColor != "0" && strColor.Left(1).IsInteger()) if (strColor.Contains(",")) pline.Color = HAEP.AcColorFromString(strColor); else pline.ColorIndex = strColor.ToInteger(); //add verticies for (int i = 0; i < verts.Count - 1; i++) { if (i == 0) pline.AddVertexAt(0, verts[i], 0.0, width, width); pline.AddVertexAt(i + 1, verts[i + 1], 0.0, width, width); if (bulges != null) pline.SetBulgeAt(i, bulges[i]); } pline.Elevation = elev; if (width > 0.0) pline.ConstantWidth = width;
0 -
Hi James,
I just got tired of fighting it, so I took a different approach. Since I was already inserting a basic drawing that contains my dimension styles, mleader styles, and assorted blocks, I just created the delta block in this base drawing. So now it exists from the start and I don't have to waste my time trying to get the code to create it.0