Uy Võ Lê 5272 Uy Võ Lê

Nội dung buổi 10

  1. Tìm hiểu SaveFileDialog. 
  2. Tìm hiểu về class trong C#.
  3. Sử dụng LRE và thư viện EPPlus để xuất thông tin đối tượng sang Excel.

 


Link download thư viện LRE: https://leuyvo.com/getLRE

  • LRE bản 1.0.3 (29/08/2019)
  • Chọn phiên bản Revit bạn sử dụng tương ứng, hiện tại chỉ có Revit 2018. Tạm thời bạn có thể sử dụng bản 2018 này trên cả Revit 2019 và 2020.
  • Giải nén file .zip bạn có file *.dll, Add Reference vào dự án của bạn và thử sử dụng.
  • Add thư viện EPPlus theo hướng dẫn trong video.
  • Add thư viện System.Windows.Forms nếu chưa có, xem hướng dẫn trong video.

Code tham khảo

        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;

            IEnumerable<FamilyInstance> AllColumns = new FilteredElementCollector(doc, doc.ActiveView.Id)
                .WhereElementIsNotElementType()
                .OfClass(typeof(FamilyInstance))
                .OfCategory(BuiltInCategory.OST_StructuralColumns)
                .Cast<FamilyInstance>();

            if (AllColumns == null || AllColumns.Count() == 0)
            {
                message = "Không có cột kết cấu nào trong view hiện tại!";
                return Result.Failed;
            }


            IList<ToExcel> toExcels = new List<ToExcel>();

            foreach(FamilyInstance familyInstance in AllColumns)
            {
                double volume = familyInstance.get_Parameter(BuiltInParameter.HOST_VOLUME_COMPUTED).AsDouble();
                volume = Math.Round(volume * 0.3048 * 0.3048 * 0.3048, 3);

                toExcels.Add(new ToExcel(familyInstance.Name, volume));
            }


            ExcelExportSetting excelExportSettings = new ExcelExportSetting();

            excelExportSettings.PrefixFileName = "LUV_";
            excelExportSettings.SheetName = "Test Sheet Name";

            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Title = "Chọn nơi lưu file..";
                saveFileDialog.Filter = "Excel Files (*.xlsx)|*.xlsx";
                saveFileDialog.RestoreDirectory = true;

                if(saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    excelExportSettings.FileName = saveFileDialog.FileName;
                }
                else
                {
                    return Result.Cancelled;
                }
            }

            if(ExportToExcel.Export(toExcels, excelExportSettings))
            {
                MessageBox.Show("Đã xuất Excel thành công!");
            }
            else
            {
                MessageBox.Show("Lỗi: Xuất Excel không thành công!");
            }

            return Result.Cancelled;
        }

        private class ToExcel
        {
            public string Name { get; set; }
            public double Volume { get; set; }

            public ToExcel(string m_Name, double m_Volume)
            {
                Name = m_Name;
                Volume = m_Volume;
            }

            public ToExcel()
            {
            }
        }

Thư viện sử dụng

#region Namespaces
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using LUV_RevitAPI_Extensions;
using static LUV_RevitAPI_Extensions.ExportToExcel;
using Application = Autodesk.Revit.ApplicationServices.Application;
using System.Windows.Forms;
using MessageBox = System.Windows.Forms.MessageBox;
#endregion

 

 


Reply 2

Chào Võ,

Mình add thư viện LUV của bạn vào nhưng bị lỗi thế này:

Resolved file has a bad image, no metadata, or is otherwise inaccessible. Index not found. (Exception from HRESULT: 0x80131124) ..\LRE_2018\LUV_RevitAPI_Extensions_2018_1_0_4\LUV_RevitAPI_Extensions.dll 

 

Bạn xem giúp mình cái nhé.

Cám ơn
 

Văn

Uy Võ Lê Uy Võ Lê

Bạn nói rõ là lỗi khi sử dụng chức năng hay function nào để mình kiểm tra nhé.