赞
踩
public void AddBlockAttr() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction tr = db.TransactionManager.StartTransaction()) { // 获取选择对象 PromptSelectionResult psr = ed.GetSelection(); if (psr.Status != PromptStatus.OK) { ed.WriteMessage("没有选择任何块。"); return; } SelectionSet ss = psr.Value; foreach (SelectedObject selObj in ss) { // 获取块参照对象 BlockReference blockRef = tr.GetObject(selObj.ObjectId, OpenMode.ForWrite) as BlockReference; if (blockRef == null) { ed.WriteMessage("选择的对象不是块参照。"); continue; } // 添加自定义属性 AttributeDefinition attDef = new AttributeDefinition(); attDef.Position = blockRef.Position; attDef.Tag = "MyCustomProperty"; //YourAttributeName" attDef.Prompt = "请输入自定义属性值:"; //"Enter a value for the attribute:"; attDef.TextString = "12345"; //Default value attDef.Height = 2.5; attDef.Justify = AttachmentPoint.MiddleCenter; attDef.AlignmentPoint = blockRef.Position; attDef.Layer = "0"; attDef.ColorIndex = 256; // 将属性添加到块参照对象的属性集合中 AttributeReference attRef = new AttributeReference(); attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform); blockRef.AttributeCollection.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } tr.Commit(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。