zhangyuekai
2024-08-08 1a0f5a002c0c513c8d48be3b382c1e01242642b3
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Service;
using System;
using System.IO;
using System.Linq;
using System.Windows;
 
namespace HStation.RevitDev.RevitDataExport.Entity
{
    public class ExternalEvent_CreateInstance : IExternalEventHandler
    {
        public string RfaPath { get; set; } = null;
 
        private bool m_isFirstTrigger = true;
 
        private UIApplication m_app = null;
 
        public void Execute(UIApplication app)
        {
            if (app == null || RfaPath == null || !File.Exists(RfaPath)) { return; }
            m_app = app;
            m_app.Idling += App_Idling;
            UIDocument uiDoc = app.ActiveUIDocument;
            Document doc = uiDoc.Document;
            Family family = null;
            FamilySymbol symbol = null;
 
            LoadFamilyFile(doc, ref family, ref symbol);
            if (family == null || symbol == null) { return; }
 
            PlacementFamilyInstance(uiDoc, symbol);
        }
 
        private void App_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
        {
            if (m_isFirstTrigger)
            {
                m_isFirstTrigger = false;
            }
            else
            {
                GlobalResource.PlacingType = Model.ModelEnum.RevitType.RFT_Unknown;
                m_app.Idling -= App_Idling;
                m_isFirstTrigger = true;
            }
        }
 
        private static void PlacementFamilyInstance(UIDocument uiDoc, FamilySymbol symbol)
        {
            if (uiDoc.CanPlaceElementType(symbol))
            {
                try
                {
                    uiDoc.PromptForFamilyInstancePlacement(symbol, new PromptForFamilyInstancePlacementOptions());
                }
                catch (Autodesk.Revit.Exceptions.OperationCanceledException e)
                {
                    return;
                }
            }
            else
            {
                MessageBox.Show("无法放置此类族!");
            }
        }
 
        private void LoadFamilyFile(Document doc, ref Family family, ref FamilySymbol symbol)
        {
            using (Transaction trans = new Transaction(doc, "创建族实例"))
            {
                trans.Start();
                try
                {
                    if (!doc.LoadFamily(RfaPath, new FamilyLoadOption(), out family))
                    {
                        //说明文档中已经存在, 需要搜索整个文档
                        var elems = SearchService.SearchElements(doc, typeof(Family), Path.GetFileNameWithoutExtension(RfaPath));
                        if (elems != null && elems.Count > 0)
                        {
                            family = elems[0] as Family;
                        }
                    }
                    if (family == null)
                    {
                        trans.Commit();
                        return;
                    }
 
                    var symbolId = family.GetFamilySymbolIds().FirstOrDefault();
                    symbol = doc.GetElement(symbolId) as FamilySymbol;
                    if (symbol == null)
                    {
                        trans.Commit();
                        return;
                    }
                    symbol.Activate();
                }
                catch (Exception ex)
                {
 
                }
                trans.Commit();
            }
        }
 
        public string GetName()
        {
            return "Create Family Instance";
        }
    }
 
    public class ExternalEvent_CreatePipe : IExternalEventHandler
    {
        UIApplication m_app = null;
        bool m_isFirstTrigger = true;
 
        public void Execute(UIApplication app)
        {
            m_app = app;
            app.Idling += App_Idling;
 
            RevitCommandId id = RevitCommandId.LookupPostableCommandId(PostableCommand.Pipe);
            if (m_app.CanPostCommand(id))
            {
                m_app.PostCommand(id);
            }
        }
 
        private void App_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
        {
            if (m_isFirstTrigger)
            {
                m_isFirstTrigger = false;
            }
            else
            {
                GlobalResource.PlacingType = Model.ModelEnum.RevitType.RFT_Unknown;
                m_app.Idling -= App_Idling;
                m_isFirstTrigger = true;
            }
        }
 
        public string GetName()
        {
            return "Create Pipe";
        }
    }
}