【Revit】族及项目下创建拉伸,融合,放样
族:
Ps. 函数里面的ConvertToInternalUnits这个方法,是将毫米单位转成recit内部单位,这个转换可以自行转换,代码这边没有贴出来。
public static Element GetHostInstance(Document doc, CurveArrArray curveArrs, XYZ faceNormal, double start, double end, bool IsSolid = true) { Element element = null; #if Revit2018 element = doc.FamilyCreate.NewExtrusion(IsSolid, curveArrs, SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(faceNormal, new XYZ(start * faceNormal.X, start * faceNormal.Y, start * faceNormal.Z).ConvertToInternalUnits())), Math.Abs(end - start).ConvertToInternalUnits()); #elif Revit2019 element = doc.FamilyCreate.NewExtrusion(IsSolid, curveArrs, SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(faceNormal, new XYZ(start * faceNormal.X, start * faceNormal.Y, start * faceNormal.Z).ConvertToInternalUnits())), Math.Abs(end - start).ConvertToInternalUnits()); #else element = doc.FamilyCreate.NewExtrusion(IsSolid, curveArrs, SketchPlane.Create(doc, new Plane(faceNormal, new XYZ(start * faceNormal.X, start * faceNormal.Y, start * faceNormal.Z).ConvertToInternalUnits())), Math.Abs(end - start).ConvertToInternalUnits()); #endif return element; } //创建拉伸 public static Extrusion CreateExtrusion(Document doc, CurveArrArray curveArrs, XYZ faceNormal, double start, double end, bool IsSolid = true) { return GetHostInstance(doc, curveArrs, faceNormal, start, end, IsSolid) as Extrusion; } //创建融合 public static Blend CreateBlend(Document doc, CurveArray topCurves, CurveArray bottomCurves, XYZ faceNormal, double start, bool IsSolid = true) { var plane = CreateSketchPlane(doc, faceNormal, start); var blend = doc.FamilyCreate.NewBlend(IsSolid, topCurves, bottomCurves, plane); return blend; } //创建放样 public static Sweep CreateSweep(Document doc, CurveArrArray profileArray, XYZ start, XYZ end, bool IsSolid = true) { XYZ xvec = new XYZ(end.X - start.X, end.Y - start.Y, 0).Normalize(); XYZ yvec = XYZ.BasisZ; start = start.ConvertToInternalUnits(); end = end.ConvertToInternalUnits(); var plane = SketchPlane.Create(doc, CreatePlane(xvec, yvec, start)); SweepProfile profile = doc.Application.Create.NewCurveLoopsProfile(profileArray); Curve curve = Line.CreateBound(start, end); CurveArray curves = new CurveArray(); curves.Append(curve); return doc.FamilyCreate.NewSweep(IsSolid, curves, plane, profile, 0, ProfilePlaneLocation.Start); } public static SketchPlane CreateSketchPlane(Document doc, XYZ faceNormal, XYZ origin) { SketchPlane element = null; #if Revit2018 element = SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(faceNormal, origin)); #elif Revit2019 element = SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(faceNormal, origin)); #else element = SketchPlane.Create(doc, new Plane(faceNormal, origin)); #endif return element; } public static SketchPlane CreateSketchPlane(Document doc, XYZ faceNormal, double start) { SketchPlane element = null; #if Revit2018 element = SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(faceNormal, new XYZ(start * faceNormal.X, start * faceNormal.Y, start * faceNormal.Z).ConvertToInternalUnits())); #elif Revit2019 element = SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(faceNormal, new XYZ(start * faceNormal.X, start * faceNormal.Y, start * faceNormal.Z).ConvertToInternalUnits())); #else element = SketchPlane.Create(doc, new Plane(faceNormal, new XYZ(start * faceNormal.X, start * faceNormal.Y, start * faceNormal.Z).ConvertToInternalUnits())); #endif return element; }
补充PS:拉伸方向貌似和轮廓线的方向有关系。
项目:
先来一段项目文档下,拉伸的测试。复制可直接用(需要自己开事务):
public static void CreateCubicDirectShape(Autodesk.Revit.UI.UIApplication app) { List<Curve> profile = new List<Curve>(); double edgeLength = 2.0; XYZ p1 = new XYZ(edgeLength, 0, 0); XYZ p2 = new XYZ(edgeLength, edgeLength, 0); XYZ p3 = new XYZ(0, edgeLength, 0); XYZ p4 = new XYZ(0, 0, 0); profile.Add(Line.CreateBound(p1, p2)); profile.Add(Line.CreateBound(p2, p3)); profile.Add(Line.CreateBound(p3, p4)); profile.Add(Line.CreateBound(p4, p1)); CurveLoop curveLoop = CurveLoop.Create(profile); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); Solid cubic = GeometryCreationUtilities.CreateExtrusionGeometry(new CurveLoop[] { curveLoop }, XYZ.BasisZ, 10); // create direct shape and assign the sphere shape DirectShape ds = DirectShape.CreateElement(app.ActiveUIDocument.Document, new ElementId(BuiltInCategory.OST_Floors), app.ActiveAddInId.GetGUID().ToString(), ""); ds.AppendShape(new GeometryObject[] { cubic }); edgeLength = 3.0; p1 = new XYZ(edgeLength, 0, 0); p2 = new XYZ(edgeLength, edgeLength, 0); p3 = new XYZ(0, edgeLength, 0); p4 = new XYZ(0, 0, 0); profile = new List<Curve>(); profile.Add(Line.CreateBound(p1, p2)); profile.Add(Line.CreateBound(p2, p3)); profile.Add(Line.CreateBound(p3, p4)); profile.Add(Line.CreateBound(p4, p1)); var curveLoop2 = CurveLoop.Create(profile); var cubic2 = GeometryCreationUtilities.CreateExtrusionGeometry(new CurveLoop[] { curveLoop2 }, -XYZ.BasisZ, 5); ds.AppendShape(new GeometryObject[] { cubic2 }); }
以下是正式代码:
正式代码没有了。。。因为目前没有找到在项目下空心剪切的实现方法,那就没必要继续研究下去了。。
补充 2022-5-17
BooleanOperationsUtils 据说这个可以实现项目下空心剪切。还没试,有时间试试。