データシートはスプレッドシートのデータから定義され、塗りつぶされています。次に、別の関数では、そのデータテーブルにアクセスして、それらの値をテキストとして属性にキャストします。 DrawCircuits関数で同じデータテーブルをどうやって使うのですか?C#新しい関数でdatatableを使用する
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();
}
}
*** 8-18-2016更新! ヘルプを使用して、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();
}
}
フィールドとして追加、またはパラメータとして渡しいずれ。 – stuartd
私はパラメータとしてテーブルを渡そうとしましたが、動作させることができませんでした。私はDrawCircuits()関数がforループのため、パラメータとして渡すことが最良の方法かもしれません。あなたは私がこれをどうやって行うのかという例を持っていますか? – nebsi04
'public void DrawCircuits(DataTable dt、文字列名、ダブルx、ダブルy、ダブルz)' –