其他分享
首页 > 其他分享> > Revit几何操作集合

Revit几何操作集合

作者:互联网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace ConsoleApp2
{

class Program
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]

public class Class1 : IExternalCommand
{
private void GetAndTransformSolidInfo(Element element, Options geoOptions)
{
// Get geometry element of the selected element
Autodesk.Revit.DB.GeometryElement geoElement = element.get_Geometry(geoOptions);

// Get geometry object
foreach (GeometryObject geoObject in geoElement)
{
// Get the geometry instance which contains the geometry information
Autodesk.Revit.DB.GeometryInstance instance = geoObject as Autodesk.Revit.DB.GeometryInstance;
if (null != instance)
{
foreach (GeometryObject instObj in instance.SymbolGeometry)
{
Solid solid = instObj as Solid;
if (null == solid || 0 == solid.Faces.Size || 0 == solid.Edges.Size)
{
continue;
}

Transform instTransform = instance.Transform;
// Get the faces and edges from solid, and transform the formed points
foreach (Face face in solid.Faces)
{
Mesh mesh = face.Triangulate();
foreach (XYZ ii in mesh.Vertices)
{
XYZ point = ii;
XYZ transformedPoint = instTransform.OfPoint(point);
TaskDialog.Show("0", "0");
}
}
foreach (Edge edge in solid.Edges)
{
foreach (XYZ ii in edge.Tessellate())
{
XYZ point = ii;
XYZ transformedPoint = instTransform.OfPoint(point);
}
}
}
}
}

}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

try
{
int[,,] Blank = new int[10, 10, 10];
for (int ii = 0; ii < 10; ii++)
{
for (int jj = 0; jj < 10; jj++)
{
for (int kk = 0; kk < 10; kk++)
{
Blank[ii, jj, kk] = 0;
}
}
}
//TaskDialog.Show("Revit",Blank[1,1,1].ToString());
double Xmin = 0; double Ymin = 0; double Xmax = 0; double Ymax=0;double Zmax = 0;double Zmin = -1;
int fc = 0;
//Options options2 = new Options();
//options2.DetailLevel = ViewDetailLevel.Fine;
//options2.ComputeReferences = true;
var doc = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector elems = new FilteredElementCollector(doc);
//获取墙几何信息
ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wall));
//int i = 0;
var walls = elems.WherePasses(elementClassFilter);
TaskDialog.Show("提示", "start");
Reference reference = commandData.Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element);
Element element = doc.GetElement(reference);
//GetAndTransformSolidInfo(element,options2);
GeometryElement geometryElement2 = element.get_Geometry(new Options());
//获取XYZ的最大、最小值
ElementArray elementArray = new ElementArray();
foreach (var elem in elems)
{
GeometryObject geoobj = elem.get_Geometry(new Options());

}
foreach (var elem in elems)
{
GeometryObject geoobj = elem.get_Geometry(new Options());
Solid solid = geoobj as Solid;
BoundingBoxXYZ solidBox = solid.GetBoundingBox();
Transform trf = solidBox.Transform;
XYZ max = solidBox.Max;
XYZ min = solidBox.Min;
XYZ maxInModelCoords = trf.OfPoint(max);
XYZ minInModelCoords = trf.OfPoint(min);


double x = 0; double y = 0;double z = -1000; //xy坐标值

//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("Revit", message);

}
foreach (var elem in elems)
{
GeometryObject geoobj = elem.get_Geometry(new Options());
Solid solid3 = geoobj as Solid;
if (solid3 != null)
{
FaceArray faceArray = solid3.Faces;
foreach ( Face face in faceArray)
{
TaskDialog.Show("Area", face.Area.ToString());
if (face is PlanarFace)
{
TaskDialog.Show("2","w");
var pf = face as PlanarFace;
//通过pf即可得到线,进而得到点,以下忽略;
IList<CurveLoop> lines =pf.GetEdgesAsCurveLoops();
foreach (var curves in lines)
{
TaskDialog.Show("5", "5");
}

}

}
//怎么判断小方块是不是在空间内
double XRange = Xmax - Xmin;
double YRange = Ymax - Ymin;
double ZRange = Zmax - Zmin;//等分为1000份
double Xstep = XRange / 10;
double Ystep = YRange / 10;
double Zstep = ZRange / 10;
//先计算一个小方格,而后套入for循环中
//初始化三维数组
int xx = 0; int yy = 0;int zz = 0;
for (double x = Xmin; x < Xmax; x+=Xstep)
{
for (double y = Ymin; y < Ymax; y+=Ystep)
{
for (double z = Zmin; z < Zmax; z+=Zstep)
{
Transaction transaction = new Transaction(doc,"start");
transaction.Start();
XYZ xyz1 = new XYZ(x, y, z);
XYZ xyz2 = new XYZ(x+Xstep, y, z);
XYZ xyz3 = new XYZ(x+Xstep, y+Ystep, z);
XYZ xyz4 = new XYZ(x, y + Ystep, z);
Line line1 = Line.CreateBound(xyz1, xyz2);
Line line2 = Line.CreateBound(xyz2, xyz3);
Line line3 = Line.CreateBound(xyz3, xyz4);
Line line4 = Line.CreateBound(xyz4, xyz1);
//
CurveLoop curveArr = new CurveLoop();
curveArr.Append(line1);
curveArr.Append(line2);
curveArr.Append(line3);
curveArr.Append(line4);
List<CurveLoop> curves = new List<CurveLoop>();
curves.Add(curveArr);
//创建z方向
XYZ vector = new XYZ(0, 0, 1);
Solid solidXYZ = GeometryCreationUtilities.CreateExtrusionGeometry(curves,vector,Zstep);

//判断solid是否和全部元素相交
FilteredElementCollector elements1 = new FilteredElementCollector(doc);
ElementIntersectsSolidFilter solidFilter = new ElementIntersectsSolidFilter(solidXYZ);
if (elements1.WherePasses(solidFilter)!=null)
{
//将三维数组标为1
Blank[xx, yy, zz] = 1;
TaskDialog.Show("Revit", "三维数组标为1");
//Blank[xx, yy, zz] = 1;
TaskDialog.Show("Revit",xx.ToString());
//Blank[xx, yy, zz] = 1;
TaskDialog.Show("Revit",yy.ToString());
TaskDialog.Show("Revit", zz.ToString());
TaskDialog.Show("Revit",Blank[xx,yy,zz].ToString());
}
//delete,via Bool method
Solid solidNull = BooleanOperationsUtils.ExecuteBooleanOperation(solidXYZ, solidXYZ, BooleanOperationsType.Intersect);

TaskDialog.Show("annotion", Blank[2, 1, 2].ToString());
zz++;

transaction.Commit();
}
yy++;
}
xx++;
}


}
}
TaskDialog.Show("提示", "seletion");

if (element is Wall)
{
var wall0 = element as Wall;
var wallGeo = wall0.get_Geometry(new Options());
TaskDialog.Show("提示", wallGeo.ToString());
GeometryObject wallGeoobj = wallGeo as GeometryObject;
TaskDialog.Show("提示", wallGeoobj.ToString());
Autodesk.Revit.DB.GeometryInstance instance = wallGeoobj as Autodesk.Revit.DB.GeometryInstance;
TaskDialog.Show("提示", instance.ToString());

GeometryObject geometryObject= element.GetGeometryObjectFromReference(reference);
GeometryElement geometryElement0 = element.get_Geometry(new Options());
var obj=geometryElement0 as GeometryObject;
Solid solid1 = obj as Solid;
//TaskDialog.Show("提示", solid1.ToString());
foreach (var s in solid1.Faces)
{
TaskDialog.Show("提示", "Edges");
}

List<PlanarFace> planarFaces = new List<PlanarFace>();

 

// TaskDialog.Show("提示", "Wall");

if (geometryObject is GeometryElement)
{

//GeometryElement geometryElement = geometryObject as GeometryElement;
Solid solid = geometryObject as Solid;
if (solid==null)
{
TaskDialog.Show("提示", "Null");
}
//TaskDialog.Show("提示", solid.ToString());
TaskDialog.Show("提示", "FamilyInstance");
}
}
//元素几何选项
Options options1 = new Options();
options1.DetailLevel = ViewDetailLevel.Fine;
options1.ComputeReferences = true;
GeometryElement geometryElement1 = element.get_Geometry(options1);
foreach ( GeometryObject geometryObject in geometryElement1)
{
TaskDialog.Show("提示", "Geoobject");
if (geometryObject is GeometryInstance)
{
TaskDialog.Show("提示", "YYY");
GeometryInstance geometryInstance = geometryObject as GeometryInstance;
GeometryElement geometryElement = geometryInstance.GetSymbolGeometry();
foreach (GeometryObject geometryObject1 in geometryElement)
{
if (geometryObject1 is Solid)
{
TaskDialog.Show("提示", "Solid");
Solid solid2 = geometryObject1 as Solid;
foreach (Face face in solid2.Faces)
{
if (face is PlanarFace)
{
PlanarFace face1 = face as PlanarFace;
TaskDialog.Show("提示", "Face");
}
}
}
}
}
}

foreach (var wall in walls)
{
Options options = new Options();
options.ComputeReferences = true;
options.DetailLevel = ViewDetailLevel.Fine;
//TaskDialog.Show("提示", wall.ToString());
FamilyInstance familyInstance = wall as FamilyInstance;
GeometryElement geometryElement = familyInstance.get_Geometry(options);
//TaskDialog.Show("提示", "1");
foreach (var geomobj in geometryElement)
{
//TaskDialog.Show("提示", wall.ToString());
GeometryInstance geometryInstance = geomobj as GeometryInstance;
if (null==geometryInstance)
{
TaskDialog.Show("提示", wall.get_Geometry(options).ToString());
}
if (null!=geometryInstance)
{
TaskDialog.Show("提示", "6");
foreach (var instobj in geometryInstance.SymbolGeometry)
{
Solid solid = instobj as Solid;
TaskDialog.Show("提示", "3");
if (null==solid||0==solid.Faces.Size||0==solid.Edges.Size)
{
TaskDialog.Show("提示", "1");
continue;
}
Transform transform = geometryInstance.Transform;
//从solid获取边或面,然后变换点
TaskDialog.Show("提示", "2");
foreach (Edge e in solid.Edges)
{

foreach ( XYZ xYZ in e.Tessellate())
{
XYZ point = xYZ;
double x = 0; double y = 0; //xy坐标值
x = xYZ.X;y = xYZ.Y;
double z = xYZ.Z;
XYZ xYZ1 = new XYZ(x,y,z);
//xmin,xmax,ymin,ymax
if (x>Xmax)
{
Xmax = x;
TaskDialog.Show("x=", x.ToString());
}
if (x<Xmin)
{
Xmin = x;
}
if (y>Ymax)
{
Ymax = y;
}
if (y<Ymin)
{
Ymin = y;
}
UV uV = new UV(x, y);
foreach (var face in solid.Faces)
{
var f = face as PlanarFace;

bool v = f.IsInside(uV,out IntersectionResult intersectionResult);
if (v is true)
{
//将2D方格涂黑
double xx = x;
double yy = y;
TaskDialog.Show("x=", x.ToString());
TaskDialog.Show("y=", y.ToString());
break;
}

}
}
}
}
}
}

}

return Result.Succeeded;
}
catch (Exception)
{

return Result.Failed;
//throw;
}
//throw new NotImplementedException();
}
}
}
}

 

标签:Show,TaskDialog,XYZ,double,foreach,几何,集合,new,Revit
来源: https://www.cnblogs.com/3118460692wwx/p/15196635.html