Files
Shrlalgo.RvKits/ShrlAlgo.Toolkit.Revit/Assist/ImportInstanceAssist.cs

201 lines
7.0 KiB
C#

using System.IO;
using System.Text;
using Autodesk.Revit.DB;
namespace ShrlAlgo.Toolkit.Revit.Assist;
public static class ImportInstanceAssist
{
/// <summary>
/// 获取块参照
/// </summary>
/// <param name="dwg"></param>
/// <param name="reference">选择的块参考</param>
/// <returns></returns>
public static List<GeometryInstance> GetBlocksByRef(this ImportInstance dwg, Reference reference)
{
var geoElem = dwg.get_Geometry(new Options());
//dwg块
var geoObj = dwg.GetGeometryObjectFromReference(reference);
GeometryInstance referBlock = null;
foreach (var obj in geoElem)
{
if (obj is GeometryInstance dwgGeo) //dwg几何实例
{
foreach (var block in dwgGeo.SymbolGeometry) //块
{
if (block is GeometryInstance ins)
{
if (ins.GetHashCode() == geoObj.GetHashCode())
{
referBlock = ins;
break;
}
foreach (var subBlock in ins.SymbolGeometry) //嵌套块/子块
{
if (subBlock is GeometryInstance subIns && subIns.GetHashCode() == geoObj.GetHashCode())
{
referBlock = ins;
break;
}
}
}
}
}
}
if (referBlock == null)
{
return null;
}
List<GeometryInstance> blocks = [];
foreach (var obj in geoElem)
{
if (obj is GeometryInstance dwgGeo)
{
foreach (var block in dwgGeo.SymbolGeometry) //图元
{
#if REVIT2018 || REVIT2020
if (block is GeometryInstance ins && ins.Symbol.Name == referBlock.Symbol.Name)
{
blocks.Add(ins);
}
#elif REVIT2025
if (block is GeometryInstance ins && ins.GetSymbolGeometryId().SymbolId == referBlock.GetSymbolGeometryId().SymbolId)
{
blocks.Add(ins);
}
#endif
}
}
}
return blocks;
}
public static CurveByPoints ConvertToMassCurve(this ImportInstance importInstance)
{
var famdoc = importInstance.Document;
var curves = importInstance.GetCurves();
var array = new ReferencePointArray();
foreach (var c in curves)
{
var referencePoint = famdoc.FamilyCreate.NewReferencePoint(c.GetEndPoint(0));
array.Append(referencePoint);
}
array.Append(famdoc.FamilyCreate.NewReferencePoint(curves.Last().GetEndPoint(1)));
return famdoc.FamilyCreate.NewCurveByPoints(array);
}
/// <summary>
/// 获取dwg的曲线
/// </summary>
/// <param name="importInstance"></param>
/// <returns></returns>
public static List<Curve> GetCurves(this ImportInstance importInstance)
{
List<Curve> curves = [];
var geoElem = importInstance.get_Geometry(new Options());
foreach (var geomObj in geoElem)
{
var geomInstance = geomObj as GeometryInstance;
if (geomInstance != null)
{
foreach (var instObj in geomInstance.GetInstanceGeometry())
{
//polyLine拆成线
if (instObj is PolyLine line)
{
var poly = line.GetCoordinates();
for (var i = 0; i < poly.Count - 1; i++)
{
var l = Line.CreateBound(poly[i], poly[i + 1]);
curves.Add(l);
}
}
if (instObj is Arc arc)
{
curves.Add(arc);
}
}
}
}
return curves;
}
/// <summary>
/// 获取包含的图块,图层
/// </summary>
/// <param name="dwg"></param>
public static StringBuilder GetDwgInfo(this ImportInstance dwg)
{
StringBuilder sb = new();
var geoElem = dwg.get_Geometry(new Options());
foreach (var obj in geoElem)
{
if (obj is GeometryInstance instance) //dwg
{
foreach (var obj1 in instance.SymbolGeometry) //图元
{
if (obj1 is GeometryInstance block)
{
var graphicsStyle = dwg.Document.GetElement(obj1.GraphicsStyleId) as GraphicsStyle;
var layerName = graphicsStyle?.GraphicsStyleCategory.Name;
#if REVIT2018 || REVIT2020
sb.AppendLine($"{block.Symbol.Name}({obj1.GraphicsStyleId})-{layerName}-{block.Symbol.UniqueId}");
#elif REVIT2025
var id = block.GetSymbolGeometryId().SymbolId;
var elem = dwg.Document.GetElement(id);
sb.AppendLine($"{elem.Name}({obj1.GraphicsStyleId})-{layerName}-{elem.UniqueId}");
#endif
}
}
}
}
return sb;
}
/// <summary>
/// 获取dwg所在的路径
/// </summary>
/// <param name="dwg"></param>
/// <returns></returns>
public static string GetFilePath(this ImportInstance dwg)
{
var cadLinkType = dwg.Document.GetElement(dwg.GetTypeId()) as CADLinkType;
var filePath = cadLinkType?.GetExternalFileReference().GetLinkedFileStatus() == LinkedFileStatus.Loaded
? ModelPathUtils.ConvertModelPathToUserVisiblePath(cadLinkType?.GetExternalFileReference().GetAbsolutePath())
: throw new InvalidOperationException("该dwg不是链接");
return !File.Exists(filePath) ? throw new InvalidOperationException("链接文件路径不存在") : filePath;
}
/// <summary>
/// 获取参考所在的图层名称
/// </summary>
/// <param name="dwg"></param>
/// <param name="reference"></param>
/// <returns></returns>
public static string GetLayerName(this ImportInstance dwg, Reference reference)
{
var obj = dwg.GetGeometryObjectFromReference(reference);
var style = dwg.Document.GetElement(obj.GraphicsStyleId) as GraphicsStyle;
return style?.GraphicsStyleCategory.Name;
}
/// <summary>
/// 获取变换
/// </summary>
/// <param name="importInstance"></param>
/// <param name="reference"></param>
/// <returns></returns>
public static Transform GetTransform(this ImportInstance importInstance, Reference reference)
{
var geoObj = importInstance.GetGeometryObjectFromReference(reference);
var geomInstance = geoObj as GeometryInstance;
return geomInstance?.Transform;
}
}