-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.cs
121 lines (81 loc) · 3.89 KB
/
test.cs
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
#region Namespaces
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
using System.Numerics;
#endregion
namespace RevitTest
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public static UIDocument _activeRevitUIDoc { get; private set; }
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
FamilySymbol fs = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).First(q => q.Name.Equals("Family1")) as FamilySymbol;
Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).ToElements().First() as Level;
Options opt = new Options();
IList<Reference> linkModelRefs = uidoc.Selection.PickObjects(ObjectType.LinkedElement, "Select Elements");
using (Transaction t = new Transaction(doc, "Copy Linked Elements"))
{
t.Start();
foreach (var linkModelRef in linkModelRefs) {
var e = doc.GetElement(linkModelRef.ElementId);
RevitLinkInstance revitLinkInst = e as RevitLinkInstance;
Document linkRvtDoc = (e as RevitLinkInstance).GetLinkDocument();
Transform transf = revitLinkInst.GetTransform();
Element eLinked = linkRvtDoc.GetElement(linkModelRef.LinkedElementId);
GeometryElement fiGeometry = eLinked.get_Geometry(opt);
List<XYZ> points = new List<XYZ>();
foreach (GeometryObject geoObj in fiGeometry)
{
if (geoObj is Solid){
TaskDialog.Show("R", geoObj.ToString());
List<Face> allFaces = new List<Face>();
Solid solid = geoObj as Solid;
foreach (Face face in solid.Faces)
{
allFaces.Add(face);
foreach (var v in face.Triangulate().Vertices) {
points.Add(v);
}
}
}
}
XYZ centroid = XYZ.Zero;
foreach (XYZ point in points) {
centroid += point;
}
centroid /= points.Count;
XYZ direction = points[1] - points[0];
for(int i = 0; i < 100; i++){
XYZ nextDirection = XYZ.Zero;
foreach (XYZ point in points) {
XYZ centeredPoint = point - centroid;
double dp = direction.DotProduct(centeredPoint);
nextDirection += dp * centeredPoint;
}
nextDirection.Normalize();
direction = nextDirection;
}
FamilyInstance dsOrigin = doc.Create.NewFamilyInstance(centroid, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
FamilyInstance dsDirection = doc.Create.NewFamilyInstance(transf.OfPoint(direction), fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
TaskDialog.Show("R", string.Format("{0},{1},{2}",centroid.X, centroid.Y, centroid.Z));
}
t.Commit();
}
return Result.Succeeded;
}
}//close class
}