新手求帮忙块参照,翻了好几天的论坛也没找到办法(因实力有限)
本帖最后由 糕笔仔 于 2015-4-25 17:22 编辑新手求帮忙,解决两个问题 谢谢!
1.AttributeDefinitionad1 ad2 这两个属性的位置(添加第二个参照的时候,ad1 ad2 不能跟随块参照改变位置)
2. 如何另外添加 AttributeDefinition,求一个方法(希望调用另一个方法,将另一个AttributeDefinition ad3添加到指定参照)。
代码如下。
public void AddAttributeBlock()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
ObjectId id;
BlockTableRecord btr = new BlockTableRecord();
Point3d pt = Pick1("\n指定位置");
Line li = new Line(pt, new Point3d(0, 0, 0));
String cbf = GetString("\n承包方");
String dkbh = GetString("地块编号");
AttributeDefinition ad1 = AttributeDefinition("cbr", "承包方", cbf, new Point3d(pt.X + 3, pt.Y + 3, 0));
AttributeDefinition ad2 = AttributeDefinition("dkbh", "地块编号", dkbh, new Point3d(pt.X - 3, pt.Y - 3, 0));
btr.Name = "LineBlock";
btr.AppendEntity(li);
btr.AppendEntity(ad1);
btr.AppendEntity(ad2);
id = AddToBlockTable(btr);
ObjectId id1 = ToModelSpace(id, pt, db);
adKzsx(id1);
}
//创建属性
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)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database dbd = doc.Database;
ObjectId blkrfid = new ObjectId();
using (Transaction trans = dbd.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.ForWrite) 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;
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
BlockTable table = transaction.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
if (table.Has("LineBlock"))
{
id = table["LineBlock"];
}
else
{
id = table.Add(Record);
transaction.AddNewlyCreatedDBObject(Record, true);
transaction.Commit();
}
}
return id;
}
public void adKzsx(ObjectId id)
{
Document doc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DocumentLock doclock = doc.LockDocument();
Entity acent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
acent.ColorIndex = 1;
RegAppTable appTbl = trans.GetObject(db.RegAppTableId,
OpenMode.ForWrite) as RegAppTable;
if (!appTbl.Has("Qmap"))
{
RegAppTableRecord appTblRcd = new RegAppTableRecord();
appTblRcd.Name = "Qmap";
appTbl.Add(appTblRcd);
trans.AddNewlyCreatedDBObject(appTblRcd, true);
}
ResultBuffer resBuf = new ResultBuffer();
resBuf.Add(new TypedValue(1001, "Qmap"));//注册程序名称
resBuf.Add(new TypedValue(1000, "2015"));
acent.XData = resBuf;
trans.Commit();
doclock.Dispose();
}
}
attref.SetAttributeFromBlock(attdef, blkref.BlockTransform);
页:
[1]