C#根据数据画表并导出CAD 示例软件为Revit(其他软件原理也一样吧大概)

由于业务需求,需要从Revit视图中读取信息,并在视图上绘制一个表,并将该视图导出为CAD的dwg格式,由于刚学没多久,写的很烂,跟我一样的菜鸡看看就好,找找思路。

        private void ExportToCad_Click(object sender, RoutedEventArgs e)
        {
     
            SaveFileDialog saveFileDialog = new SaveFileDialog();
          
            saveFileDialog.Filter = "CAD文件(.DWG)|*.dwg";
            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
     

                Transaction ts = new Transaction(document, "创建图纸");
                Autodesk.Revit.DB.View view = document.ActiveView;
                //  List allPipe = new FilteredElementCollector(document, view.Id).OfCategory(BuiltInCategory.OST_PipeCurves).ToElements().ToList();
                List<MaterialModel> mtd = this.materialdata;
                try
                {
     
                    ts.Start();
                    //过滤视图平面
                    List<Element> views = new FilteredElementCollector(document).OfClass(typeof(ViewFamilyType)).ToElements().ToList();
                    List<ViewFamilyType> needviews = new List<ViewFamilyType>();
                    needviews = views.Cast<ViewFamilyType>().Where(i => i.ViewFamily == ViewFamily.FloorPlan).ToList();
                    //准备画表的线集合
                    List<RevitLine> lines = new List<RevitLine>();
                    //存放每列最大长度的List
                    List<double> columnMax = new List<double>();
                    //用于接收每列最大值

                    int NameMax, LengthMax, PipeSectionMax, NotesMax;
                    //存放列头坐标的List
                    List<XYZ> headerLocation = new List<XYZ>();

                    //设置单元格宽度初始值
                    NameMax = 1;
                    int CountMax = 3;
                    LengthMax = 1;
                    PipeSectionMax = 1;
                    NotesMax = 4;
                    //XYZ[,] LocalTable=  new XYZ[mtd.Count,5];
                    //遍历数组找最大
                    foreach (var m in mtd)
                    {
     
                        if (NameMax <= m.MaterailName.Length)
                        {
     
                            NameMax = m.MaterailName.Length;
                        }
                        if (LengthMax <= m.MaterailLength.ToString().Length)
                        {
     
                            LengthMax = m.MaterailLength.ToString().Length;
                        }
                        if (PipeSectionMax <= m.PipeSection.Length)
                        {
     
                            PipeSectionMax = m.PipeSection.Length;
                        }
                        if (m.Notes != null)
                        {
     
                            if (NotesMax <= m.Notes.Length)
                            {
     
                                NotesMax = m.Notes.Length;
                            }
                        }


                    }
                    //找完最大后 添加到保存最大的数组
                    columnMax.Add(NameMax / 2);
                    columnMax.Add(CountMax / 1.5);
                    columnMax.Add(LengthMax / 2);
                    columnMax.Add(PipeSectionMax / 2);
                    columnMax.Add(NotesMax / 1.5);

                    //因最后一条列线不需要再增加距离  故设置为0
                    columnMax.Add(0);
                    //创建视图

                    Level level = Level.Create(document, 1024);
                    ViewPlan floorView = ViewPlan.Create(document, needviews[0].Id, level.Id);
                    List<RevitLine> lines1 = new List<RevitLine>();
                    floorView.Name = "test10086";
                    //行数量  
                    int count = mtd.Count;

                    //设置一个TEMP 记录X轴坐标  每次遍历temp=temp+字符长度*固定值
                    double temp = 0;
                    //设置一个宽 为生成行线时使用
                    double width = (NameMax / 2 + CountMax / 1.5 + LengthMax / 2 + PipeSectionMax / 2 + NotesMax / 1.5) * 2.5;
                    //读字符长度 设置固定值*字符长度  每生成一个

                    //获取模型属性格式 设置生成的列数
                    var props = typeof(MaterialModel).GetProperties();
                    int NeedLineCount = props.Length + 1;

                    //创建列线

                    for (int i = 0; i < NeedLineCount; i++)
                    {
     

                        double ThisNeedWidth = columnMax[i] * 2.5;
                        XYZ start = new XYZ(temp, 2.5 * count + 2.5, 0);
                        XYZ end = new XYZ(temp, 0, 0);
                        Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(start, end);
                        lines1.Add(line);
                        temp = temp + ThisNeedWidth;
                        //temp等于temp+该列最长的长度

                    }
                    //创建行线
                    for (int i = 0; i < count + 2; i++)
                    {
     
                        double height = 2.5 * i;
                        XYZ start = new XYZ(0, height, 0);
                        XYZ end = new XYZ(width, height, 0);
                        Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(start, end);
                        lines1.Add(line);
                    }
                    //绘制线
                    foreach (Autodesk.Revit.DB.Line l in lines1)
                    {
     
                        DetailCurve detailLine = document.Create.NewDetailCurve(floorView, l);
                    }

                    // _document.Delete(level.Id);

                 

                    #region 向表中填充数据
                    ElementId defaultTextTypeId = document.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);
                    TextNoteOptions opts = new TextNoteOptions(defaultTextTypeId);
                    opts.HorizontalAlignment = HorizontalTextAlignment.Left;
                    //填充材料名
                    for (int i = 0; i < mtd.Count; i++)
                    {
     
                        TextNote note = TextNote.Create(document, floorView.Id, new XYZ(1, 2 + i * 2.5, 0), mtd[i].MaterailName, opts);
                        if (i == mtd.Count - 1)
                        {
     
                            TextNote note1 = TextNote.Create(document, floorView.Id, new XYZ(1, 2 + (i + 1) * 2.5, 0), "材料名称", opts);
                        }
                    }
                    //填表头

                    //填充材料数量
                    for (int i = 0; i < mtd.Count; i++)
                    {
     
                        TextNote note = TextNote.Create(document, floorView.Id, new XYZ(1 + NameMax / 2 * 2.5, 2 + i * 2.5, 0), mtd[i].MaterailCount.ToString(), opts);
                        if (i == mtd.Count - 1)
                        {
     
                            TextNote note1 = TextNote.Create(document, floorView.Id, new XYZ(1 + NameMax / 2 * 2.5, 2 + (i + 1) * 2.5, 0), "数量", opts);
                        }
                    }
                    // 填充材料长度
                    for (int i = 0; i < mtd.Count; i++)
                    {
     
                        TextNote note = TextNote.Create(document, floorView.Id, new XYZ(1 + NameMax / 2 * 2.5 + CountMax / 1.5 + LengthMax / 2, 2 + i * 2.5, 0), mtd[i].MaterailLength.ToString(), opts);
                        if (i == mtd.Count - 1)
                        {
     
                            TextNote note1 = TextNote.Create(document, floorView.Id, new XYZ(1 + NameMax / 2 * 2.5 + CountMax / 1.5 + LengthMax / 2, 2 + (i + 1) * 2.5, 0), "长度", opts);
                        }
                    }
                    //填充管段信息
                    for (int i = 0; i < mtd.Count; i++)
                    {
     

                        TextNote note = TextNote.Create(document, floorView.Id, new XYZ(1 + NameMax / 2 * 2.5 + CountMax / 1.5 + LengthMax / 2 + PipeSectionMax / 2, 2 + i * 2.5, 0), mtd[i].PipeSection.ToString(), opts); if (i == mtd.Count - 1)
                            if (i == mtd.Count - 1)
                            {
     
                                TextNote note1 = TextNote.Create(document, floorView.Id, new XYZ(1 + NameMax / 2 * 2.5 + CountMax / 1.5 + LengthMax / 2 + PipeSectionMax / 2, 2 + (i + 1) * 2.5, 0), "管段", opts);
                            }
                    }
                    // 填充注释信息
                    for (int i = 0; i < mtd.Count; i++)
                    {
     
                        if (mtd[i].Notes != null)
                        {
     
                            TextNote note = TextNote.Create(document, floorView.Id, new XYZ((NameMax / 2 + CountMax / 1.5 + LengthMax / 2 + PipeSectionMax / 2) * 2.5 + 0.5, 2 + i * 2.5, 0), mtd[i].Notes, opts);

                        }
                        if (i == mtd.Count - 1)
                        {
     
                            TextNote note1 = TextNote.Create(document, floorView.Id, new XYZ((NameMax / 2 + CountMax / 1.5 + LengthMax / 2 + PipeSectionMax / 2) * 2.5 + 0.5, 2 + (i + 1) * 2.5, 0), "注释", opts);
                        }

                    }
                    #endregion
                    if (exportToCad.ExportDWG(document, floorView, "SET 1.0", saveFileDialog.FileName) == true) {
     
                        System.Windows.Forms.MessageBox.Show("导出CAD成功");
                        document.Delete(level.Id);
                    
                    }
                }
                catch (Exception ex)
                {
     
                    if (ts.GetStatus() == TransactionStatus.Started)
                    {
     
                        ts.RollBack();
                    }
                    System.Windows.Forms.MessageBox.Show(ex.Message);

                }
                finally
                {
     
                    if (ts.GetStatus() == TransactionStatus.Started)
                    {
     
                        ts.Commit();
                    }
                }
            }
        }

导出CAD封装类下载:
点此下载

你可能感兴趣的:(C#,c#,cad,算法)