2025-12-23 21:35:54 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
|
|
using Autodesk.Revit.Attributes;
|
2025-07-11 09:20:23 +08:00
|
|
|
|
using Autodesk.Revit.DB;
|
|
|
|
|
|
using Autodesk.Revit.UI.Selection;
|
|
|
|
|
|
|
2026-02-22 20:03:42 +08:00
|
|
|
|
using ShrlAlgoToolkit.RevitAddins.RvFamily;
|
|
|
|
|
|
using ShrlAlgoToolkit;
|
|
|
|
|
|
using ShrlAlgoToolkit.RevitAddins;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ShrlAlgoToolkit.RevitAddins.FamMaster;
|
2025-07-11 09:20:23 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 炸开族中的dwg或嵌套族图元
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Transaction(TransactionMode.Manual)]
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public class ExplodeDwgCmd : IExternalCommand
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2025-12-23 21:35:54 +08:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
2025-07-11 09:20:23 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-12 23:08:54 +08:00
|
|
|
|
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>(), "请选择导入图元");
|
2025-07-11 09:20:23 +08:00
|
|
|
|
var element = Document.GetElement(refer);
|
|
|
|
|
|
var geoElem = element.get_Geometry(new Options() { ComputeReferences = true, DetailLevel = ViewDetailLevel.Fine });
|
2025-08-12 23:08:54 +08:00
|
|
|
|
var f = Document.OwnerFamily;
|
2026-02-12 21:29:00 +08:00
|
|
|
|
Document.InvokeGroup(
|
|
|
|
|
|
tg =>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
{
|
2026-02-12 21:29:00 +08:00
|
|
|
|
Document.Invoke(
|
|
|
|
|
|
_ =>
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2026-02-12 21:29:00 +08:00
|
|
|
|
foreach (var geoObj in geoElem)
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2026-02-12 21:29:00 +08:00
|
|
|
|
if (geoObj is GeometryInstance instance)
|
2025-07-31 20:12:24 +08:00
|
|
|
|
{
|
2026-02-12 21:29:00 +08:00
|
|
|
|
foreach (var obj in instance.GetInstanceGeometry())
|
2025-12-23 21:35:54 +08:00
|
|
|
|
{
|
2026-02-12 21:29:00 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (obj is Solid solid && solid.Faces.Size > 0 && solid.Volume > 10e-6)
|
|
|
|
|
|
{
|
|
|
|
|
|
FreeFormElement.Create(Document, solid);//和分解的功能一样,仅保留几何信息
|
|
|
|
|
|
|
2025-12-23 21:35:54 +08:00
|
|
|
|
|
2026-02-12 21:29:00 +08:00
|
|
|
|
//try
|
|
|
|
|
|
//{
|
|
|
|
|
|
// // 如果是项目文档,只能使用DirectShape创建,适用于导入的几何体,不能进行二次编辑
|
|
|
|
|
|
|
|
|
|
|
|
// // 如果是族文档,使用FreeFormElement创建,可以有更多的几何体支持,支持编辑手柄,族文档也支持DirectShape
|
|
|
|
|
|
// var shape = DirectShape.CreateElement(Document, f.FamilyCategory.Id);
|
|
|
|
|
|
// shape.SetShape([solid]);
|
|
|
|
|
|
//}
|
|
|
|
|
|
//catch (Exception)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// FreeFormElement.Create(Document, solid);//和分解的功能一样,仅保留几何信息
|
|
|
|
|
|
//}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.AppendLine(ex.Message);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (geoObj is Solid topSolid && topSolid.Faces.Size > 0 && topSolid.Volume > 10e-6)
|
|
|
|
|
|
{
|
|
|
|
|
|
FreeFormElement.Create(Document, topSolid);//和分解的功能一样,仅保留几何信息
|
2025-12-23 21:35:54 +08:00
|
|
|
|
|
2025-08-12 23:08:54 +08:00
|
|
|
|
|
2025-12-23 21:35:54 +08:00
|
|
|
|
//try
|
|
|
|
|
|
//{
|
2025-08-12 23:08:54 +08:00
|
|
|
|
|
2026-02-12 21:29:00 +08:00
|
|
|
|
// var directShape = DirectShape.CreateElement(Document, f.FamilyCategory.Id);
|
|
|
|
|
|
// directShape.SetShape([topSolid]);
|
|
|
|
|
|
|
2025-12-23 21:35:54 +08:00
|
|
|
|
//}
|
|
|
|
|
|
//catch (Exception)
|
|
|
|
|
|
//{
|
2026-02-12 21:29:00 +08:00
|
|
|
|
// FreeFormElement.Create(Document, topSolid);
|
2025-12-23 21:35:54 +08:00
|
|
|
|
//}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.AppendLine(ex.Message);
|
|
|
|
|
|
continue;
|
2025-07-31 20:12:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-12 21:29:00 +08:00
|
|
|
|
Document.Delete(element.Id);
|
|
|
|
|
|
});
|
|
|
|
|
|
MessageBox.Show("几何体炸开完成,请等待材质赋值完成", "提示");
|
|
|
|
|
|
Document.Invoke(
|
|
|
|
|
|
_ =>
|
2025-12-23 21:35:54 +08:00
|
|
|
|
{
|
2026-02-12 21:29:00 +08:00
|
|
|
|
var freeFormElements = Document.OfClass<FreeFormElement>().Cast<FreeFormElement>().ToList();
|
|
|
|
|
|
foreach (var form in freeFormElements)
|
|
|
|
|
|
{
|
|
|
|
|
|
var geoElement = form.get_Geometry(new Options() { ComputeReferences = true, DetailLevel = ViewDetailLevel.Fine });
|
|
|
|
|
|
foreach (var obj in geoElement)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (obj is Solid solid && solid.Faces.Size > 0 && solid.Volume > 10e-6)
|
|
|
|
|
|
{
|
|
|
|
|
|
Face face = solid.Faces.get_Item(0);
|
|
|
|
|
|
Material material = Document.GetElement(face.MaterialElementId) as Material;
|
|
|
|
|
|
if (material != null)
|
|
|
|
|
|
form.get_Parameter(BuiltInParameter.MATERIAL_ID_PARAM).Set(material.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
},"炸开dwg");
|
2025-08-12 23:08:54 +08:00
|
|
|
|
|
2025-07-11 09:20:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-12-23 21:35:54 +08:00
|
|
|
|
if (sb.Length > 0)
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2025-12-23 21:35:54 +08:00
|
|
|
|
MessageBox.Show(sb.ToString(), "部分几何体创建失败");
|
2025-07-11 09:20:23 +08:00
|
|
|
|
}
|
2025-08-12 23:08:54 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-11 09:20:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|