sourceFileName = ed.GetFileNameForOpen("\nEnter the name of the coordinates file to be imported:"); if (sourceFileName.Status == PromptStatus.OK) { if (Path.GetExtension(sourceFileName.StringResult).Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase)) { try { using (db) { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
FileInfo fileInfo = new FileInfo(sourceFileName.StringResult); using (ExcelPackage package = new ExcelPackage(fileInfo)) { ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; int rowCount = worksheet.Dimension.Rows;
{
[CommandMethod("IMPORTCOORDS")]
public void ImportCoords()
{
DocumentCollection dm = Platform.ApplicationServices.Application.DocumentManager;
Database db = dm.MdiActiveDocument.Database;
Editor ed = dm.MdiActiveDocument.Editor;
PromptFileNameResult sourceFileName;
Matrix3d ucsMatrix = ed.CurrentUserCoordinateSystem;
sourceFileName = ed.GetFileNameForOpen("\nEnter the name of the coordinates file to be imported:");
if (sourceFileName.Status == PromptStatus.OK)
{
if (Path.GetExtension(sourceFileName.StringResult).Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
try
{
using (db)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
FileInfo fileInfo = new FileInfo(sourceFileName.StringResult);
using (ExcelPackage package = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
int rowCount = worksheet.Dimension.Rows;
for (int row = 2; row <= rowCount; row++)
{
string label = worksheet.Cells[row, 1].GetValue<string>();
double coordX = worksheet.Cells[row, 2].GetValue<double>();
double coordY = worksheet.Cells[row, 3].GetValue<double>();
double height = worksheet.Cells[row, 4].GetValue<double>();
DBPoint point = new DBPoint(new Point3d(coordX, coordY, height));
point.TransformBy(ucsMatrix.Inverse());
btr.AppendEntity(point);
tr.AddNewlyCreatedDBObject(point, true);
DBText text = new DBText
{
Position = new Point3d(coordX, coordY, height + 1),
Height = 5,
TextString = label,
HorizontalMode = TextHorizontalMode.TextCenter,
VerticalMode = TextVerticalMode.TextBase
};
btr.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
}
}
btr.Dispose();
tr.Commit();
}
}
}
catch (PlatformDb.Runtime.Exception ex)
{