整合自定义控件

This commit is contained in:
GG Z
2025-08-12 23:08:54 +08:00
parent f209e7d3ad
commit d0cfc64450
520 changed files with 30954 additions and 38968 deletions

View File

@@ -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;
}
}
}
}
}