整合自定义控件
This commit is contained in:
132
ShrlAlgoToolkit.RevitAddins/RvFamily/CutGeologyByLoopCmd.cs
Normal file
132
ShrlAlgoToolkit.RevitAddins/RvFamily/CutGeologyByLoopCmd.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Autodesk.Revit.DB;
|
||||
|
||||
using Nice3point.Revit.Toolkit.External;
|
||||
|
||||
|
||||
namespace Szmedi.RvKits.Civil
|
||||
{
|
||||
/// <summary>
|
||||
/// Revit执行命令
|
||||
/// </summary>
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
[Regeneration(RegenerationOption.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(
|
||||
ts =>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,58 +11,76 @@ namespace ShrlAlgoToolkit.RevitAddins.RvFamily;
|
||||
/// </summary>
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
[Regeneration(RegenerationOption.Manual)]
|
||||
public class ExplodeDwgCmd : ExternalCommand
|
||||
public class ExplodeDwgCmd : IExternalCommand
|
||||
{
|
||||
public override void Execute()
|
||||
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
||||
{
|
||||
try
|
||||
{
|
||||
var refer = UiDocument.Selection.PickObject(ObjectType.Element);
|
||||
|
||||
ExternalCommandData data = commandData;
|
||||
Document Document = data.Application.ActiveUIDocument.Document;
|
||||
UIDocument UiDocument = data.Application.ActiveUIDocument;
|
||||
if (!Document.IsFamilyDocument)
|
||||
{
|
||||
TaskDialog.Show("提示", "请在族文档中使用此命令");
|
||||
return Result.Cancelled;
|
||||
}
|
||||
//bool useFreeFrom = true;
|
||||
var refer = UiDocument.Selection.PickObject(ObjectType.Element, new GenericFilter<ImportInstance>(), "请选择导入图元");
|
||||
var element = Document.GetElement(refer);
|
||||
var geoElem = element.get_Geometry(new Options() { ComputeReferences = true, DetailLevel = ViewDetailLevel.Fine });
|
||||
if (Document.IsFamilyDocument)
|
||||
{
|
||||
var f = Document.OwnerFamily;
|
||||
Document.Invoke(
|
||||
ts =>
|
||||
var f = Document.OwnerFamily;
|
||||
Document.Invoke(
|
||||
ts =>
|
||||
{
|
||||
foreach (var geoObj in geoElem)
|
||||
{
|
||||
foreach (var geoObj in geoElem)
|
||||
if (geoObj is GeometryInstance instance)
|
||||
{
|
||||
if (geoObj is GeometryInstance instance)
|
||||
foreach (var obj in instance.GetInstanceGeometry())
|
||||
{
|
||||
foreach (var obj in instance.GetInstanceGeometry())
|
||||
|
||||
if (obj is Solid solid && solid.Faces.Size > 0 && solid.Volume > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (obj is Solid solid)
|
||||
{
|
||||
var directShape = DirectShape.CreateElement(Document, f.FamilyCategory.Id);
|
||||
directShape.SetShape(new GeometryObject[] { solid });
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
FreeFormElement.Create(Document, solid);//和分解的功能一样,仅保留几何信息
|
||||
|
||||
//try
|
||||
//{
|
||||
// // 如果是项目文档,只能使用DirectShape创建,适用于导入的几何体,不能进行二次编辑
|
||||
|
||||
// // 如果是族文档,使用FreeFormElement创建,可以有更多的几何体支持,支持编辑手柄,族文档也支持DirectShape
|
||||
// var shape = DirectShape.CreateElement(Document, f.FamilyCategory.Id);
|
||||
// shape.SetShape([solid]);
|
||||
//}
|
||||
//catch (Exception)
|
||||
//{
|
||||
// FreeFormElement.Create(Document, solid);//和分解的功能一样,仅保留几何信息
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
if (geoObj is Solid solid)
|
||||
{
|
||||
var directShape = DirectShape.CreateElement(Document, f.FamilyCategory.Id);
|
||||
directShape.SetShape(new GeometryObject[] { solid });
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Document.Delete(element.Id);
|
||||
});
|
||||
}
|
||||
else if (geoObj is Solid topSolid && topSolid.Faces.Size > 0 && topSolid.Volume > 0)
|
||||
{
|
||||
FreeFormElement.Create(Document, topSolid);//和分解的功能一样,仅保留几何信息
|
||||
|
||||
//try
|
||||
//{
|
||||
|
||||
// var directShape = DirectShape.CreateElement(Document, f.FamilyCategory.Id);
|
||||
// directShape.SetShape([topSolid]);
|
||||
|
||||
//}
|
||||
//catch (Exception)
|
||||
//{
|
||||
// FreeFormElement.Create(Document, topSolid);
|
||||
//}
|
||||
}
|
||||
}
|
||||
Document.Delete(element.Id);
|
||||
});
|
||||
|
||||
}
|
||||
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
||||
{
|
||||
@@ -72,5 +90,30 @@ public class ExplodeDwgCmd : ExternalCommand
|
||||
{
|
||||
TaskDialog.Show("Error", ex.Message);
|
||||
}
|
||||
return Result.Succeeded;
|
||||
}
|
||||
|
||||
private void ProcessGeometry(Document doc, Category category, GeometryElement geometryElement)
|
||||
{
|
||||
foreach (var geoObj in geometryElement)
|
||||
{
|
||||
if (geoObj is GeometryInstance instance)
|
||||
{
|
||||
// 递归处理嵌套的GeometryInstance
|
||||
ProcessGeometry(doc, category, instance.GetInstanceGeometry());
|
||||
}
|
||||
else if (geoObj is Solid solid && solid.Faces.Size > 0 && solid.Volume > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var shape = DirectShape.CreateElement(doc, category.Id);
|
||||
shape.SetShape([solid]);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user