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

时间:2016-08-18 08:16:55

标签: c# autocad

这是我的代码,它在块中检索我的属性,但是我想在table中显示这个属性,其中attribute.tag是heading,而attribute.textstring是表中的内容。请给出相同的解决方案。< / p>

      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));
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

请查看此博文,了解如何在AutoCAD.Net中创建表格。

Trough the interface Table