I'm trying to create a custom command for a rectangular array. As a starting point I've just copied all the code from the AutoCad .Net Developers Guide (see large block of code below) and pasted it straight into Visual Studio, however it's picking up an error (see image). Can someone help me out with correcting this. It's strange that I'm getting this error even through the code is directly from the Guide.
Thanks in advance.
<CommandMethod("RectangularArrayObject")>
Public Shared Sub RectangularArrayObject()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Block table record for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead)
'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),
OpenMode.ForWrite)
'' Create a circle that is at 2,2 with a radius of 0.5
Dim acCirc As Circle = New Circle()
acCirc.SetDatabaseDefaults()
acCirc.Center = New Point3d(2, 2, 0)
acCirc.Radius = 0.5
'' Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
'' Create a rectangular array with 5 rows and 5 columns
Dim nRows As Integer = 5
Dim nColumns As Integer = 5
'' Set the row and column offsets along with the base array angle
Dim dRowOffset As Double = 1
Dim dColumnOffset As Double = 1
Dim dArrayAng As Double = 0
'' Get the angle from X for the current UCS
Dim curUCSMatrix As Matrix3d = acDoc.Editor.CurrentUserCoordinateSystem
Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d
Dim acVec2dAng As Vector2d = New Vector2d(curUCS.Xaxis.X,
curUCS.Xaxis.Y)
'' If the UCS is rotated, adjust the array angle accordingly
dArrayAng = dArrayAng acVec2dAng.Angle
'' Use the upper-left corner of the objects extents for the array base point
Dim acExts As Extents3d = acCirc.Bounds.GetValueOrDefault()
Dim acPt2dArrayBase As Point2d = New Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y)
'' Track the objects created for each column
Dim acDBObjCollCols As DBObjectCollection = New DBObjectCollection()
acDBObjCollCols.Add(acCirc)
'' Create the number of objects for the first column
Dim nColumnsCount As Integer = 1
While (nColumns > nColumnsCount)
Dim acEntClone As Entity = acCirc.Clone()
acDBObjCollCols.Add(acEntClone)
'' Caclucate the new point for the copied object (move)
Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase,
dArrayAng,
dColumnOffset * nColumnsCount)
Dim acVec2d As Vector2d = acPt2dArrayBase.GetVectorTo(acPt2dTo)
Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))
acBlkTblRec.AppendEntity(acEntClone)
acTrans.AddNewlyCreatedDBObject(acEntClone, True)
nColumnsCount = nColumnsCount 1
End While
'' Set a value in radians for 90 degrees
Dim dAng As Double = Math.PI / 2
'' Track the objects created for each row and column
Dim acDBObjCollLvls As DBObjectCollection = New DBObjectCollection()
For Each acObj As DBObject In acDBObjCollCols
acDBObjCollLvls.Add(acObj)
Next
'' Create the number of objects for each row
For Each acEnt As Entity In acDBObjCollCols
Dim nRowsCount As Integer = 1
While (nRows > nRowsCount)
Dim acEntClone As Entity = acEnt.Clone()
acDBObjCollLvls.Add(acEntClone)
'' Caclucate the new point for the copied object (move)
Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase,
dArrayAng dAng,
dRowOffset * nRowsCount)
Dim acVec2d As Vector2d = acPt2dArrayBase.GetVectorTo(acPt2dTo)
Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))
acBlkTblRec.AppendEntity(acEntClone)
acTrans.AddNewlyCreatedDBObject(acEntClone, True)
nRowsCount = nRowsCount 1
End While
Next
'' Save the new objects to the database
acTrans.Commit()
End Using
End Sub
CodePudding user response:
This error must be due to something else than the code, probably the general context (AutoCAD version, referenced libraries, ...). In any case, this code example from the AutoCad .Net Developers Guide is, as often, not so well written. In the case of an entity that we are sure has a valid Extend3d (a circle of radius 0.5), using Entity.GeometricExtents is much cheaper than using Drawable.Bounds.
Dim acExts As Extents3d = acCirc.GeometricExtents