调整代码
This commit is contained in:
124
ShrlAlgoToolkit.RevitAddins/FamMaster/CutGeologyByLoopCmd.cs
Normal file
124
ShrlAlgoToolkit.RevitAddins/FamMaster/CutGeologyByLoopCmd.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using System.Windows;
|
||||
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Autodesk.Revit.DB;
|
||||
|
||||
using Nice3point.Revit.Toolkit.External;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.FamMaster;
|
||||
|
||||
/// <summary>
|
||||
/// 剪切内建模型
|
||||
/// </summary>
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
public class CutGeologyByLoopCmd : ExternalCommand
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
try
|
||||
{
|
||||
var curveElements = UiDocument.Selection
|
||||
.PickElementsByRectangle(new GenericFilter<CurveElement>(), "请框选闭合的模型线或符号线")
|
||||
.Cast<CurveElement>();
|
||||
var curvesSelected = curveElements.Select(x => x.GeometryCurve).ToList();
|
||||
if(curvesSelected.Count < 3)
|
||||
{
|
||||
MessageBox.Show("请至少选择三条线", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
var curveList = SpatialAssist.GroupContinuousCurves(curvesSelected);
|
||||
List<CurveLoop> curveLoops = new List<CurveLoop>();
|
||||
foreach(var curves in curveList)
|
||||
{
|
||||
var loop = CurveLoop.Create(curves);
|
||||
if(loop.IsOpen())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
curveLoops.Add(loop);
|
||||
}
|
||||
|
||||
if(curveLoops.Count == 0)
|
||||
{
|
||||
MessageBox.Show("未能构成闭合环", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
if(curveLoops.Count > 1)
|
||||
{
|
||||
MessageBox.Show("存在多个闭合环,请确保只选择一个闭合环", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
var solidInstances = UiDocument.Selection
|
||||
.PickElementsByRectangle(
|
||||
new FuncFilter(
|
||||
e => e is FamilyInstance instance && instance.Symbol.Family.IsInPlace),
|
||||
"请选择一个实体族")
|
||||
.Cast<FamilyInstance>()
|
||||
.ToList();
|
||||
//var solidInstances = solidReferences.Select(x => x as FamilyInstance).ToList();
|
||||
var solidCut = GeometryCreationUtilities.CreateExtrusionGeometry(curveLoops, new XYZ(0, 0, 1), 100000);
|
||||
//StringBuilder sb = new StringBuilder();
|
||||
|
||||
Document.Invoke(
|
||||
_ =>
|
||||
{
|
||||
foreach(var instance in solidInstances)
|
||||
{
|
||||
var geomElem = instance.get_Geometry(new Options());
|
||||
foreach(var geometryObject in geomElem)
|
||||
{
|
||||
if(geometryObject is GeometryInstance geomInst)
|
||||
{
|
||||
var instanceGeoms = geomInst.GetInstanceGeometry();
|
||||
foreach(var geom in instanceGeoms)
|
||||
{
|
||||
if(geom is Solid solid)
|
||||
{
|
||||
var solidResult = BooleanOperationsUtils.ExecuteBooleanOperation(
|
||||
solid,
|
||||
solidCut,
|
||||
BooleanOperationsType.Intersect);
|
||||
var shape = Document.CreateDirectShapeInstance(
|
||||
instance.Name,
|
||||
BuiltInCategory.OST_GenericModel,
|
||||
[ solidResult ]);
|
||||
Document.Regenerate();
|
||||
|
||||
var materialId = shape.GetMaterialIds(false).FirstOrDefault();
|
||||
|
||||
var subGeoElem = shape.get_Geometry(new Options());
|
||||
foreach(var ins in subGeoElem)
|
||||
{
|
||||
if(ins is Solid subSolid)
|
||||
{
|
||||
foreach(Face item in subSolid.Faces)
|
||||
{
|
||||
try
|
||||
{
|
||||
Document.Paint(shape.Id, item, materialId);
|
||||
} catch(Exception)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//shape.GetMaterialIds()
|
||||
}
|
||||
}
|
||||
}
|
||||
//var geomInst = geometryObject.GetInstanceGeometry();
|
||||
}
|
||||
}
|
||||
},
|
||||
"剪切几何");
|
||||
//MessageBox.Show(sb.ToString());
|
||||
} catch(Autodesk.Revit.Exceptions.OperationCanceledException)
|
||||
{
|
||||
} catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user