- 积分
- 162
- 明经币
- 个
- 注册时间
- 2015-4-14
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 糕笔仔 于 2015-4-18 13:17 编辑
1.加载了自己的程序,出现界面,打开DWG文件,随意点击DWG里的块参照。但是,怎么才能让插件和点击块参照有联系。。可能我解释的不清楚,上个图希望大家帮忙。 就是希望实现 选取了块参照 就show出面板上的panel
2.这段代码希望大家帮忙看看,我实在是解决不了了。就是希望添加块,添加信息,然后这些信息可以增删改。我用的datatable存储信息,不知道可不可以实现。。另外此代码只能运行成功一下,第二次就有对象错误问题(也就不能连续添加块记录),实在不会解决.代码如下,谢谢大家!万分感谢!
[CommandMethod("atat")] //输入此命令可运行
public void atat()
{
Writedata("男","25");
}
[CommandMethod("AddAttributeBlock")]
public void AddAttributeBlock(String cbf,string dkbh)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId id;
BlockTableRecord btr = new BlockTableRecord();
Point3d pt = Pick1("\n指定位置");
//String cbf = GetString("\n承包方");
//String dkbh = GetString("地块编号");
Line li = new Line(pt,new Point3d(0,0,0));//new Point3d(30, 30, 0)
// Line li = new Line();
// PromptEntityResult ent;
// DBText txt = DBText(pt, str, 5);
AttributeDefinition ad1 = AttributeDefinition("Length", "L", cbf, new Point3d(pt.X + 3, pt.Y + 3, 0));
AttributeDefinition ad2 = AttributeDefinition("Length", "L", dkbh, new Point3d(pt.X - 3, pt.Y - 3, 0));
btr.Name = "LineBlock";
btr.AppendEntity(li);
btr.AppendEntity(ad1);
btr.AppendEntity(ad2);
// Writedata(str1,str2);
id =AddToBlockTable(btr);
ToModelSpace(id, pt, db);
}
//创建属性
public static AttributeDefinition AttributeDefinition(string label, string prompt, string value, Point3d pt)
{
AttributeDefinition ad = new AttributeDefinition();
ad.Constant = false;
ad.Tag = label;
ad.Prompt = prompt;
ad.TextString = value;
ad.Position = pt;
return ad;
}
//将指定的块定义变成块参照添加到指定的模型空间
public static ObjectId ToModelSpace(ObjectId blkid, Point3d pt, Database db)
{
ObjectId blkrfid = new ObjectId();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord modelspace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
BlockTableRecord block = trans.GetObject(blkid, OpenMode.ForRead) as BlockTableRecord;
BlockReference br = new BlockReference(pt, blkid);//通过块定义 添加块参照
blkrfid = modelspace.AppendEntity(br);//把块参照添加到块表记录
trans.AddNewlyCreatedDBObject(br, true);
foreach (ObjectId id in block)
{
if (id.ObjectClass.Equals(RXClass.GetClass(typeof(AttributeDefinition))))
{
AttributeDefinition ad = trans.GetObject(id, OpenMode.ForRead) as AttributeDefinition;
AttributeReference ar = new AttributeReference(ad.Position, ad.TextString, ad.Tag, new ObjectId());
br.AttributeCollection.AppendAttribute(ar);
}
}
trans.Commit();
}
return blkrfid;
}
//将块表记录加入到块表中
public static ObjectId AddToBlockTable(BlockTableRecord Record)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId id = new ObjectId();
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
BlockTable table = transaction.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
//BlockTableRecord btr = transaction.GetObject(table.);
if (table.Has("LineBlock"))
{
id = table["LineBlock"];
return id;
/* BlockTableRecord btr = new BlockTableRecord();//这几行可以继续添加块参照,但是快参照是空白。什么都没有
btr.Name = "aaa";
id = table.Add(btr);
transaction.AddNewlyCreatedDBObject(btr,true);
transaction.Commit();
return id;*/
}
id = table.Add(Record);
transaction.AddNewlyCreatedDBObject(Record, true);
transaction.Commit();
}
return id;
}
[CommandMethod("WriteData")]//写入数据
public void Writedata(String fbfbmstr, String cbfbmstr)
{
Autodesk.AutoCAD.DatabaseServices.DataTable dt = new Autodesk.AutoCAD.DatabaseServices.DataTable();
dt.TableName = "ParameterTable";
dt.AppendColumn(CellType.CharPtr, "姓名");
dt.AppendColumn(CellType.CharPtr, "地址");
dt.AppendColumn(CellType.CharPtr, "性别");
dt.AppendColumn(CellType.CharPtr, "年龄");
DataCellCollection Row = new DataCellCollection();
DataCell cbf = new DataCell();
DataCell dkbh = new DataCell();
DataCell fbfbm = new DataCell();
DataCell cbfbm = new DataCell();
string xm = GetString("\n姓名");
string dz = GetString("\n地址");
AddAttributeBlock(xm, dz);
cbf.SetString(xm);
dkbh.SetString(dz);
fbfbm.SetString(fbfbmstr);
cbfbm.SetString(cbfbmstr);
// Parameter.SetString(GetString("\n输入尺寸"));
Row.Add(cbf);
Row.Add(dkbh);
Row.Add(fbfbm);
Row.Add(cbfbm);
// Row.Add(Parameter);
dt.AppendRow(Row, true);
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityResult ent = ed.GetEntity("\n选择要写数据的对象");
if (ent.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity entity = tr.GetObject(ent.ObjectId, OpenMode.ForWrite, true) as Entity;
if (entity.ExtensionDictionary == new ObjectId())
entity.CreateExtensionDictionary();
DBDictionary extensionDic = tr.GetObject(entity.ExtensionDictionary, OpenMode.ForWrite, false) as DBDictionary;
extensionDic.SetAt("ParameterTable", dt);
tr.AddNewlyCreatedDBObject(dt, true);
tr.Commit();
}
}
}
[CommandMethod("ReadData")] //读取数据
public void ReadData()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Entity entity = null;
PromptEntityResult ent = ed.GetEntity("\n选择要读取数据的对象");
if (ent.Status == PromptStatus.OK)
{
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
entity = transaction.GetObject(ent.ObjectId, OpenMode.ForRead, true) as Entity;
DBDictionary extensionDic = transaction.GetObject(entity.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;
Autodesk.AutoCAD.DatabaseServices.DataTable dt = transaction.GetObject(extensionDic.GetAt("ParameterTable"), OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.DataTable;
ed.WriteMessage("\n Name:" + dt.GetCellAt(0, 0).Value.ToString());
ed.WriteMessage("\n Meterial:" + dt.GetCellAt(0, 1).Value.ToString());
ed.WriteMessage("\n Parameter:" + dt.GetCellAt(0, 2).Value.ToString());
Autodesk.AutoCAD.DatabaseServices.DataColumn nameColumn = dt.GetColumnAt(0);
Autodesk.AutoCAD.DatabaseServices.DataColumn meterialColumn = dt.GetColumnAt(1);
Autodesk.AutoCAD.DatabaseServices.DataColumn parameterColumn = dt.GetColumnAt(2);
ed.WriteMessage("\n Meterial:" + meterialColumn.GetCellAt(0).ToString());
ed.WriteMessage("\n Meterial:" + parameterColumn.GetCellAt(0).Value.ToString());
transaction.Commit();
}
}
}
|
|