先谢谢大家,开发插件遇到的几个小问题,希望大神帮忙解决,谢谢!
本帖最后由 糕笔仔 于 2015-4-18 13:17 编辑1.加载了自己的程序,出现界面,打开DWG文件,随意点击DWG里的块参照。但是,怎么才能让插件和点击块参照有联系。。可能我解释的不清楚,上个图希望大家帮忙。 就是希望实现 选取了块参照 就show出面板上的panel
2.这段代码希望大家帮忙看看,我实在是解决不了了。就是希望添加块,添加信息,然后这些信息可以增删改。我用的datatable存储信息,不知道可不可以实现。。另外此代码只能运行成功一下,第二次就有对象错误问题(也就不能连续添加块记录),实在不会解决.代码如下,谢谢大家!万分感谢!
//输入此命令可运行
public void atat()
{
Writedata("男","25");
}
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, 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;
}
//写入数据
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();
}
}
}
//读取数据
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();
}
}
}
本帖最后由 糕笔仔 于 2015-4-14 22:30 编辑
在线等,希望大家可以帮帮忙,小弟能力有限,只能麻烦大家了~ 环境CAD2010+VS2008 数据放在字典没错
但一般都是存储成一条记录
像你这样放在datatable....应该是前所未闻的 如果数据比较简单 用扩展数据xdata也行 本帖最后由 糕笔仔 于 2015-4-15 00:11 编辑
雪山飞狐_lzh 发表于 2015-4-14 23:15 static/image/common/back.gif
如果数据比较简单 用扩展数据xdata也行
谢谢!!我看的书是深入浅出.net cad二次开发。 xdata只有三行文字的介绍,实在无力。可以先帮忙解决第一个问题吗?Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult i = ed.SelectPrevious();
if (i.Status == PromptStatus.OK)
{
this.panel1.Location = new Point(0,0);
this.panel1.Visible = true;
}
else
{
this.panel1.Location = new Point(-10000,-10000);
}
我在帖子里搜索到说用SelectPrevious,写了这个 ,不过运行之后没有显示出panel,另外 扩展数据xdata 是不是 -3?? 雪山飞狐_lzh 发表于 2015-4-14 23:13 static/image/common/back.gif
数据放在字典没错
但一般都是存储成一条记录
像你这样放在datatable....应该是前所未闻的
飞狐哥,我该怎么做呢?可以给个思路吗 我只是把那本“深入浅出”代码以及实例做了一遍,具体该怎么联系起来,没有一点思路 可以教教我吗? 其实要输入的数据全在块参照里,修改也在块参照,显示也是通过块参照,可以教我怎么做吗?谢谢! 建议你先把基础的东西搞清楚 而不是直接Copy代码
置顶帖的指南翻译版 你可以看看 里面的代码很多 雪山飞狐_lzh 发表于 2015-4-15 08:12 static/image/common/back.gif
建议你先把基础的东西搞清楚 而不是直接Copy代码
置顶帖的指南翻译版 你可以看看 里面的代码很多
好的,谢谢啊 雪山飞狐_lzh 发表于 2015-4-15 08:12 static/image/common/back.gif
建议你先把基础的东西搞清楚 而不是直接Copy代码
置顶帖的指南翻译版 你可以看看 里面的代码很多
谢谢飞狐哥,问题已经解决。不过第一个问题解决了一半,操作显得麻烦不通畅,暂时研究其他重要点的功能,谢谢你· 和界面关联据说用绑定是正解
页:
[1]