Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
213 lines (182 loc) · 7.98 KB
/
Copy pathProgram.cs
File metadata and controls
213 lines (182 loc) · 7.98 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.IO;
namespace RevitLines
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class PatternToModelLine : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
message = string.Empty;
// Verificar se a vista ativa é 2D
if (!(uiDoc.ActiveView is ViewPlan) && !(uiDoc.ActiveView is ViewSection))
{
message = "Por favor, selecione um elemento em uma vista 2D.";
return Result.Failed;
}
// Selecionar o objeto na vista 2D
Reference pickedRef = uiDoc.Selection.PickObject(ObjectType.Element, "Selecione um elemento");
Element element = doc.GetElement(pickedRef);
if (element == null)
{
message = "Elemento não encontrado.";
return Result.Failed;
}
// Pegar a posição do elemento
XYZ position = GetElementPosition(element);
if (position == null)
{
message = "Não foi possível obter a posição do elemento.";
return Result.Failed;
}
// Criar um arquivo DWG temporário
string tempDwgFilePath = Path.GetTempFileName().Replace(".tmp", ".dwg");
using (Transaction transaction = new Transaction(doc, "Exportar e Importar DWG"))
{
transaction.Start();
try
{
// Criar uma vista temporária
View tempView = CreateTemporaryView(doc, uiDoc.ActiveView, element);
// Exportar o elemento para o arquivo temporário
ExportToTemporaryDWG(doc, tempView, element, tempDwgFilePath);
// Importar o DWG temporário de volta para o Revit
ImportDwgToRevit(doc, tempDwgFilePath, position);
// Excluir a vista temporária
doc.Delete(tempView.Id);
}
catch (Exception ex)
{
message = $"Falha ao exportar ou importar o DWG: {ex.Message}";
transaction.RollBack(); // Reverter a transação em caso de falha
return Result.Failed;
}
finally
{
// Excluir o arquivo temporário após a importação
if (File.Exists(tempDwgFilePath))
{
File.Delete(tempDwgFilePath);
}
}
transaction.Commit();
}
return Result.Succeeded;
}
private XYZ GetElementPosition(Element element)
{
// Verifica se o elemento tem uma localização direta (LocationPoint ou LocationCurve)
if (element.Location != null)
{
LocationPoint locationPoint = element.Location as LocationPoint;
if (locationPoint != null)
{
return locationPoint.Point;
}
LocationCurve locationCurve = element.Location as LocationCurve;
if (locationCurve != null)
{
return locationCurve.Curve.GetEndPoint(0);
}
}
// Tenta obter a posição a partir da geometria do forro (se for um forro)
if (element.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Ceilings)
{
Options geomOptions = new Options { ComputeReferences = true };
GeometryElement geomElement = element.get_Geometry(geomOptions);
if (geomElement != null)
{
foreach (GeometryObject geomObj in geomElement)
{
Solid solid = geomObj as Solid;
if (solid != null && solid.Faces.Size > 0)
{
BoundingBoxXYZ bbox = solid.GetBoundingBox();
if (bbox != null)
{
XYZ center = (bbox.Min + bbox.Max) / 2;
return center;
}
}
}
}
}
// Se o elemento for uma família, tenta obter a localização da instância
FamilyInstance familyInstance = element as FamilyInstance;
if (familyInstance != null)
{
LocationPoint familyLocationPoint = familyInstance.Location as LocationPoint;
if (familyLocationPoint != null)
{
return familyLocationPoint.Point;
}
}
return null;
}
private View CreateTemporaryView(Document doc, View originalView, Element element)
{
// Duplicar a vista ativa como uma nova vista temporária
ViewDuplicateOption duplicateOption = ViewDuplicateOption.Duplicate;
View tempView = doc.GetElement(originalView.Duplicate(duplicateOption)) as View;
// Isolar o elemento na nova vista temporária
tempView.IsolateElementTemporary(element.Id); // Passando o ElementId diretamente
return tempView;
}
private void ExportToTemporaryDWG(Document doc, View view, Element element, string dwgFilePath)
{
DWGExportOptions options = new DWGExportOptions
{
FileVersion = ACADVersion.R2013,
ExportOfSolids = SolidGeometry.ACIS // Exportar sólidos
};
ICollection<ElementId> ids = new List<ElementId> { element.Id };
// Exportar o elemento selecionado como DWG a partir da vista temporária
doc.Export(Path.GetDirectoryName(dwgFilePath), Path.GetFileNameWithoutExtension(dwgFilePath), new List<ElementId> { view.Id }, options);
}
private void ImportDwgToRevit(Document doc, string dwgFilePath, XYZ position)
{
DWGImportOptions importOptions = new DWGImportOptions
{
Placement = ImportPlacement.Origin,
CustomScale = 1.0,
Unit = ImportUnit.Default
};
bool importSuccess = doc.Import(dwgFilePath, importOptions, doc.ActiveView, out ElementId importedElementId);
if (importSuccess && importedElementId != ElementId.InvalidElementId)
{
ImportInstance importInstance = doc.GetElement(importedElementId) as ImportInstance;
if (importInstance != null)
{
// Verificar se o elemento está fixado (pinned)
bool wasPinned = importInstance.Pinned;
if (wasPinned)
{
// Desfixar o elemento antes de movê-lo
importInstance.Pinned = false;
}
// Mover o DWG para a posição correta
ElementTransformUtils.MoveElement(doc, importInstance.Id, position);
if (wasPinned)
{
// Refixar o elemento após movê-lo
importInstance.Pinned = true;
}
}
else
{
throw new InvalidOperationException("Falha ao importar o arquivo DWG.");
}
}
else
{
throw new InvalidOperationException("Falha ao importar o arquivo DWG.");
}
}
}
}