207 lines
8.5 KiB
C#
207 lines
8.5 KiB
C#
|
||
using System;
|
||
using System.Linq;
|
||
using System.Text.RegularExpressions;
|
||
using System.Windows;
|
||
|
||
using Autodesk.Revit.DB;
|
||
using Autodesk.Revit.UI;
|
||
using Autodesk.Revit.UI.Selection;
|
||
|
||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||
|
||
using Szmedi.RvKits.Assists;
|
||
|
||
namespace Szmedi.RvKits.InfoManager
|
||
{
|
||
public partial class WriteFacilityCodeViewModel : ObservableObject
|
||
{
|
||
public WriteFacilityCodeViewModel(UIApplication uiapp)
|
||
{
|
||
this.uiapp = uiapp;
|
||
}
|
||
private List<KeyValuePair<string, XYZ>> OriginCodes;
|
||
|
||
[ObservableProperty]
|
||
private List<KeyValuePair<string, XYZ>> codes;
|
||
[ObservableProperty]
|
||
private KeyValuePair<string, XYZ> selectedCode;
|
||
[ObservableProperty]
|
||
private string propertyName = "ID-100-编号";
|
||
[ObservableProperty]
|
||
[NotifyCanExecuteChangedFor(nameof(WriteCodeCommand))]
|
||
private string code;
|
||
|
||
[ObservableProperty]
|
||
private bool isIncrement;
|
||
|
||
private bool CanWrite => !string.IsNullOrEmpty(Code);
|
||
private readonly ActionEventHandler handler = new();
|
||
private readonly UIApplication uiapp;
|
||
[ObservableProperty]
|
||
private string searchText;
|
||
|
||
partial void OnSearchTextChanged(string value)
|
||
{
|
||
Codes = OriginCodes
|
||
.Where(s => string.IsNullOrEmpty(value) || s.Key.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
|
||
.ToList();
|
||
}
|
||
partial void OnSelectedCodeChanged(KeyValuePair<string, XYZ> value)
|
||
{
|
||
Code = value.Key;
|
||
BoundingBoxXYZ box = new BoundingBoxXYZ()
|
||
{
|
||
Max = value.Value.Add(new XYZ(1, 1, 1)),
|
||
Min = value.Value.Subtract(new XYZ(1, 1, 1))
|
||
};
|
||
|
||
UIView uiView = uiapp.ActiveUIDocument.GetOpenUIViews().FirstOrDefault(v => v.ViewId == uiapp.ActiveUIDocument.ActiveGraphicalView.Id);
|
||
uiView!.ZoomAndCenterRectangle(box.Min, box.Max);
|
||
}
|
||
[RelayCommand]
|
||
private void ReadDwgCodes()
|
||
{
|
||
handler.Raise(uiapp =>
|
||
{
|
||
Autodesk.Revit.ApplicationServices.Application application = uiapp.Application;
|
||
Autodesk.Revit.UI.UIDocument uiDocument = uiapp.ActiveUIDocument;
|
||
Document doc = uiDocument.Document;
|
||
Reference textRefer;
|
||
try
|
||
{
|
||
//图层ID;
|
||
textRefer = uiDocument.Selection.PickObject(
|
||
ObjectType.PointOnElement,
|
||
new FuncFilter(e => e is ImportInstance import && import.IsLinked && doc.GetElement(e.GetTypeId()) is CADLinkType),
|
||
"请选择“链接(非导入)”的Cad文字图层"
|
||
);
|
||
}
|
||
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
ImportInstance dwg = doc.GetElement(textRefer) as ImportInstance;
|
||
//Transform dwgTransform = dwg!.GetTransform();
|
||
string textLayerName = dwg.GetLayerName(textRefer);
|
||
//string path = GetDwgPath(dwg);
|
||
//using ACadSharp.IO.DwgReader reader = new(path);
|
||
//ACadSharp.CadDocument cadDocument = reader.Read();
|
||
//var texts = cadDocument.Entities.Where(entity=>entity is MText||entity is TextEntity)
|
||
// .Where(e => e.Layer.Name == textLayerName);
|
||
//IEnumerable<XYZ> locations = texts.Select(t => dwgTransform.OfPoint(new XYZ(t.InsertPoint.X, t.InsertPoint.Y, t.InsertPoint.Z) / 304.8));
|
||
//doc.Invoke(ts =>
|
||
//{
|
||
// foreach (XYZ loc in locations)
|
||
// {
|
||
// Plane plane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, loc);
|
||
// SketchPlane sketch = SketchPlane.Create(doc, plane);
|
||
// Autodesk.Revit.DB.Arc arc = Autodesk.Revit.DB.Arc.Create(plane, 0.1, 0, Math.PI * 2);
|
||
// ModelCurve mc = doc.Create.NewModelCurve(arc, sketch);
|
||
// }
|
||
//});
|
||
//Codes = OriginCodes = cadDocument.Entities
|
||
// .Where(entity => ( entity is MText || entity is TextEntity) && entity.Layer.Name == textLayerName)
|
||
// .Select(e =>
|
||
// {
|
||
// if (e is MText mText)
|
||
// {
|
||
// if (mText.Value.Contains(@"\f") && mText.Value.Contains(@"|b") && mText.Value.Contains(@"i0"))//多行字体编码问题
|
||
// {
|
||
// return mText.Value.Split(';').Last().TrimEnd('}');
|
||
// }
|
||
// return mText.Value;
|
||
// }
|
||
// if (e is TextEntity text)
|
||
// {
|
||
// return text.Value;
|
||
// }
|
||
// return null;
|
||
// })
|
||
// .ToList();
|
||
Codes = OriginCodes = dwg.GetTextsByLayer(textLayerName);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
|
||
|
||
[RelayCommand(CanExecute = nameof(CanWrite))]
|
||
private void WriteCode()
|
||
{
|
||
handler.Raise(uiapp =>
|
||
{
|
||
try
|
||
{
|
||
Element elem;
|
||
Autodesk.Revit.UI.UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
Document doc = uidoc.Document;
|
||
if (uidoc.Selection.GetElementIds().Any())
|
||
{
|
||
elem = doc.GetElement(uidoc.Selection.GetElementIds().FirstOrDefault());
|
||
}
|
||
else
|
||
{
|
||
Reference reference = uidoc.Selection.PickObject(ObjectType.Element, new GenericFilter<FamilyInstance>());
|
||
elem = doc.GetElement(reference);
|
||
}
|
||
|
||
if (!elem.IsValidObject)
|
||
{
|
||
return;
|
||
}
|
||
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
Parameter param = elem.GetParameters(PropertyName).FirstOrDefault();
|
||
if (param.IsReadOnly)
|
||
{
|
||
MessageBox.Show("该属性为只读");
|
||
return;
|
||
}
|
||
var b = param?.Set(Code);
|
||
if (b == true && IsIncrement)
|
||
{
|
||
Regex regex = new Regex(@"(\d+)$");
|
||
Match match = regex.Match(Code);
|
||
|
||
if (match.Success)
|
||
{
|
||
// 提取匹配的数字部分
|
||
string numberPart = match.Groups[1].Value;
|
||
|
||
// 将数字部分转换为整数并自增
|
||
int number = int.Parse(numberPart);
|
||
number++;
|
||
// 保持原始数字部分的长度
|
||
int length = numberPart.Length;
|
||
// 将原始字符串的非数字部分与自增后的数字部分拼接
|
||
Code = Code.Substring(0, match.Index) + number.ToString().PadLeft(length, '0');
|
||
}
|
||
}
|
||
},
|
||
"填写设备编码"
|
||
);
|
||
|
||
//foreach (var elementId in uidoc.Selection.GetElementIds())
|
||
//{
|
||
// var elem = doc.GetElement(elementId);
|
||
// var param = elem.GetParameters("ID-100-编号").FirstOrDefault();
|
||
|
||
// param?.Set($"{Code}");
|
||
//}
|
||
}
|
||
catch (Autodesk.Revit.Exceptions.OperationCanceledException) { }
|
||
});
|
||
}
|
||
}
|
||
}
|