zhangyuekai
2024-07-01 0e3464b6adf776686e66bb3189441ab1a65a088e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using MFire;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
 
namespace HStation.RevitDev.RevitToBimface.Forms
{
    public partial class UploadFile : System.Windows.Forms.Form
    {
        private UIDocument _uiDocument;
        private Document _doc;
        private string bimAppKey = "";
        private string bimAppSecret = "";
 
        private long step = 0;
        public UploadFile(ExternalCommandData data)
        {
            _uiDocument = data.Application.ActiveUIDocument;
            _doc = _uiDocument.Document;
            InitializeComponent();
 
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(Do);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(DoChanged);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DoCompleted);
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void Do(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                backgroundWorker1.ReportProgress(i * 10);
                Thread.Sleep(int.Parse(step.ToString()));
            }
        }
 
        private void DoChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            label1.Text = "正在上传中... " + e.ProgressPercentage + "%";
            label1.Refresh();
        }
 
        private void DoCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            label1.Text = "上传完成!";
            label1.Refresh();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "正在上传中...";
            label2.Text = "";
            textBox1.Text = "";
            textBox1.Visible = false;
            var info = new FileInfo(_doc.PathName);
            step = info.Length / 1024 / 1024 / 10 * 100;
            backgroundWorker1.RunWorkerAsync();
 
            var dir = "d:\\Rvt" + DateTime.Now.ToString("yyyyMM");
            var newFilePath = dir + _doc.PathName.Substring(_doc.PathName.LastIndexOf("\\"));
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            if (File.Exists(newFilePath))
                File.Delete(newFilePath);
            File.Copy(_doc.PathName, newFilePath);
 
            var client = BimfaceClient.GetClient(bimAppKey, bimAppSecret);
 
            var uploadResult = client.UploadFilePolicy(newFilePath);
            if (uploadResult != null && uploadResult.status == "success")
            {
 
                label2.Text = "模型上传成功,请将此字符串复制到业务系统中:";
                textBox1.Text = uploadResult.fileId.Value.ToString();
                textBox1.Visible = true;
 
                File.Delete(newFilePath);
                var transResult = client.TranslateRvt(uploadResult.fileId.Value, MFire.Bimface.eRvtTranslateStyle.Real);
                if (transResult != null && transResult.status == "success")
                {
 
                }
            }
        }
    }
}