Revit计算各类空间构件总范围

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Mechanical;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;

namespace ClassLibrary1
{

[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]

public class Class1 : IExternalCommand
{

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
UIApplication uIApplication = commandData.Application;
UIDocument uIDocument = uIApplication.ActiveUIDocument;
Document doc = uIDocument.Document;
FilteredElementCollector elems = new FilteredElementCollector(doc);
TaskDialog.Show("获取最大最小值", "start");

String ExcelPath = "D:\\各类项目\\海岛地震论文资料\\excel点";//file address
Application app = new Application();
app.Visible = true;
//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);

Workbooks wbs = app.Workbooks;
_Workbook wb = wbs.Add(ExcelPath);
Sheets shs = wb.Sheets;
//_Worksheet worksheet1 = wb.Sheets[1];
_Worksheet worksheet2 = wb.Sheets[1];
TaskDialog.Show("Excel","excel");
//elems.WherePasses(new ElementClassFilter(typeof(Wall)));
FilteredElementCollector elemswall =elems.WherePasses(new ElementClassFilter(typeof(Wall)));
//FilteredElementCollector elemsbeam = elems.WherePasses(new ElementClassFilter(typeof(BeamSystem)));
//FilteredElementCollector elemsfloor = elems.WherePasses(new ElementClassFilter(typeof(Floor)));
//获取XYZ的最大、最小值
//ElementArray elementArray = new ElementArray();
//TaskDialog.Show("获取最大最小值", "start0");
XYZ Vmin =default; XYZ Vmax=default; int i = 0; int j = 0; int k = 0;
double Xmin = 0; double Ymin = 0; double Xmax = 0; double Ymax = 0; double Zmax = 0; double Zmin = 0;
foreach (var item in elemswall)//找到所有的墙的范围
{

TaskDialog.Show("item", item.Id.ToString());
GeometryElement geoobj = item.get_Geometry(new Options());
foreach (var geo in geoobj)
{
i++;
GeometryObject geometryObject = geo as GeometryObject;
Solid solid = geometryObject as Solid;
TaskDialog.Show("item0", solid.ComputeCentroid().ToString());

BoundingBoxXYZ solidBox = solid.GetBoundingBox();

TaskDialog.Show("item1", item.Id.ToString());
Transform trf = solidBox.Transform;
XYZ max = solidBox.Max;
XYZ min = solidBox.Min;
XYZ maxInModelCoords = trf.OfPoint(max);
XYZ minInModelCoords = trf.OfPoint(min);

//worksheet1.Activate();
TaskDialog.Show("wall", max.X.ToString());

/*
//只适用于各墙、梁、板、柱平行于坐标轴的情况
for (j = 1; j < 10; j++)
{
if (j==1)
{

worksheet1.Cells[i, j] = solid.ComputeCentroid().X;//中心点
}
if (j==2)
{
worksheet1.Cells[i, j] = solid.ComputeCentroid().Y;//中心点
}
if (j==3)
{
worksheet1.Cells[i, j] = solid.ComputeCentroid().Z;//中心点
}
if (j==4)
{
worksheet1.Cells[i, j] = max.X;
}
if (j==5)
{
worksheet1.Cells[i, j] = max.Y;
}
if (j==6)
{
worksheet1.Cells[i, j] = max.Z;
}
if (j==7)
{
worksheet1.Cells[i, j] = min.X;
}
if (j==8)
{
worksheet1.Cells[i, j] = min.Y;
}
if (j==9)
{
worksheet1.Cells[i, j] = min.Z;
}

}


*/
//xmin,xmax,ymin,ymax
if (maxInModelCoords.X > Xmax)
{
Xmax = maxInModelCoords.X;
TaskDialog.Show("x=", Xmax.ToString());
}
if (minInModelCoords.X < Xmin)
{
Xmin = minInModelCoords.X;
}
if (maxInModelCoords.Y > Ymax)
{
Ymax = maxInModelCoords.Y;
}
if (minInModelCoords.Y < Ymin)
{
Ymin = minInModelCoords.Y;
}
if (maxInModelCoords.Z > Zmax)
{
Zmax = maxInModelCoords.Z;
}
if (minInModelCoords.Z < Zmin)
{
Zmin = minInModelCoords.Z;
}
/*
message += "\nView has an active section box: ";
message += "\n‘Maximum‘ coordinates: " + maxInModelCoords;
message += "\n‘Minimum‘ coordinates: " + minInModelCoords;
TaskDialog.Show("获取极值点", message);
*/
}

}
worksheet2.Activate();
message += "极值点: ";
message += "\n‘Maximum‘ coordinatesZ: " + Zmax;
message += "\n‘Minimum‘ coordinatesZ: " + Zmin;
message += "\n‘Minimum‘ coordinatesX: " + Xmin;
message += "\n‘Maximum‘ coordinatesX: " + Xmax;
message += "\n‘Minimum‘ coordinatesY: " + Ymin;
message += "\n‘Maximum‘ coordinates:Y " + Ymax;
TaskDialog.Show("极值点", message);

worksheet2.Cells[1, 1] = Xmax;
worksheet2.Cells[1, 2] = Ymax;
worksheet2.Cells[1, 3] = Zmax;
worksheet2.Cells[1, 4] = Xmin;
worksheet2.Cells[1, 5] = Ymin;
worksheet2.Cells[1, 6] = Zmin;

/*
foreach (var item in elemsbeam)//找到所有梁的范围
{

TaskDialog.Show("item", item.Id.ToString());
GeometryElement geoobj = item.get_Geometry(new Options());
foreach (var geo in geoobj)
{
GeometryObject geometryObject = geo as GeometryObject;
Solid solid = geometryObject as Solid;
TaskDialog.Show("item0", solid.ComputeCentroid().ToString());

BoundingBoxXYZ solidBox = solid.GetBoundingBox();

TaskDialog.Show("item1", item.Id.ToString());
Transform trf = solidBox.Transform;
XYZ max = solidBox.Max;
XYZ min = solidBox.Min;
XYZ maxInModelCoords = trf.OfPoint(max);
XYZ minInModelCoords = trf.OfPoint(min);

TaskDialog.Show("wall", max.X.ToString());

//xmin,xmax,ymin,ymax
if (maxInModelCoords.X > Xmax)
{
Xmax = maxInModelCoords.X;
TaskDialog.Show("x=", Xmax.ToString());
}
if (minInModelCoords.X < Xmin)
{
Xmin = minInModelCoords.X;
}
if (maxInModelCoords.Y > Ymax)
{
Ymax = maxInModelCoords.Y;
}
if (minInModelCoords.Y < Ymin)
{
Ymin = minInModelCoords.Y;
}
if (maxInModelCoords.Z > Zmax)
{
Zmax = maxInModelCoords.Z;
}
if (minInModelCoords.Z < Zmin)
{
Zmin = minInModelCoords.Z;
}
message += "\nView has an active section box: ";
message += "\n‘Maximum‘ coordinates: " + maxInModelCoords;
message += "\n‘Minimum‘ coordinates: " + minInModelCoords;
TaskDialog.Show("获取极值点", message);
}

}
foreach (var item in elemsfloor)//找到所有的板的范围
{

TaskDialog.Show("item", item.Id.ToString());
GeometryElement geoobj = item.get_Geometry(new Options());
foreach (var geo in geoobj)
{
GeometryObject geometryObject = geo as GeometryObject;
Solid solid = geometryObject as Solid;
TaskDialog.Show("item0", solid.ComputeCentroid().ToString());

BoundingBoxXYZ solidBox = solid.GetBoundingBox();

TaskDialog.Show("item1", item.Id.ToString());
Transform trf = solidBox.Transform;
XYZ max = solidBox.Max;
XYZ min = solidBox.Min;
XYZ maxInModelCoords = trf.OfPoint(max);
XYZ minInModelCoords = trf.OfPoint(min);

TaskDialog.Show("wall", max.X.ToString());

//xmin,xmax,ymin,ymax
if (maxInModelCoords.X > Xmax)
{
Xmax = maxInModelCoords.X;
TaskDialog.Show("x=", Xmax.ToString());
}
if (minInModelCoords.X < Xmin)
{
Xmin = minInModelCoords.X;
}
if (maxInModelCoords.Y > Ymax)
{
Ymax = maxInModelCoords.Y;
}
if (minInModelCoords.Y < Ymin)
{
Ymin = minInModelCoords.Y;
}
if (maxInModelCoords.Z > Zmax)
{
Zmax = maxInModelCoords.Z;
}
if (minInModelCoords.Z < Zmin)
{
Zmin = minInModelCoords.Z;
}
message += "\nView has an active section box: ";
message += "\n‘Maximum‘ coordinates: " + maxInModelCoords;
message += "\n‘Minimum‘ coordinates: " + minInModelCoords;
TaskDialog.Show("获取极值点", message);
}

}

 

 

*/
wb.Save();
//wb.Close();
return Result.Succeeded;
}
catch (Exception)
{
message = "unexpected errors";
return Result.Failed;
//throw;
}
//throw new NotImplementedException();
}
}
}

Revit计算各类空间构件总范围

上一篇:Data7.27核桃的数量


下一篇:从零开始制作一个粒子系统