我正在尝试导入Excel工作表,因为我们有块属性。我要求通过命令导入工作表,然后我的C#代码将通过工作表获取块属性值,并在Autocad中设计块,保存在系统上的绘图。
static public void InsertingABlock(System.Data.DataTable dt)
{
double _xCoord =0;
double _yCoord = 0;
double _zCoord = 0;
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("CircleBlock1"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";
//= Convert.ToInt32(dt.Rows[i]["columnname"]);
// Set the insertion point for the block
if (dt.Rows.Count > 0)
{
DataRow row = dt.Rows[0];
_xCoord = double.Parse(row["Position X"].ToString());
_yCoord = double.Parse(row["Position Y"].ToString());
_zCoord = double.Parse(row["Position Z"].ToString());
}
acBlkTblRec.Origin = new Point3d(_xCoord, _yCoord, _zCoord);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(800, 300, 0);
acCirc.Radius = 10;
acBlkTblRec.AppendEntity(acCirc);
acBlkTbl.UpgradeOpen();
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = acBlkTbl["CircleBlock"];
}
// Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
}