C# 在autocad中创建表格并根据块添加数据

C# 在autocad中创建表格并根据块添加数据,c#,autocad,C#,Autocad,这是我的代码,用于检索块中的属性,但我希望在表中显示此属性,其中attribute.tag为标题,attribute.textstring为表中的内容。请建议解决方案 namespace MyApplication { public class DumpAttributes { [CommandMethod("NLTAB")] public void ListAttributes() { Document

这是我的代码,用于检索块中的属性,但我希望在表中显示此属性,其中attribute.tag为标题,attribute.textstring为表中的内容。请建议解决方案

      namespace MyApplication
       {
      public class DumpAttributes
       {
    [CommandMethod("NLTAB")]
    public void ListAttributes()
    {
        Document acDoc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = acDoc.Editor;
        Database db = acDoc.Database;

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            // Start the transaction
            try
            {
                // Build a filter list so that only
                // block references with attributes are selected
                TypedValue[] filList = new TypedValue[2] { new TypedValue((int)DxfCode.Start, "INSERT"), new TypedValue((int)DxfCode.HasSubentities, 1) };
                SelectionFilter filter = new SelectionFilter(filList);
                PromptSelectionOptions opts = new PromptSelectionOptions();
                opts.MessageForAdding = "Select block references: ";
                PromptSelectionResult res = ed.GetSelection(opts, filter);
                // Do nothing if selection is unsuccessful
                if (res.Status != PromptStatus.OK)
                    return;

                SelectionSet selSet = res.Value;

                ObjectId[] idArray = selSet.GetObjectIds();

                PromptPointResult ppr;
                PromptPointOptions ppo = new PromptPointOptions("");
                ppo.Message = "\n Select the place for print output:";
                //get the coordinates from user
                ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK)
                    return;
                Point3d startPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
                Vector3d disp = new Vector3d(0.0, -2.0 * db.Textsize, 0.0);
                TextStyleTable ts = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                ObjectId mtStyleid = db.Textstyle;
                if (ts.Has("NAL-TEXT"))
                {
                    mtStyleid = ts["NAL-FORMAT"];
                }

                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                MText _outputHeading = new MText();

                _outputHeading.Location = startPoint;
                _outputHeading.Width = 75.207;
                _outputHeading.Height = 1.488;

                _outputHeading.TextStyleId = mtStyleid;
                string file = acDoc.Name;
                string str1 = Path.GetFileNameWithoutExtension(file);
                //string str1 = ("534-W10A-R1");
                //var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                Match match = Regex.Match(str1, @"^(\w+-[CSDWM]\d+[A-Z]-.)$");
                var pattern = @"^(\d+)-[A-Z](\d+)([A-Z])-";

                var groups = Regex.Match(str1, pattern).Groups;
                var _projectCode = groups[1].Value;
                var _phaseCode = _projectCode + "-" + groups[2].Value;
                var _zoneCode = _phaseCode + groups[3].Value;
                _outputHeading.Contents = "Project:" + _projectCode + "\n";
                curSpace.AppendEntity(_outputHeading);
                tr.AddNewlyCreatedDBObject(_outputHeading, true);
                db.TransactionManager.QueueForGraphicsFlush();

                startPoint += disp;
                HashSet<string> attValues = new HashSet<string>();

                Table tb = new Table();

                foreach (ObjectId blkId in idArray)
                {
                    BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite);

                    //ed.WriteMessage("\nBlock: " + btr.Name);

                    AttributeCollection attCol = blkRef.AttributeCollection;
                    foreach (ObjectId attId in attCol)
                    {
                        AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
                        string str = (attRef.TextString);
                        string tag = attRef.Tag;
                        //ed.WriteMessage("\n" + str);
                        if (attValues.Contains(str))
                            continue;
                        if (btr.Name == "NAL-TAG crs ref")
                        {
                            if (ts.Has("NAL-TEXT"))
                            {
                                mtStyleid = ts["NAL-FORMAT"];
                            }
                            var curSpace1 = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                            MText mtext = new MText();
                            mtext.TextStyleId = mtStyleid;

                            mtext.Location = startPoint;

                            mtext.Contents = tag + " : " + str + "\n";

                            //ed.WriteMessage(text);
                            curSpace.AppendEntity(mtext);
                            tr.AddNewlyCreatedDBObject(mtext, true);
                            db.TransactionManager.QueueForGraphicsFlush();

                            attValues.Add(str);

                            startPoint += disp;
                        }
                    }
                }

                tr.Commit();
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage(("Exception: " + ex.Message));
            }
        }
    }
}
名称空间MyApplication
{
公共类转储属性
{
[命令方法(“NLTAB”)]
公共空列表属性()
{
Document acDoc=Application.DocumentManager.MdiActiveDocument;
Editor ed=acDoc.Editor;
数据库db=acDoc.Database;
使用(Transaction tr=db.TransactionManager.StartTransaction())
{
//启动事务
尝试
{
//构建一个过滤器列表,以便
//将选择具有属性的块参照
TypedValue[]filList=newtypedValue[2]{newtypedValue((int)DxfCode.Start,“INSERT”),newtypedValue((int)DxfCode.HasSubentities,1)};
SelectionFilter=新建SelectionFilter(填充列表);
PromptSelectionOptions opts=新的PromptSelectionOptions();
opts.MessageForAdding=“选择块引用:”;
PromptSelectionResult=ed.GetSelection(选项、过滤器);
//如果选择不成功,则不执行任何操作
if(res.Status!=PromptStatus.OK)
返回;
SelectionSet selSet=分辨率值;
ObjectId[]idArray=selSet.getObjectId();
提示点结果ppr;
PromptPointOptions ppo=新的PromptPointOptions(“”);
ppo.Message=“\n选择打印输出的位置:”;
//从用户处获取坐标
ppr=ed.GetPoint(ppo);
如果(ppr.Status!=PromptStatus.OK)
返回;
Point3d startPoint=ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
Vector3d显示=新Vector3d(0.0,-2.0*db.Textsize,0.0);
TextStyleTable ts=(TextStyleTable)tr.GetObject(db.TextStyleTableId,OpenMode.ForRead);
ObjectId mtStyleid=db.Textstyle;
如果(ts.Has(“NAL-TEXT”))
{
mtStyleid=ts[“NAL格式”];
}
var curSpace=(BlockTableRecord)tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite);
多行文字输出=新多行文字();
_输出。位置=起始点;
_输出标题。宽度=75.207;
_输出授权。高度=1.488;
_outputhreading.TextStyleId=mtStyleid;
字符串文件=acDoc.Name;
字符串str1=Path.GetFileNameWithoutExtension(文件);
//字符串str1=(“534-W10A-R1”);
//var curSpace=(BlockTableRecord)tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite);
Match Match=Regex.Match(str1,@“^(\w+-[CSDWM]\d+[A-Z]-)$”;
变量模式=@“^(\d+)-[A-Z](\d+)([A-Z])-”;
var groups=Regex.Match(str1,pattern).groups;
var\u projectCode=组[1]。值;
变量_phaseCode=_projectCode+“-”+组[2]。值;
var _zoneCode=_phaseCode+组[3]。值;
_outputHeading.Contents=“项目:+\u项目代码+”\n”;
curSpace.AppendEntity(_输出授权);
tr.addNewlyCreatedBobObject(_输出授权,true);
db.TransactionManager.QueueForGraphicsFlush();
startPoint+=disp;
HashSet attValues=新HashSet();
Table tb=新表();
foreach(位于爱达理的ObjectId blkId)
{
BlockReference blkRef=(BlockReference)tr.GetObject(blkId,OpenMode.ForRead);
BlockTableRecord btr=(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord,OpenMode.ForWrite);
//ed.WriteMessage(“\n块:+btr.Name”);
AttributeCollection attCol=blkRef.AttributeCollection;
foreach(attCol中的ObjectId attId)
{
AttributeReference attRef=(AttributeReference)tr.GetObject(attId,OpenMode.ForRead);
字符串str=(attRef.TextString);
string tag=attRef.tag;
//ed.WriteMessage(“\n”+str);
if(attValues.Contains(str))
继续;
如果(btr.Name==“NAL-TAG crs ref”)
{
如果(ts.Has(“NAL-TEXT”))
{
mtStyleid=ts[“NAL格式”];
}
var curSpace1=(BlockTableRecord)tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite);
多行文字多行文字=新多行文字();
mtext.TextStyleId=mtStyleid;
多行文字位置=起始点;
mtext.Contents=tag+“:“+str+”\n”;
//ed.书面信息(文本);
curSpace.AppendEntity(多行文字);
tr.addNewlyCreatedBobObject(多行文字,true);
db.TransactionManager.QueueForGraphicsFlush();
attValues.Add(str);
startPoint+=disp;
}
}
}
tr.Commit();
}
捕捉(Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage((“例外:+ex.Message”);
}
}
}
}

}

查看此博客文章,了解如何在AutoCAD.Net中创建表