- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Trying to insert multiple blocks at once using their name definition, coordinates, scale and rotation data from an excel. A windows form is created to prompt user to select the excel file then this snippet of code will insert the blocks, reading that file, Code works successfully but blocks are not visible on screen or they are not drawn. what are the necessary changes required in the code. It is able to successfully read excel data and convert the data in the required form but nothing appears on screen. Any help will be appreciated thank you.
public static void Blocks(string selectedFile)
{
Document document = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
Editor editor = document.Editor;
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
using (var stream = File.Open(selectedFile, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet dataset = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
});
var dataTable = dataset.Tables[0];
string columnHeader1 = dataTable.Columns[0].ColumnName;
string columnHeader2 = dataTable.Columns[1].ColumnName;
string columnHeader3 = dataTable.Columns[2].ColumnName;
string columnHeader4 = dataTable.Columns[3].ColumnName;
string columnHeader5 = dataTable.Columns[4].ColumnName;
if (columnHeader1 == "Name" && columnHeader2 == "x,y,z" && columnHeader3 == "Scale_X" && columnHeader4 == "Scale_Y" && columnHeader5 == "Rotation")
{
document.LockDocument();
BlockTable bt = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
for (int row = 2; row < dataTable.Rows.Count; row++)
{
string blockName = dataTable.Rows[row][0].ToString();
double scaleX = Convert.ToDouble(dataTable.Rows[row][2]);
double scaleY = Convert.ToDouble(dataTable.Rows[row][3]);
double rotation = Convert.ToDouble(dataTable.Rows[row][4]);
string coordinatesValue = dataTable.Rows[row][1].ToString();
string[] coordinatesArray = coordinatesValue.Split(',');
if (coordinatesArray.Length == 3)
{
double x = Convert.ToDouble(coordinatesArray[0]);
double y = Convert.ToDouble(coordinatesArray[1]);
double z = Convert.ToDouble(coordinatesArray[2]);
if (bt.Has(blockName))
{
// BlockTableRecord btr = transaction.GetObject(database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
using (BlockTableRecord btrc = new BlockTableRecord())
{
BlockReference br = new BlockReference(new Point3d(x, y, z), bt[blockName])
{
ScaleFactors = new Scale3d(scaleX, scaleY, 1.0),
Rotation = rotation,
};
transaction.GetObject(database.BlockTableId, OpenMode.ForWrite);
btrc.AppendEntity(br);
bt.Add(btrc);
// transaction.AddNewlyCreatedDBObject(br, true);
document.Editor.UpdateScreen();
editor.WriteMessage($"\nBlock {blockName} inserted {row} ");
}
}
else
{
editor.WriteMessage($"\nBlock '{blockName}' not found.");
return;
}
}
}
}
else
{
MessageBox.Show("The Excel file is not in the proper format. Please make sure the column headers are correct.", "Excel Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
transaction.Commit();
}
}
Solved! Go to Solution.