423 lines
20 KiB
C#
423 lines
20 KiB
C#
using Autodesk.Revit.Attributes;
|
||
using Autodesk.Revit.DB;
|
||
using Autodesk.Revit.UI;
|
||
|
||
using Nice3point.Revit.Toolkit.External;
|
||
|
||
using System;
|
||
using System.Linq;
|
||
using System.Windows;
|
||
|
||
using System.Windows.Interop;
|
||
|
||
using Szmedi.RvKits.ModelManager;
|
||
|
||
namespace Szmedi.RvKits.DrawingTools;
|
||
|
||
[Transaction(TransactionMode.Manual)]
|
||
|
||
public class Dimension2DTo3DCmd : ExternalCommand
|
||
{
|
||
public override void Execute()
|
||
{
|
||
View viewPlan = Document.ActiveView;
|
||
CopyAnnotationViewModel viewModel = new(UiApplication);
|
||
CopyAnnotationWin win = null;
|
||
//AssemblyLoaderHelpers loader = new(GlobalVariables.DirAssembly);
|
||
//loader.HookAssemblyResolve();
|
||
List<MessageModel> models = new();
|
||
try
|
||
{
|
||
win = new CopyAnnotationWin(viewModel);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
MessageBox.Show(e.Message);
|
||
LogAssists.WriteLog(e.StackTrace);
|
||
}
|
||
//finally
|
||
//{
|
||
// loader.UnhookAssemblyResolve();
|
||
//}
|
||
|
||
List<Dimension> dimensions = new FilteredElementCollector(Document, viewPlan.Id)
|
||
.OfClass(typeof(Dimension))
|
||
.WhereElementIsNotElementType()
|
||
.Cast<Dimension>()
|
||
.ToList();
|
||
if (win != null && win.ShowDialog() == true)
|
||
{
|
||
if (win.DialogResult == true)
|
||
{
|
||
using TransactionGroup group = new(Document, "复制平面标注");
|
||
group.Start();
|
||
|
||
foreach (View3D view3D in viewModel.View3DsSelected)
|
||
{
|
||
SketchPlane originSketch = view3D.SketchPlane;
|
||
using (Transaction ts = new(Document, "复制尺寸标注"))
|
||
{
|
||
foreach (Dimension dimension in dimensions)
|
||
{
|
||
try
|
||
{
|
||
ts.Start();
|
||
List<Reference> references = new();
|
||
Curve curve = dimension.Curve;
|
||
curve.MakeBound(0, 100);
|
||
Plane plane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, curve.GetEndPoint(0));
|
||
view3D.SketchPlane = SketchPlane.Create(Document, plane);
|
||
switch (dimension.DimensionShape)
|
||
{
|
||
case DimensionShape.Linear:
|
||
bool isContainMepCurve = false;
|
||
Curve locCurve = null;
|
||
ReferenceArray array = new();
|
||
foreach (Reference reference in dimension.References)
|
||
{
|
||
//重新定义参照平面为管线的平面
|
||
if (Document.GetElement(reference) is MEPCurve mepCurve)
|
||
{
|
||
LocationCurve loc = mepCurve.Location as LocationCurve;
|
||
locCurve = loc.Curve;
|
||
plane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, locCurve.GetEndPoint(0));
|
||
view3D.SketchPlane = SketchPlane.Create(Document, plane);
|
||
isContainMepCurve = true;
|
||
if (reference.ConvertToStableRepresentation(Document).EndsWith(":0:LINEAR"))
|
||
{
|
||
array.Append(new Reference(mepCurve));
|
||
continue;
|
||
}
|
||
}
|
||
|
||
array.Append(reference);
|
||
}
|
||
|
||
if (isContainMepCurve)
|
||
{
|
||
double dis = locCurve.GetEndPoint(0).Z - curve.GetEndPoint(0).Z;
|
||
Curve finalCurve = dimension.Curve.CreateTransformed(
|
||
Transform.CreateTranslation(dis * XYZ.BasisZ)
|
||
);
|
||
Document.Create.NewDimension(view3D, finalCurve as Line, array, dimension.DimensionType);
|
||
}
|
||
else
|
||
{
|
||
Document.Create.NewDimension(
|
||
view3D,
|
||
dimension.Curve as Line,
|
||
dimension.References,
|
||
dimension.DimensionType
|
||
);
|
||
//dim.Curve.CreateTransformed(Transform.CreateTranslation(10 * XYZ.BasisZ));
|
||
}
|
||
|
||
//var elem = doc.GetElement(dimension.References.get_Item(0));
|
||
break;
|
||
case DimensionShape.Angular:
|
||
foreach (Reference reference in dimension.References)
|
||
{
|
||
references.Add(reference);
|
||
}
|
||
|
||
AngularDimension.Create(
|
||
Document,
|
||
view3D,
|
||
dimension.Curve as Arc,
|
||
references,
|
||
dimension.DimensionType
|
||
);
|
||
break;
|
||
case DimensionShape.Radial:
|
||
break;
|
||
case DimensionShape.ArcLength:
|
||
break;
|
||
case DimensionShape.Spot:
|
||
int code = dimension.Category.GetHashCode();
|
||
Reference refer = dimension.References.get_Item(0);
|
||
if (code == -2000263) //高程点
|
||
{
|
||
SpotDimension spot = Document.Create.NewSpotElevation(
|
||
view3D,
|
||
refer,
|
||
dimension.Origin,
|
||
dimension.Origin,
|
||
dimension.Origin,
|
||
dimension.Origin,
|
||
true
|
||
);
|
||
continue;
|
||
}
|
||
|
||
if (code == -2000264) //坐标点
|
||
{
|
||
SpotDimension spot = Document.Create.NewSpotCoordinate(
|
||
view3D,
|
||
refer,
|
||
dimension.Origin,
|
||
dimension.Origin,
|
||
dimension.Origin,
|
||
dimension.Origin,
|
||
true
|
||
);
|
||
|
||
continue;
|
||
}
|
||
|
||
break;
|
||
case DimensionShape.Diameter:
|
||
break;
|
||
case DimensionShape.Unknown:
|
||
break;
|
||
}
|
||
|
||
ts.Commit();
|
||
}
|
||
catch (Exception)
|
||
{
|
||
models.Add(new MessageModel(dimension, "无法复制"));
|
||
if (ts.GetStatus() == TransactionStatus.Started)
|
||
{
|
||
ts.Commit();
|
||
}
|
||
//TaskDialog.Show("错误", e.Message);
|
||
}
|
||
}
|
||
//if (dimension.DimensionShape == DimensionShape.Radial)
|
||
//{
|
||
// continue;
|
||
//}
|
||
//if (dimension.DimensionShape == DimensionShape.Diameter)
|
||
//{
|
||
// continue;
|
||
//}
|
||
}
|
||
|
||
using (Transaction ts = new(Document, "重置工作平面"))
|
||
{
|
||
ts.Start();
|
||
view3D.SketchPlane = originSketch;
|
||
ts.Commit();
|
||
}
|
||
|
||
using Transaction trans = new(Document, "复制标记");
|
||
trans.Start();
|
||
|
||
bool canCopyTag = false;
|
||
if (view3D.CanBeLocked() && view3D.CanSaveOrientation())
|
||
{
|
||
view3D.SaveOrientationAndLock();
|
||
canCopyTag = true;
|
||
}
|
||
else
|
||
{
|
||
MessageBoxResult result = MessageBox.Show(
|
||
"视图名存在意外的字符,族的标记将不会复制,是否继续?",
|
||
"错误",
|
||
MessageBoxButton.OKCancel,
|
||
MessageBoxImage.Warning
|
||
);
|
||
if (result == MessageBoxResult.Cancel)
|
||
{
|
||
Result = Result.Cancelled;
|
||
return;
|
||
}
|
||
}
|
||
|
||
IEnumerable<TextNote> textList = new FilteredElementCollector(Document, viewPlan.Id)
|
||
.OfClass(typeof(TextNote))
|
||
.ToElements()
|
||
.Cast<TextNote>();
|
||
IEnumerable<IndependentTag> tags = new FilteredElementCollector(Document, viewPlan.Id)
|
||
.OfClass(typeof(IndependentTag))
|
||
.ToElements()
|
||
.Cast<IndependentTag>();
|
||
foreach (TextNote text in textList)
|
||
{
|
||
XYZ position = text.Coord + (viewPlan.GenLevel.Elevation * XYZ.BasisZ);
|
||
TextNoteOptions options =
|
||
new()
|
||
{
|
||
KeepRotatedTextReadable = false,
|
||
HorizontalAlignment = text.HorizontalAlignment,
|
||
Rotation = 0,
|
||
TypeId = text.TextNoteType.Id
|
||
};
|
||
TextNote note = TextNote.Create(Document, view3D.Id, position, text.Width, text.Text, options);
|
||
note.SetFormattedText(text.GetFormattedText());
|
||
note.LeaderLeftAttachment = text.LeaderLeftAttachment;
|
||
note.LeaderRightAttachment = text.LeaderRightAttachment;
|
||
}
|
||
|
||
//try
|
||
//{
|
||
if (canCopyTag)
|
||
{
|
||
foreach (IndependentTag tag in tags)
|
||
{
|
||
IndependentTag tagCopy;
|
||
|
||
double maxZ = Document.GetElement(tag.GetTaggedReference()).get_BoundingBox(view3D).Max.Z;
|
||
|
||
XYZ position = new(tag.TagHeadPosition.X, tag.TagHeadPosition.Y, maxZ);
|
||
if (tag.IsOrphaned)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
tagCopy = tag.IsMaterialTag
|
||
? IndependentTag.Create(
|
||
Document,
|
||
view3D.Id,
|
||
tag.GetTaggedReference(),
|
||
tag.HasLeader,
|
||
TagMode.TM_ADDBY_MATERIAL,
|
||
tag.TagOrientation,
|
||
position
|
||
)
|
||
: tag.IsMulticategoryTag
|
||
? IndependentTag.Create(
|
||
Document,
|
||
view3D.Id,
|
||
tag.GetTaggedReference(),
|
||
tag.HasLeader,
|
||
TagMode.TM_ADDBY_MULTICATEGORY,
|
||
tag.TagOrientation,
|
||
position
|
||
)
|
||
: IndependentTag.Create(
|
||
Document,
|
||
view3D.Id,
|
||
tag.GetTaggedReference(),
|
||
tag.HasLeader,
|
||
TagMode.TM_ADDBY_CATEGORY,
|
||
tag.TagOrientation,
|
||
position
|
||
);
|
||
|
||
tagCopy.ChangeTypeId(tag.GetTypeId());
|
||
Document.Regenerate();
|
||
|
||
if (tagCopy.IsValidObject)
|
||
{
|
||
tagCopy.HasLeader = tag.HasLeader;
|
||
if (tagCopy.HasLeader)
|
||
{
|
||
switch (tag.LeaderEndCondition)
|
||
{
|
||
case LeaderEndCondition.Attached:
|
||
tagCopy.TagHeadPosition = position;
|
||
break;
|
||
case LeaderEndCondition.Free:
|
||
tagCopy.TagHeadPosition = position;
|
||
try
|
||
{
|
||
tagCopy.LeaderEnd = new XYZ(tag.LeaderEnd.X, tag.LeaderEnd.Y, maxZ);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogAssists.WriteLog(e.Message);
|
||
models.Add(new MessageModel(tag, "无法修改引线端点值,需重建"));
|
||
//stringBuilder.AppendLine($"{tag.Id.IntegerValue}");
|
||
}
|
||
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (tagCopy.CanLeaderEndConditionBeAssigned(tag.LeaderEndCondition))
|
||
{
|
||
tagCopy.LeaderEndCondition = tag.LeaderEndCondition;
|
||
}
|
||
|
||
if (tag.HasLeader && tag.HasElbow)
|
||
{
|
||
tagCopy.LeaderElbow = new XYZ(tag.LeaderElbow.X, tag.LeaderElbow.Y, maxZ);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
models.Add(new MessageModel(tag, "无法复制"));
|
||
//stringBuilder.AppendLine($"标注类型:{tag.Name},标注Id:{tag.Id.IntegerValue}");
|
||
}
|
||
}
|
||
}
|
||
|
||
trans.Commit();
|
||
//}
|
||
//catch (Exception ex)
|
||
//{
|
||
// message = ex.InnerException.ToString();
|
||
// if (trans.GetStatus() == TransactionStatus.Started)
|
||
// {
|
||
// trans.RollBack();
|
||
// }
|
||
// return Result.Failed;
|
||
//}
|
||
}
|
||
|
||
if (group.GetStatus() == TransactionStatus.Started)
|
||
{
|
||
group.Assimilate();
|
||
}
|
||
|
||
//if (sb.ToString().Length > 0)
|
||
//{
|
||
// var result = TaskDialog.Show("标注复制问题", "需要手动解决的标注:\n\n" + sb + "\n是否保存到桌面并打开?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
|
||
// if (result == TaskDialogResult.Yes)
|
||
// {
|
||
// var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
|
||
// $"\\标注问题 {DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-ffff")}.txt";
|
||
// File.WriteAllText(filePath
|
||
// , sb.ToString());
|
||
// System.Diagnostics.Process.Start(filePath);
|
||
// }
|
||
//}
|
||
|
||
if (models.Count > 0)
|
||
{
|
||
MessageWin messageWin = new() { DataContext = new MessageViewModel(UiDocument, models) };
|
||
new WindowInteropHelper(messageWin) { Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle };
|
||
messageWin.Show();
|
||
//var result = TaskDialog.Show("标记复制问题", "需要手动解决的标注:\n\n" + stringBuilder + "\n是否保存到桌面并打开?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
|
||
//if (result == TaskDialogResult.Yes)
|
||
//{
|
||
// var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
|
||
// $"\\标注问题 {DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-ffff")}.txt";
|
||
// File.WriteAllText(filePath
|
||
// , stringBuilder.ToString());
|
||
// System.Diagnostics.Process.Start(filePath);
|
||
//}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void AdjustTextDirection(Document Document)
|
||
{
|
||
IEnumerable<TextNote> textList = new FilteredElementCollector(Document, Document.ActiveView.Id)
|
||
.OfClass(typeof(TextNote))
|
||
.ToElements()
|
||
.Cast<TextNote>();
|
||
using Transaction trans = new(Document, "调整文字朝向");
|
||
foreach (TextNote text in textList)
|
||
{
|
||
try
|
||
{
|
||
trans.Start();
|
||
text.KeepRotatedTextReadable = false;
|
||
Document.Regenerate();
|
||
text.KeepRotatedTextReadable = true;
|
||
trans.Commit();
|
||
}
|
||
catch (Exception)
|
||
{
|
||
if (trans.GetStatus() == TransactionStatus.Started)
|
||
{
|
||
trans.RollBack();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|