63 lines
2.8 KiB
C#
63 lines
2.8 KiB
C#
|
|
using Autodesk.Revit.DB;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
namespace Szmedi.RvKits.Assists
|
|
{
|
|
internal static class CADExtensions
|
|
{
|
|
/// <summary>
|
|
/// 获取dwg所在的路径
|
|
/// </summary>
|
|
/// <param name="dwg"></param>
|
|
/// <returns></returns>
|
|
public static string GetDwgPath(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;
|
|
}
|
|
public static List<KeyValuePair<string, XYZ>> GetTextsByLayer(this ImportInstance dwg, string textLayerName)
|
|
{
|
|
List<KeyValuePair<string, XYZ>> keyValuePairs = [];
|
|
var dwgTransform = dwg!.GetTransform();
|
|
var path = dwg.GetDwgPath();
|
|
using ACadSharp.IO.DwgReader reader = new(path);
|
|
var cadDocument = reader.Read();
|
|
var entities = cadDocument.Entities
|
|
.Where(
|
|
entity => (entity is ACadSharp.Entities.MText ||
|
|
entity is ACadSharp.Entities.TextEntity) &&
|
|
entity.Layer.Name == textLayerName)
|
|
.ToList();
|
|
foreach (var entity in entities)
|
|
{
|
|
if (entity is ACadSharp.Entities.MText mText)
|
|
{
|
|
var loc = dwgTransform.OfPoint(new XYZ(mText.InsertPoint.X, mText.InsertPoint.Y, mText.InsertPoint.Z) / 304.8);
|
|
if (mText.Value.Contains(@"\f") && mText.Value.Contains(@"|b") && mText.Value.Contains(@"i0"))//多行字体编码问题
|
|
{
|
|
var str = mText.Value.Split(';').Last().TrimEnd('}');
|
|
keyValuePairs.Add(new KeyValuePair<string, XYZ>(str, loc));
|
|
}
|
|
else
|
|
{
|
|
keyValuePairs.Add(new KeyValuePair<string, XYZ>(mText.Value, loc));
|
|
}
|
|
}
|
|
if (entity is ACadSharp.Entities.TextEntity text)
|
|
{
|
|
var loc = dwgTransform.OfPoint(new XYZ(text.InsertPoint.X, text.InsertPoint.Y, text.InsertPoint.Z) / 304.8);
|
|
keyValuePairs.Add(new KeyValuePair<string, XYZ>(text.Value, loc));
|
|
}
|
|
}
|
|
return keyValuePairs;
|
|
}
|
|
}
|
|
}
|