我有一个数据表,它是根据电子表格中的数据定义和填充的。然后,在另一个函数中,我想访问该数据表,将这些值作为文本转换为属性。如何在DrawCircuits功能中使用相同的数据表?
public static System.Data.DataTable ReadExcelToTable(string path)
{
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
System.Data.DataSet set = new DataSet();
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string firstSheetName = sheetsName.Rows[0][2].ToString();
string sql = string.Format("SELECT * FROM [{0}]", firstSheetName);
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
ada.Fill(set);
conn.Close();
}
return set.Tables[0];
}
//this command can insert a block and fill out attributes (text)
[CommandMethod("DrawCircuits")]
public void DrawCircuits(string name, double x, double y, double z)
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition
string blockName = name;
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open paper space - we'll be adding our BlockReference to it
BlockTableRecord ps =
bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForWrite)
as BlockTableRecord;
//Create new BlockReference, and link it to our block definition
Point3d point = new Point3d(x, y, z);
using (BlockReference blockRef =
new BlockReference(point, blockDef.ObjectId))
{
//Add the block reference to paper space
ps.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
// below is where I want to access the
// datatable loaded into memory in the other
// function. I want the att.Ref.TextString
// value to start from row 0 column 0 of the
// datatable
if (attRef.Tag == "TAG1")
{
attRef.TextString = "";
}
if (attRef.Tag == "XXX-NNNN+")
{
attRef.TextString = "";
}
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}
*** 2016年8月18日更新! 在帮助下,通过调用DrawCircuits()函数的参数,我可以在新函数中使用从Excel电子表格加载到内存中的数据表。我认为这是我想要实现的最佳方法。但是,我现在再一次尝试在另一个函数中使用这个数据表,我将所有我想要使用的东西捆绑在一起,Test3()。一切正常,attRef.TextString = dr.Table.Rows [1] [0] .ToString(); DrawCircuits()函数中的语句通过将数据表中的值填充为属性的字符串来正常工作。
我遇到的问题是,现在,在另一个函数Test3()中,我无法获得Test3()的for循环以使用数据表的下一行填充下一组属性。所有属性都使用数据表中的相同行值填充。我自己尝试了很多尝试。如果您有任何关于正确解决方案的想法,请告诉我。
[CommandMethod("Test3")]
public void Test3()
{
string Path = SelectSpreadsheet();
System.Data.DataTable table = ReadExcelToTable(Path);
InsertBlocks();
DrawModule("AI-1756-IF16H-SHEET1");
for (int i = 0; i < 8; i++)
{
DrawCircuits("AI-AIT-CIRCUIT", 24, 17 - (i * 1), 0, table.Rows[i]);
}
}
public static System.Data.DataTable ReadExcelToTable(string path)
{
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
System.Data.DataSet set = new DataSet();
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string firstSheetName = sheetsName.Rows[0][2].ToString();
string sql = string.Format("SELECT * FROM [{0}]", firstSheetName);
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
ada.Fill(set);
conn.Close();
}
return set.Tables[0];
}
[CommandMethod("DrawCircuits")]
public void DrawCircuits(string name, int x, int y, int z, System.Data.DataRow dr)
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition
string blockName = name;
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open paper space - we'll be adding our BlockReference to it
BlockTableRecord ps =
bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForWrite)
as BlockTableRecord;
//Create new BlockReference, and link it to our block definition
Point3d point = new Point3d(x, y, z);
using (BlockReference blockRef =
new BlockReference(point, blockDef.ObjectId))
{
//Add the block reference to paper space
ps.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
if (attRef.Tag == "TAG1")
{
attRef.TextString = dr.Table.Rows[1][0].ToString();
}
if (attRef.Tag == "XXX-NNNN+")
{
attRef.TextString = dr.Table.Rows[1][1].ToString();
}
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}
答案 0 :(得分:1)
public static System.Data.DataTable ReadExcelToTable(string path)
{
//Method codes
...
// Return the data table
return set.Tables[0];
}
public void DrawCircuits(string name, double x, double y, double z,DataTable dt)
{
...
attRef.TextString = dt.Rows[0][0];
...
}
public DataTable ChangeDt(DataTable dt)
{
// Change dt codes
...
// Return changed dt
return dt;
}
public void Use(DataTable dt)
{
var myDt = ChangeDt(dt);
for(i=0; i<=10; i++)
{
DrawCircuits("name", 1, 2, i, myDt);
}
}
或不带参数的Use
方法:
public void Use()
{
var dt = ReadExcelToTable("....The Path....");
var myDt = ChangeDt(dt);
for(i=0; i<=10; i++)
{
DrawCircuits("name", 1, 2, i, dt);
}
}