169 lines
4.7 KiB
C#
169 lines
4.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.DB.Architecture;
|
|
using Autodesk.Revit.UI;
|
|
|
|
using Nice3point.Revit.Toolkit.External;
|
|
|
|
namespace GeologyToolkit
|
|
{
|
|
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
|
|
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
|
|
public class GenerateGeologyCmd : ExternalCommand
|
|
{
|
|
public override void Execute()
|
|
{
|
|
using var ts = new Transaction(Document, "生成地质体");
|
|
ts.Start();
|
|
{
|
|
try
|
|
{
|
|
var surfaces = UiDocument.Selection
|
|
.PickObjects(
|
|
Autodesk.Revit.UI.Selection.ObjectType.Element,
|
|
new SelectTopographySurface(),
|
|
"请选择地层面"
|
|
)
|
|
.Select(r => Document.GetElement(r))
|
|
.Cast<TopographySurface>();
|
|
var lib = DirectShapeLibrary.GetDirectShapeLibrary(Document);
|
|
var loops = new List<CurveLoop>();
|
|
foreach (var e in surfaces)
|
|
{
|
|
var boundpoints = e.GetBoundaryPoints();
|
|
var interiorpoints = e.GetInteriorPoints();
|
|
foreach (var p in interiorpoints)
|
|
{
|
|
double tempDistance = 10000;
|
|
XYZ first = null;
|
|
XYZ second = null;
|
|
for (var i = 0; i < boundpoints.Count; i++)
|
|
{
|
|
Line line;
|
|
if (i == boundpoints.Count - 1)
|
|
{
|
|
line = Line.CreateBound(boundpoints[i], boundpoints[0]);
|
|
var d = line.Distance(p);
|
|
if (d < tempDistance)
|
|
{
|
|
tempDistance = d;
|
|
first = boundpoints[0];
|
|
second = boundpoints[i];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
line = Line.CreateBound(boundpoints[i], boundpoints[i + 1]);
|
|
var d = line.Distance(p);
|
|
if (d < tempDistance)
|
|
{
|
|
tempDistance = d;
|
|
first = boundpoints[i];
|
|
second = boundpoints[i + 1];
|
|
}
|
|
}
|
|
}
|
|
|
|
boundpoints.Insert(boundpoints.IndexOf(second), p);
|
|
}
|
|
|
|
var curves = new List<Curve>();
|
|
for (var i = 0; i < boundpoints.Count; i++)
|
|
{
|
|
Line line;
|
|
var xyz = boundpoints[i];
|
|
|
|
if (i == boundpoints.Count - 1)
|
|
{
|
|
line = Line.CreateBound(xyz, boundpoints[0]);
|
|
}
|
|
else
|
|
{
|
|
var xyz1 = boundpoints[i + 1];
|
|
|
|
line = Line.CreateBound(xyz, xyz1);
|
|
}
|
|
|
|
curves.Add(line);
|
|
}
|
|
|
|
var loop = CurveLoop.Create(curves);
|
|
if (loop.IsOpen())
|
|
{
|
|
TaskDialog.Show("错误", "未闭合");
|
|
}
|
|
|
|
loops.Add(loop);
|
|
//var geoEle = e.get_Geometry(new Options());
|
|
//foreach (GeometryObject obj in geoEle)
|
|
//{
|
|
// //PolymeshTopology
|
|
|
|
// //if (obj is Mesh)
|
|
// //{
|
|
// // Mesh mesh = (Mesh)obj;
|
|
// // for (int i = 0; i < mesh.NumTriangles; i++)
|
|
// // {
|
|
// // var triangle = mesh.get_Triangle(i);
|
|
// // var point = triangle.get_Vertex(0);
|
|
// // var point1 = triangle.get_Vertex(1);
|
|
// // var point2 = triangle.get_Vertex(2);
|
|
// // }
|
|
// //}
|
|
|
|
// //CurveLoop.Create
|
|
// GeometryCreationUtilities.CreateLoftGeometry()
|
|
//}
|
|
}
|
|
|
|
var options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId);
|
|
var cylinder = GeometryCreationUtilities.CreateLoftGeometry(loops, options);
|
|
|
|
var shapeTypes = new FilteredElementCollector(Document)
|
|
.OfClass(typeof(DirectShapeType))
|
|
.OfCategory(BuiltInCategory.OST_GenericModel)
|
|
.WhereElementIsElementType();
|
|
var name = "地质";
|
|
var elems = shapeTypes.Where(t => t.Name == name);
|
|
var directShapeType =
|
|
elems.Count() == 0
|
|
? DirectShapeType.Create(Document, name, new ElementId(BuiltInCategory.OST_GenericModel))
|
|
: elems.FirstOrDefault() as DirectShapeType;
|
|
directShapeType.SetShape(new List<GeometryObject> { cylinder });
|
|
lib.AddDefinitionType("borehole", directShapeType.Id);
|
|
var cateId = Category.GetCategory(Document, BuiltInCategory.OST_Mass).Id;
|
|
var ds = DirectShape.CreateElementInstance(
|
|
Document,
|
|
directShapeType.Id,
|
|
cateId,
|
|
"地质",
|
|
Transform.Identity
|
|
);
|
|
ds.SetTypeId(directShapeType.Id);
|
|
//ds.ApplicationId = "Application id";
|
|
//ds.ApplicationDataId = "Geometry object id";
|
|
ds.SetShape(new GeometryObject[] { cylinder });
|
|
ds.SetName(name);
|
|
}
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|
{
|
|
if (ts.GetStatus() == TransactionStatus.Started)
|
|
{
|
|
ts.RollBack();
|
|
}
|
|
}
|
|
//catch (Exception)
|
|
//{
|
|
// if (ts.GetStatus() == TransactionStatus.Started)
|
|
// {
|
|
// ts.RollBack();
|
|
// }
|
|
//}
|
|
}
|
|
ts.Commit();
|
|
}
|
|
}
|
|
}
|